diff --git a/src/integrationTest/kotlin/dev/dnpm/etl/processor/EtlProcessorApplicationTests.kt b/src/integrationTest/kotlin/dev/dnpm/etl/processor/EtlProcessorApplicationTests.kt index 736bdf8..8984e60 100644 --- a/src/integrationTest/kotlin/dev/dnpm/etl/processor/EtlProcessorApplicationTests.kt +++ b/src/integrationTest/kotlin/dev/dnpm/etl/processor/EtlProcessorApplicationTests.kt @@ -23,6 +23,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import de.ukw.ccc.bwhc.dto.* import dev.dnpm.etl.processor.monitoring.RequestRepository import dev.dnpm.etl.processor.monitoring.RequestStatus +import dev.dnpm.etl.processor.output.BwhcV1MtbFileRequest import dev.dnpm.etl.processor.output.MtbFileSender import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach @@ -91,7 +92,7 @@ class EtlProcessorApplicationTests : AbstractTestcontainerTest() { fun mtbFileIsTransformed() { doAnswer { MtbFileSender.Response(RequestStatus.SUCCESS) - }.whenever(mtbFileSender).send(any()) + }.whenever(mtbFileSender).send(any()) val mtbFile = MtbFile.builder() .withPatient( @@ -134,9 +135,9 @@ class EtlProcessorApplicationTests : AbstractTestcontainerTest() { } } - val captor = argumentCaptor() + val captor = argumentCaptor() verify(mtbFileSender).send(captor.capture()) - assertThat(captor.firstValue.mtbFile.diagnoses).hasSize(1).allMatch { diagnosis -> + assertThat(captor.firstValue.content.diagnoses).hasSize(1).allMatch { diagnosis -> diagnosis.icd10.version == "2014" } } diff --git a/src/integrationTest/kotlin/dev/dnpm/etl/processor/input/MtbFileRestControllerTest.kt b/src/integrationTest/kotlin/dev/dnpm/etl/processor/input/MtbFileRestControllerTest.kt index 85b1f1f..f1b1476 100644 --- a/src/integrationTest/kotlin/dev/dnpm/etl/processor/input/MtbFileRestControllerTest.kt +++ b/src/integrationTest/kotlin/dev/dnpm/etl/processor/input/MtbFileRestControllerTest.kt @@ -91,7 +91,7 @@ class MtbFileRestControllerTest { status { isAccepted() } } - verify(requestProcessor, times(1)).processMtbFile(any()) + verify(requestProcessor, times(1)).processMtbFile(any()) } @Test @@ -104,7 +104,7 @@ class MtbFileRestControllerTest { status { isAccepted() } } - verify(requestProcessor, times(1)).processMtbFile(any()) + verify(requestProcessor, times(1)).processMtbFile(any()) } @Test @@ -117,7 +117,7 @@ class MtbFileRestControllerTest { status { isUnauthorized() } } - verify(requestProcessor, never()).processMtbFile(any()) + verify(requestProcessor, never()).processMtbFile(any()) } @Test @@ -130,7 +130,7 @@ class MtbFileRestControllerTest { status { isForbidden() } } - verify(requestProcessor, never()).processMtbFile(any()) + verify(requestProcessor, never()).processMtbFile(any()) } @Test @@ -177,7 +177,7 @@ class MtbFileRestControllerTest { status { isAccepted() } } - verify(requestProcessor, times(1)).processMtbFile(any()) + verify(requestProcessor, times(1)).processMtbFile(any()) } @Test @@ -190,7 +190,7 @@ class MtbFileRestControllerTest { status { isAccepted() } } - verify(requestProcessor, times(1)).processMtbFile(any()) + verify(requestProcessor, times(1)).processMtbFile(any()) } } diff --git a/src/main/kotlin/dev/dnpm/etl/processor/input/MtbFileRestController.kt b/src/main/kotlin/dev/dnpm/etl/processor/input/MtbFileRestController.kt index 432711a..e67a380 100644 --- a/src/main/kotlin/dev/dnpm/etl/processor/input/MtbFileRestController.kt +++ b/src/main/kotlin/dev/dnpm/etl/processor/input/MtbFileRestController.kt @@ -26,7 +26,6 @@ import dev.dnpm.etl.processor.PatientId import dev.dnpm.etl.processor.services.RequestProcessor import dev.pcvolkmer.mv64e.mtb.Mtb import org.slf4j.LoggerFactory -import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.* @@ -47,10 +46,10 @@ class MtbFileRestController( @PostMapping( consumes = [ MediaType.APPLICATION_JSON_VALUE ] ) fun mtbFile(@RequestBody mtbFile: MtbFile): ResponseEntity { if (mtbFile.consent.status == Consent.Status.ACTIVE) { - logger.debug("Accepted MTB File for processing") + logger.debug("Accepted MTB File (bwHC V1) for processing") requestProcessor.processMtbFile(mtbFile) } else { - logger.debug("Accepted MTB File and process deletion") + logger.debug("Accepted MTB File (bwHC V1) and process deletion") val patientId = PatientId(mtbFile.patient.id) requestProcessor.processDeletion(patientId) } @@ -59,7 +58,9 @@ class MtbFileRestController( @PostMapping( consumes = [ CustomMediaType.APPLICATION_VND_DNPM_V2_MTB_JSON_VALUE] ) fun mtbFile(@RequestBody mtbFile: Mtb): ResponseEntity { - return ResponseEntity.status(HttpStatus.NOT_IMPLEMENTED).build() + logger.debug("Accepted MTB File (DNPM V2) for processing") + requestProcessor.processMtbFile(mtbFile) + return ResponseEntity.accepted().build() } @DeleteMapping(path = ["{patientId}"]) diff --git a/src/main/kotlin/dev/dnpm/etl/processor/output/KafkaMtbFileSender.kt b/src/main/kotlin/dev/dnpm/etl/processor/output/KafkaMtbFileSender.kt index 6391e99..c00b2fd 100644 --- a/src/main/kotlin/dev/dnpm/etl/processor/output/KafkaMtbFileSender.kt +++ b/src/main/kotlin/dev/dnpm/etl/processor/output/KafkaMtbFileSender.kt @@ -1,7 +1,7 @@ /* * This file is part of ETL-Processor * - * Copyright (c) 2024 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors + * Copyright (c) 2025 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published @@ -22,10 +22,12 @@ package dev.dnpm.etl.processor.output import com.fasterxml.jackson.databind.ObjectMapper import de.ukw.ccc.bwhc.dto.Consent import de.ukw.ccc.bwhc.dto.MtbFile -import dev.dnpm.etl.processor.RequestId +import dev.dnpm.etl.processor.CustomMediaType import dev.dnpm.etl.processor.config.KafkaProperties import dev.dnpm.etl.processor.monitoring.RequestStatus +import org.apache.kafka.clients.producer.ProducerRecord import org.slf4j.LoggerFactory +import org.springframework.http.MediaType import org.springframework.kafka.core.KafkaTemplate import org.springframework.retry.support.RetryTemplate @@ -38,14 +40,20 @@ class KafkaMtbFileSender( private val logger = LoggerFactory.getLogger(KafkaMtbFileSender::class.java) - override fun send(request: MtbFileSender.MtbFileRequest): MtbFileSender.Response { + override fun send(request: MtbFileRequest): MtbFileSender.Response { return try { return retryTemplate.execute { - val result = kafkaTemplate.send( - kafkaProperties.outputTopic, - key(request), - objectMapper.writeValueAsString(Data(request.requestId, request.mtbFile)) - ) + val record = + ProducerRecord(kafkaProperties.outputTopic, key(request), objectMapper.writeValueAsString(request)) + when (request) { + is BwhcV1MtbFileRequest -> record.headers() + .add("contentType", MediaType.APPLICATION_JSON_VALUE.toByteArray()) + + is DnpmV2MtbFileRequest -> record.headers() + .add("contentType", CustomMediaType.APPLICATION_VND_DNPM_V2_MTB_JSON_VALUE.toByteArray()) + } + + val result = kafkaTemplate.send(record) if (result.get() != null) { logger.debug("Sent file via KafkaMtbFileSender") MtbFileSender.Response(RequestStatus.UNKNOWN) @@ -59,7 +67,7 @@ class KafkaMtbFileSender( } } - override fun send(request: MtbFileSender.DeleteRequest): MtbFileSender.Response { + override fun send(request: DeleteRequest): MtbFileSender.Response { val dummyMtbFile = MtbFile.builder() .withConsent( Consent.builder() @@ -71,12 +79,15 @@ class KafkaMtbFileSender( return try { return retryTemplate.execute { - val result = kafkaTemplate.send( - kafkaProperties.outputTopic, - key(request), - objectMapper.writeValueAsString(Data(request.requestId, dummyMtbFile)) - ) + val record = + ProducerRecord( + kafkaProperties.outputTopic, + key(request), + // Always use old BwhcV1FileRequest with Consent REJECT + objectMapper.writeValueAsString(BwhcV1MtbFileRequest(request.requestId, dummyMtbFile)) + ) + val result = kafkaTemplate.send(record) if (result.get() != null) { logger.debug("Sent deletion request via KafkaMtbFileSender") MtbFileSender.Response(RequestStatus.UNKNOWN) @@ -94,13 +105,12 @@ class KafkaMtbFileSender( return "${this.kafkaProperties.servers} (${this.kafkaProperties.outputTopic}/${this.kafkaProperties.outputResponseTopic})" } - private fun key(request: MtbFileSender.MtbFileRequest): String { - return "{\"pid\": \"${request.mtbFile.patient.id}\"}" + private fun key(request: MtbRequest): String { + return when (request) { + is BwhcV1MtbFileRequest -> "{\"pid\": \"${request.content.patient.id}\"}" + is DnpmV2MtbFileRequest -> "{\"pid\": \"${request.content.patient.id}\"}" + is DeleteRequest -> "{\"pid\": \"${request.patientId.value}\"}" + else -> throw IllegalArgumentException("Unsupported request type: ${request::class.simpleName}") + } } - - private fun key(request: MtbFileSender.DeleteRequest): String { - return "{\"pid\": \"${request.patientId.value}\"}" - } - - data class Data(val requestId: RequestId, val content: MtbFile) -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/dnpm/etl/processor/output/MtbFileSender.kt b/src/main/kotlin/dev/dnpm/etl/processor/output/MtbFileSender.kt index 8d994c5..285ce07 100644 --- a/src/main/kotlin/dev/dnpm/etl/processor/output/MtbFileSender.kt +++ b/src/main/kotlin/dev/dnpm/etl/processor/output/MtbFileSender.kt @@ -19,25 +19,17 @@ package dev.dnpm.etl.processor.output -import de.ukw.ccc.bwhc.dto.MtbFile -import dev.dnpm.etl.processor.PatientPseudonym -import dev.dnpm.etl.processor.RequestId import dev.dnpm.etl.processor.monitoring.RequestStatus import org.springframework.http.HttpStatusCode interface MtbFileSender { - fun send(request: MtbFileRequest): Response + fun send(request: MtbFileRequest): Response fun send(request: DeleteRequest): Response fun endpoint(): String data class Response(val status: RequestStatus, val body: String = "") - - data class MtbFileRequest(val requestId: RequestId, val mtbFile: MtbFile) - - data class DeleteRequest(val requestId: RequestId, val patientId: PatientPseudonym) - } fun Int.asRequestStatus(): RequestStatus { @@ -51,4 +43,4 @@ fun Int.asRequestStatus(): RequestStatus { fun HttpStatusCode.asRequestStatus(): RequestStatus { return this.value().asRequestStatus() -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/dnpm/etl/processor/output/MtbRequest.kt b/src/main/kotlin/dev/dnpm/etl/processor/output/MtbRequest.kt new file mode 100644 index 0000000..9b500f0 --- /dev/null +++ b/src/main/kotlin/dev/dnpm/etl/processor/output/MtbRequest.kt @@ -0,0 +1,59 @@ +/* + * This file is part of ETL-Processor + * + * Copyright (c) 2025 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package dev.dnpm.etl.processor.output + +import de.ukw.ccc.bwhc.dto.MtbFile +import dev.dnpm.etl.processor.PatientPseudonym +import dev.dnpm.etl.processor.RequestId +import dev.pcvolkmer.mv64e.mtb.Mtb + +interface MtbRequest { + val requestId: RequestId +} + +sealed interface MtbFileRequest : MtbRequest { + override val requestId: RequestId + val content: T + + fun patientPseudonym(): PatientPseudonym +} + +data class BwhcV1MtbFileRequest( + override val requestId: RequestId, + override val content: MtbFile +) : MtbFileRequest { + override fun patientPseudonym(): PatientPseudonym { + return PatientPseudonym(content.patient.id) + } +} + +data class DnpmV2MtbFileRequest( + override val requestId: RequestId, + override val content: Mtb +) : MtbFileRequest { + override fun patientPseudonym(): PatientPseudonym { + return PatientPseudonym(content.patient.id) + } +} + +data class DeleteRequest( + override val requestId: RequestId, + val patientId: PatientPseudonym +) : MtbRequest diff --git a/src/main/kotlin/dev/dnpm/etl/processor/output/RestMtbFileSender.kt b/src/main/kotlin/dev/dnpm/etl/processor/output/RestMtbFileSender.kt index 90e3629..78222b2 100644 --- a/src/main/kotlin/dev/dnpm/etl/processor/output/RestMtbFileSender.kt +++ b/src/main/kotlin/dev/dnpm/etl/processor/output/RestMtbFileSender.kt @@ -1,7 +1,7 @@ /* * This file is part of ETL-Processor * - * Copyright (c) 2024 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors + * Copyright (c) 2025 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published @@ -19,10 +19,11 @@ package dev.dnpm.etl.processor.output -import dev.dnpm.etl.processor.config.RestTargetProperties -import dev.dnpm.etl.processor.monitoring.RequestStatus +import dev.dnpm.etl.processor.CustomMediaType import dev.dnpm.etl.processor.PatientPseudonym +import dev.dnpm.etl.processor.config.RestTargetProperties import dev.dnpm.etl.processor.monitoring.ReportService +import dev.dnpm.etl.processor.monitoring.RequestStatus import dev.dnpm.etl.processor.monitoring.asRequestStatus import org.slf4j.LoggerFactory import org.springframework.http.HttpEntity @@ -46,11 +47,11 @@ abstract class RestMtbFileSender( abstract fun deleteUrl(patientId: PatientPseudonym): String - override fun send(request: MtbFileSender.MtbFileRequest): MtbFileSender.Response { + override fun send(request: MtbFileRequest): MtbFileSender.Response { try { return retryTemplate.execute { - val headers = getHttpHeaders() - val entityReq = HttpEntity(request.mtbFile, headers) + val headers = getHttpHeaders(request) + val entityReq = HttpEntity(request.content, headers) val response = restTemplate.postForEntity( sendUrl(), entityReq, @@ -76,10 +77,10 @@ abstract class RestMtbFileSender( return MtbFileSender.Response(RequestStatus.ERROR, "Sonstiger Fehler bei der Übertragung") } - override fun send(request: MtbFileSender.DeleteRequest): MtbFileSender.Response { + override fun send(request: DeleteRequest): MtbFileSender.Response { try { return retryTemplate.execute { - val headers = getHttpHeaders() + val headers = getHttpHeaders(request) val entityReq = HttpEntity(null, headers) restTemplate.delete( deleteUrl(request.patientId), @@ -102,11 +103,15 @@ abstract class RestMtbFileSender( return this.restTargetProperties.uri.orEmpty() } - private fun getHttpHeaders(): HttpHeaders { + private fun getHttpHeaders(request: MtbRequest): HttpHeaders { val username = restTargetProperties.username val password = restTargetProperties.password val headers = HttpHeaders() - headers.contentType = MediaType.APPLICATION_JSON + headers.contentType = when (request) { + is BwhcV1MtbFileRequest -> MediaType.APPLICATION_JSON + is DnpmV2MtbFileRequest -> CustomMediaType.APPLICATION_VND_DNPM_V2_MTB_JSON + else -> MediaType.APPLICATION_JSON + } if (username.isNullOrBlank() || password.isNullOrBlank()) { return headers @@ -116,4 +121,4 @@ abstract class RestMtbFileSender( return headers } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/dnpm/etl/processor/services/RequestProcessor.kt b/src/main/kotlin/dev/dnpm/etl/processor/services/RequestProcessor.kt index 5b2c42a..f25452e 100644 --- a/src/main/kotlin/dev/dnpm/etl/processor/services/RequestProcessor.kt +++ b/src/main/kotlin/dev/dnpm/etl/processor/services/RequestProcessor.kt @@ -1,7 +1,7 @@ /* * This file is part of ETL-Processor * - * Copyright (c) 2023 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors + * Copyright (c) 2025 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published @@ -27,10 +27,11 @@ import dev.dnpm.etl.processor.monitoring.Report import dev.dnpm.etl.processor.monitoring.Request import dev.dnpm.etl.processor.monitoring.RequestStatus import dev.dnpm.etl.processor.monitoring.RequestType -import dev.dnpm.etl.processor.output.MtbFileSender +import dev.dnpm.etl.processor.output.* import dev.dnpm.etl.processor.pseudonym.PseudonymizeService import dev.dnpm.etl.processor.pseudonym.anonymizeContentWith import dev.dnpm.etl.processor.pseudonym.pseudonymizeWith +import dev.pcvolkmer.mv64e.mtb.Mtb import org.apache.commons.codec.binary.Base32 import org.apache.commons.codec.digest.DigestUtils import org.springframework.context.ApplicationEventPublisher @@ -55,29 +56,40 @@ class RequestProcessor( fun processMtbFile(mtbFile: MtbFile, requestId: RequestId) { val pid = PatientId(mtbFile.patient.id) - mtbFile pseudonymizeWith pseudonymizeService mtbFile anonymizeContentWith pseudonymizeService + val request = BwhcV1MtbFileRequest(requestId, transformationService.transform(mtbFile)) + saveAndSend(request, pid) + } - val request = MtbFileSender.MtbFileRequest(requestId, transformationService.transform(mtbFile)) + fun processMtbFile(mtbFile: Mtb) { + processMtbFile(mtbFile, randomRequestId()) + } - val patientPseudonym = PatientPseudonym(request.mtbFile.patient.id) + fun processMtbFile(mtbFile: Mtb, requestId: RequestId) { + val pid = PatientId(mtbFile.patient.id) + mtbFile pseudonymizeWith pseudonymizeService + mtbFile anonymizeContentWith pseudonymizeService + val request = DnpmV2MtbFileRequest(requestId, transformationService.transform(mtbFile)) + saveAndSend(request, pid) + } + private fun saveAndSend(request: MtbFileRequest, pid: PatientId) { requestService.save( Request( - requestId, - patientPseudonym, + request.requestId, + request.patientPseudonym(), pid, - fingerprint(request.mtbFile), + fingerprint(request), RequestType.MTB_FILE, RequestStatus.UNKNOWN ) ) - if (appConfigProperties.duplicationDetection && isDuplication(mtbFile)) { + if (appConfigProperties.duplicationDetection && isDuplication(request)) { applicationEventPublisher.publishEvent( ResponseEvent( - requestId, + request.requestId, Instant.now(), RequestStatus.DUPLICATION ) @@ -89,7 +101,7 @@ class RequestProcessor( applicationEventPublisher.publishEvent( ResponseEvent( - requestId, + request.requestId, Instant.now(), responseStatus.status, when (responseStatus.status) { @@ -100,8 +112,11 @@ class RequestProcessor( ) } - private fun isDuplication(pseudonymizedMtbFile: MtbFile): Boolean { - val patientPseudonym = PatientPseudonym(pseudonymizedMtbFile.patient.id) + private fun isDuplication(pseudonymizedMtbFileRequest: MtbFileRequest): Boolean { + val patientPseudonym = when (pseudonymizedMtbFileRequest) { + is BwhcV1MtbFileRequest -> PatientPseudonym(pseudonymizedMtbFileRequest.content.patient.id) + is DnpmV2MtbFileRequest -> PatientPseudonym(pseudonymizedMtbFileRequest.content.patient.id) + } val lastMtbFileRequestForPatient = requestService.lastMtbFileRequestForPatientPseudonym(patientPseudonym) @@ -109,7 +124,7 @@ class RequestProcessor( return null != lastMtbFileRequestForPatient && !isLastRequestDeletion - && lastMtbFileRequestForPatient.fingerprint == fingerprint(pseudonymizedMtbFile) + && lastMtbFileRequestForPatient.fingerprint == fingerprint(pseudonymizedMtbFileRequest) } fun processDeletion(patientId: PatientId) { @@ -131,7 +146,7 @@ class RequestProcessor( ) ) - val responseStatus = sender.send(MtbFileSender.DeleteRequest(requestId, patientPseudonym)) + val responseStatus = sender.send(DeleteRequest(requestId, patientPseudonym)) applicationEventPublisher.publishEvent( ResponseEvent( @@ -160,8 +175,11 @@ class RequestProcessor( } } - private fun fingerprint(mtbFile: MtbFile): Fingerprint { - return fingerprint(objectMapper.writeValueAsString(mtbFile)) + private fun fingerprint(request: MtbFileRequest): Fingerprint { + return when (request) { + is BwhcV1MtbFileRequest -> fingerprint(objectMapper.writeValueAsString(request.content)) + is DnpmV2MtbFileRequest -> fingerprint(objectMapper.writeValueAsString(request.content)) + } } private fun fingerprint(s: String): Fingerprint { @@ -172,4 +190,4 @@ class RequestProcessor( ) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/dnpm/etl/processor/services/TransformationService.kt b/src/main/kotlin/dev/dnpm/etl/processor/services/TransformationService.kt index 2a9dc5b..9447a84 100644 --- a/src/main/kotlin/dev/dnpm/etl/processor/services/TransformationService.kt +++ b/src/main/kotlin/dev/dnpm/etl/processor/services/TransformationService.kt @@ -23,10 +23,21 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.PathNotFoundException import de.ukw.ccc.bwhc.dto.MtbFile +import dev.pcvolkmer.mv64e.mtb.Mtb class TransformationService(private val objectMapper: ObjectMapper, private val transformations: List) { fun transform(mtbFile: MtbFile): MtbFile { - var json = objectMapper.writeValueAsString(mtbFile) + val json = transform(objectMapper.writeValueAsString(mtbFile)) + return objectMapper.readValue(json, MtbFile::class.java) + } + + fun transform(mtbFile: Mtb): Mtb { + val json = transform(objectMapper.writeValueAsString(mtbFile)) + return objectMapper.readValue(json, Mtb::class.java) + } + + private fun transform(content: String): String { + var json = content transformations.forEach { transformation -> val jsonPath = JsonPath.parse(json) @@ -48,7 +59,7 @@ class TransformationService(private val objectMapper: ObjectMapper, private val json = jsonPath.jsonString() } - return objectMapper.readValue(json, MtbFile::class.java) + return json } fun getTransformations(): List { diff --git a/src/test/kotlin/dev/dnpm/etl/processor/input/KafkaInputListenerTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/input/KafkaInputListenerTest.kt index 10900a8..f2abd27 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/input/KafkaInputListenerTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/input/KafkaInputListenerTest.kt @@ -74,7 +74,7 @@ class KafkaInputListenerTest { ) ) - verify(requestProcessor, times(1)).processMtbFile(any()) + verify(requestProcessor, times(1)).processMtbFile(any()) } @Test @@ -121,7 +121,7 @@ class KafkaInputListenerTest { ) ) - verify(requestProcessor, times(1)).processMtbFile(any(), anyValueClass()) + verify(requestProcessor, times(1)).processMtbFile(any(), anyValueClass()) } @Test diff --git a/src/test/kotlin/dev/dnpm/etl/processor/input/MtbFileRestControllerTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/input/MtbFileRestControllerTest.kt index faaf778..4a33078 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/input/MtbFileRestControllerTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/input/MtbFileRestControllerTest.kt @@ -23,6 +23,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import de.ukw.ccc.bwhc.dto.* import dev.dnpm.etl.processor.CustomMediaType import dev.dnpm.etl.processor.services.RequestProcessor +import dev.pcvolkmer.mv64e.mtb.Mtb import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test @@ -72,7 +73,7 @@ class MtbFileRestControllerTest { } } - verify(requestProcessor, times(1)).processMtbFile(any()) + verify(requestProcessor, times(1)).processMtbFile(any()) } @Test @@ -128,7 +129,7 @@ class MtbFileRestControllerTest { } } - verify(requestProcessor, times(1)).processMtbFile(any()) + verify(requestProcessor, times(1)).processMtbFile(any()) } @Test @@ -182,11 +183,11 @@ class MtbFileRestControllerTest { contentType = CustomMediaType.APPLICATION_VND_DNPM_V2_MTB_JSON }.andExpect { status { - isNotImplemented() + isAccepted() } } - verify(requestProcessor, times(0)).processMtbFile(any()) + verify(requestProcessor, times(1)).processMtbFile(any()) } } diff --git a/src/test/kotlin/dev/dnpm/etl/processor/output/KafkaMtbFileSenderTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/output/KafkaMtbFileSenderTest.kt index 655e29e..e5fb925 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/output/KafkaMtbFileSenderTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/output/KafkaMtbFileSenderTest.kt @@ -1,7 +1,7 @@ /* * This file is part of ETL-Processor * - * Copyright (c) 2024 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors + * Copyright (c) 2025 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published @@ -21,20 +21,25 @@ package dev.dnpm.etl.processor.output import com.fasterxml.jackson.databind.ObjectMapper import de.ukw.ccc.bwhc.dto.* +import de.ukw.ccc.bwhc.dto.Patient +import dev.dnpm.etl.processor.CustomMediaType import dev.dnpm.etl.processor.PatientPseudonym import dev.dnpm.etl.processor.RequestId import dev.dnpm.etl.processor.config.KafkaProperties import dev.dnpm.etl.processor.monitoring.RequestStatus +import dev.pcvolkmer.mv64e.mtb.* +import org.apache.kafka.clients.producer.ProducerRecord import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.MethodSource -import org.mockito.ArgumentMatchers.anyString import org.mockito.Mock import org.mockito.junit.jupiter.MockitoExtension import org.mockito.kotlin.* +import org.springframework.http.MediaType import org.springframework.kafka.core.KafkaTemplate import org.springframework.kafka.support.SendResult import org.springframework.retry.policy.SimpleRetryPolicy @@ -45,142 +50,231 @@ import java.util.concurrent.ExecutionException @ExtendWith(MockitoExtension::class) class KafkaMtbFileSenderTest { - private lateinit var kafkaTemplate: KafkaTemplate + @Nested + inner class BwhcV1Record { - private lateinit var kafkaMtbFileSender: KafkaMtbFileSender + private lateinit var kafkaTemplate: KafkaTemplate - private lateinit var objectMapper: ObjectMapper + private lateinit var kafkaMtbFileSender: KafkaMtbFileSender - @BeforeEach - fun setup( - @Mock kafkaTemplate: KafkaTemplate - ) { - val kafkaProperties = KafkaProperties("testtopic") - val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(1)).build() + private lateinit var objectMapper: ObjectMapper - this.objectMapper = ObjectMapper() - this.kafkaTemplate = kafkaTemplate + @BeforeEach + fun setup( + @Mock kafkaTemplate: KafkaTemplate + ) { + val kafkaProperties = KafkaProperties("testtopic") + val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(1)).build() - this.kafkaMtbFileSender = KafkaMtbFileSender(kafkaTemplate, kafkaProperties, retryTemplate, objectMapper) - } + this.objectMapper = ObjectMapper() + this.kafkaTemplate = kafkaTemplate - @ParameterizedTest - @MethodSource("requestWithResponseSource") - fun shouldSendMtbFileRequestAndReturnExpectedState(testData: TestData) { - doAnswer { - if (null != testData.exception) { - throw testData.exception - } - completedFuture(SendResult(null, null)) - }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - - val response = kafkaMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile(Consent.Status.ACTIVE))) - assertThat(response.status).isEqualTo(testData.requestStatus) - } - - @ParameterizedTest - @MethodSource("requestWithResponseSource") - fun shouldSendDeleteRequestAndReturnExpectedState(testData: TestData) { - doAnswer { - if (null != testData.exception) { - throw testData.exception - } - completedFuture(SendResult(null, null)) - }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - - val response = kafkaMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) - assertThat(response.status).isEqualTo(testData.requestStatus) - } - - @Test - fun shouldSendMtbFileRequestWithCorrectKeyAndBody() { - doAnswer { - completedFuture(SendResult(null, null)) - }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - - kafkaMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile(Consent.Status.ACTIVE))) - - val captor = argumentCaptor() - verify(kafkaTemplate, times(1)).send(anyString(), captor.capture(), captor.capture()) - assertThat(captor.firstValue).isNotNull - assertThat(captor.firstValue).isEqualTo("{\"pid\": \"PID\"}") - assertThat(captor.secondValue).isNotNull - assertThat(captor.secondValue).isEqualTo(objectMapper.writeValueAsString(kafkaRecordData(TEST_REQUEST_ID, Consent.Status.ACTIVE))) - } - - @Test - fun shouldSendDeleteRequestWithCorrectKeyAndBody() { - doAnswer { - completedFuture(SendResult(null, null)) - }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - - kafkaMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) - - val captor = argumentCaptor() - verify(kafkaTemplate, times(1)).send(anyString(), captor.capture(), captor.capture()) - assertThat(captor.firstValue).isNotNull - assertThat(captor.firstValue).isEqualTo("{\"pid\": \"PID\"}") - assertThat(captor.secondValue).isNotNull - assertThat(captor.secondValue).isEqualTo(objectMapper.writeValueAsString(kafkaRecordData(TEST_REQUEST_ID, Consent.Status.REJECTED))) - } - - @ParameterizedTest - @MethodSource("requestWithResponseSource") - fun shouldRetryOnMtbFileKafkaSendError(testData: TestData) { - val kafkaProperties = KafkaProperties("testtopic") - val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(3)).build() - this.kafkaMtbFileSender = KafkaMtbFileSender(this.kafkaTemplate, kafkaProperties, retryTemplate, this.objectMapper) - - doAnswer { - if (null != testData.exception) { - throw testData.exception - } - completedFuture(SendResult(null, null)) - }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - - kafkaMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile(Consent.Status.ACTIVE))) - - val expectedCount = when (testData.exception) { - // OK - No Retry - null -> times(1) - // Request failed - Retry max 3 times - else -> times(3) + this.kafkaMtbFileSender = KafkaMtbFileSender(kafkaTemplate, kafkaProperties, retryTemplate, objectMapper) } - verify(kafkaTemplate, expectedCount).send(anyString(), anyString(), anyString()) - } + @ParameterizedTest + @MethodSource("dev.dnpm.etl.processor.output.KafkaMtbFileSenderTest#requestWithResponseSource") + fun shouldSendMtbFileRequestAndReturnExpectedState(testData: TestData) { + doAnswer { + if (null != testData.exception) { + throw testData.exception + } + completedFuture(SendResult(null, null)) + }.whenever(kafkaTemplate).send(any>()) - @ParameterizedTest - @MethodSource("requestWithResponseSource") - fun shouldRetryOnDeleteKafkaSendError(testData: TestData) { - val kafkaProperties = KafkaProperties("testtopic") - val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(3)).build() - this.kafkaMtbFileSender = KafkaMtbFileSender(this.kafkaTemplate, kafkaProperties, retryTemplate, this.objectMapper) - - doAnswer { - if (null != testData.exception) { - throw testData.exception - } - completedFuture(SendResult(null, null)) - }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - - kafkaMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) - - val expectedCount = when (testData.exception) { - // OK - No Retry - null -> times(1) - // Request failed - Retry max 3 times - else -> times(3) + val response = kafkaMtbFileSender.send(BwhcV1MtbFileRequest(TEST_REQUEST_ID, bwhcV1MtbFile(Consent.Status.ACTIVE))) + assertThat(response.status).isEqualTo(testData.requestStatus) + } + + @ParameterizedTest + @MethodSource("dev.dnpm.etl.processor.output.KafkaMtbFileSenderTest#requestWithResponseSource") + fun shouldSendDeleteRequestAndReturnExpectedState(testData: TestData) { + doAnswer { + if (null != testData.exception) { + throw testData.exception + } + completedFuture(SendResult(null, null)) + }.whenever(kafkaTemplate).send(any>()) + + val response = kafkaMtbFileSender.send(DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) + assertThat(response.status).isEqualTo(testData.requestStatus) + } + + @Test + fun shouldSendMtbFileRequestWithCorrectKeyAndHeaderAndBody() { + doAnswer { + completedFuture(SendResult(null, null)) + }.whenever(kafkaTemplate).send(any>()) + + kafkaMtbFileSender.send(BwhcV1MtbFileRequest(TEST_REQUEST_ID, bwhcV1MtbFile(Consent.Status.ACTIVE))) + + val captor = argumentCaptor>() + verify(kafkaTemplate, times(1)).send(captor.capture()) + assertThat(captor.firstValue.key()).isNotNull + assertThat(captor.firstValue.key()).isEqualTo("{\"pid\": \"PID\"}") + assertThat(captor.firstValue.headers().headers("contentType")).isNotNull + assertThat(captor.firstValue.headers().headers("contentType")?.firstOrNull()?.value()).isEqualTo(MediaType.APPLICATION_JSON_VALUE.toByteArray()) + assertThat(captor.firstValue.value()).isNotNull + assertThat(captor.firstValue.value()).isEqualTo(objectMapper.writeValueAsString(bwhcV1kafkaRecordData(TEST_REQUEST_ID, Consent.Status.ACTIVE))) + } + + @Test + fun shouldSendDeleteRequestWithCorrectKeyAndBody() { + doAnswer { + completedFuture(SendResult(null, null)) + }.whenever(kafkaTemplate).send(any>()) + + kafkaMtbFileSender.send(DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) + + val captor = argumentCaptor>() + verify(kafkaTemplate, times(1)).send(captor.capture()) + assertThat(captor.firstValue.key()).isNotNull + assertThat(captor.firstValue.key()).isEqualTo("{\"pid\": \"PID\"}") + assertThat(captor.firstValue.value()).isNotNull + assertThat(captor.firstValue.value()).isEqualTo(objectMapper.writeValueAsString(bwhcV1kafkaRecordData(TEST_REQUEST_ID, Consent.Status.REJECTED))) + } + + @ParameterizedTest + @MethodSource("dev.dnpm.etl.processor.output.KafkaMtbFileSenderTest#requestWithResponseSource") + fun shouldRetryOnMtbFileKafkaSendError(testData: TestData) { + val kafkaProperties = KafkaProperties("testtopic") + val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(3)).build() + this.kafkaMtbFileSender = KafkaMtbFileSender(this.kafkaTemplate, kafkaProperties, retryTemplate, this.objectMapper) + + doAnswer { + if (null != testData.exception) { + throw testData.exception + } + completedFuture(SendResult(null, null)) + }.whenever(kafkaTemplate).send(any>()) + + kafkaMtbFileSender.send(BwhcV1MtbFileRequest(TEST_REQUEST_ID, bwhcV1MtbFile(Consent.Status.ACTIVE))) + + val expectedCount = when (testData.exception) { + // OK - No Retry + null -> times(1) + // Request failed - Retry max 3 times + else -> times(3) + } + + verify(kafkaTemplate, expectedCount).send(any>()) + } + + @ParameterizedTest + @MethodSource("dev.dnpm.etl.processor.output.KafkaMtbFileSenderTest#requestWithResponseSource") + fun shouldRetryOnDeleteKafkaSendError(testData: TestData) { + val kafkaProperties = KafkaProperties("testtopic") + val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(3)).build() + this.kafkaMtbFileSender = KafkaMtbFileSender(this.kafkaTemplate, kafkaProperties, retryTemplate, this.objectMapper) + + doAnswer { + if (null != testData.exception) { + throw testData.exception + } + completedFuture(SendResult(null, null)) + }.whenever(kafkaTemplate).send(any>()) + + kafkaMtbFileSender.send(DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) + + val expectedCount = when (testData.exception) { + // OK - No Retry + null -> times(1) + // Request failed - Retry max 3 times + else -> times(3) + } + + verify(kafkaTemplate, expectedCount).send(any>()) + } + + } + + @Nested + inner class DnpmV2Record { + + private lateinit var kafkaTemplate: KafkaTemplate + + private lateinit var kafkaMtbFileSender: KafkaMtbFileSender + + private lateinit var objectMapper: ObjectMapper + + @BeforeEach + fun setup( + @Mock kafkaTemplate: KafkaTemplate + ) { + val kafkaProperties = KafkaProperties("testtopic") + val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(1)).build() + + this.objectMapper = ObjectMapper() + this.kafkaTemplate = kafkaTemplate + + this.kafkaMtbFileSender = KafkaMtbFileSender(kafkaTemplate, kafkaProperties, retryTemplate, objectMapper) + } + + @ParameterizedTest + @MethodSource("dev.dnpm.etl.processor.output.KafkaMtbFileSenderTest#requestWithResponseSource") + fun shouldSendMtbFileRequestAndReturnExpectedState(testData: TestData) { + doAnswer { + if (null != testData.exception) { + throw testData.exception + } + completedFuture(SendResult(null, null)) + }.whenever(kafkaTemplate).send(any>()) + + val response = kafkaMtbFileSender.send(DnpmV2MtbFileRequest(TEST_REQUEST_ID, dnpmV2MtbFile())) + assertThat(response.status).isEqualTo(testData.requestStatus) + } + + @Test + fun shouldSendMtbFileRequestWithCorrectKeyAndHeaderAndBody() { + doAnswer { + completedFuture(SendResult(null, null)) + }.whenever(kafkaTemplate).send(any>()) + + kafkaMtbFileSender.send(DnpmV2MtbFileRequest(TEST_REQUEST_ID, dnpmV2MtbFile())) + + val captor = argumentCaptor>() + verify(kafkaTemplate, times(1)).send(captor.capture()) + assertThat(captor.firstValue.key()).isNotNull + assertThat(captor.firstValue.key()).isEqualTo("{\"pid\": \"PID\"}") + assertThat(captor.firstValue.headers().headers("contentType")).isNotNull + assertThat(captor.firstValue.headers().headers("contentType")?.firstOrNull()?.value()).isEqualTo(CustomMediaType.APPLICATION_VND_DNPM_V2_MTB_JSON_VALUE.toByteArray()) + assertThat(captor.firstValue.value()).isNotNull + assertThat(captor.firstValue.value()).isEqualTo(objectMapper.writeValueAsString(dnmpV2kafkaRecordData(TEST_REQUEST_ID))) + } + + @ParameterizedTest + @MethodSource("dev.dnpm.etl.processor.output.KafkaMtbFileSenderTest#requestWithResponseSource") + fun shouldRetryOnMtbFileKafkaSendError(testData: TestData) { + val kafkaProperties = KafkaProperties("testtopic") + val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(3)).build() + this.kafkaMtbFileSender = KafkaMtbFileSender(this.kafkaTemplate, kafkaProperties, retryTemplate, this.objectMapper) + + doAnswer { + if (null != testData.exception) { + throw testData.exception + } + completedFuture(SendResult(null, null)) + }.whenever(kafkaTemplate).send(any>()) + + kafkaMtbFileSender.send(DnpmV2MtbFileRequest(TEST_REQUEST_ID, dnpmV2MtbFile())) + + val expectedCount = when (testData.exception) { + // OK - No Retry + null -> times(1) + // Request failed - Retry max 3 times + else -> times(3) + } + + verify(kafkaTemplate, expectedCount).send(any>()) } - verify(kafkaTemplate, expectedCount).send(anyString(), anyString(), anyString()) } companion object { val TEST_REQUEST_ID = RequestId("TestId") val TEST_PATIENT_PSEUDONYM = PatientPseudonym("PID") - fun mtbFile(consentStatus: Consent.Status): MtbFile { + fun bwhcV1MtbFile(consentStatus: Consent.Status): MtbFile { return if (consentStatus == Consent.Status.ACTIVE) { MtbFile.builder() .withPatient( @@ -215,8 +309,31 @@ class KafkaMtbFileSenderTest { }.build() } - fun kafkaRecordData(requestId: RequestId, consentStatus: Consent.Status): KafkaMtbFileSender.Data { - return KafkaMtbFileSender.Data(requestId, mtbFile(consentStatus)) + fun dnpmV2MtbFile(): Mtb = Mtb.builder() + .withPatient( + dev.pcvolkmer.mv64e.mtb.Patient.builder() + .withId("PID") + .withBirthDate("2000-08-08") + .withGender(CodingGender.builder().withCode(CodingGender.Code.MALE).build()) + .build() + ) + .withEpisodesOfCare( + listOf( + MTBEpisodeOfCare.builder() + .withId("1") + .withPatient(Reference("PID")) + .withPeriod(PeriodDate.builder().withStart("2023-08-08").build()) + .build() + ) + ) + .build() + + fun bwhcV1kafkaRecordData(requestId: RequestId, consentStatus: Consent.Status): MtbRequest { + return BwhcV1MtbFileRequest(requestId, bwhcV1MtbFile(consentStatus)) + } + + fun dnmpV2kafkaRecordData(requestId: RequestId): MtbRequest { + return DnpmV2MtbFileRequest(requestId, dnpmV2MtbFile()) } data class TestData(val requestStatus: RequestStatus, val exception: Throwable? = null) @@ -231,4 +348,4 @@ class KafkaMtbFileSenderTest { } } -} \ No newline at end of file +} diff --git a/src/test/kotlin/dev/dnpm/etl/processor/output/RestBwhcMtbFileSenderTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/output/RestBwhcMtbFileSenderTest.kt index ffbc65c..ead2496 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/output/RestBwhcMtbFileSenderTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/output/RestBwhcMtbFileSenderTest.kt @@ -30,16 +30,16 @@ import dev.dnpm.etl.processor.monitoring.RequestStatus import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.Arguments import org.junit.jupiter.params.provider.MethodSource +import org.springframework.http.HttpHeaders import org.springframework.http.HttpMethod import org.springframework.http.HttpStatus +import org.springframework.http.MediaType import org.springframework.retry.policy.SimpleRetryPolicy import org.springframework.retry.support.RetryTemplateBuilder import org.springframework.test.web.client.ExpectedCount import org.springframework.test.web.client.MockRestServiceServer -import org.springframework.test.web.client.match.MockRestRequestMatchers.method -import org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo +import org.springframework.test.web.client.match.MockRestRequestMatchers.* import org.springframework.test.web.client.response.MockRestResponseCreators.withStatus import org.springframework.web.client.RestTemplate @@ -73,7 +73,7 @@ class RestBwhcMtbFileSenderTest { withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) } - val response = restMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) + val response = restMtbFileSender.send(DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) assertThat(response.status).isEqualTo(requestWithResponse.response.status) assertThat(response.body).isEqualTo(requestWithResponse.response.body) } @@ -84,11 +84,12 @@ class RestBwhcMtbFileSenderTest { this.mockRestServiceServer .expect(method(HttpMethod.POST)) .andExpect(requestTo("http://localhost:9000/mtbfile/MTBFile")) + .andExpect(header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)) .andRespond { withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) } - val response = restMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile)) + val response = restMtbFileSender.send(BwhcV1MtbFileRequest(TEST_REQUEST_ID, mtbFile)) assertThat(response.status).isEqualTo(requestWithResponse.response.status) assertThat(response.body).isEqualTo(requestWithResponse.response.body) } @@ -118,7 +119,7 @@ class RestBwhcMtbFileSenderTest { withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) } - val response = restMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile)) + val response = restMtbFileSender.send(BwhcV1MtbFileRequest(TEST_REQUEST_ID, mtbFile)) assertThat(response.status).isEqualTo(requestWithResponse.response.status) assertThat(response.body).isEqualTo(requestWithResponse.response.body) } @@ -148,7 +149,7 @@ class RestBwhcMtbFileSenderTest { withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) } - val response = restMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) + val response = restMtbFileSender.send(DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) assertThat(response.status).isEqualTo(requestWithResponse.response.status) assertThat(response.body).isEqualTo(requestWithResponse.response.body) } @@ -309,4 +310,4 @@ class RestBwhcMtbFileSenderTest { } -} \ No newline at end of file +} diff --git a/src/test/kotlin/dev/dnpm/etl/processor/output/RestDipMtbFileSenderTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/output/RestDipMtbFileSenderTest.kt index 005c0fd..b35fb47 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/output/RestDipMtbFileSenderTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/output/RestDipMtbFileSenderTest.kt @@ -22,6 +22,8 @@ package dev.dnpm.etl.processor.output import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinModule import de.ukw.ccc.bwhc.dto.* +import de.ukw.ccc.bwhc.dto.Patient +import dev.dnpm.etl.processor.CustomMediaType import dev.dnpm.etl.processor.PatientPseudonym import dev.dnpm.etl.processor.RequestId import dev.dnpm.etl.processor.config.AppConfigProperties @@ -29,136 +31,206 @@ import dev.dnpm.etl.processor.config.AppConfiguration import dev.dnpm.etl.processor.config.RestTargetProperties import dev.dnpm.etl.processor.monitoring.ReportService import dev.dnpm.etl.processor.monitoring.RequestStatus -import dev.dnpm.etl.processor.output.RestBwhcMtbFileSenderTest.Companion +import dev.pcvolkmer.mv64e.mtb.* import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Nested import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.MethodSource +import org.springframework.http.HttpHeaders import org.springframework.http.HttpMethod import org.springframework.http.HttpStatus +import org.springframework.http.MediaType import org.springframework.retry.backoff.NoBackOffPolicy import org.springframework.retry.policy.SimpleRetryPolicy import org.springframework.retry.support.RetryTemplateBuilder import org.springframework.test.web.client.ExpectedCount import org.springframework.test.web.client.MockRestServiceServer -import org.springframework.test.web.client.match.MockRestRequestMatchers.method -import org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo +import org.springframework.test.web.client.match.MockRestRequestMatchers.* import org.springframework.test.web.client.response.MockRestResponseCreators.withStatus import org.springframework.web.client.RestTemplate class RestDipMtbFileSenderTest { - private lateinit var mockRestServiceServer: MockRestServiceServer + @Nested + inner class BwhcV1ContentRequest { - private lateinit var restMtbFileSender: RestMtbFileSender + private lateinit var mockRestServiceServer: MockRestServiceServer - private var reportService = ReportService(ObjectMapper().registerModule(KotlinModule.Builder().build())) + private lateinit var restMtbFileSender: RestMtbFileSender - @BeforeEach - fun setup() { - val restTemplate = RestTemplate() - val restTargetProperties = RestTargetProperties("http://localhost:9000/api", null, null, false) - val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(1)).build() + private var reportService = ReportService(ObjectMapper().registerModule(KotlinModule.Builder().build())) - this.mockRestServiceServer = MockRestServiceServer.createServer(restTemplate) + @BeforeEach + fun setup() { + val restTemplate = RestTemplate() + val restTargetProperties = RestTargetProperties("http://localhost:9000/api", null, null, false) + val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(1)).build() - this.restMtbFileSender = RestDipMtbFileSender(restTemplate, restTargetProperties, retryTemplate, reportService) - } + this.mockRestServiceServer = MockRestServiceServer.createServer(restTemplate) - @ParameterizedTest - @MethodSource("deleteRequestWithResponseSource") - fun shouldReturnExpectedResponseForDelete(requestWithResponse: RequestWithResponse) { - this.mockRestServiceServer - .expect(method(HttpMethod.DELETE)) - .andExpect(requestTo("http://localhost:9000/api/mtb/etl/patient/${TEST_PATIENT_PSEUDONYM.value}")) - .andRespond { - withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) - } - - val response = restMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) - assertThat(response.status).isEqualTo(requestWithResponse.response.status) - assertThat(response.body).isEqualTo(requestWithResponse.response.body) - } - - @ParameterizedTest - @MethodSource("mtbFileRequestWithResponseSource") - fun shouldReturnExpectedResponseForMtbFilePost(requestWithResponse: RequestWithResponse) { - this.mockRestServiceServer - .expect(method(HttpMethod.POST)) - .andExpect(requestTo("http://localhost:9000/api/mtb/etl/patient-record")) - .andRespond { - withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) - } - - val response = restMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile)) - assertThat(response.status).isEqualTo(requestWithResponse.response.status) - assertThat(response.body).isEqualTo(requestWithResponse.response.body) - } - - @ParameterizedTest - @MethodSource("mtbFileRequestWithResponseSource") - fun shouldRetryOnMtbFileHttpRequestError(requestWithResponse: RequestWithResponse) { - val restTemplate = RestTemplate() - val restTargetProperties = RestTargetProperties("http://localhost:9000/api", null, null, false) - val retryTemplate = AppConfiguration().retryTemplate(AppConfigProperties("http://localhost:9000")) - retryTemplate.setBackOffPolicy(NoBackOffPolicy()) - - this.mockRestServiceServer = MockRestServiceServer.createServer(restTemplate) - this.restMtbFileSender = - RestDipMtbFileSender(restTemplate, restTargetProperties, retryTemplate, reportService) - - val expectedCount = when (requestWithResponse.httpStatus) { - // OK - No Retry - HttpStatus.OK, HttpStatus.CREATED, HttpStatus.UNPROCESSABLE_ENTITY, HttpStatus.BAD_REQUEST -> ExpectedCount.max( - 1 - ) - // Request failed - Retry max 3 times - else -> ExpectedCount.max(3) + this.restMtbFileSender = + RestDipMtbFileSender(restTemplate, restTargetProperties, retryTemplate, reportService) } - this.mockRestServiceServer - .expect(expectedCount, method(HttpMethod.POST)) - .andExpect(requestTo("http://localhost:9000/api/mtb/etl/patient-record")) - .andRespond { - withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) - } + @ParameterizedTest + @MethodSource("dev.dnpm.etl.processor.output.RestDipMtbFileSenderTest#mtbFileRequestWithResponseSource") + fun shouldReturnExpectedResponseForMtbFilePost(requestWithResponse: RequestWithResponse) { + this.mockRestServiceServer + .expect(method(HttpMethod.POST)) + .andExpect(requestTo("http://localhost:9000/api/mtb/etl/patient-record")) + .andExpect(header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)) + .andRespond { + withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) + } - val response = restMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile)) - assertThat(response.status).isEqualTo(requestWithResponse.response.status) - assertThat(response.body).isEqualTo(requestWithResponse.response.body) - } - - @ParameterizedTest - @MethodSource("deleteRequestWithResponseSource") - fun shouldRetryOnDeleteHttpRequestError(requestWithResponse: RequestWithResponse) { - val restTemplate = RestTemplate() - val restTargetProperties = RestTargetProperties("http://localhost:9000/api", null, null, false) - val retryTemplate = AppConfiguration().retryTemplate(AppConfigProperties("http://localhost:9000")) - retryTemplate.setBackOffPolicy(NoBackOffPolicy()) - - this.mockRestServiceServer = MockRestServiceServer.createServer(restTemplate) - this.restMtbFileSender = - RestDipMtbFileSender(restTemplate, restTargetProperties, retryTemplate, reportService) - - val expectedCount = when (requestWithResponse.httpStatus) { - // OK - No Retry - HttpStatus.OK, HttpStatus.CREATED, HttpStatus.UNPROCESSABLE_ENTITY, HttpStatus.BAD_REQUEST -> ExpectedCount.max( - 1 - ) - // Request failed - Retry max 3 times - else -> ExpectedCount.max(3) + val response = restMtbFileSender.send(BwhcV1MtbFileRequest(TEST_REQUEST_ID, bwhcV1mtbFile)) + assertThat(response.status).isEqualTo(requestWithResponse.response.status) + assertThat(response.body).isEqualTo(requestWithResponse.response.body) } - this.mockRestServiceServer - .expect(expectedCount, method(HttpMethod.DELETE)) - .andExpect(requestTo("http://localhost:9000/api/mtb/etl/patient/${TEST_PATIENT_PSEUDONYM.value}")) - .andRespond { - withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) + @ParameterizedTest + @MethodSource("dev.dnpm.etl.processor.output.RestDipMtbFileSenderTest#mtbFileRequestWithResponseSource") + fun shouldRetryOnMtbFileHttpRequestError(requestWithResponse: RequestWithResponse) { + val restTemplate = RestTemplate() + val restTargetProperties = RestTargetProperties("http://localhost:9000/api", null, null, false) + val retryTemplate = AppConfiguration().retryTemplate(AppConfigProperties("http://localhost:9000")) + retryTemplate.setBackOffPolicy(NoBackOffPolicy()) + + this.mockRestServiceServer = MockRestServiceServer.createServer(restTemplate) + this.restMtbFileSender = + RestDipMtbFileSender(restTemplate, restTargetProperties, retryTemplate, reportService) + + val expectedCount = when (requestWithResponse.httpStatus) { + // OK - No Retry + HttpStatus.OK, HttpStatus.CREATED, HttpStatus.UNPROCESSABLE_ENTITY, HttpStatus.BAD_REQUEST -> ExpectedCount.max( + 1 + ) + // Request failed - Retry max 3 times + else -> ExpectedCount.max(3) } - val response = restMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) - assertThat(response.status).isEqualTo(requestWithResponse.response.status) - assertThat(response.body).isEqualTo(requestWithResponse.response.body) + this.mockRestServiceServer + .expect(expectedCount, method(HttpMethod.POST)) + .andExpect(requestTo("http://localhost:9000/api/mtb/etl/patient-record")) + .andRespond { + withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) + } + + val response = restMtbFileSender.send(BwhcV1MtbFileRequest(TEST_REQUEST_ID, bwhcV1mtbFile)) + assertThat(response.status).isEqualTo(requestWithResponse.response.status) + assertThat(response.body).isEqualTo(requestWithResponse.response.body) + } + + } + + @Nested + inner class DnpmV2ContentRequest { + + private lateinit var mockRestServiceServer: MockRestServiceServer + + private lateinit var restMtbFileSender: RestMtbFileSender + + private var reportService = ReportService(ObjectMapper().registerModule(KotlinModule.Builder().build())) + + @BeforeEach + fun setup() { + val restTemplate = RestTemplate() + val restTargetProperties = RestTargetProperties("http://localhost:9000/api", null, null, false) + val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(1)).build() + + this.mockRestServiceServer = MockRestServiceServer.createServer(restTemplate) + + this.restMtbFileSender = RestDipMtbFileSender(restTemplate, restTargetProperties, retryTemplate, reportService) + } + + @ParameterizedTest + @MethodSource("dev.dnpm.etl.processor.output.RestDipMtbFileSenderTest#mtbFileRequestWithResponseSource") + fun shouldReturnExpectedResponseForDnpmV2MtbFilePost(requestWithResponse: RequestWithResponse) { + this.mockRestServiceServer + .expect(method(HttpMethod.POST)) + .andExpect(requestTo("http://localhost:9000/api/mtb/etl/patient-record")) + .andExpect(header(HttpHeaders.CONTENT_TYPE, CustomMediaType.APPLICATION_VND_DNPM_V2_MTB_JSON_VALUE)) + .andRespond { + withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) + } + + val response = restMtbFileSender.send(DnpmV2MtbFileRequest(TEST_REQUEST_ID, dnpmV2MtbFile)) + assertThat(response.status).isEqualTo(requestWithResponse.response.status) + assertThat(response.body).isEqualTo(requestWithResponse.response.body) + } + + } + + @Nested + inner class DeleteRequest { + + private lateinit var mockRestServiceServer: MockRestServiceServer + + private lateinit var restMtbFileSender: RestMtbFileSender + + private var reportService = ReportService(ObjectMapper().registerModule(KotlinModule.Builder().build())) + + @BeforeEach + fun setup() { + val restTemplate = RestTemplate() + val restTargetProperties = RestTargetProperties("http://localhost:9000/api", null, null, false) + val retryTemplate = RetryTemplateBuilder().customPolicy(SimpleRetryPolicy(1)).build() + + this.mockRestServiceServer = MockRestServiceServer.createServer(restTemplate) + + this.restMtbFileSender = + RestDipMtbFileSender(restTemplate, restTargetProperties, retryTemplate, reportService) + } + + @ParameterizedTest + @MethodSource("dev.dnpm.etl.processor.output.RestDipMtbFileSenderTest#deleteRequestWithResponseSource") + fun shouldReturnExpectedResponseForDelete(requestWithResponse: RequestWithResponse) { + this.mockRestServiceServer + .expect(method(HttpMethod.DELETE)) + .andExpect(requestTo("http://localhost:9000/api/mtb/etl/patient/${TEST_PATIENT_PSEUDONYM.value}")) + .andRespond { + withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) + } + + val response = restMtbFileSender.send(DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) + assertThat(response.status).isEqualTo(requestWithResponse.response.status) + assertThat(response.body).isEqualTo(requestWithResponse.response.body) + } + + @ParameterizedTest + @MethodSource("dev.dnpm.etl.processor.output.RestDipMtbFileSenderTest#deleteRequestWithResponseSource") + fun shouldRetryOnDeleteHttpRequestError(requestWithResponse: RequestWithResponse) { + val restTemplate = RestTemplate() + val restTargetProperties = RestTargetProperties("http://localhost:9000/api", null, null, false) + val retryTemplate = AppConfiguration().retryTemplate(AppConfigProperties("http://localhost:9000")) + retryTemplate.setBackOffPolicy(NoBackOffPolicy()) + + this.mockRestServiceServer = MockRestServiceServer.createServer(restTemplate) + this.restMtbFileSender = + RestDipMtbFileSender(restTemplate, restTargetProperties, retryTemplate, reportService) + + val expectedCount = when (requestWithResponse.httpStatus) { + // OK - No Retry + HttpStatus.OK, HttpStatus.CREATED, HttpStatus.UNPROCESSABLE_ENTITY, HttpStatus.BAD_REQUEST -> ExpectedCount.max( + 1 + ) + // Request failed - Retry max 3 times + else -> ExpectedCount.max(3) + } + + this.mockRestServiceServer + .expect(expectedCount, method(HttpMethod.DELETE)) + .andExpect(requestTo("http://localhost:9000/api/mtb/etl/patient/${TEST_PATIENT_PSEUDONYM.value}")) + .andRespond { + withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) + } + + val response = restMtbFileSender.send(DeleteRequest(TEST_REQUEST_ID, TEST_PATIENT_PSEUDONYM)) + assertThat(response.status).isEqualTo(requestWithResponse.response.status) + assertThat(response.body).isEqualTo(requestWithResponse.response.body) + } + } companion object { @@ -171,7 +243,7 @@ class RestDipMtbFileSenderTest { val TEST_REQUEST_ID = RequestId("TestId") val TEST_PATIENT_PSEUDONYM = PatientPseudonym("PID") - val mtbFile: MtbFile = MtbFile.builder() + val bwhcV1mtbFile: MtbFile = MtbFile.builder() .withPatient( Patient.builder() .withId("PID") @@ -195,6 +267,25 @@ class RestDipMtbFileSenderTest { ) .build() + val dnpmV2MtbFile: Mtb = Mtb.builder() + .withPatient( + dev.pcvolkmer.mv64e.mtb.Patient.builder() + .withId("PID") + .withBirthDate("2000-08-08") + .withGender(CodingGender.builder().withCode(CodingGender.Code.MALE).build()) + .build() + ) + .withEpisodesOfCare( + listOf( + MTBEpisodeOfCare.builder() + .withId("1") + .withPatient(Reference("PID")) + .withPeriod(PeriodDate.builder().withStart("2023-08-08").build()) + .build() + ) + ) + .build() + private const val ERROR_RESPONSE_BODY = "Sonstiger Fehler bei der Übertragung" /** @@ -311,4 +402,4 @@ class RestDipMtbFileSenderTest { } -} \ No newline at end of file +} diff --git a/src/test/kotlin/dev/dnpm/etl/processor/services/RequestProcessorTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/services/RequestProcessorTest.kt index 5578c7b..fe61852 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/services/RequestProcessorTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/services/RequestProcessorTest.kt @@ -21,14 +21,19 @@ package dev.dnpm.etl.processor.services import com.fasterxml.jackson.databind.ObjectMapper import de.ukw.ccc.bwhc.dto.* -import dev.dnpm.etl.processor.* +import dev.dnpm.etl.processor.Fingerprint +import dev.dnpm.etl.processor.PatientId +import dev.dnpm.etl.processor.PatientPseudonym import dev.dnpm.etl.processor.config.AppConfigProperties import dev.dnpm.etl.processor.monitoring.Request import dev.dnpm.etl.processor.monitoring.RequestStatus import dev.dnpm.etl.processor.monitoring.RequestType +import dev.dnpm.etl.processor.output.BwhcV1MtbFileRequest +import dev.dnpm.etl.processor.output.DeleteRequest import dev.dnpm.etl.processor.output.MtbFileSender import dev.dnpm.etl.processor.output.RestMtbFileSender import dev.dnpm.etl.processor.pseudonym.PseudonymizeService +import dev.dnpm.etl.processor.randomRequestId import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -109,7 +114,7 @@ class RequestProcessorTest { doAnswer { it.arguments[0] - }.whenever(transformationService).transform(any()) + }.whenever(transformationService).transform(any()) val mtbFile = MtbFile.builder() .withPatient( @@ -168,7 +173,7 @@ class RequestProcessorTest { doAnswer { it.arguments[0] - }.whenever(transformationService).transform(any()) + }.whenever(transformationService).transform(any()) val mtbFile = MtbFile.builder() .withPatient( @@ -223,7 +228,7 @@ class RequestProcessorTest { doAnswer { MtbFileSender.Response(status = RequestStatus.SUCCESS) - }.whenever(sender).send(any()) + }.whenever(sender).send(any()) doAnswer { it.arguments[0] as String @@ -231,7 +236,7 @@ class RequestProcessorTest { doAnswer { it.arguments[0] - }.whenever(transformationService).transform(any()) + }.whenever(transformationService).transform(any()) val mtbFile = MtbFile.builder() .withPatient( @@ -286,7 +291,7 @@ class RequestProcessorTest { doAnswer { MtbFileSender.Response(status = RequestStatus.ERROR) - }.whenever(sender).send(any()) + }.whenever(sender).send(any()) doAnswer { it.arguments[0] as String @@ -294,7 +299,7 @@ class RequestProcessorTest { doAnswer { it.arguments[0] - }.whenever(transformationService).transform(any()) + }.whenever(transformationService).transform(any()) val mtbFile = MtbFile.builder() .withPatient( @@ -336,7 +341,7 @@ class RequestProcessorTest { doAnswer { MtbFileSender.Response(status = RequestStatus.UNKNOWN) - }.whenever(sender).send(any()) + }.whenever(sender).send(any()) this.requestProcessor.processDeletion(TEST_PATIENT_ID) @@ -354,7 +359,7 @@ class RequestProcessorTest { doAnswer { MtbFileSender.Response(status = RequestStatus.SUCCESS) - }.whenever(sender).send(any()) + }.whenever(sender).send(any()) this.requestProcessor.processDeletion(TEST_PATIENT_ID) @@ -372,7 +377,7 @@ class RequestProcessorTest { doAnswer { MtbFileSender.Response(status = RequestStatus.ERROR) - }.whenever(sender).send(any()) + }.whenever(sender).send(any()) this.requestProcessor.processDeletion(TEST_PATIENT_ID) @@ -404,11 +409,11 @@ class RequestProcessorTest { doAnswer { it.arguments[0] - }.whenever(transformationService).transform(any()) + }.whenever(transformationService).transform(any()) doAnswer { MtbFileSender.Response(status = RequestStatus.SUCCESS) - }.whenever(sender).send(any()) + }.whenever(sender).send(any()) val mtbFile = MtbFile.builder() .withPatient( @@ -446,4 +451,4 @@ class RequestProcessorTest { val TEST_PATIENT_ID = PatientId("TEST_12345678901") } -} \ No newline at end of file +}