diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..f5b4c9c --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,2 @@ +# Only the specified owner can approve changes to workflow files +/.github/workflows/ @swisscom/swisscom-health diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..f8ac316 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,97 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ "develop" ] + pull_request: + branches: [ "develop" ] + schedule: + - cron: '16 5 * * 3' + +jobs: + analyze: + name: Analyze (${{ matrix.language }}) + # Runner size impacts CodeQL analysis time. To learn more, please see: + # - https://gh.io/recommended-hardware-resources-for-running-codeql + # - https://gh.io/supported-runners-and-hardware-resources + # - https://gh.io/using-larger-runners (GitHub.com only) + # Consider using larger runners or machines with greater resources for possible analysis time improvements. + runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} + timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }} + permissions: + # required for all workflows + security-events: write + + # required to fetch internal or private CodeQL packs + packages: read + + # only required for workflows in private repositories + actions: read + contents: read + + strategy: + fail-fast: false + matrix: + include: + - language: java-kotlin + build-mode: manual + # CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' + # Use `c-cpp` to analyze code written in C, C++ or both + # Use 'java-kotlin' to analyze code written in Java, Kotlin or both + # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both + # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, + # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. + # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how + # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + # Configure Gradle for optimal use in GitHub Actions, including caching of downloaded dependencies. + # See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md + - name: Setup Gradle + uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: ${{ matrix.language }} + build-mode: ${{ matrix.build-mode }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + # queries: security-extended,security-and-quality + + # If the analyze step fails for one of the languages you are analyzing with + # "We were unable to automatically build your code", modify the matrix above + # to set the build mode to "manual" for that language. Then modify this step + # to build your code. + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + - if: matrix.build-mode == 'manual' + shell: bash + run: ./gradlew clean build + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v3 + with: + category: "/language:${{matrix.language}}" diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml new file mode 100644 index 0000000..73d16cf --- /dev/null +++ b/.github/workflows/gradle.yml @@ -0,0 +1,56 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle + +name: Java CI with Gradle + +on: + push: + branches: [ "develop" ] + pull_request: + branches: [ "develop" ] + +jobs: + build: + + runs-on: ubuntu-latest + permissions: + contents: read + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + # Configure Gradle for optimal use in GitHub Actions, including caching of downloaded dependencies. + # See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md + - name: Setup Gradle + uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0 + + - name: Build with Gradle Wrapper + run: ./gradlew clean build + + dependency-submission: + + runs-on: ubuntu-latest + permissions: + contents: write + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + # Generates and submits a dependency graph, enabling Dependabot Alerts for all project dependencies. + # See: https://github.com/gradle/actions/blob/main/dependency-submission/README.md + - name: Generate and submit dependency graph + uses: gradle/actions/dependency-submission@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0 diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..84d5c05 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,35 @@ +name: Publish release + +on: + push: + branches: + - develop + release: + types: [created] + +jobs: + publish-release: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout latest code + uses: actions/checkout@v4 + + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: 17 + + # Configure Gradle for optimal use in GitHub Actions, including caching of downloaded dependencies. + # See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md + - name: Setup Gradle + uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0 + + - name: Build and publish with Gradle Wrapper + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: ./gradlew clean build publishVersion diff --git a/.gitignore b/.gitignore index 524f096..4e76afb 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,18 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* replay_pid* + +# IntelliJ IDEA +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +# other +*.gradle +Java.gitignore +build/ +bin/ diff --git a/README.md b/README.md index 3f8a40c..f3907bb 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,73 @@ -# cdr-client +# CDR Client The Swisscom Health Confidential Data Routing (CDR) Client + +## API +There is no endpoint (beside actuator/health) that are offered here. + +The CDR Client is triggered by a scheduler and synchronizes by the given delay time the files from the CDR API. + +### Functionality +For each defined connector the CDR Client calls the defined endpoints of the CDR API. + +For each connector one file after the other is pulled. Each file is written into a temporary folder defined as 'local-folder'. +The file is named after the received 'cdr-document-uuid' header that is a unique identifier created by the CDR API. +After saving the file to the temporary folder, a delete request for the given 'cdr-document-uuid' is sent to the CDR API. +After successfully deleting the file in the CDR API, the file is moved to the connector defined 'target-folder'. + +The temporary folders need to be monitored by another tool to make sure that no files are forgotten (should only happen if the move +to the destination folder is failing). + +For each connector one file after the other is pushed from the defined 'source-folder'. After the file is successfully uploaded it will be deleted. +If the upload failed with a response code of 4xx the file will be appended with '.error' and an additional file with the same name as the sent file, but with +the extension '.log' will be created and the received response body will be saved to this file. +If the upload failed with a response code of 5xx the file will be retried a defined amount of times, +see retry-delay in the [application-client.yaml](./src/main/resources/config/application-client.yaml) file. After reaching the max retry count the file will +be appended with '.error' and an additional file with the same name as the sent file, but with the extension '.log' will be created and the received response +body will be saved to this file. + +## Local development +To test some usecases there is a [docker-compose.yaml](./docker-compose/docker-compose.yaml) with wiremock that simulates the CDR API. Run with ```docker-compose down && docker-compose up --build```. + +If you want to work with a deployed CDR API you need to change the [application-dev.yaml](./src/main/resources/config/application-dev.yaml) + +Set the following spring profile to active: dev + +Following environment variables need to be set: +* cdrClient.localFolder=~/Documents/cdr/inflight +* cdrClient.targetFolder=~/Documents/cdr/target +* cdrClient.sourceFolder=~/Documents/cdr/source + +## Application Plugin +To create scripts to run the application locally one needs to run following gradle cmd: ```gradlew installDist``` + +This creates a folder ```build/install/cdr-client``` with scripts for windows and unix servers in the ```bin``` folder. + +To run the application locally one can call ```./build/install/cdr-client/bin/cdr-client```. It is required to have a ```application-customer.yaml``` and link it by adding following command line: ```JVM_OPTS="-Dspring.config.additional-location=./application-customer.yaml"```. +With a minimum configuration that looks like this: +``` +client: + local-folder: /tmp/cdr + endpoint: + host: cdr.health.swisscom.com + base-path: api/documents + customer: + - connector-id: 8000000000000 + content-type: application/forumdatenaustausch+xml;charset=UTF-8;version=4.5 + target-folder: /tmp/download/8000000000000 + source-folder: /tmp/source/8000000000000 + mode: test +``` + +## Running the Jar +If the provided jar should be run directly, the following command can be used: +```java -jar cdr-client.jar``` +The jar can be found in build/libs. + +Following environment variables need to be present (and correctly configured) so that the application can start successfully: +``` +SPRING_CONFIG_ADDITIONAL_LOCATION={{ cdr_client_dir }}/config/application-customer.yaml" +LOGGING_FILE_NAME={{ cdr_client_dir }}/logs/cdr-client.log" +``` +The LOGGING_FILE_NAME is just so that the log file is not auto created where the jar is run from. + +See [Application Plugin](#application-plugin) regarding the content of the application-customer.yaml diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..2fb8c52 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,214 @@ +import io.gitlab.arturbosch.detekt.Detekt +import org.springframework.boot.gradle.tasks.bundling.BootJar +import java.net.URI + +group = "com.swisscom.health.des.cdr" +version = "3.0.0" +java.sourceCompatibility = JavaVersion.VERSION_17 + +val jvmVersion: String by project +val springCloudVersion: String by project +val jacocoVersion: String by project +val kotlinLoggingVersion: String by project +val mockkVersion: String by project +val logstashEncoderVersion: String by project +val micrometerTracingVersion: String by project +val detektKotlinVersion: String by project + +val outputDir: Provider = layout.buildDirectory.dir(".") + +plugins { + id("org.springframework.boot") + id("io.spring.dependency-management") + id("io.gitlab.arturbosch.detekt") + jacoco + application + kotlin("jvm") + kotlin("plugin.spring") + // https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.kotlin.configuration-properties + // KAPT is end of life, but KSP is not supported yet: https://github.com/spring-projects/spring-boot/issues/28046 + kotlin("kapt") + `maven-publish` + idea +} + +idea { + module { + isDownloadJavadoc = true + isDownloadSources = true + } +} + +application { + mainClass = "com.swisscom.health.des.cdr.clientvm.CdrClientVmApplicationKt" + applicationDefaultJvmArgs = listOf("-Dspring.profiles.active=client,customer") +} +dependencyManagement { + imports { + mavenBom("org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}") + } +} + +dependencies { + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-web-services") + implementation("org.springframework.boot:spring-boot-starter-actuator") + implementation("org.springframework.cloud:spring-cloud-commons") + implementation("com.squareup.okhttp3:okhttp") + implementation("org.jetbrains.kotlin:kotlin-reflect") + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core") + implementation("io.github.oshai:kotlin-logging:${kotlinLoggingVersion}") + implementation("net.logstash.logback:logstash-logback-encoder:${logstashEncoderVersion}") + implementation("io.micrometer:micrometer-tracing:${micrometerTracingVersion}") + implementation("io.micrometer:micrometer-tracing-bridge-otel:${micrometerTracingVersion}") + + kapt("org.springframework.boot:spring-boot-configuration-processor") + + testImplementation("org.jacoco:org.jacoco.core:${jacocoVersion}") + testImplementation("org.springframework.boot:spring-boot-starter-test") + testImplementation("org.springframework.boot:spring-boot-starter-webflux") + testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test") + testImplementation("com.squareup.okhttp3:mockwebserver") { + // Unfortunately we cannot exclude JUnit 4 as MockWebServer implements interfaces from that version +// exclude(group = "junit", config = "junit") + } + testImplementation("io.mockk:mockk:${mockkVersion}") { + exclude(group = "junit", module = "junit") + } + testImplementation("org.junit.jupiter:junit-jupiter") + testImplementation("io.micrometer:micrometer-tracing-test") + testRuntimeOnly("org.junit.platform:junit-platform-launcher") +} + +springBoot { + buildInfo() +} + +repositories { + mavenCentral() +} + +kotlin { + jvmToolchain { + languageVersion.set(JavaLanguageVersion.of(jvmVersion.toInt())) + } + compilerOptions { + freeCompilerArgs = listOf("-Xjsr305=strict") + } +} + +val jacocoTestCoverageVerification = tasks.named("jacocoTestCoverageVerification") { + violationRules { + /** + * Ensure tests cover at least 75% of the LoC. + */ + rule { + limit { + minimum = "0.75".toBigDecimal() + } + } + } +} + +val jacocoTestReport = tasks.named("jacocoTestReport") { + finalizedBy(jacocoTestCoverageVerification) // Verify after generating the report. + group = "Reporting" + + reports { + xml.required.set(true) + xml.outputLocation.set(File("${outputDir.get().asFile.absolutePath}/reports/jacoco.xml")) + csv.required.set(false) + html.required.set(true) + html.outputLocation.set(File("${outputDir.get().asFile.absolutePath}/reports/coverage")) + } +} + +tasks.withType { + useJUnitPlatform { + includeEngines("junit-jupiter") + } + finalizedBy(jacocoTestReport) +} + +jacoco { + toolVersion = jacocoVersion +} + +tasks.named("bootJar") { + manifest { + // Required so application name and version get rendered in the banner.txt; see + // https://stackoverflow.com/questions/34519759/application-version-does-not-show-up-in-spring-boot-banner-txt + attributes("Implementation-Title" to rootProject.name) + attributes("Implementation-Version" to archiveVersion) + } +} + +// https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.properties-and-configuration.expand-properties.gradle +// and https://stackoverflow.com/questions/40096007/how-to-configure-the-processresources-task-in-a-gradle-kotlin-build +tasks.processResources { + filesMatching("**/application.yaml") { + expand(project.properties) + } +} + +tasks.withType { + baseline.set(File("${projectDir}/detekt_baseline.xml")) + reports { + xml { + required.set(true) + outputLocation.set(File("${outputDir.get().asFile.absolutePath}/reports/detekt.xml")) + } + html.required.set(false) + sarif.required.set(false) + txt.required.set(false) + } +} + +/** + * Detekt + * See https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-gradle/ + */ +detekt { + buildUponDefaultConfig = false // preconfigure defaults + allRules = true + parallel = true + config.setFrom(files("$rootDir/config/detekt.yml")) // Global Detekt rule set. + baseline = file("$projectDir/detekt_baseline.xml") // Module specific suppression list. +} + +// https://github.com/detekt/detekt/issues/6198 +project.afterEvaluate { + configurations["detekt"].resolutionStrategy.eachDependency { + if (requested.group == "org.jetbrains.kotlin") { + useVersion(detektKotlinVersion) + } + } +} + +tasks.register("publishVersion") { + group = "publishing" + description = "Publishes boot jar" + dependsOn(tasks.withType().matching { + it.repository == publishing.repositories["GitHubPackages"] && it.publication == publishing.publications["bootJava"] + }) +} + + +publishing { + publications { + create("bootJava") { + artifact(tasks.named("bootJar")) + } + } + repositories { + maven { + name = "GitHubPackages" + url = URI("https://maven.pkg.github.com/swisscom/cdr-client") + credentials { + username = System.getenv("GITHUB_ACTOR") + password = System.getenv("GITHUB_TOKEN") + } + } + } +} diff --git a/buildSrc/README.md b/buildSrc/README.md new file mode 100644 index 0000000..0584ba5 --- /dev/null +++ b/buildSrc/README.md @@ -0,0 +1 @@ +Currently not in use diff --git a/config/detekt.yml b/config/detekt.yml new file mode 100644 index 0000000..fb6b5af --- /dev/null +++ b/config/detekt.yml @@ -0,0 +1,781 @@ +build: + maxIssues: 0 + excludeCorrectable: false + weights: + # complexity: 2 + # LongParameterList: 1 + # style: 1 + # comments: 1 + +config: + validation: true + warningsAsErrors: false + checkExhaustiveness: false + # when writing own rules with new properties, exclude the property path e.g.: 'my_rule_set,.*>.*>[my_property]' + excludes: '' + +processors: + active: true + exclude: + - 'DetektProgressListener' + # - 'KtFileCountProcessor' + # - 'PackageCountProcessor' + # - 'ClassCountProcessor' + # - 'FunctionCountProcessor' + # - 'PropertyCountProcessor' + # - 'ProjectComplexityProcessor' + # - 'ProjectCognitiveComplexityProcessor' + # - 'ProjectLLOCProcessor' + # - 'ProjectCLOCProcessor' + # - 'ProjectLOCProcessor' + # - 'ProjectSLOCProcessor' + # - 'LicenseHeaderLoaderExtension' + +console-reports: + active: true + exclude: + - 'ProjectStatisticsReport' + - 'ComplexityReport' + - 'NotificationReport' + - 'FindingsReport' + - 'FileBasedFindingsReport' + # - 'LiteFindingsReport' + +output-reports: + active: true + exclude: [] + # - 'TxtOutputReport' + # - 'XmlOutputReport' + # - 'HtmlOutputReport' + # - 'MdOutputReport' + # - 'SarifOutputReport' + +comments: + active: true + AbsentOrWrongFileLicense: + active: false + licenseTemplateFile: 'license.template' + licenseTemplateIsRegex: false + CommentOverPrivateFunction: + active: false + CommentOverPrivateProperty: + active: false + DeprecatedBlockTag: + active: false + EndOfSentenceFormat: + active: false + endOfSentenceFormat: '([.?!][ \t\n\r\f<])|([.?!:]$)' + KDocReferencesNonPublicProperty: + active: false + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + OutdatedDocumentation: + active: false + matchTypeParameters: true + matchDeclarationsOrder: true + allowParamOnConstructorProperties: false + UndocumentedPublicClass: + active: false + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + searchInNestedClass: true + searchInInnerClass: true + searchInInnerObject: true + searchInInnerInterface: true + searchInProtectedClass: false + UndocumentedPublicFunction: + active: false + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + searchProtectedFunction: false + UndocumentedPublicProperty: + active: false + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + searchProtectedProperty: false + +complexity: + active: true + CognitiveComplexMethod: + active: false + threshold: 15 + ComplexCondition: + active: true + threshold: 4 + ComplexInterface: + active: false + threshold: 10 + includeStaticDeclarations: false + includePrivateDeclarations: false + ignoreOverloaded: false + CyclomaticComplexMethod: + active: true + threshold: 15 + ignoreSingleWhenExpression: false + ignoreSimpleWhenEntries: false + ignoreNestingFunctions: false + nestingFunctions: + - 'also' + - 'apply' + - 'forEach' + - 'isNotNull' + - 'ifNull' + - 'let' + - 'run' + - 'use' + - 'with' + LabeledExpression: + active: false + ignoredLabels: [] + LargeClass: + active: true + threshold: 600 + LongMethod: + active: true + threshold: 60 + LongParameterList: + active: true + functionThreshold: 6 + constructorThreshold: 7 + ignoreDefaultParameters: false + ignoreDataClasses: true + ignoreAnnotatedParameter: [] + MethodOverloading: + active: false + threshold: 6 + NamedArguments: + active: false + threshold: 3 + ignoreArgumentsMatchingNames: false + NestedBlockDepth: + active: true + threshold: 4 + NestedScopeFunctions: + active: false + threshold: 1 + functions: + - 'kotlin.apply' + - 'kotlin.run' + - 'kotlin.with' + - 'kotlin.let' + - 'kotlin.also' + ReplaceSafeCallChainWithRun: + active: false + StringLiteralDuplication: + active: false + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + threshold: 3 + ignoreAnnotation: true + excludeStringsWithLessThan5Characters: true + ignoreStringsRegex: '$^' + TooManyFunctions: + active: true + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + thresholdInFiles: 11 + thresholdInClasses: 11 + thresholdInInterfaces: 11 + thresholdInObjects: 11 + thresholdInEnums: 11 + ignoreDeprecated: false + ignorePrivate: false + ignoreOverridden: false + ignoreAnnotatedFunctions: [] + +coroutines: + active: true + GlobalCoroutineUsage: + active: false + InjectDispatcher: + active: true + dispatcherNames: + - 'IO' + - 'Default' + - 'Unconfined' + RedundantSuspendModifier: + active: true + SleepInsteadOfDelay: + active: true + SuspendFunSwallowedCancellation: + active: false + SuspendFunWithCoroutineScopeReceiver: + active: false + SuspendFunWithFlowReturnType: + active: true + +empty-blocks: + active: true + EmptyCatchBlock: + active: true + allowedExceptionNameRegex: '_|(ignore|expected).*' + EmptyClassBlock: + active: true + EmptyDefaultConstructor: + active: true + EmptyDoWhileBlock: + active: true + EmptyElseBlock: + active: true + EmptyFinallyBlock: + active: true + EmptyForBlock: + active: true + EmptyFunctionBlock: + active: true + ignoreOverridden: false + EmptyIfBlock: + active: true + EmptyInitBlock: + active: true + EmptyKtFile: + active: true + EmptySecondaryConstructor: + active: true + EmptyTryBlock: + active: true + EmptyWhenBlock: + active: true + EmptyWhileBlock: + active: true + +exceptions: + active: true + ExceptionRaisedInUnexpectedLocation: + active: true + methodNames: + - 'equals' + - 'finalize' + - 'hashCode' + - 'toString' + InstanceOfCheckForException: + active: true + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + NotImplementedDeclaration: + active: false + ObjectExtendsThrowable: + active: false + PrintStackTrace: + active: true + RethrowCaughtException: + active: true + ReturnFromFinally: + active: true + ignoreLabeled: false + SwallowedException: + active: true + ignoredExceptionTypes: + - 'InterruptedException' + - 'MalformedURLException' + - 'NumberFormatException' + - 'ParseException' + allowedExceptionNameRegex: '_|(ignore|expected).*' + ThrowingExceptionFromFinally: + active: true + ThrowingExceptionInMain: + active: false + ThrowingExceptionsWithoutMessageOrCause: + active: true + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + exceptions: + - 'ArrayIndexOutOfBoundsException' + - 'Exception' + - 'IllegalArgumentException' + - 'IllegalMonitorStateException' + - 'IllegalStateException' + - 'IndexOutOfBoundsException' + - 'NullPointerException' + - 'RuntimeException' + - 'Throwable' + ThrowingNewInstanceOfSameException: + active: true + TooGenericExceptionCaught: + active: true + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + exceptionNames: + - 'ArrayIndexOutOfBoundsException' + - 'Error' + - 'Exception' + - 'IllegalMonitorStateException' + - 'IndexOutOfBoundsException' + - 'NullPointerException' + - 'RuntimeException' + - 'Throwable' + allowedExceptionNameRegex: '_|(ignore|expected).*' + TooGenericExceptionThrown: + active: true + exceptionNames: + - 'Error' + - 'Exception' + - 'RuntimeException' + - 'Throwable' + +naming: + active: true + BooleanPropertyNaming: + active: false + allowedPattern: '^(is|has|are)' + ClassNaming: + active: true + classPattern: '[A-Z][a-zA-Z0-9]*' + ConstructorParameterNaming: + active: true + parameterPattern: '[a-z][A-Za-z0-9]*' + privateParameterPattern: '[a-z][A-Za-z0-9]*' + excludeClassPattern: '$^' + EnumNaming: + active: true + enumEntryPattern: '[A-Z][_a-zA-Z0-9]*' + ForbiddenClassName: + active: false + forbiddenName: [] + FunctionMaxLength: + active: false + maximumFunctionNameLength: 30 + FunctionMinLength: + active: false + minimumFunctionNameLength: 3 + FunctionNaming: + active: true + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + functionPattern: '[a-z][a-zA-Z0-9]*' + excludeClassPattern: '$^' + FunctionParameterNaming: + active: true + parameterPattern: '[a-z][A-Za-z0-9]*' + excludeClassPattern: '$^' + InvalidPackageDeclaration: + active: true + rootPackage: '' + requireRootInDeclaration: false + LambdaParameterNaming: + active: false + parameterPattern: '[a-z][A-Za-z0-9]*|_' + MatchingDeclarationName: + active: true + mustBeFirst: true + MemberNameEqualsClassName: + active: true + ignoreOverridden: true + NoNameShadowing: + active: true + NonBooleanPropertyPrefixedWithIs: + active: false + ObjectPropertyNaming: + active: true + constantPattern: '[A-Za-z][_A-Za-z0-9]*' + propertyPattern: '[A-Za-z][_A-Za-z0-9]*' + privatePropertyPattern: '(_)?[A-Za-z][_A-Za-z0-9]*' + PackageNaming: + active: true + packagePattern: '[a-z]+(\.[a-z][A-Za-z0-9]*)*' + TopLevelPropertyNaming: + active: true + constantPattern: '[A-Z][_A-Z0-9]*' + propertyPattern: '[A-Za-z][_A-Za-z0-9]*' + privatePropertyPattern: '_?[A-Za-z][_A-Za-z0-9]*' + VariableMaxLength: + active: false + maximumVariableNameLength: 64 + VariableMinLength: + active: false + minimumVariableNameLength: 1 + VariableNaming: + active: true + variablePattern: '[a-z][A-Za-z0-9]*' + privateVariablePattern: '(_)?[a-z][A-Za-z0-9]*' + excludeClassPattern: '$^' + +performance: + active: true + ArrayPrimitive: + active: true + CouldBeSequence: + active: false + threshold: 3 + ForEachOnRange: + active: true + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + SpreadOperator: + active: true + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + UnnecessaryPartOfBinaryExpression: + active: false + UnnecessaryTemporaryInstantiation: + active: true + +potential-bugs: + active: true + AvoidReferentialEquality: + active: true + forbiddenTypePatterns: + - 'kotlin.String' + CastNullableToNonNullableType: + active: false + CastToNullableType: + active: false + Deprecation: + active: false + DontDowncastCollectionTypes: + active: false + DoubleMutabilityForCollection: + active: true + mutableTypes: + - 'kotlin.collections.MutableList' + - 'kotlin.collections.MutableMap' + - 'kotlin.collections.MutableSet' + - 'java.util.ArrayList' + - 'java.util.LinkedHashSet' + - 'java.util.HashSet' + - 'java.util.LinkedHashMap' + - 'java.util.HashMap' + ElseCaseInsteadOfExhaustiveWhen: + active: false + ignoredSubjectTypes: [] + EqualsAlwaysReturnsTrueOrFalse: + active: true + EqualsWithHashCodeExist: + active: true + ExitOutsideMain: + active: false + ExplicitGarbageCollectionCall: + active: true + HasPlatformType: + active: true + IgnoredReturnValue: + active: true + restrictToConfig: true + returnValueAnnotations: + - 'CheckResult' + - '*.CheckResult' + - 'CheckReturnValue' + - '*.CheckReturnValue' + ignoreReturnValueAnnotations: + - 'CanIgnoreReturnValue' + - '*.CanIgnoreReturnValue' + returnValueTypes: + - 'kotlin.sequences.Sequence' + - 'kotlinx.coroutines.flow.*Flow' + - 'java.util.stream.*Stream' + ignoreFunctionCall: [] + ImplicitDefaultLocale: + active: true + ImplicitUnitReturnType: + active: false + allowExplicitReturnType: true + InvalidRange: + active: true + IteratorHasNextCallsNextMethod: + active: true + IteratorNotThrowingNoSuchElementException: + active: true + LateinitUsage: + active: false + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + ignoreOnClassesPattern: '' + MapGetWithNotNullAssertionOperator: + active: true + MissingPackageDeclaration: + active: false + excludes: ['**/*.kts'] + NullCheckOnMutableProperty: + active: false + NullableToStringCall: + active: false + PropertyUsedBeforeDeclaration: + active: false + UnconditionalJumpStatementInLoop: + active: false + UnnecessaryNotNullCheck: + active: false + UnnecessaryNotNullOperator: + active: true + UnnecessarySafeCall: + active: true + UnreachableCatchBlock: + active: true + UnreachableCode: + active: true + UnsafeCallOnNullableType: + active: true + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**'] + UnsafeCast: + active: true + UnusedUnaryOperator: + active: true + UselessPostfixExpression: + active: true + WrongEqualsTypeParameter: + active: true + +style: + active: true + AlsoCouldBeApply: + active: false + BracesOnIfStatements: + active: false + singleLine: 'never' + multiLine: 'always' + BracesOnWhenStatements: + active: false + singleLine: 'necessary' + multiLine: 'consistent' + CanBeNonNullable: + active: false + CascadingCallWrapping: + active: false + includeElvis: true + ClassOrdering: + active: false + CollapsibleIfStatements: + active: false + DataClassContainsFunctions: + active: false + conversionFunctionPrefix: + - 'to' + allowOperators: false + DataClassShouldBeImmutable: + active: false + DestructuringDeclarationWithTooManyEntries: + active: true + maxDestructuringEntries: 3 + DoubleNegativeLambda: + active: false + negativeFunctions: + - reason: 'Use `takeIf` instead.' + value: 'takeUnless' + - reason: 'Use `all` instead.' + value: 'none' + negativeFunctionNameParts: + - 'not' + - 'non' + EqualsNullCall: + active: true + EqualsOnSignatureLine: + active: false + ExplicitCollectionElementAccessMethod: + active: false + ExplicitItLambdaParameter: + active: true + ExpressionBodySyntax: + active: false + includeLineWrapping: false + ForbiddenAnnotation: + active: false + annotations: + - reason: 'it is a java annotation. Use `Suppress` instead.' + value: 'java.lang.SuppressWarnings' + - reason: 'it is a java annotation. Use `kotlin.Deprecated` instead.' + value: 'java.lang.Deprecated' + - reason: 'it is a java annotation. Use `kotlin.annotation.MustBeDocumented` instead.' + value: 'java.lang.annotation.Documented' + - reason: 'it is a java annotation. Use `kotlin.annotation.Target` instead.' + value: 'java.lang.annotation.Target' + - reason: 'it is a java annotation. Use `kotlin.annotation.Retention` instead.' + value: 'java.lang.annotation.Retention' + - reason: 'it is a java annotation. Use `kotlin.annotation.Repeatable` instead.' + value: 'java.lang.annotation.Repeatable' + - reason: 'Kotlin does not support @Inherited annotation, see https://youtrack.jetbrains.com/issue/KT-22265' + value: 'java.lang.annotation.Inherited' + ForbiddenComment: + active: true + comments: + - reason: 'Forbidden STOPSHIP todo marker in comment, please address the problem before shipping the code.' + value: 'STOPSHIP:' + allowedPatterns: '' + ForbiddenImport: + active: false + imports: [] + forbiddenPatterns: '' + ForbiddenMethodCall: + active: false + methods: + - reason: 'print does not allow you to configure the output stream. Use a logger instead.' + value: 'kotlin.io.print' + - reason: 'println does not allow you to configure the output stream. Use a logger instead.' + value: 'kotlin.io.println' + ForbiddenSuppress: + active: false + rules: [] + ForbiddenVoid: + active: true + ignoreOverridden: false + ignoreUsageInGenerics: false + FunctionOnlyReturningConstant: + active: true + ignoreOverridableFunction: true + ignoreActualFunction: true + excludedFunctions: [] + LoopWithTooManyJumpStatements: + active: true + maxJumpCount: 1 + MagicNumber: + active: true + excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**', '**/*.kts'] + ignoreNumbers: + - '-1' + - '0' + - '1' + - '2' + ignoreHashCodeFunction: true + ignorePropertyDeclaration: false + ignoreLocalVariableDeclaration: false + ignoreConstantDeclaration: true + ignoreCompanionObjectPropertyDeclaration: true + ignoreAnnotation: false + ignoreNamedArgument: true + ignoreEnums: false + ignoreRanges: false + ignoreExtensionFunctions: true + MandatoryBracesLoops: + active: false + MaxChainedCallsOnSameLine: + active: false + maxChainedCalls: 5 + MaxLineLength: + active: true + maxLineLength: 160 + excludePackageStatements: true + excludeImportStatements: true + excludeCommentStatements: false + excludeRawStrings: true + MayBeConst: + active: true + ModifierOrder: + active: true + MultilineLambdaItParameter: + active: false + MultilineRawStringIndentation: + active: false + indentSize: 4 + trimmingMethods: + - 'trimIndent' + - 'trimMargin' + NestedClassesVisibility: + active: true + NewLineAtEndOfFile: + active: true + NoTabs: + active: false + NullableBooleanCheck: + active: false + ObjectLiteralToLambda: + active: true + OptionalAbstractKeyword: + active: true + OptionalUnit: + active: false + PreferToOverPairSyntax: + active: false + ProtectedMemberInFinalClass: + active: true + RedundantExplicitType: + active: false + RedundantHigherOrderMapUsage: + active: true + RedundantVisibilityModifierRule: + active: false + ReturnCount: + active: true + max: 2 + excludedFunctions: + - 'equals' + excludeLabeled: false + excludeReturnFromLambda: true + excludeGuardClauses: false + SafeCast: + active: true + SerialVersionUIDInSerializableClass: + active: true + SpacingBetweenPackageAndImports: + active: false + StringShouldBeRawString: + active: false + maxEscapedCharacterCount: 2 + ignoredCharacters: [] + ThrowsCount: + active: true + max: 2 + excludeGuardClauses: false + TrailingWhitespace: + active: false + TrimMultilineRawString: + active: false + trimmingMethods: + - 'trimIndent' + - 'trimMargin' + UnderscoresInNumericLiterals: + active: false + acceptableLength: 4 + allowNonStandardGrouping: false + UnnecessaryAbstractClass: + active: true + UnnecessaryAnnotationUseSiteTarget: + active: false + UnnecessaryApply: + active: true + UnnecessaryBackticks: + active: false + UnnecessaryBracesAroundTrailingLambda: + active: false + UnnecessaryFilter: + active: true + UnnecessaryInheritance: + active: true + UnnecessaryInnerClass: + active: false + UnnecessaryLet: + active: false + UnnecessaryParentheses: + active: false + allowForUnclearPrecedence: false + UntilInsteadOfRangeTo: + active: false + UnusedImports: + active: false + UnusedParameter: + active: true + allowedNames: 'ignored|expected' + UnusedPrivateClass: + active: true + UnusedPrivateMember: + active: true + allowedNames: '' + UnusedPrivateProperty: + active: true + allowedNames: '_|ignored|expected|serialVersionUID' + UseAnyOrNoneInsteadOfFind: + active: true + UseArrayLiteralsInAnnotations: + active: true + UseCheckNotNull: + active: true + UseCheckOrError: + active: true + UseDataClass: + active: false + allowVars: false + UseEmptyCounterpart: + active: false + UseIfEmptyOrIfBlank: + active: false + UseIfInsteadOfWhen: + active: false + ignoreWhenContainingVariableDeclaration: false + UseIsNullOrEmpty: + active: true + UseLet: + active: false + UseOrEmpty: + active: true + UseRequire: + active: true + UseRequireNotNull: + active: true + UseSumOfInsteadOfFlatMapSize: + active: false + UselessCallOnNotNull: + active: true + UtilityClassWithPublicConstructor: + active: true + VarCouldBeVal: + active: true + ignoreLateinitVar: false + WildcardImport: + active: true + excludeImports: + - 'java.util.*' diff --git a/detekt_baseline.xml b/detekt_baseline.xml new file mode 100644 index 0000000..d1c253e --- /dev/null +++ b/detekt_baseline.xml @@ -0,0 +1,8 @@ + + + + + MagicNumber:PasswordUtil.kt$3 + TooGenericExceptionCaught:CustomFunctionInvoker.kt$CustomFunctionInvoker$e: Exception + + diff --git a/docker-compose/docker-compose.yaml b/docker-compose/docker-compose.yaml new file mode 100644 index 0000000..512cc54 --- /dev/null +++ b/docker-compose/docker-compose.yaml @@ -0,0 +1,12 @@ +services: + wiremock: + image: local.wiremock.cdrapi + build: ./wiremock + mem_limit: 256m + ports: + - "9090:8080" + - "8443:8443" + environment: + - TZ=Europe/Zurich + command: ["--verbose", "--https-port", "8443", "--global-response-templating"] + diff --git a/docker-compose/wiremock/Dockerfile b/docker-compose/wiremock/Dockerfile new file mode 100644 index 0000000..6186de3 --- /dev/null +++ b/docker-compose/wiremock/Dockerfile @@ -0,0 +1,3 @@ +FROM wiremock/wiremock:3.6.0-alpine + +COPY ./mappings/* /home/wiremock/mappings/ diff --git a/docker-compose/wiremock/mappings/deleteDefault.json b/docker-compose/wiremock/mappings/deleteDefault.json new file mode 100644 index 0000000..d2bdfce --- /dev/null +++ b/docker-compose/wiremock/mappings/deleteDefault.json @@ -0,0 +1,17 @@ +{ + "request": { + "urlPattern": "/documents/([a-z0-9-]*)", + "method": "DELETE", + "headers": { + "cdr-connector-id": { + "doesNotMatch": "(1|5|666|1234|2345)" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*" + } + } +} diff --git a/docker-compose/wiremock/mappings/get_files_1.json b/docker-compose/wiremock/mappings/get_files_1.json new file mode 100644 index 0000000..34403f0 --- /dev/null +++ b/docker-compose/wiremock/mappings/get_files_1.json @@ -0,0 +1,167 @@ +{ + "mappings": [ + { + "scenarioName": "Get the only File for connector 1 test", + "requiredScenarioState": "Started", + "newScenarioState": "File Downloaded", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "1" + }, + "cdr-processing-mode": { + "equalTo": "test" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*", + "cdr-document-uuid": "234-test" + }, + "base64Body": "IlRoZSBjb3Ntb3MgaXMgd2l0aGluIHVzLiBXZSBhcmUgbWFkZSBvZiBzdGFyLXN0dWZmLiBXZSBhcmUgYSB3YXkgZm9yIHRoZSB1bml2ZXJzZSB0byBrbm93IGl0c2VsZi4iIC0gKENhcmwgU2FnYW4p" + } + }, + { + "scenarioName": "Get the only File for connector 1 test", + "requiredScenarioState": "File Downloaded", + "newScenarioState": "Done", + "request": { + "method": "DELETE", + "url": "/documents/234-test", + "headers": { + "cdr-connector-id": { + "equalTo": "1" + }, + "cdr-processing-mode": { + "equalTo": "test" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*" + } + } + }, + { + "scenarioName": "Get the only File for connector 1 test", + "requiredScenarioState": "Done", + "newScenarioState": "Started", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "1" + }, + "cdr-processing-mode": { + "equalTo": "test" + } + } + }, + "response": { + "status": 204, + "headers": { + "Content-Type": "*/*" + } + } + }, + + { + "scenarioName": "Get the only File for connector 1 production", + "requiredScenarioState": "Started", + "newScenarioState": "File Downloaded", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "1" + }, + "cdr-processing-mode": { + "equalTo": "production" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*", + "cdr-document-uuid": "234-prod" + }, + "base64Body": "IkJlIHlvdXJzZWxmOyBldmVyeW9uZSBlbHNlIGlzIGFscmVhZHkgdGFrZW4uIiAtIE9zY2FyIFdpbGRl" + } + }, + { + "scenarioName": "Get the only File for connector 1 production", + "requiredScenarioState": "File Downloaded", + "newScenarioState": "Done", + "request": { + "method": "DELETE", + "url": "/documents/234-prod", + "headers": { + "cdr-connector-id": { + "equalTo": "1" + }, + "cdr-processing-mode": { + "equalTo": "production" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*" + } + } + }, + { + "scenarioName": "Get the only File for connector 1 production", + "requiredScenarioState": "Done", + "newScenarioState": "Started", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "1" + }, + "cdr-processing-mode": { + "equalTo": "production" + } + } + }, + "response": { + "status": 204, + "headers": { + "Content-Type": "*/*" + } + } + } + ] +} diff --git a/docker-compose/wiremock/mappings/get_files_1234.json b/docker-compose/wiremock/mappings/get_files_1234.json new file mode 100644 index 0000000..cf9dcd0 --- /dev/null +++ b/docker-compose/wiremock/mappings/get_files_1234.json @@ -0,0 +1,30 @@ +{ + "mappings": + [ + { + "scenarioName": "Get Files for connector 1234 - none there", + "requiredScenarioState": "Started", + "newScenarioState": "Started", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "1234" + } + } + }, + "response": { + "status": 204, + "headers": { + "Content-Type": "*/*" + } + } + } + ] +} diff --git a/docker-compose/wiremock/mappings/get_files_2345.json b/docker-compose/wiremock/mappings/get_files_2345.json new file mode 100644 index 0000000..f2f9ad8 --- /dev/null +++ b/docker-compose/wiremock/mappings/get_files_2345.json @@ -0,0 +1,52 @@ +{ + "mappings": [ + { + "scenarioName": "Get the only File for connector 2345", + "requiredScenarioState": "Started", + "newScenarioState": "File Downloaded", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "2345" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*", + "cdr-document-uuid": "456" + }, + "base64Body": "IlRoZSBjb3Ntb3MgaXMgd2l0aGluIHVzLiBXZSBhcmUgbWFkZSBvZiBzdGFyLXN0dWZmLiBXZSBhcmUgYSB3YXkgZm9yIHRoZSB1bml2ZXJzZSB0byBrbm93IGl0c2VsZi4iIC0gKENhcmwgU2FnYW4p" + } + }, + { + "scenarioName": "Get the only File for connector 2345", + "requiredScenarioState": "File Downloaded", + "newScenarioState": "Started", + "request": { + "method": "DELETE", + "url": "/documents/456", + "headers": { + "cdr-connector-id": { + "equalTo": "2345" + } + } + }, + "response": { + "status": 500, + "headers": { + "Content-Type": "application/problem+json; charset=utf-8" + }, + "body": "{\"type\":\"about:blank\",\"title\":\"Internal Server Error\",\"status\":500,\"detail\":\"This will alternate with OK responses\"}" + } + } + ] +} diff --git a/docker-compose/wiremock/mappings/get_files_5.json b/docker-compose/wiremock/mappings/get_files_5.json new file mode 100644 index 0000000..94b049d --- /dev/null +++ b/docker-compose/wiremock/mappings/get_files_5.json @@ -0,0 +1,217 @@ +{ + "mappings": [ + { + "scenarioName": "Get all Files for connector 5", + "requiredScenarioState": "Started", + "newScenarioState": "File Downloaded 1", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "5" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*", + "cdr-document-uuid": "999" + }, + "base64Body": "IlRoZSBjb3Ntb3MgaXMgd2l0aGluIHVzLiBXZSBhcmUgbWFkZSBvZiBzdGFyLXN0dWZmLiBXZSBhcmUgYSB3YXkgZm9yIHRoZSB1bml2ZXJzZSB0byBrbm93IGl0c2VsZi4iIC0gKENhcmwgU2FnYW4p" + } + }, + { + "scenarioName": "Get all Files for connector 5", + "requiredScenarioState": "File Downloaded 1", + "newScenarioState": "Download File 2", + "request": { + "method": "DELETE", + "url": "/documents/999", + "headers": { + "cdr-connector-id": { + "equalTo": "5" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*" + } + } + }, + { + "scenarioName": "Get all Files for connector 5", + "requiredScenarioState": "Download File 2", + "newScenarioState": "File Downloaded 2", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "5" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*", + "cdr-document-uuid": "888" + }, + "base64Body": "IkJlIHlvdXJzZWxmOyBldmVyeW9uZSBlbHNlIGlzIGFscmVhZHkgdGFrZW4uIiAtIE9zY2FyIFdpbGRl" + } + }, + { + "scenarioName": "Get all Files for connector 5", + "requiredScenarioState": "File Downloaded 2", + "newScenarioState": "Download File 3", + "request": { + "method": "DELETE", + "url": "/documents/888", + "headers": { + "cdr-connector-id": { + "equalTo": "5" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*" + } + } + }, + { + "scenarioName": "Get all Files for connector 5", + "requiredScenarioState": "Download File 3", + "newScenarioState": "File Downloaded 3", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "5" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*", + "cdr-document-uuid": "777" + }, + "base64Body": "IkEgcm9vbSB3aXRob3V0IGJvb2tzIGlzIGxpa2UgYSBib2R5IHdpdGhvdXQgYSBzb3VsLiIgLSBNYXJjdXMgVHVsbGl1cyBDaWNlcm8=" + } + }, + { + "scenarioName": "Get all Files for connector 5", + "requiredScenarioState": "File Downloaded 3", + "newScenarioState": "Download File 4", + "request": { + "method": "DELETE", + "url": "/documents/777", + "headers": { + "cdr-connector-id": { + "equalTo": "5" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*" + } + } + }, + { + "scenarioName": "Get all Files for connector 5", + "requiredScenarioState": "Download File 4", + "newScenarioState": "File Downloaded 4", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "5" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*", + "cdr-document-uuid": "666" + }, + "base64Body": "IkJlIHRoZSBjaGFuZ2UgdGhhdCB5b3Ugd2lzaCB0byBzZWUgaW4gdGhlIHdvcmxkLiIgLSBNYWhhdG1hIEdhbmRoaQ==" + } + }, + { + "scenarioName": "Get all Files for connector 5", + "requiredScenarioState": "File Downloaded 4", + "newScenarioState": "No more Files", + "request": { + "method": "DELETE", + "url": "/documents/666", + "headers": { + "cdr-connector-id": { + "equalTo": "5" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "*/*" + } + } + }, + { + "scenarioName": "Get all Files for connector 5", + "requiredScenarioState": "No more Files", + "newScenarioState": "Started", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "5" + } + } + }, + "response": { + "status": 204, + "headers": { + "Content-Type": "*/*" + } + } + } + ] +} diff --git a/docker-compose/wiremock/mappings/get_files_666.json b/docker-compose/wiremock/mappings/get_files_666.json new file mode 100644 index 0000000..322b06e --- /dev/null +++ b/docker-compose/wiremock/mappings/get_files_666.json @@ -0,0 +1,31 @@ +{ + "mappings": + [ + { + "scenarioName": "Get Files for connector 666 - Internal Server Error", + "requiredScenarioState": "Started", + "newScenarioState": "Started", + "request": { + "method": "GET", + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "headers": { + "cdr-connector-id": { + "equalTo": "666" + } + } + }, + "response": { + "status": 500, + "headers": { + "Content-Type": "application/problem+json; charset=utf-8" + }, + "body": "{\"type\":\"about:blank\",\"title\":\"Internal Server Error\",\"status\":500,\"detail\":\"This will be repeated endlessly\"}" + } + } + ] +} diff --git a/docker-compose/wiremock/mappings/pullDefault.json b/docker-compose/wiremock/mappings/pullDefault.json new file mode 100644 index 0000000..1d9332f --- /dev/null +++ b/docker-compose/wiremock/mappings/pullDefault.json @@ -0,0 +1,57 @@ +{ + "mappings": [ + { + "scenarioName": "Default pull", + "requiredScenarioState": "Started", + "newScenarioState": "NoContent", + "request": { + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "method": "GET", + "headers": { + "cdr-connector-id": { + "doesNotMatch": "(1|5|666|1234|2345)" + } + } + }, + "response": { + "transformers": ["random-value"], + "status": 200, + "headers": { + "Content-Type": "*/*", + "cdr-document-uuid": "{{randomValue length=10 type='ALPHANUMERIC'}}" + }, + "base64Body": "IlRoZSBjb3Ntb3MgaXMgd2l0aGluIHVzLiBXZSBhcmUgbWFkZSBvZiBzdGFyLXN0dWZmLiBXZSBhcmUgYSB3YXkgZm9yIHRoZSB1bml2ZXJzZSB0byBrbm93IGl0c2VsZi4iIC0gKENhcmwgU2FnYW4p" + } + }, + { + "scenarioName": "Default pull", + "requiredScenarioState": "NoContent", + "newScenarioState": "Started", + "request": { + "urlPath": "/documents", + "queryParameters": { + "limit": { + "matches": "1" + } + }, + "method": "GET", + "headers": { + "cdr-connector-id": { + "doesNotMatch": "(1|5|666|1234|2345)" + } + } + }, + "response": { + "status": 204, + "headers": { + "Content-Type": "*/*" + } + } + } + ] +} diff --git a/docker-compose/wiremock/mappings/pushDefault.json b/docker-compose/wiremock/mappings/pushDefault.json new file mode 100644 index 0000000..4754f7b --- /dev/null +++ b/docker-compose/wiremock/mappings/pushDefault.json @@ -0,0 +1,22 @@ +{ + "mappings": [ + { + "request": { + "urlPath": "/documents", + "method": "POST", + "headers": { + "cdr-connector-id": { + "doesNotMatch": "(1|5|666)" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "application/json; charset=utf-8" + }, + "body": "{\"message\": \"Upload successful\"}" + } + } + ] +} diff --git a/docker-compose/wiremock/mappings/push_files_1.json b/docker-compose/wiremock/mappings/push_files_1.json new file mode 100644 index 0000000..9a6577e --- /dev/null +++ b/docker-compose/wiremock/mappings/push_files_1.json @@ -0,0 +1,48 @@ +{ + "mappings": [ + { + "request": { + "method": "POST", + "urlPath": "/documents", + "headers": { + "cdr-connector-id": { + "equalTo": "1" + }, + "cdr-processing-mode": { + "not": { + "equalTo": "test" + } + } + } + }, + "response": { + "status": 400, + "headers": { + "Content-Type": "application/problem+json; charset=utf-8" + }, + "body": "{\"type\":\"about:blank\",\"title\":\"Bad Request\",\"status\":400,\"detail\":\"Invalid input.\"}" + } + }, + { + "request": { + "method": "POST", + "url": "/documents", + "headers": { + "cdr-connector-id": { + "equalTo": "1" + }, + "cdr-processing-mode": { + "equalTo": "test" + } + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "application/json; charset=utf-8" + }, + "body": "{\"message\": \"Upload successful\"}" + } + } + ] +} diff --git a/docker-compose/wiremock/mappings/push_files_5.json b/docker-compose/wiremock/mappings/push_files_5.json new file mode 100644 index 0000000..54b8dd0 --- /dev/null +++ b/docker-compose/wiremock/mappings/push_files_5.json @@ -0,0 +1,22 @@ +{ + "mappings": [ + { + "request": { + "method": "POST", + "urlPath": "/documents", + "headers": { + "cdr-connector-id": { + "equalTo": "5" + } + } + }, + "response": { + "status": 500, + "headers": { + "Content-Type": "application/problem+json; charset=utf-8" + }, + "body": "{\"type\":\"about:blank\",\"title\":\"Internal Server Error\",\"status\":500,\"detail\":\"This will be repeated endlessly\"}" + } + } + ] +} diff --git a/docker-compose/wiremock/mappings/push_files_666.json b/docker-compose/wiremock/mappings/push_files_666.json new file mode 100644 index 0000000..ccd53be --- /dev/null +++ b/docker-compose/wiremock/mappings/push_files_666.json @@ -0,0 +1,22 @@ +{ + "mappings": [ + { + "request": { + "method": "POST", + "urlPath": "/documents", + "headers": { + "cdr-connector-id": { + "equalTo": "666" + } + } + }, + "response": { + "status": 403, + "headers": { + "Content-Type": "application/problem+json; charset=utf-8" + }, + "body": "{\"type\":\"about:blank\",\"title\":\"Forbidden\",\"status\":403,\"detail\":\"The user is not authorized.\"}" + } + } + ] +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..adab95a --- /dev/null +++ b/gradle.properties @@ -0,0 +1,26 @@ +kotlin.code.style=official +############ +# Version Management +############ +############ +# Kotlin +############ +jvmVersion=17 +kotlinVersion=1.9.24 +kotlinxSerializationVersion=1.6.3 +############ +# Plugins +############ +detektVersion=1.23.6 +springBootVersion=3.3.0 +springDependencyManagementVersion=1.1.5 +############ +# Dependencies +############ +detektKotlinVersion=1.9.23 +jacocoVersion=0.8.12 +kotlinLoggingVersion=6.0.9 +logstashEncoderVersion=7.4 +micrometerTracingVersion=1.3.1 +mockkVersion=1.13.11 +springCloudVersion=2023.0.2 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..943f0cb Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..2617362 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip +networkTimeout=10000 +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..65dcd68 --- /dev/null +++ b/gradlew @@ -0,0 +1,244 @@ +#!/bin/sh + +# +# Copyright Š 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions ÂĢ$varÂģ, ÂĢ${var}Âģ, ÂĢ${var:-default}Âģ, ÂĢ${var+SET}Âģ, +# ÂĢ${var#prefix}Âģ, ÂĢ${var%suffix}Âģ, and ÂĢ$( cmd )Âģ; +# * compound commands having a testable exit status, especially ÂĢcaseÂģ; +# * various built-in commands including ÂĢcommandÂģ, ÂĢsetÂģ, and ÂĢulimitÂģ. +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..93e3f59 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..1305796 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,17 @@ +rootProject.name = "cdr-client" + +pluginManagement { + val kotlinVersion: String by settings + val springBootVersion: String by settings + val springDependencyManagementVersion: String by settings + val detektVersion: String by settings + + plugins { + id("org.springframework.boot") version springBootVersion + id("io.spring.dependency-management") version springDependencyManagementVersion + id("io.gitlab.arturbosch.detekt") version detektVersion + kotlin("jvm") version kotlinVersion + kotlin("plugin.spring") version kotlinVersion + kotlin("kapt") version kotlinVersion + } +} diff --git a/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/CdrClientVmApplication.kt b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/CdrClientVmApplication.kt new file mode 100644 index 0000000..337e980 --- /dev/null +++ b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/CdrClientVmApplication.kt @@ -0,0 +1,20 @@ +package com.swisscom.health.des.cdr.clientvm + +import com.swisscom.health.des.cdr.clientvm.config.CdrClientConfig +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.boot.runApplication +import org.springframework.scheduling.annotation.EnableScheduling + +/** + * Spring Boot entry point + */ +@SpringBootApplication +@EnableConfigurationProperties(CdrClientConfig::class) +@EnableScheduling +class CdrClientVmApplication + +@Suppress("SpreadOperator") +fun main(args: Array) { + runApplication(*args) +} diff --git a/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/config/CdrClientConfig.kt b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/config/CdrClientConfig.kt new file mode 100644 index 0000000..67b4ab1 --- /dev/null +++ b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/config/CdrClientConfig.kt @@ -0,0 +1,84 @@ +package com.swisscom.health.des.cdr.clientvm.config + +import io.github.oshai.kotlinlogging.KotlinLogging +import jakarta.annotation.PostConstruct +import org.springframework.boot.context.properties.ConfigurationProperties +import java.io.File +import java.time.Duration + + +private val logger = KotlinLogging.logger {} + +/** + * CDR client specific configuration + */ +@ConfigurationProperties("client") +data class CdrClientConfig( + val functionKey: String, + val scheduleDelay: String, + val localFolder: String, + val endpoint: Endpoint, + val customer: List, + val pullThreadPoolSize: Int, + val pushThreadPoolSize: Int, + val retryDelay: Array, +) { + + /** + * Clients identified by their customer id + */ + data class Connector( + val connectorId: String, + val targetFolder: String, + val sourceFolder: String, + val contentType: String, + val mode: Mode, + ) + + /** + * CDR API definition + */ + data class Endpoint( + val scheme: String, + val host: String, + val port: Int, + val basePath: String, + ) + + enum class Mode(val value: String) { + TEST("test"), PRODUCTION("production"); + } + + @PostConstruct + fun checkAndReport() { + if (customer.isEmpty()) { + error("There where no customer entries configured") + } + // we don't check target folder for duplicate as this can be configured deliberately by customers + if (duplicateFolderUsage() || duplicateUsage(Connector::sourceFolder)) { + error("Duplicate folder usage detected. Please make sure that each customer has a unique source and that no target folder is used " + + "at the same time as source folder.") + } + checkNoConnectorIdHasTheSameModeDefinedTwice() + File(localFolder).mkdirs() + logger.debug { "Client configuration: $this" } + } + + private fun duplicateFolderUsage(): Boolean = customer.map { it.sourceFolder }.intersect(customer.map { it.targetFolder }.toSet()).isNotEmpty() + + private fun duplicateUsage(selector: (Connector) -> T): Boolean = + customer.map(selector).distinct().size != customer.size + + private fun checkNoConnectorIdHasTheSameModeDefinedTwice(): Unit = + customer.groupBy { it.connectorId }.filter { cd -> cd.value.size > 1 }.values.forEach { connector -> + val modeConnectorsMap: Map> = connector.groupBy { cr -> cr.mode } + if (modeConnectorsMap.values.any { it.size > 1 }) { + error("A single connector ID has production or test defined twice.") + } + } + + override fun toString(): String { + return "CdrClientConfig(functionKey='xxx', scheduleDelay='$scheduleDelay', localFolder='$localFolder', " + + "customer=$customer, endpoint=$endpoint)" + } +} diff --git a/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/config/CdrClientContext.kt b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/config/CdrClientContext.kt new file mode 100644 index 0000000..d6f79f1 --- /dev/null +++ b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/config/CdrClientContext.kt @@ -0,0 +1,50 @@ +package com.swisscom.health.des.cdr.clientvm.config + +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.asCoroutineDispatcher +import okhttp3.OkHttpClient +import org.springframework.beans.factory.annotation.Value +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit + +/** + * A Spring configuration class for creating and configuring beans used by the CDR client. + */ +@Configuration +class CdrClientContext { + + /** + * Creates and returns an instance of the OkHttpClient. + * + * @param builder The OkHttpClient.Builder used to build the client. + * @return The fully constructed OkHttpClient. + */ + @Bean + fun okHttpClient( + builder: OkHttpClient.Builder, + @Value("\${client.connection-timeout-ms}") timeout: Long, + @Value("\${client.read-timeout-ms}") readTimeout: Long + ): OkHttpClient = + builder.connectTimeout(timeout, TimeUnit.MILLISECONDS).readTimeout(readTimeout, TimeUnit.MILLISECONDS) + .build() + + /** + * Creates and returns an instance of the OkHttpClient.Builder if one does not already exist. + * + * @return The OkHttpClient.Builder instance. + */ + @Bean + @ConditionalOnMissingBean + fun okHttpClientBuilder(): OkHttpClient.Builder? { + return OkHttpClient.Builder() + } + + @Bean(name = ["pullDispatcher"]) + fun pullDispatcher(config: CdrClientConfig): CoroutineDispatcher = Executors.newFixedThreadPool(config.pullThreadPoolSize).asCoroutineDispatcher() + + @Bean(name = ["pushDispatcher"]) + fun pushDispatcher(config: CdrClientConfig): CoroutineDispatcher = Executors.newFixedThreadPool(config.pushThreadPoolSize).asCoroutineDispatcher() +} diff --git a/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/handler/FileHandlingBase.kt b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/handler/FileHandlingBase.kt new file mode 100644 index 0000000..e8efbfb --- /dev/null +++ b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/handler/FileHandlingBase.kt @@ -0,0 +1,87 @@ +package com.swisscom.health.des.cdr.clientvm.handler + +import com.swisscom.health.des.cdr.clientvm.config.CdrClientConfig +import io.github.oshai.kotlinlogging.KLogger +import io.micrometer.tracing.Tracer +import okhttp3.Headers +import org.springframework.util.LinkedMultiValueMap +import org.springframework.util.MultiValueMap +import org.springframework.web.util.UriComponentsBuilder +import java.net.URL +import java.nio.file.Files +import java.nio.file.Path + + +internal const val FUNCTION_KEY_HEADER = "x-functions-key" +internal const val CONNECTOR_ID_HEADER = "cdr-connector-id" +internal const val CDR_PROCESSING_MODE_HEADER = "cdr-processing-mode" +internal const val AZURE_TRACE_ID_HEADER = "x-ms-request-id" + +/** + * Helper function to check for each call if a path is a directory and writable. + * This to prevent unexpected behaviour should access rights change during runtime. + */ +fun pathIsDirectoryAndWritable(path: Path, what: String, logger: KLogger): Boolean = + when { + !Files.isDirectory(path) -> { + logger.error { "Configured path '$path' isn't a directory. Therefore no files are $what until a directory is configured." } + false + } + + !Files.isWritable(path) -> { + logger.error { "Configured path '$path' isn't writable by running user. Therefore no files are $what until access rights are corrected." } + false + } + + else -> true + } + +/** + * Basic functionality needed in all FileHandling classes + */ +abstract class FileHandlingBase(protected val cdrClientConfig: CdrClientConfig, private val tracer: Tracer) { + + /** + * Builds a target URL from an endpoint and a path. + * + * @param path the path to add to the URL + * @return the resulting URL + */ + protected fun buildTargetUrl(path: String, queryParameters: MultiValueMap = LinkedMultiValueMap()): URL { + return UriComponentsBuilder + .newInstance() + .scheme(cdrClientConfig.endpoint.scheme) + .host(cdrClientConfig.endpoint.host) + .port(cdrClientConfig.endpoint.port) + .path(path) + .queryParams(queryParameters) + .build() + .toUri() + .toURL() + } + + /** + * Build headers with connector-id, function key, processing mode and trace id. + */ + protected fun buildBaseHeaders(connectorId: String, mode: CdrClientConfig.Mode): Headers { + val traceId = tracer.currentSpan()?.context()?.traceId() ?: "" + return Headers.Builder().run { + this[CONNECTOR_ID_HEADER] = connectorId + this[FUNCTION_KEY_HEADER] = cdrClientConfig.functionKey + this[CDR_PROCESSING_MODE_HEADER] = mode.value + this[AZURE_TRACE_ID_HEADER] = traceId + this.build() + } + } + + /** + * Helper function to run a block of code with a new span. + */ + protected fun traced(spanName: String, block: () -> A): A { + val newSpan = tracer.spanBuilder().name(spanName).start() + return tracer.withSpan(newSpan).use { + block() + } + } + +} diff --git a/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PullFileHandling.kt b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PullFileHandling.kt new file mode 100644 index 0000000..1171fbf --- /dev/null +++ b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PullFileHandling.kt @@ -0,0 +1,234 @@ +package com.swisscom.health.des.cdr.clientvm.handler + +import com.swisscom.health.des.cdr.clientvm.config.CdrClientConfig +import io.github.oshai.kotlinlogging.KotlinLogging +import io.micrometer.tracing.Tracer +import okhttp3.Headers +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.Response +import org.springframework.http.HttpStatus +import org.springframework.stereotype.Component +import org.springframework.util.LinkedMultiValueMap +import java.io.File +import java.io.IOException +import java.io.InputStream +import java.net.URL +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.StandardCopyOption +import kotlin.io.path.outputStream + +private val logger = KotlinLogging.logger {} +internal const val PULL_RESULT_ID_HEADER = "cdr-document-uuid" + +/** + * A class responsible for handling files for a customer by syncing the files to a local folder, + * and moving the files to the customer folder after successfully downloading them locally. + */ +@Component +@Suppress("TooManyFunctions") +class PullFileHandling( + cdrClientConfig: CdrClientConfig, + private val httpClient: OkHttpClient, + tracer: Tracer, +) : FileHandlingBase(cdrClientConfig, tracer) { + /** + * Downloads files for a specific customer. + * + * @param connector the connector to synchronize + */ + suspend fun pullSyncConnector(connector: CdrClientConfig.Connector) { + traced("Pull Sync Connector ${connector.connectorId}") { + logger.info { "Sync connector '${connector.connectorId}' (${connector.mode}) - pulling" } + var counter = 0 + var tryNext: Boolean + runCatching { + do { + tryNext = checkDirectoryAndProcessFile(connector) + if (tryNext) counter++ + } while (tryNext) + }.onFailure { + logger.info { "Synced '$counter' file(s) before exception happened" } + throw it + } + logger.info { "Sync connector done - '$counter' file(s) pulled" } + } + } + + private fun checkDirectoryAndProcessFile(connector: CdrClientConfig.Connector): Boolean = + if (pathIsDirectoryAndWritable(Path.of(connector.targetFolder), "pulled", logger)) { + requestFileAndDecideIfNextFileShouldBeCalled(connector) + } else { + false + } + + /** + * Requests a file and decides if the next file should be called. + * + * @param connector the connector to request a file for + * @return whether to try the next file + */ + private fun requestFileAndDecideIfNextFileShouldBeCalled(connector: CdrClientConfig.Connector): Boolean = + requestFile(connector).use { response -> + return when { + response.isNoContentFound() -> false + + !response.isSuccessWithContent() -> { + logger.error { + "Error requesting file for connector '${connector.connectorId}' (${connector.mode}), because of: (${response.code}) ${response.message}" + } + false + } + + else -> { + val pullResultHeader = response.headers[PULL_RESULT_ID_HEADER] + return if (pullResultHeader == null) { + logger.error { "didn't receive header '$PULL_RESULT_ID_HEADER'" } + false + } else { + syncFileToLocalFolder(response.body!!.byteStream(), pullResultHeader) + acknowledgeFileDownload(connector, pullResultHeader) + && moveFileToClientFolder(connector, pullResultHeader) + } + } + } + } + + /** + * Sends a GET-Request to the CDR API to request a file. + * + * @param connector the connector to request a file for + * @return the HTTP response for the request + */ + private fun requestFile(connector: CdrClientConfig.Connector): Response { + logger.debug { "Request file start" } + val queryParameters = LinkedMultiValueMap() + queryParameters.add("limit", "1") + val response = httpClient.newCall( + createGetRequest( + buildTargetUrl(cdrClientConfig.endpoint.basePath, queryParameters), buildBaseHeaders(connector.connectorId, connector.mode) + ) + ).execute() + logger.debug { "Request file done" } + return response + } + + /** + * Checks if an HTTP response is successful. + * + * @return whether the response is a success or not + */ + fun Response.isSuccessWithContent(): Boolean = this.isSuccessful && this.body != null + + /** + * Checks if the HTTP response is of status code 204 (NO_CONTENT). + */ + fun Response.isNoContentFound(): Boolean = this.code == HttpStatus.NO_CONTENT.value() + + /** + * Creates a GET request with the given target and headers + * @param to the target URL for the request + * @param headers the headers for the request + * @return the created GET request + */ + private fun createGetRequest(to: URL, headers: Headers): Request { + return Request.Builder() + .url(to) + .headers(headers) + .get() + .build() + } + + /** + * Creates a DELETE request with the given target and headers + * @param to the target URL for the request + * @param headers the headers for the request + * @return the created DELETE request + */ + private fun createDeleteRequest(to: URL, headers: Headers): Request { + return Request.Builder() + .url(to) + .headers(headers) + .delete() + .build() + } + + /** + * Synchronizes the file for the given transaction ID to the local folder + * @param inputStream the input stream for the file + * @param pullResultId the ID to identify the pulled file + */ + private fun syncFileToLocalFolder(inputStream: InputStream, pullResultId: String) = + Path.of(cdrClientConfig.localFolder, getTempFileName(pullResultId)).outputStream().use { output -> inputStream.copyTo(output) } + + /** + * Reports the success of the transaction to the connectors' endpoint. + * Needs to be done after each file as otherwise the CDR API will provide the same file again. + * + * @param connector the connector for whom the file was requested + * @param pullResultId the ID to identify the pulled file + * @return true if the reporting was successful, false otherwise + */ + private fun acknowledgeFileDownload(connector: CdrClientConfig.Connector, pullResultId: String): Boolean { + logger.debug { "Acknowledge pulled file start" } + httpClient.newCall( + createDeleteRequest( + buildTargetUrl("${cdrClientConfig.endpoint.basePath}/$pullResultId"), + buildBaseHeaders(connector.connectorId, connector.mode) + ) + ).execute().use { response -> + return if (response.isSuccessful) { + logger.debug { "Acknowledge pulled file done" } + true + } else { + logger.error { + "Error when acknowledging file with identifier '$pullResultId' of connector '${connector.connectorId}' (${connector.mode}): " + + "(${response.code}) ${response.message}" + } + false + } + } + } + + /** + * Moves the file from the local folder to the connectors target folder. + * If the target folder does not exist, it will be created. + * If the file already exists in the target folder, it will be overwritten. + * The file that is currently on the local file system, without file extension, will be moved to the target folder, also with no file extension. + * The file extension is changed from .tmp to .xml after a successful file copy. + * + * @param connector the connector for whom the file was requested + * @param pullResultId the ID to identify the pulled file + */ + private fun moveFileToClientFolder(connector: CdrClientConfig.Connector, pullResultId: String): Boolean { + return try { + logger.debug { "Move file to target directory start" } + val tmpFileName = getTempFileName(pullResultId) + val sourceFile = File(cdrClientConfig.localFolder, tmpFileName) + val targetFolder = File(connector.targetFolder) + targetFolder.mkdirs() + val targetFile = File(targetFolder, tmpFileName) + Files.move( + sourceFile.toPath(), + targetFile.toPath(), + StandardCopyOption.REPLACE_EXISTING + ) + logger.debug { "Move file to target directory done" } + // be aware, that this is not an atomic operation on Windows operating system (but it is on Unix-based systems) + if (targetFile.renameTo(targetFile.toPath().resolveSibling("$pullResultId.xml").toFile())) { + logger.debug { "Rename file in target directory done" } + } else { + logger.error { "Unable to rename file for transactionId '$pullResultId'" } + return false + } + true + } catch (e: IOException) { + logger.error { "Unable to move file for transactionId '$pullResultId': ${e.message}" } + false + } + } + + private fun getTempFileName(pullResultId: String) = "${pullResultId}.tmp" + +} diff --git a/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PushFileHandling.kt b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PushFileHandling.kt new file mode 100644 index 0000000..cb751c9 --- /dev/null +++ b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PushFileHandling.kt @@ -0,0 +1,163 @@ +package com.swisscom.health.des.cdr.clientvm.handler + +import com.swisscom.health.des.cdr.clientvm.config.CdrClientConfig +import io.github.oshai.kotlinlogging.KotlinLogging +import io.micrometer.tracing.Tracer +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import okhttp3.Headers +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody +import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.Response +import org.springframework.http.HttpStatus +import org.springframework.stereotype.Component +import java.net.URL +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.StandardOpenOption +import kotlin.io.path.deleteIfExists +import kotlin.io.path.readBytes + + +private val logger = KotlinLogging.logger {} + +/** + * A class responsible for handling files for a connector by syncing the files from a local folder to the CDR API. + * Only files with an '.xml' extension are uploaded. + * 4xx errors are not retried, the xml is rewritten to .xml.error and a .xml.response file is created with the response body. + * Other non-2xx responses are retried with a delay. + * Deletes the local file after successful upload. + */ +@Component +@Suppress("TooManyFunctions") +class PushFileHandling( + cdrClientConfig: CdrClientConfig, + private val httpClient: OkHttpClient, + tracer: Tracer, +) : FileHandlingBase(cdrClientConfig, tracer) { + + /** + * Uploads files for a specific customer. + * @param connector the connector to synchronize + */ + suspend fun pushSyncConnector(connector: CdrClientConfig.Connector) { + traced("Push Sync Connector ${connector.connectorId}") { + logger.info { "Sync connector '${connector.connectorId}' (${connector.mode}) - pushing" } + val path = Path.of(connector.sourceFolder) + var counter = 0 + if (pathIsDirectoryAndWritable(path, "uploaded", logger)) { + runCatching { + Files.list(path).use { files -> + files.filter { it.fileName.toString().endsWith(".xml") } + .forEach { file -> + runBlocking { + handleFile(file, connector, counter).let { newCount -> + counter = newCount + } + } + } + } + }.onFailure { + logger.info { "Synced '$counter' file(s) for connector '${connector.connectorId}' (${connector.mode}) before exception happened" } + throw it + } + } + logger.info { "Sync connector done - '$counter' file(s) pushed" } + } + } + + /** + * Retries the upload of a file until it is successful or a 4xx error occured. + */ + private suspend fun handleFile(file: Path, connector: CdrClientConfig.Connector, counter: Int): Int { + logger.debug { "Push file '$file'" } + var returnCount = counter + var retryCount = 0 + var retryNeeded = true + while (retryNeeded) { + writeFileToApi(file, connector).use { + retryNeeded = handleResponseAndReturnTrueIfRetryIsNeeded(file, it, retryCount) + if (!retryNeeded && it.isSuccessful) { + returnCount++ + } + if (retryNeeded && retryCount < cdrClientConfig.retryDelay.size - 1) { + retryCount++ + } + } + } + return returnCount + } + + private fun writeFileToApi(file: Path, connector: CdrClientConfig.Connector): Response { + // TODO: change this to handle big files + return httpClient.newCall( + createPostRequest( + file.readBytes().toRequestBody(connector.contentType.toMediaType()), + buildTargetUrl(cdrClientConfig.endpoint.basePath), + buildBaseHeaders(connector.connectorId, connector.mode) + ) + ).execute() + } + + /** + * Checks the HTTP return status of the API and decides whether a retry is needed and/or an error needs to be logged. + */ + private suspend fun handleResponseAndReturnTrueIfRetryIsNeeded(file: Path, response: Response, retryCount: Int): Boolean { + val responseStatus: HttpStatus? = HttpStatus.resolve(response.code) + return if (responseStatus != null) { + when { + responseStatus.is2xxSuccessful -> { + if (!file.deleteIfExists()) { + logger.warn { "Tried to delete the file '$file' but it was already gone" } + } + false + } + + responseStatus.is4xxClientError -> { + logger.error { + "File synchronization failed for '${file.fileName}'. Received a 4xx client error (response code: '${response.code}'). " + + "No retry will be attempted due to client-side issue." + } + renameFileToErrorAndCreateLogFile(file, response) + false + } + + else -> { + logger.error { + "Failed to sync file '${file.fileName}', retry will be attempted in '${cdrClientConfig.retryDelay[retryCount]}' - " + + "'${response.message}': ${response.body?.string() ?: "no response body"}" + } + delay(cdrClientConfig.retryDelay[retryCount].toMillis()) + true + } + } + } else { + logger.error { "Unknown HTTP response code retrieved ('${response.code}') - couldn't evaluate HttpStatus" } + true + } + } + + /** + * For an error case and to prevent a failed file to be uploaded again it renames the file to '.error' and creates a file with the response body. + */ + private fun renameFileToErrorAndCreateLogFile(file: Path, response: Response) { + val errorFile = file.resolveSibling("${file.fileName}.error") + val logFile = file.resolveSibling("${file.fileName}.response") + if (!file.toFile().renameTo(errorFile.toFile())) { + logger.error { "Failed to rename file '${file.fileName}'" } + } + response.body?.byteStream() + ?.use { inputStream -> Files.write(logFile, inputStream.readBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE) } + } + + private fun createPostRequest(requestBody: RequestBody, to: URL, headers: Headers): Request = + Request.Builder() + .url(to) + .headers(headers) + .post(requestBody) + .build() + +} diff --git a/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/scheduling/Scheduler.kt b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/scheduling/Scheduler.kt new file mode 100644 index 0000000..ae6f8d2 --- /dev/null +++ b/src/main/kotlin/com/swisscom/health/des/cdr/clientvm/scheduling/Scheduler.kt @@ -0,0 +1,88 @@ +package com.swisscom.health.des.cdr.clientvm.scheduling + +import com.swisscom.health.des.cdr.clientvm.config.CdrClientConfig +import com.swisscom.health.des.cdr.clientvm.handler.PullFileHandling +import com.swisscom.health.des.cdr.clientvm.handler.PushFileHandling +import com.swisscom.health.des.cdr.clientvm.handler.pathIsDirectoryAndWritable +import io.github.oshai.kotlinlogging.KotlinLogging +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext +import org.springframework.scheduling.annotation.Scheduled +import org.springframework.stereotype.Service +import java.nio.file.Path + +private val logger = KotlinLogging.logger {} + +/** + * A Spring service that defines a scheduled task to synchronize files to client folders. + * @property cdrClientConfig An instance of [CdrClientConfig], which is a configuration class for the CDR client. + * @property pullFileHandling An instance of [PullFileHandling], which is a service that provides methods for syncing files to client folders. + */ +@Service +class Scheduler( + private val cdrClientConfig: CdrClientConfig, + private val pullFileHandling: PullFileHandling, + private val pushFileHandling: PushFileHandling, +) { + + /** + * A scheduled task that syncs files to client folders at regular intervals. + */ + @Scheduled(fixedDelayString = "\${client.schedule-delay}") + fun syncFilesToClientFolders() { + logger.info { "Triggered pull sync" } + runBlocking { + callPullFileHandling() + } + } + + /** + * Calls the file handling service for each connector in the configuration, in parallel using coroutines. + */ + private suspend fun callPullFileHandling() { + withContext(Dispatchers.IO) { + if (pathIsDirectoryAndWritable(Path.of(cdrClientConfig.localFolder), "pulled", logger)) { + cdrClientConfig.customer.forEach { connector -> + launch { + runCatching { + pullFileHandling.pullSyncConnector(connector) + }.onFailure { + logger.error(it) { "Error syncing connector '${connector.connectorId}'. Reason: ${it.message}" } + } + } + } + } + } + } + + /** + * A scheduled task that syncs local files to the CDR API at regular intervals. + */ + @Scheduled(fixedDelayString = "\${client.schedule-delay}") + fun syncFilesToApi() { + logger.info { "Triggered push sync" } + runBlocking { + callPushFileHandling() + } + } + + /** + * Calls the file handling service for each connector in the configuration, in parallel using coroutines. + */ + private suspend fun callPushFileHandling() { + withContext(Dispatchers.IO) { + cdrClientConfig.customer.forEach { connector -> + launch { + runCatching { + pushFileHandling.pushSyncConnector(connector) + }.onFailure { + logger.error(it) { "Error syncing connector '${connector.connectorId}'. Reason: ${it.message}" } + } + } + } + } + } + +} diff --git a/src/main/resources/banner.txt b/src/main/resources/banner.txt new file mode 100644 index 0000000..07294c9 --- /dev/null +++ b/src/main/resources/banner.txt @@ -0,0 +1,10 @@ + .:. ,-----. ,--. ,-----.,--.,--. ,--. ,--. ,--. + .:.:.:::. ' .--./,-| |,--.--.' .--./| |`--' ,---. ,--,--, ,-' '-.\ `.' /,--,--,--. + .::::...:::: | | ' .-. || .--'| | | |,--.| .-. :| \'-. .-' \ / | | +:::::. :::..:. ' '--'\ `-' || | ' '--'\| || |\ --.| || | | | \ / | | | | +.::. ::::::. `-----'`---' `--' `-----'`--'`--' `----'`--''--' `--' `-' `--`--`--' + .... ::::::::. + ..: ::::::::: ${application.title} ${application.version} + ::..::::::::: Powered by Spring Boot ${spring-boot.version} + .. :::::::. + ::::.. diff --git a/src/main/resources/config/application-client.yaml b/src/main/resources/config/application-client.yaml new file mode 100644 index 0000000..7b57e7a --- /dev/null +++ b/src/main/resources/config/application-client.yaml @@ -0,0 +1,26 @@ +client: + functionKey: ${cdrClient.functionKey} + local-folder: ${cdrClient.localFolder} + schedule-delay: PT10M + connection-timeout-ms: 5000 + #we want the read timeout to be longer than a connection timeout between backend and db + read-timeout-ms: 35000 + pull-thread-pool-size: 10 + push-thread-pool-size: 10 + retry-delay: + - 1s + - 2s + - 8s + - 32s + - 10m + endpoint: + scheme: https + port: 443 + # host: dev-cdr-functions.azurewebsites.net + base-path: documents + customer: + # MUST BE SET IN ENVIRONMENT + # - connector-id: 1 + # content-type: application/forumdatenaustausch+xml;charset=UTF-8;version=4.5 + # target-folder: /tmp/download + # source-folder: /tmp/upload diff --git a/src/main/resources/config/application-dev.yaml b/src/main/resources/config/application-dev.yaml new file mode 100644 index 0000000..ce35d43 --- /dev/null +++ b/src/main/resources/config/application-dev.yaml @@ -0,0 +1,40 @@ +client: + functionKey: ${cdrClient.functionKey} + local-folder: ${cdrClient.localFolder} + schedule-delay: PT30S + endpoint: + scheme: http + port: 9090 + host: localhost + base-path: documents + customer: + - connector-id: 1 + content-type: application/forumdatenaustausch+xml;charset=UTF-8;version=4.5 + target-folder: ${cdrClient.targetFolder}/${client.customer[0].connector-id}/test + source-folder: ${cdrClient.sourceFolder}/${client.customer[0].connector-id}/test + mode: test + - connector-id: 1 + content-type: application/forumdatenaustausch+xml;charset=UTF-8;version=4.5 + target-folder: ${cdrClient.targetFolder}/${client.customer[1].connector-id} + source-folder: ${cdrClient.sourceFolder}/${client.customer[1].connector-id} + mode: production + - connector-id: 1234 + content-type: application/forumdatenaustausch+xml;charset=UTF-8;version=4.5 + target-folder: ${cdrClient.targetFolder}/${client.customer[2].connector-id} + source-folder: ${cdrClient.sourceFolder}/${client.customer[2].connector-id} + mode: test + - connector-id: 5 + content-type: application/forumdatenaustausch+xml;charset=UTF-8;version=4.5 + target-folder: ${cdrClient.targetFolder}/${client.customer[3].connector-id} + source-folder: ${cdrClient.sourceFolder}/${client.customer[3].connector-id} + mode: test + - connector-id: 666 + content-type: application/forumdatenaustausch+xml;charset=UTF-8;version=4.5 + target-folder: ${cdrClient.targetFolder}/${client.customer[4].connector-id} + source-folder: ${cdrClient.sourceFolder}/${client.customer[4].connector-id} + mode: production + - connector-id: 2345 + content-type: application/forumdatenaustausch+xml;charset=UTF-8;version=4.5 + target-folder: ${cdrClient.targetFolder}/${client.customer[5].connector-id} + source-folder: ${cdrClient.sourceFolder}/${client.customer[5].connector-id} + mode: production diff --git a/src/main/resources/config/application.yaml b/src/main/resources/config/application.yaml new file mode 100644 index 0000000..465add3 --- /dev/null +++ b/src/main/resources/config/application.yaml @@ -0,0 +1,44 @@ +spring: + main: + web-application-type: none + application: + name: CDR Client + output: + ansi: + enabled: detect + profiles: + include: + - client + task: + scheduling: + pool: + size: 2 + +server: + address: 0.0.0.0 + port: 8080 + shutdown: "graceful" + servlet: + encoding: + charset: UTF-8 + enabled: true + force: true + tomcat: + connection-timeout: 1s + keep-alive-timeout: 30s + +management: + endpoints: + web: + exposure: + include: "actuator,health" + enabled-by-default: true + +logging: + pattern: + dateformat: "\"yyyy-MM-dd'T'HH:mm:ss.SSSXXX\", UTC" + level: + root: INFO + com.swisscom.health.des.cdr: INFO + charset: + console: UTF-8 diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..79abafd --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,66 @@ + + + + + + + + + + ${LOGGING_FILE_NAME:-./cdr-client.log} + true + + + ${LOGGING_FILE_NAME:-./cdr-client.log}.%d{yyyy-MM-dd}.%i.log + ${LOGGING_LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE:-100MB} + ${LOGGING_LOGBACK_ROLLINGPOLICY_MAX_HISTORY:-2} + + + + %d{"yyyy-MM-dd'T'HH:mm:ss.SSSXXX", UTC} | ${%level:-%5p} | ${PID:- } | %X{traceId:-None} | [%15.15t] | %-40.40logger{39} | %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%ex} + + + + + + + timestamp + UTC + + + logger + + + level + + + thread + + + mdc + + + + + + stackTrace + + + 200 + 14000 + true + + + + + exceptionClass + + + + + + + + + + diff --git a/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/CdrClientVmApplicationTest.kt b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/CdrClientVmApplicationTest.kt new file mode 100644 index 0000000..4d36df2 --- /dev/null +++ b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/CdrClientVmApplicationTest.kt @@ -0,0 +1,21 @@ +package com.swisscom.health.des.cdr.clientvm + +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ActiveProfiles + +@SpringBootTest +@ActiveProfiles("test") +internal class CdrClientVmApplicationTest { + + @Autowired + private lateinit var cdrClientVmApplication: CdrClientVmApplication + + @Test + fun `test that application is starting`(){ + assertNotNull(cdrClientVmApplication) + } + +} diff --git a/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/PullSchedulerAndFileHandlerMultipleConnectorTest.kt b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/PullSchedulerAndFileHandlerMultipleConnectorTest.kt new file mode 100644 index 0000000..1ea3aba --- /dev/null +++ b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/PullSchedulerAndFileHandlerMultipleConnectorTest.kt @@ -0,0 +1,277 @@ +package com.swisscom.health.des.cdr.clientvm + +import com.swisscom.health.des.cdr.clientvm.config.CdrClientConfig +import com.swisscom.health.des.cdr.clientvm.handler.CONNECTOR_ID_HEADER +import com.swisscom.health.des.cdr.clientvm.handler.PULL_RESULT_ID_HEADER +import com.swisscom.health.des.cdr.clientvm.handler.PullFileHandling +import com.swisscom.health.des.cdr.clientvm.handler.PushFileHandling +import com.swisscom.health.des.cdr.clientvm.scheduling.Scheduler +import io.micrometer.tracing.Span +import io.micrometer.tracing.TraceContext +import io.micrometer.tracing.Tracer +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.junit5.MockKExtension +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import okhttp3.OkHttpClient +import okhttp3.mockwebserver.Dispatcher +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.RecordedRequest +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.junit.jupiter.api.io.TempDir +import org.springframework.core.io.ClassPathResource +import org.springframework.http.HttpStatus +import java.io.File +import java.nio.charset.StandardCharsets +import java.util.UUID + +@ExtendWith(MockKExtension::class) +internal class PullSchedulerAndFileHandlerMultipleConnectorTest { + + @MockK + private lateinit var config: CdrClientConfig + + @MockK + private lateinit var tracer: Tracer + + @MockK + private lateinit var spanBuilder: Span.Builder + + @MockK + private lateinit var span: Span + + @MockK + private lateinit var spanInScope: Tracer.SpanInScope + + @MockK + private lateinit var traceContext: TraceContext + + @MockK + private lateinit var pushFileHandling: PushFileHandling + + @TempDir + private lateinit var folder: File + + private lateinit var cdrServiceMock: MockWebServer + + private lateinit var scheduler: Scheduler + private lateinit var pullFileHandling: PullFileHandling + + private var counterOne = 0 + private var counterTwo = 0 + + private val directory1 = "customer" + private val directory2 = "otherOne" + private val inflightFolder = "inflight" + private val connectorId1 = "1234" + private val connectorId2 = "3456" + + @BeforeEach + fun setup() { + folder.mkdirs() + cdrServiceMock = MockWebServer() + cdrServiceMock.start() + + val endpoint = CdrClientConfig.Endpoint( + host = cdrServiceMock.hostName, + basePath = "documents", + scheme = "http", + port = cdrServiceMock.port, + ) + val connector1 = + CdrClientConfig.Connector( + connectorId = connectorId1, + targetFolder = "${folder.absolutePath}${File.separator}$directory1", + sourceFolder = "${folder.absolutePath}${File.separator}$directory1/source", + contentType = "application/forumdatenaustausch+xml;charset=UTF-8;version=4.5", + mode = CdrClientConfig.Mode.TEST + ) + val connector2 = + CdrClientConfig.Connector( + connectorId = connectorId2, + targetFolder = "${folder.absolutePath}${File.separator}$directory2", + sourceFolder = "${folder.absolutePath}${File.separator}$directory2/source", + contentType = "application/forumdatenaustausch+xml;charset=UTF-8;version=4.5", + mode = CdrClientConfig.Mode.PRODUCTION + ) + File("${folder.absolutePath}${File.separator}$directory1").mkdirs() + File("${folder.absolutePath}${File.separator}$directory2").mkdirs() + File("${folder.absolutePath}${File.separator}$inflightFolder").mkdirs() + every { config.customer } returns listOf(connector1, connector2) + every { config.endpoint } returns endpoint + every { config.localFolder } returns "${folder.absolutePath}${File.separator}$inflightFolder" + every { config.functionKey } returns "1" + mockTracer() + + pullFileHandling = PullFileHandling(config, OkHttpClient.Builder().build(), tracer) + scheduler = Scheduler( + config, + pullFileHandling, + pushFileHandling, + ) + + } + + private fun mockTracer() { + every { tracer.spanBuilder() } returns spanBuilder + every { tracer.currentSpan() } returns null + every { spanBuilder.setNoParent() } returns spanBuilder + every { spanBuilder.name(any()) } returns spanBuilder + every { spanBuilder.start() } returns span + every { tracer.withSpan(any()) } returns spanInScope + every { span.name(any()) } returns span + every { span.start() } returns span + every { span.event(any()) } returns span + every { span.end() } returns Unit + every { span.tag(any(), any()) } returns span + every { span.context() } returns traceContext + every { spanInScope.close() } returns Unit + } + + private fun handleDispatcher(request: RecordedRequest, practOneMaxCount: Int, practTwoMaxCount: Int): MockResponse { + return if (request.method == "GET" && request.headers[CONNECTOR_ID_HEADER] == connectorId1 + ) { + mockResponseDependingOnPath(request) { handleConnectorOne(practOneMaxCount) } + } else if (request.method == "GET" && request.headers[CONNECTOR_ID_HEADER] == connectorId2 + ) { + mockResponseDependingOnPath(request) { handleConnectorTwo(practTwoMaxCount) } + } else if (request.method == "DELETE") { + MockResponse().setResponseCode(HttpStatus.OK.value()) + } else { + MockResponse().setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value()) + .setBody("I'm sorry. My responses are limited. You must ask the right questions.") + } + } + + private fun mockResponseDependingOnPath(request: RecordedRequest, handleMethod: () -> MockResponse): MockResponse { + return with(request.path!!) { + when { + contains("limit=1") -> handleMethod() + else -> MockResponse().setResponseCode(HttpStatus.NOT_FOUND.value()) + } + } + } + + private fun handleConnectorOne(maxCount: Int): MockResponse { + return if (counterOne < maxCount) { + counterOne++ + MockResponse().setResponseCode(HttpStatus.OK.value()) + .setHeader(PULL_RESULT_ID_HEADER, UUID.randomUUID().toString()) + .setBody(String(ClassPathResource("messages/dummy.txt").inputStream.readAllBytes(), StandardCharsets.UTF_8)) + } else { + MockResponse().setResponseCode(HttpStatus.NO_CONTENT.value()) + } + } + + private fun handleConnectorTwo(maxCount: Int): MockResponse { + return if (counterTwo < maxCount) { + counterTwo++ + MockResponse().setResponseCode(HttpStatus.OK.value()) + .setHeader(PULL_RESULT_ID_HEADER, UUID.randomUUID().toString()) + .setBody(String(ClassPathResource("messages/dummy.txt").inputStream.readAllBytes(), StandardCharsets.UTF_8)) + } else { + MockResponse().setResponseCode(HttpStatus.NO_CONTENT.value()) + } + } + + @AfterEach + fun tearDown() { + folder.delete() + cdrServiceMock.shutdown() + counterOne = 0 + counterTwo = 0 + } + + @Test + fun `test sync of multiple files to folder for two connectors`() = runTest { + val practOneMaxCount = 75 + val practTwoMaxCount = 50 + cdrServiceMock.dispatcher = object : Dispatcher() { + override fun dispatch(request: RecordedRequest): MockResponse { + return handleDispatcher(request, practOneMaxCount, practTwoMaxCount) + } + } + + scheduler.syncFilesToClientFolders() + + + Assertions.assertEquals(practTwoMaxCount * 2 + practOneMaxCount * 2 + 2, cdrServiceMock.requestCount) + + val listFiles = folder.listFiles() + Assertions.assertNotNull(listFiles) + listFiles!! + listFiles.let { Assertions.assertEquals(3, it.size) } + listFiles.filter { !it.endsWith(inflightFolder) }.forEach { Assertions.assertTrue(it.list().size > 5) } + listFiles.filter { it.endsWith(directory1) }[0].list().let { + if (it != null) { + Assertions.assertEquals(counterOne, it.size) + } else { + Assertions.fail("No file list for folder $directory1 found") + } + } + listFiles.filter { it.endsWith(directory2) }[0].list().let { + if (it != null) { + Assertions.assertEquals(counterTwo, it.size) + } else { + Assertions.fail("No file list for folder $directory2 found") + } + } + listFiles.filter { it.endsWith(inflightFolder) }[0].list().let { + if (it != null) { + Assertions.assertEquals(0, it.size) + } else { + Assertions.fail("No file list for folder $inflightFolder found") + } + } + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `test sync of multiple files to folder for one connector`() = runTest { + val practOneMaxCount = 10 + val practTwoMaxCount = 0 + cdrServiceMock.dispatcher = object : Dispatcher() { + override fun dispatch(request: RecordedRequest): MockResponse { + return handleDispatcher(request, practOneMaxCount, practTwoMaxCount) + } + } + + scheduler.syncFilesToClientFolders() + advanceUntilIdle() + + Assertions.assertEquals(practTwoMaxCount * 2 + practOneMaxCount * 2 + 2, cdrServiceMock.requestCount) + val listFiles = folder.listFiles() + Assertions.assertNotNull(listFiles) + listFiles!! + listFiles.let { Assertions.assertEquals(3, it.size) } + listFiles.filter { it.endsWith(directory1) }[0].list().let { + if (it != null) { + Assertions.assertEquals(counterOne, it.size) + } else { + Assertions.fail("No file list for folder $directory1 found") + } + } + listFiles.filter { it.endsWith(directory2) }[0].list().let { + if (it != null) { + Assertions.assertEquals(counterTwo, it.size) + } else { + Assertions.fail("No file list for folder $directory2 found") + } + } + listFiles.filter { it.endsWith(inflightFolder) }[0].list().let { + if (it != null) { + Assertions.assertEquals(0, it.size) + } else { + Assertions.fail("No file list for folder $inflightFolder found") + } + } + } + +} diff --git a/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/config/CdrClientConfigTest.kt b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/config/CdrClientConfigTest.kt new file mode 100644 index 0000000..fe958ed --- /dev/null +++ b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/config/CdrClientConfigTest.kt @@ -0,0 +1,243 @@ +package com.swisscom.health.des.cdr.clientvm.config + +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.MethodSource +import java.time.Duration + +class CdrClientConfigTest { + + private lateinit var cdrClientConfig: CdrClientConfig + + @Test + fun `test bluesky configuration`() { + cdrClientConfig = createCdrClientConfig( + listOf( + CdrClientConfig.Connector( + connectorId = "connectorId", + targetFolder = "targetFolder", + sourceFolder = "sourceFolder", + contentType = "contentType", + mode = CdrClientConfig.Mode.TEST + ), + CdrClientConfig.Connector( + connectorId = "connectorId", + targetFolder = "targetFolder1", + sourceFolder = "sourceFolder1", + contentType = "contentType", + mode = CdrClientConfig.Mode.PRODUCTION + ), + CdrClientConfig.Connector( + connectorId = "connectorId2", + targetFolder = "targetFolder", + sourceFolder = "sourceFolder2", + contentType = "contentType", + mode = CdrClientConfig.Mode.TEST + ), + CdrClientConfig.Connector( + connectorId = "connectorId3", + targetFolder = "targetFolder", + sourceFolder = "sourceFolder3", + contentType = "contentType", + mode = CdrClientConfig.Mode.PRODUCTION + ), + CdrClientConfig.Connector( + connectorId = "connectorId4", + targetFolder = "targetFolder4", + sourceFolder = "sourceFolder4", + contentType = "contentType", + mode = CdrClientConfig.Mode.TEST + ) + ) + ) + + assertDoesNotThrow { cdrClientConfig.checkAndReport() } + assertFalse(cdrClientConfig.toString().contains(functionKey)) + } + + @Test + fun `test fail because no customer is configured`() { + cdrClientConfig = createCdrClientConfig(emptyList()) + + assertThrows { cdrClientConfig.checkAndReport() } + } + + @ParameterizedTest + @MethodSource("provideSameFolderConnectors") + fun `test fail because same folder is used`(customers: List) { + cdrClientConfig = createCdrClientConfig(customers) + + assertThrows { cdrClientConfig.checkAndReport() } + } + + @ParameterizedTest + @MethodSource("provideSameModeConnectors") + fun `test fail because same mode is used multiple times for the same connector id`(customers: List) { + cdrClientConfig = createCdrClientConfig(customers) + + assertThrows { cdrClientConfig.checkAndReport() } + } + + + private fun createCdrClientConfig(customers: List): CdrClientConfig { + return CdrClientConfig( + functionKey = functionKey, + scheduleDelay = "scheduleDelay", + localFolder = "localFolder", + endpoint = CdrClientConfig.Endpoint( + scheme = "http", + host = "localhost", + port = 8080, + basePath = "/api" + ), + customer = customers, + pullThreadPoolSize = 1, + pushThreadPoolSize = 1, + retryDelay = arrayOf(Duration.ofSeconds(1)) + ) + } + + companion object { + const val functionKey = "functionKey123" + + @JvmStatic + @Suppress("LongMethod") + fun provideSameFolderConnectors() = listOf( + listOf( + CdrClientConfig.Connector( + connectorId = "connectorId", + targetFolder = "targetFolder", + sourceFolder = "sourceFolder", + contentType = "contentType", + mode = CdrClientConfig.Mode.TEST + ), + CdrClientConfig.Connector( + connectorId = "connectorId", + targetFolder = "targetFolder", + sourceFolder = "sourceFolder", + contentType = "contentType", + mode = CdrClientConfig.Mode.PRODUCTION + ) + ), + listOf( + CdrClientConfig.Connector( + connectorId = "connectorId2", + targetFolder = "targetFolder2", + sourceFolder = "targetFolder2", + contentType = "contentType", + mode = CdrClientConfig.Mode.TEST + ), + CdrClientConfig.Connector( + connectorId = "connectorId3", + targetFolder = "targetFolder3", + sourceFolder = "sourceFolder3", + contentType = "contentType", + mode = CdrClientConfig.Mode.PRODUCTION + ) + ), + listOf( + CdrClientConfig.Connector( + connectorId = "connectorId2", + targetFolder = "targetFolder", + sourceFolder = "sourceFolder", + contentType = "contentType", + mode = CdrClientConfig.Mode.TEST + ), + CdrClientConfig.Connector( + connectorId = "connectorId3", + targetFolder = "sourceFolder", + sourceFolder = "targetFolder2", + contentType = "contentType", + mode = CdrClientConfig.Mode.PRODUCTION + ) + ), + listOf( + CdrClientConfig.Connector( + connectorId = "connectorId2", + targetFolder = "targetFolder", + sourceFolder = "sourceFolder", + contentType = "contentType", + mode = CdrClientConfig.Mode.TEST + ), + CdrClientConfig.Connector( + connectorId = "connectorId3", + targetFolder = "sourceFolder2", + sourceFolder = "targetFolder", + contentType = "contentType", + mode = CdrClientConfig.Mode.PRODUCTION + ) + ) + ) + + @JvmStatic + @Suppress("LongMethod") + fun provideSameModeConnectors() = listOf( + listOf( + CdrClientConfig.Connector( + connectorId = "connectorId", + targetFolder = "targetFolder", + sourceFolder = "sourceFolder", + contentType = "contentType", + mode = CdrClientConfig.Mode.TEST + ), + CdrClientConfig.Connector( + connectorId = "connectorId", + targetFolder = "targetFolder2", + sourceFolder = "sourceFolder2", + contentType = "contentType", + mode = CdrClientConfig.Mode.TEST + ) + ), + listOf( + CdrClientConfig.Connector( + connectorId = "connectorId2", + targetFolder = "targetFolder2", + sourceFolder = "sourceFolder2", + contentType = "contentType", + mode = CdrClientConfig.Mode.PRODUCTION + ), + CdrClientConfig.Connector( + connectorId = "connectorId2", + targetFolder = "targetFolder", + sourceFolder = "sourceFolder", + contentType = "contentType", + mode = CdrClientConfig.Mode.PRODUCTION + ) + ), + listOf( + CdrClientConfig.Connector( + connectorId = "connectorId2", + targetFolder = "targetFolder2", + sourceFolder = "sourceFolder2", + contentType = "contentType", + mode = CdrClientConfig.Mode.PRODUCTION + ), + CdrClientConfig.Connector( + connectorId = "connectorId2", + targetFolder = "targetFolder", + sourceFolder = "sourceFolder", + contentType = "contentType", + mode = CdrClientConfig.Mode.PRODUCTION + ), + CdrClientConfig.Connector( + connectorId = "connectorId2", + targetFolder = "targetFolder3", + sourceFolder = "sourceFolder3", + contentType = "contentType", + mode = CdrClientConfig.Mode.TEST + ), + CdrClientConfig.Connector( + connectorId = "connectorId2", + targetFolder = "targetFolder4", + sourceFolder = "sourceFolder4", + contentType = "contentType", + mode = CdrClientConfig.Mode.PRODUCTION + ) + ) + ) + } + +} diff --git a/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/handler/FileHandlingBaseTest.kt b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/handler/FileHandlingBaseTest.kt new file mode 100644 index 0000000..7e1cc4b --- /dev/null +++ b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/handler/FileHandlingBaseTest.kt @@ -0,0 +1,27 @@ +package com.swisscom.health.des.cdr.clientvm.handler + +import io.github.oshai.kotlinlogging.KotlinLogging +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import java.nio.file.Files +import kotlin.io.path.deleteIfExists + +class FileHandlingBaseTest { + + @Test + fun `test non writable folder`() { + val createTempDirectory = Files.createTempDirectory("nonWritableTest") + Assertions.assertTrue(pathIsDirectoryAndWritable(createTempDirectory, "test", KotlinLogging.logger {})) + createTempDirectory.toFile().setWritable(false) + Assertions.assertFalse(pathIsDirectoryAndWritable(createTempDirectory, "test", KotlinLogging.logger {})) + createTempDirectory.toFile().setWritable(true) + Assertions.assertTrue(createTempDirectory.deleteIfExists()) + } + + @Test + fun `test non directory`() { + val createTempFile = Files.createTempFile("nonDirectoryTest", "test") + Assertions.assertFalse(pathIsDirectoryAndWritable(createTempFile, "test", KotlinLogging.logger {})) + Assertions.assertTrue(createTempFile.deleteIfExists()) + } +} diff --git a/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PullFileHandlingTest.kt b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PullFileHandlingTest.kt new file mode 100644 index 0000000..db2ec89 --- /dev/null +++ b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PullFileHandlingTest.kt @@ -0,0 +1,316 @@ +package com.swisscom.health.des.cdr.clientvm.handler + +import com.swisscom.health.des.cdr.clientvm.config.CdrClientConfig +import io.micrometer.tracing.Span +import io.micrometer.tracing.TraceContext +import io.micrometer.tracing.Tracer +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.junit5.MockKExtension +import kotlinx.coroutines.runBlocking +import okhttp3.OkHttpClient +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Assertions.fail +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.junit.jupiter.api.io.TempDir +import org.springframework.core.io.ClassPathResource +import org.springframework.http.HttpStatus +import java.io.File +import java.nio.charset.StandardCharsets +import java.util.UUID + +@ExtendWith(MockKExtension::class) +internal class PullFileHandlingTest { + @MockK + private lateinit var config: CdrClientConfig + + @MockK + private lateinit var tracer: Tracer + + @MockK + private lateinit var spanBuilder: Span.Builder + + @MockK + private lateinit var span: Span + + @MockK + private lateinit var spanInScope: Tracer.SpanInScope + + @MockK + private lateinit var traceContext: TraceContext + + @TempDir + private lateinit var folder: File + + private lateinit var cdrServiceMock: MockWebServer + + private lateinit var pullFileHandling: PullFileHandling + private val inflightFolder = "inflight" + private val targetDirectory = "customer" + private val sourceDirectory = "source" + private lateinit var endpoint: CdrClientConfig.Endpoint + + @BeforeEach + fun setup() { + folder.mkdirs() + cdrServiceMock = MockWebServer() + cdrServiceMock.start() + mockTracer() + + endpoint = CdrClientConfig.Endpoint( + host = cdrServiceMock.hostName, + basePath = "documents", + scheme = "http", + port = cdrServiceMock.port, + ) + + File("${folder.absolutePath}${File.separator}$targetDirectory").mkdirs() + File("${folder.absolutePath}${File.separator}$inflightFolder").mkdirs() + every { config.endpoint } returns endpoint + every { config.localFolder } returns "${folder.absolutePath}${File.separator}$inflightFolder" + every { config.functionKey } returns "1" + + pullFileHandling = PullFileHandling(config, OkHttpClient.Builder().build(), tracer) + } + + @AfterEach + fun tearDown() { + folder.delete() + cdrServiceMock.shutdown() + } + + @Test + fun `test sync of single file to folder`() { + enqueueFileResponseWithReportResponse() + enqueueEmptyResponse() + + runBlocking { + pullFileHandling.pullSyncConnector(createConnector("1-2-3-4")) + } + + assertEquals(3, cdrServiceMock.requestCount, "more requests where done than expected") + val listFiles = folder.listFiles() + assertNotNull(listFiles) + listFiles!! + assertEquals(2, listFiles.size) + + listFiles.filter { it.endsWith(targetDirectory) }[0].list().let { + if (it != null) { + assertEquals(1, it.size) + assertTrue(it[0].endsWith(".xml"), "File extension is not .xml") + } else { + fail("No file list for folder $targetDirectory found") + } + } + listFiles.filter { it.endsWith(inflightFolder) }[0].list().let { + if (it != null) { + assertEquals(0, it.size) + } else { + fail("A file was found in folder $inflightFolder, which shouldn't be there") + } + } + } + + @Test + fun `test sync of single file no header present`() { + enqueueFileResponseNoHeader() + + runBlocking { + pullFileHandling.pullSyncConnector(createConnector("1-2-3-4")) + } + + assertEquals(1, cdrServiceMock.requestCount, "more requests where done than expected") + val listFiles = folder.listFiles() + assertNotNull(listFiles) + listFiles!! + assertEquals(2, listFiles.size) + + listFiles.filter { it.endsWith(targetDirectory) }[0].list().let { + if (it != null) { + assertEquals(0, it.size) + } else { + fail("A file was found in folder $targetDirectory, which shouldn't be there") + } + } + listFiles.filter { it.endsWith(inflightFolder) }[0].list().let { + if (it != null) { + assertEquals(0, it.size) + } else { + fail("A file was found in folder $inflightFolder, which shouldn't be there") + } + } + } + + @Test + fun `test sync of single file to folder with failed report for success`() { + enqueueFileResponse() + enqueueExceptionResponse() + enqueueEmptyResponse() + + runBlocking { + pullFileHandling.pullSyncConnector(createConnector("1-2-3-4")) + } + + assertEquals(2, cdrServiceMock.requestCount, "more requests where done than expected") + val listFiles = folder.listFiles() + assertNotNull(listFiles) + listFiles!! + assertEquals(2, listFiles.size) + + listFiles.filter { it.endsWith(targetDirectory) }[0].list().let { + if (it != null) { + assertEquals(0, it.size) + } else { + fail("No file list for folder $targetDirectory found") + } + } + listFiles.filter { it.endsWith(inflightFolder) }[0].list().let { + if (it != null) { + assertEquals(1, it.size) + } else { + fail("No file list for folder $inflightFolder found") + } + } + } + + @Test + fun `test sync of multiple files to folder`() { + enqueueFileResponseWithReportResponse() + enqueueFileResponseWithReportResponse() + enqueueFileResponseWithReportResponse() + enqueueFileResponseWithReportResponse() + enqueueFileResponseWithReportResponse() + enqueueEmptyResponse() + + runBlocking { + pullFileHandling.pullSyncConnector(createConnector("1-2-3-4")) + } + + + assertEquals(11, cdrServiceMock.requestCount, "more requests where done than expected") + val listFiles = folder.listFiles() + assertNotNull(listFiles) + listFiles!! + assertEquals(2, listFiles.size) + + listFiles.filter { it.endsWith(targetDirectory) }[0].list().let { + if (it != null) { + assertEquals(5, it.size) + } else { + fail("No file list for folder $targetDirectory found") + } + } + listFiles.filter { it.endsWith(inflightFolder) }[0].list().let { + if (it != null) { + assertEquals(0, it.size) + } else { + fail("No file list for folder $inflightFolder found") + } + } + } + + @Test + fun `test sync of multiple files with an exception to folder`() { + enqueueFileResponseWithReportResponse() + enqueueFileResponseWithReportResponse() + enqueueFileResponseWithReportResponse() + enqueueExceptionResponse() + enqueueFileResponseWithReportResponse() + enqueueEmptyResponse() + + runBlocking { + pullFileHandling.pullSyncConnector(createConnector("1-2-3-4")) + } + + assertEquals(7, cdrServiceMock.requestCount, "more requests where done than expected") + val listFiles = folder.listFiles() + assertNotNull(listFiles) + listFiles!! + assertEquals(2, listFiles.size) + + listFiles.filter { it.endsWith(targetDirectory) }[0].list().let { + if (it != null) { + assertEquals(3, it.size) + } else { + fail("No file list for folder $targetDirectory found") + } + } + listFiles.filter { it.endsWith(inflightFolder) }[0].list().let { + if (it != null) { + assertEquals(0, it.size) + } else { + fail("No file list for folder $inflightFolder found") + } + } + } + + private fun createConnector( + connectorId: String, + targetFolder: String = "${folder.absolutePath}${File.separator}$targetDirectory", + sourceFolder: String = "${folder.absolutePath}${File.separator}$sourceDirectory" + ): CdrClientConfig.Connector = + CdrClientConfig.Connector( + connectorId = connectorId, + targetFolder = targetFolder, + sourceFolder = sourceFolder, + contentType = "application/forumdatenaustausch+xml;charset=UTF-8;version=4.5", + mode = CdrClientConfig.Mode.PRODUCTION + ) + + private fun enqueueFileResponseWithReportResponse() { + enqueueFileResponse() + enqueueReportResponse() + } + + private fun enqueueFileResponse() { + val pullRequestId = UUID.randomUUID().toString() + val mockResponse = MockResponse().setResponseCode(HttpStatus.OK.value()) + .setHeader(PULL_RESULT_ID_HEADER, pullRequestId) + .setBody(String(ClassPathResource("messages/dummy.txt").inputStream.readAllBytes(), StandardCharsets.UTF_8)) + cdrServiceMock.enqueue(mockResponse) + } + + private fun enqueueFileResponseNoHeader() { + val mockResponse = MockResponse().setResponseCode(HttpStatus.OK.value()) + .setBody(String(ClassPathResource("messages/dummy.txt").inputStream.readAllBytes(), StandardCharsets.UTF_8)) + cdrServiceMock.enqueue(mockResponse) + } + + private fun enqueueReportResponse() { + cdrServiceMock.enqueue(MockResponse().setResponseCode(HttpStatus.OK.value())) + } + + private fun enqueueEmptyResponse() { + cdrServiceMock.enqueue(MockResponse().setResponseCode(HttpStatus.NO_CONTENT.value())) + } + + private fun enqueueExceptionResponse() { + cdrServiceMock.enqueue(MockResponse().setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value())) + } + + private fun mockTracer() { + every { tracer.spanBuilder() } returns spanBuilder + every { tracer.currentSpan() } returns null + every { spanBuilder.setNoParent() } returns spanBuilder + every { spanBuilder.name(any()) } returns spanBuilder + every { spanBuilder.start() } returns span + every { tracer.withSpan(any()) } returns spanInScope + every { span.name(any()) } returns span + every { span.start() } returns span + every { span.event(any()) } returns span + every { span.end() } returns Unit + every { span.tag(any(), any()) } returns span + every { span.context() } returns traceContext + every { spanInScope.close() } returns Unit + } + +} + + diff --git a/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PushFileHandlingTest.kt b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PushFileHandlingTest.kt new file mode 100644 index 0000000..0e7af5d --- /dev/null +++ b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/handler/PushFileHandlingTest.kt @@ -0,0 +1,203 @@ +package com.swisscom.health.des.cdr.clientvm.handler + +import com.swisscom.health.des.cdr.clientvm.config.CdrClientConfig +import io.micrometer.tracing.Span +import io.micrometer.tracing.TraceContext +import io.micrometer.tracing.Tracer +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.junit5.MockKExtension +import kotlinx.coroutines.runBlocking +import okhttp3.OkHttpClient +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.fail +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.junit.jupiter.api.io.TempDir +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpStatus +import org.springframework.http.MediaType +import java.io.File +import java.io.FileOutputStream +import java.time.Duration + +@ExtendWith(MockKExtension::class) +internal class PushFileHandlingTest { + @MockK + private lateinit var config: CdrClientConfig + + @MockK + private lateinit var tracer: Tracer + + @MockK + private lateinit var spanBuilder: Span.Builder + + @MockK + private lateinit var span: Span + + @MockK + private lateinit var spanInScope: Tracer.SpanInScope + + @MockK + private lateinit var traceContext: TraceContext + + @TempDir + private lateinit var folder: File + + private lateinit var cdrServiceMock: MockWebServer + + private lateinit var pushFileHandling: PushFileHandling + private val inflightFolder = "inflight" + private val targetDirectory = "customer" + private val sourceDirectory = "source" + private lateinit var endpoint: CdrClientConfig.Endpoint + + @BeforeEach + fun setup() { + folder.mkdirs() + cdrServiceMock = MockWebServer() + cdrServiceMock.start() + mockTracer() + + endpoint = CdrClientConfig.Endpoint( + host = cdrServiceMock.hostName, + basePath = "documents", + scheme = "http", + port = cdrServiceMock.port, + ) + + File("${folder.absolutePath}${File.separator}$sourceDirectory").mkdirs() + File("${folder.absolutePath}${File.separator}$inflightFolder").mkdirs() + every { config.endpoint } returns endpoint + every { config.localFolder } returns "${folder.absolutePath}${File.separator}$inflightFolder" + every { config.functionKey } returns "1" + val duration = Duration.ofMillis(100) + every { config.retryDelay } returns arrayOf(duration, duration, duration) + + pushFileHandling = PushFileHandling(config, OkHttpClient.Builder().build(), tracer) + } + + @AfterEach + fun tearDown() { + folder.delete() + cdrServiceMock.shutdown() + } + + @Test + fun `test successfully write two files to API`() { + FileOutputStream("${folder.absolutePath}${File.separator}$sourceDirectory/dummy.xml").use { it.write("Hello".toByteArray()) } + FileOutputStream("${folder.absolutePath}${File.separator}$sourceDirectory/dummy-2.xml").use { it.write("Hello 2".toByteArray()) } + + val mockResponse = MockResponse().setResponseCode(HttpStatus.OK.value()) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) + .setResponseCode(HttpStatus.OK.value()) + .setBody("{\"message\": \"Upload successful\"}") + cdrServiceMock.enqueue(mockResponse) + cdrServiceMock.enqueue(mockResponse) + + runBlocking { + pushFileHandling.pushSyncConnector(createConnector("2345")) + } + + assertEquals(2, cdrServiceMock.requestCount) + assertFolder(0) + } + + @Test + fun `test ignore non xml files`() { + FileOutputStream("${folder.absolutePath}${File.separator}$sourceDirectory/dummy.txt").use { it.write("Hello".toByteArray()) } + FileOutputStream("${folder.absolutePath}${File.separator}$sourceDirectory/dummy-2.error").use { it.write("Hello 2".toByteArray()) } + FileOutputStream("${folder.absolutePath}${File.separator}$sourceDirectory/dummy-2.log").use { it.write("Hello 2".toByteArray()) } + + runBlocking { + pushFileHandling.pushSyncConnector(createConnector("2345")) + } + + assertEquals(0, cdrServiceMock.requestCount) + assertFolder(3) + } + + @Test + fun `test successfully write two files to API fail with third`() { + FileOutputStream("${folder.absolutePath}${File.separator}$sourceDirectory/dummy.xml").use { it.write("Hello".toByteArray()) } + FileOutputStream("${folder.absolutePath}${File.separator}$sourceDirectory/dummy-2.xml").use { it.write("Hello 2".toByteArray()) } + FileOutputStream("${folder.absolutePath}${File.separator}$sourceDirectory/dummy-3.xml").use { it.write("Hello 3".toByteArray()) } + + val mockResponse = MockResponse().setResponseCode(HttpStatus.OK.value()) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) + .setBody("{\"message\": \"Upload successful\"}") + cdrServiceMock.enqueue(mockResponse) + cdrServiceMock.enqueue(mockResponse) + cdrServiceMock.enqueue(MockResponse().setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value()).setBody("{\"message\": \"Exception\"}")) + cdrServiceMock.enqueue(MockResponse().setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value()).setBody("{\"message\": \"Exception\"}")) + cdrServiceMock.enqueue(MockResponse().setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value()).setBody("{\"message\": \"Exception\"}")) + cdrServiceMock.enqueue(MockResponse().setResponseCode(HttpStatus.BAD_REQUEST.value()).setBody("{\"message\": \"Exception\"}")) + + runBlocking { + pushFileHandling.pushSyncConnector(createConnector("2345")) + } + + assertEquals(6, cdrServiceMock.requestCount) + assertFolder(2) + } + + @Test + fun `test successfully write two files to API fail with third do not retry`() { + FileOutputStream("${folder.absolutePath}${File.separator}$sourceDirectory/dummy.xml").use { it.write("Hello".toByteArray()) } + FileOutputStream("${folder.absolutePath}${File.separator}$sourceDirectory/dummy-2.xml").use { it.write("Hello 2".toByteArray()) } + FileOutputStream("${folder.absolutePath}${File.separator}$sourceDirectory/dummy-3.xml").use { it.write("Hello 3".toByteArray()) } + + val mockResponse = MockResponse().setResponseCode(HttpStatus.OK.value()) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) + .setBody("{\"message\": \"Upload successful\"}") + cdrServiceMock.enqueue(mockResponse) + cdrServiceMock.enqueue(mockResponse) + cdrServiceMock.enqueue(MockResponse().setResponseCode(HttpStatus.BAD_REQUEST.value()).setBody("{\"message\": \"Exception\"}")) + + runBlocking { + pushFileHandling.pushSyncConnector(createConnector("2345")) + } + + assertEquals(3, cdrServiceMock.requestCount) + assertFolder(2) + } + + private fun createConnector( + connectorId: String, + targetFolder: String = "${folder.absolutePath}${File.separator}$targetDirectory", + sourceFolder: String = "${folder.absolutePath}${File.separator}$sourceDirectory" + ): CdrClientConfig.Connector = + CdrClientConfig.Connector( + connectorId = connectorId, + targetFolder = targetFolder, + sourceFolder = sourceFolder, + contentType = "application/forumdatenaustausch+xml;charset=UTF-8;version=4.5", + mode = CdrClientConfig.Mode.TEST + ) + + private fun assertFolder(expectedSize: Int) { + val listFiles = folder.listFiles()!! + listFiles.filter { it.endsWith(sourceDirectory) }[0].list()?.let { assertEquals(expectedSize, it.size) } + ?: fail("No file list for folder $sourceDirectory found") + } + + private fun mockTracer() { + every { tracer.spanBuilder() } returns spanBuilder + every { tracer.currentSpan() } returns null + every { spanBuilder.setNoParent() } returns spanBuilder + every { spanBuilder.name(any()) } returns spanBuilder + every { spanBuilder.start() } returns span + every { tracer.withSpan(any()) } returns spanInScope + every { span.name(any()) } returns span + every { span.start() } returns span + every { span.event(any()) } returns span + every { span.end() } returns Unit + every { span.tag(any(), any()) } returns span + every { span.context() } returns traceContext + every { spanInScope.close() } returns Unit + } +} diff --git a/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/scheduling/SchedulerTest.kt b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/scheduling/SchedulerTest.kt new file mode 100644 index 0000000..feda11a --- /dev/null +++ b/src/test/kotlin/com/swisscom/health/des/cdr/clientvm/scheduling/SchedulerTest.kt @@ -0,0 +1,146 @@ +package com.swisscom.health.des.cdr.clientvm.scheduling + +import com.swisscom.health.des.cdr.clientvm.config.CdrClientConfig +import com.swisscom.health.des.cdr.clientvm.handler.PullFileHandling +import com.swisscom.health.des.cdr.clientvm.handler.PushFileHandling +import io.micrometer.tracing.Span +import io.micrometer.tracing.TraceContext +import io.micrometer.tracing.Tracer +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.junit5.MockKExtension +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.junit.jupiter.api.io.TempDir +import java.io.File + +@ExtendWith(MockKExtension::class) +internal class SchedulerTest { + + @TempDir + private lateinit var folder: File + + @MockK + private lateinit var config: CdrClientConfig + + @MockK + private lateinit var tracer: Tracer + + @MockK + private lateinit var spanBuilder: Span.Builder + + @MockK + private lateinit var span: Span + + @MockK + private lateinit var spanInScope: Tracer.SpanInScope + + @MockK + private lateinit var traceContext: TraceContext + + @MockK + private lateinit var pullFileHandling: PullFileHandling + + @MockK + private lateinit var pushFileHandling: PushFileHandling + + private val inflightFolder = "inflight" + private val targetDirectory = "customer" + private val sourceDirectory = "source" + private lateinit var scheduler: Scheduler + + @BeforeEach + fun setup() { + folder.mkdirs() + File("${folder.absolutePath}${File.separator}$targetDirectory").mkdirs() + File("${folder.absolutePath}${File.separator}$inflightFolder").mkdirs() + File("${folder.absolutePath}${File.separator}$sourceDirectory").mkdirs() + val connector = + CdrClientConfig.Connector( + connectorId = "1234", + targetFolder = "${folder.absolutePath}${File.separator}$targetDirectory", + sourceFolder = "${folder.absolutePath}${File.separator}$sourceDirectory", + contentType = "application/forumdatenaustausch+xml;charset=UTF-8;version=4.5", + mode = CdrClientConfig.Mode.TEST + ) + every { config.customer } returns listOf(connector) + every { config.localFolder } returns "${folder.absolutePath}${File.separator}$inflightFolder" + mockTracer() + + scheduler = Scheduler( + config, + pullFileHandling, + pushFileHandling, + ) + } + + @AfterEach + fun tearDown() { + folder.delete() + } + + private fun mockTracer() { + every { tracer.spanBuilder() } returns spanBuilder + every { spanBuilder.setNoParent() } returns spanBuilder + every { spanBuilder.name(any()) } returns spanBuilder + every { spanBuilder.start() } returns span + every { tracer.withSpan(any()) } returns spanInScope + every { span.name(any()) } returns span + every { span.start() } returns span + every { span.event(any()) } returns span + every { span.end() } returns Unit + every { span.tag(any(), any()) } returns span + every { span.context() } returns traceContext + every { spanInScope.close() } returns Unit + every { traceContext.traceId() } returns "1" + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `test sync pull of single file to folder`() = runTest { + coEvery { pullFileHandling.pullSyncConnector(any()) } returns Unit + + scheduler.syncFilesToClientFolders() + + coVerify(exactly = 1) { pullFileHandling.pullSyncConnector(any()) } + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + // Test for coverage only, as there is only a log output in the error case + fun `test sync push of single file to folder throws exception`() = runTest { + coEvery { pullFileHandling.pullSyncConnector(any()) } throws IllegalArgumentException("Exception") + + scheduler.syncFilesToClientFolders() + + coVerify(exactly = 1) { pullFileHandling.pullSyncConnector(any()) } + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `test sync push of single file to folder`() = runTest { + coEvery { pushFileHandling.pushSyncConnector(any()) } returns Unit + + scheduler.syncFilesToApi() + + coVerify(exactly = 1) { pushFileHandling.pushSyncConnector(any()) } + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + // Test for coverage only, as there is only a log output in the error case + fun `test sync pull of single file to folder throws exception`() = runTest { + coEvery { pushFileHandling.pushSyncConnector(any()) } throws IllegalArgumentException("Exception") + + scheduler.syncFilesToApi() + + coVerify(exactly = 1) { pushFileHandling.pushSyncConnector(any()) } + } + +} diff --git a/src/test/resources/application-test.yaml b/src/test/resources/application-test.yaml new file mode 100644 index 0000000..df8409d --- /dev/null +++ b/src/test/resources/application-test.yaml @@ -0,0 +1,21 @@ +client: + functionKey: none + local-folder: local + schedule-delay: PT1M + max-retries: 3 + default-delay-ms: 100 + retry-delay-seconds: + - 1 + - 1 + - 1 + endpoint: + scheme: http + port: 80 + host: localhost + base-path: documents + customer: + - connector-id: 1 + content-type: application/forumdatenaustausch+xml;charset=UTF-8;version=4.5 + target-folder: dummy + source-folder: source + mode: test diff --git a/src/test/resources/messages/dummy.txt b/src/test/resources/messages/dummy.txt new file mode 100644 index 0000000..fa6abeb --- /dev/null +++ b/src/test/resources/messages/dummy.txt @@ -0,0 +1 @@ +Dummy response file