diff options
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/kotlin/dev/dnpm/etl/processor/helpers.kt | 29 | ||||
| -rw-r--r-- | src/test/kotlin/dev/dnpm/etl/processor/input/KafkaInputListenerTest.kt | 5 | ||||
| -rw-r--r-- | src/test/kotlin/dev/dnpm/etl/processor/output/KafkaMtbFileSenderTest.kt | 21 | ||||
| -rw-r--r-- | src/test/kotlin/dev/dnpm/etl/processor/output/RestMtbFileSenderTest.kt | 11 | ||||
| -rw-r--r-- | src/test/kotlin/dev/dnpm/etl/processor/security/TokenServiceTest.kt (renamed from src/test/kotlin/dev/dnpm/etl/processor/services/TokenServiceTest.kt) | 15 | ||||
| -rw-r--r-- | src/test/kotlin/dev/dnpm/etl/processor/security/UserRoleServiceTest.kt | 202 | ||||
| -rw-r--r-- | src/test/kotlin/dev/dnpm/etl/processor/services/RequestProcessorTest.kt | 113 | ||||
| -rw-r--r-- | src/test/kotlin/dev/dnpm/etl/processor/services/RequestServiceTest.kt | 166 | ||||
| -rw-r--r-- | src/test/kotlin/dev/dnpm/etl/processor/services/ResponseProcessorTest.kt | 35 |
9 files changed, 419 insertions, 178 deletions
diff --git a/src/test/kotlin/dev/dnpm/etl/processor/helpers.kt b/src/test/kotlin/dev/dnpm/etl/processor/helpers.kt new file mode 100644 index 0000000..55d6327 --- /dev/null +++ b/src/test/kotlin/dev/dnpm/etl/processor/helpers.kt @@ -0,0 +1,29 @@ +/* + * This file is part of ETL-Processor + * + * Copyright (c) 2024 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 <https://www.gnu.org/licenses/>. + */ + +package dev.dnpm.etl.processor + +import org.mockito.ArgumentMatchers + +inline fun <reified T> anyValueClass(): T { + val unboxedClass = T::class.java.declaredFields.first().type + return ArgumentMatchers.any(unboxedClass as Class<T>) + ?: T::class.java.getDeclaredMethod("box-impl", unboxedClass) + .invoke(null, null) as T +}
\ No newline at end of file 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 1157644..58c78e4 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/input/KafkaInputListenerTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/input/KafkaInputListenerTest.kt @@ -23,6 +23,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import de.ukw.ccc.bwhc.dto.Consent import de.ukw.ccc.bwhc.dto.MtbFile import de.ukw.ccc.bwhc.dto.Patient +import dev.dnpm.etl.processor.anyValueClass import dev.dnpm.etl.processor.services.RequestProcessor import org.apache.kafka.clients.consumer.ConsumerRecord import org.apache.kafka.common.header.internals.RecordHeader @@ -92,7 +93,7 @@ class KafkaInputListenerTest { ConsumerRecord("testtopic", 0, 0, -1L, TimestampType.NO_TIMESTAMP_TYPE, -1, -1, "", this.objectMapper.writeValueAsString(mtbFile), headers, Optional.empty()) ) - verify(requestProcessor, times(1)).processMtbFile(any(), anyString()) + verify(requestProcessor, times(1)).processMtbFile(any(), anyValueClass()) } @Test @@ -106,7 +107,7 @@ class KafkaInputListenerTest { kafkaInputListener.onMessage( ConsumerRecord("testtopic", 0, 0, -1L, TimestampType.NO_TIMESTAMP_TYPE, -1, -1, "", this.objectMapper.writeValueAsString(mtbFile), headers, Optional.empty()) ) - verify(requestProcessor, times(1)).processDeletion(anyString(), anyString()) + verify(requestProcessor, times(1)).processDeletion(anyString(), anyValueClass()) } }
\ No newline at end of file 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 411c51e..b943f5f 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/output/KafkaMtbFileSenderTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/output/KafkaMtbFileSenderTest.kt @@ -21,6 +21,7 @@ package dev.dnpm.etl.processor.output import com.fasterxml.jackson.databind.ObjectMapper import de.ukw.ccc.bwhc.dto.* +import dev.dnpm.etl.processor.RequestId import dev.dnpm.etl.processor.config.KafkaProperties import dev.dnpm.etl.processor.monitoring.RequestStatus import org.assertj.core.api.Assertions.assertThat @@ -72,7 +73,7 @@ class KafkaMtbFileSenderTest { completedFuture(SendResult<String, String>(null, null)) }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - val response = kafkaMtbFileSender.send(MtbFileSender.MtbFileRequest("TestID", mtbFile(Consent.Status.ACTIVE))) + val response = kafkaMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile(Consent.Status.ACTIVE))) assertThat(response.status).isEqualTo(testData.requestStatus) } @@ -86,7 +87,7 @@ class KafkaMtbFileSenderTest { completedFuture(SendResult<String, String>(null, null)) }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - val response = kafkaMtbFileSender.send(MtbFileSender.DeleteRequest("TestID", "PID")) + val response = kafkaMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, "PID")) assertThat(response.status).isEqualTo(testData.requestStatus) } @@ -96,14 +97,14 @@ class KafkaMtbFileSenderTest { completedFuture(SendResult<String, String>(null, null)) }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - kafkaMtbFileSender.send(MtbFileSender.MtbFileRequest("TestID", mtbFile(Consent.Status.ACTIVE))) + kafkaMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile(Consent.Status.ACTIVE))) val captor = argumentCaptor<String>() 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("TestID", Consent.Status.ACTIVE))) + assertThat(captor.secondValue).isEqualTo(objectMapper.writeValueAsString(kafkaRecordData(TEST_REQUEST_ID, Consent.Status.ACTIVE))) } @Test @@ -112,14 +113,14 @@ class KafkaMtbFileSenderTest { completedFuture(SendResult<String, String>(null, null)) }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - kafkaMtbFileSender.send(MtbFileSender.DeleteRequest("TestID", "PID")) + kafkaMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, "PID")) val captor = argumentCaptor<String>() 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("TestID", Consent.Status.REJECTED))) + assertThat(captor.secondValue).isEqualTo(objectMapper.writeValueAsString(kafkaRecordData(TEST_REQUEST_ID, Consent.Status.REJECTED))) } @ParameterizedTest @@ -136,7 +137,7 @@ class KafkaMtbFileSenderTest { completedFuture(SendResult<String, String>(null, null)) }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - kafkaMtbFileSender.send(MtbFileSender.MtbFileRequest("TestID", mtbFile(Consent.Status.ACTIVE))) + kafkaMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile(Consent.Status.ACTIVE))) val expectedCount = when (testData.exception) { // OK - No Retry @@ -162,7 +163,7 @@ class KafkaMtbFileSenderTest { completedFuture(SendResult<String, String>(null, null)) }.whenever(kafkaTemplate).send(anyString(), anyString(), anyString()) - kafkaMtbFileSender.send(MtbFileSender.DeleteRequest("TestID", "PID")) + kafkaMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, "PID")) val expectedCount = when (testData.exception) { // OK - No Retry @@ -175,6 +176,8 @@ class KafkaMtbFileSenderTest { } companion object { + val TEST_REQUEST_ID = RequestId("TestId") + fun mtbFile(consentStatus: Consent.Status): MtbFile { return if (consentStatus == Consent.Status.ACTIVE) { MtbFile.builder() @@ -210,7 +213,7 @@ class KafkaMtbFileSenderTest { }.build() } - fun kafkaRecordData(requestId: String, consentStatus: Consent.Status): KafkaMtbFileSender.Data { + fun kafkaRecordData(requestId: RequestId, consentStatus: Consent.Status): KafkaMtbFileSender.Data { return KafkaMtbFileSender.Data(requestId, mtbFile(consentStatus)) } diff --git a/src/test/kotlin/dev/dnpm/etl/processor/output/RestMtbFileSenderTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/output/RestMtbFileSenderTest.kt index df19ddb..4f0eca8 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/output/RestMtbFileSenderTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/output/RestMtbFileSenderTest.kt @@ -20,6 +20,7 @@ package dev.dnpm.etl.processor.output import de.ukw.ccc.bwhc.dto.* +import dev.dnpm.etl.processor.RequestId import dev.dnpm.etl.processor.config.RestTargetProperties import dev.dnpm.etl.processor.monitoring.RequestStatus import org.assertj.core.api.Assertions.assertThat @@ -64,7 +65,7 @@ class RestMtbFileSenderTest { withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) } - val response = restMtbFileSender.send(MtbFileSender.DeleteRequest("TestID", "PID")) + val response = restMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, "PID")) assertThat(response.status).isEqualTo(requestWithResponse.response.status) assertThat(response.body).isEqualTo(requestWithResponse.response.body) } @@ -79,7 +80,7 @@ class RestMtbFileSenderTest { withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) } - val response = restMtbFileSender.send(MtbFileSender.MtbFileRequest("TestID", mtbFile)) + val response = restMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile)) assertThat(response.status).isEqualTo(requestWithResponse.response.status) assertThat(response.body).isEqualTo(requestWithResponse.response.body) } @@ -108,7 +109,7 @@ class RestMtbFileSenderTest { withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) } - val response = restMtbFileSender.send(MtbFileSender.MtbFileRequest("TestID", mtbFile)) + val response = restMtbFileSender.send(MtbFileSender.MtbFileRequest(TEST_REQUEST_ID, mtbFile)) assertThat(response.status).isEqualTo(requestWithResponse.response.status) assertThat(response.body).isEqualTo(requestWithResponse.response.body) } @@ -137,7 +138,7 @@ class RestMtbFileSenderTest { withStatus(requestWithResponse.httpStatus).body(requestWithResponse.body).createResponse(it) } - val response = restMtbFileSender.send(MtbFileSender.DeleteRequest("TestID", "PID")) + val response = restMtbFileSender.send(MtbFileSender.DeleteRequest(TEST_REQUEST_ID, "PID")) assertThat(response.status).isEqualTo(requestWithResponse.response.status) assertThat(response.body).isEqualTo(requestWithResponse.response.body) } @@ -149,6 +150,8 @@ class RestMtbFileSenderTest { val response: MtbFileSender.Response ) + val TEST_REQUEST_ID = RequestId("TestId") + private val warningBody = """ { "patient_id": "PID", diff --git a/src/test/kotlin/dev/dnpm/etl/processor/services/TokenServiceTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/security/TokenServiceTest.kt index 1fdc3d9..b93e9f5 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/services/TokenServiceTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/security/TokenServiceTest.kt @@ -17,13 +17,12 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -package dev.dnpm.etl.processor.services +package dev.dnpm.etl.processor.security import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith -import org.mockito.ArgumentCaptor import org.mockito.ArgumentMatchers.anyLong import org.mockito.ArgumentMatchers.anyString import org.mockito.Mock @@ -96,11 +95,11 @@ class TokenServiceTest { val actual = this.tokenService.addToken("Test Token") - val captor = ArgumentCaptor.forClass(Token::class.java) + val captor = argumentCaptor<Token>() verify(tokenRepository, times(1)).save(captor.capture()) assertThat(actual).satisfies(Consumer { assertThat(it.isSuccess).isTrue() }) - assertThat(captor.value).satisfies( + assertThat(captor.firstValue).satisfies( Consumer { assertThat(it.name).isEqualTo("Test Token") }, Consumer { assertThat(it.username).isEqualTo("testtoken") }, Consumer { assertThat(it.password).isEqualTo("{test}verysecret") } @@ -116,13 +115,13 @@ class TokenServiceTest { this.tokenService.deleteToken(42) - val stringCaptor = ArgumentCaptor.forClass(String::class.java) + val stringCaptor = argumentCaptor<String>() verify(userDetailsManager, times(1)).deleteUser(stringCaptor.capture()) - assertThat(stringCaptor.value).isEqualTo("testtoken") + assertThat(stringCaptor.firstValue).isEqualTo("testtoken") - val tokenCaptor = ArgumentCaptor.forClass(Token::class.java) + val tokenCaptor = argumentCaptor<Token>() verify(tokenRepository, times(1)).delete(tokenCaptor.capture()) - assertThat(tokenCaptor.value.id).isEqualTo(42) + assertThat(tokenCaptor.firstValue.id).isEqualTo(42) } @Test diff --git a/src/test/kotlin/dev/dnpm/etl/processor/security/UserRoleServiceTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/security/UserRoleServiceTest.kt new file mode 100644 index 0000000..39ba7c0 --- /dev/null +++ b/src/test/kotlin/dev/dnpm/etl/processor/security/UserRoleServiceTest.kt @@ -0,0 +1,202 @@ +/* + * This file is part of ETL-Processor + * + * Copyright (c) 2024 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 <https://www.gnu.org/licenses/>. + */ + +package dev.dnpm.etl.processor.security + +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.mockito.Mock +import org.mockito.junit.jupiter.MockitoExtension +import org.mockito.kotlin.* +import org.springframework.security.core.session.SessionInformation +import org.springframework.security.core.session.SessionRegistry +import org.springframework.security.oauth2.core.oidc.OidcIdToken +import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser +import org.springframework.security.oauth2.core.oidc.user.OidcUser +import java.time.Instant +import java.util.* + +@ExtendWith(MockitoExtension::class) +class UserRoleServiceTest { + + private lateinit var userRoleRepository: UserRoleRepository + private lateinit var sessionRegistry: SessionRegistry + + private lateinit var userRoleService: UserRoleService + + @BeforeEach + fun setup( + @Mock userRoleRepository: UserRoleRepository, + @Mock sessionRegistry: SessionRegistry + ) { + this.userRoleRepository = userRoleRepository + this.sessionRegistry = sessionRegistry + + this.userRoleService = UserRoleService(userRoleRepository, sessionRegistry) + } + + @Test + fun shouldDelegateFindAllToRepository() { + userRoleService.findAll() + + verify(userRoleRepository, times(1)).findAll() + } + + @Nested + inner class WithExistingUserRole { + + @BeforeEach + fun setup() { + doAnswer { invocation -> + Optional.of( + UserRole(invocation.getArgument(0), "patrick.tester", Role.USER) + ) + }.whenever(userRoleRepository).findById(any<Long>()) + + doAnswer { _ -> + listOf( + dummyPrincipal() + ) + }.whenever(sessionRegistry).allPrincipals + } + + @Test + fun shouldUpdateUserRole() { + userRoleService.updateUserRole(1, Role.ADMIN) + + val userRoleCaptor = argumentCaptor<UserRole>() + verify(userRoleRepository, times(1)).save(userRoleCaptor.capture()) + + assertThat(userRoleCaptor.firstValue.id).isEqualTo(1L) + assertThat(userRoleCaptor.firstValue.role).isEqualTo(Role.ADMIN) + } + + @Test + fun shouldExpireSessionOnUpdate() { + val dummySessions = dummySessions() + whenever(sessionRegistry.getAllSessions(any(), any<Boolean>())).thenReturn( + dummySessions + ) + + assertThat(dummySessions.filter { it.isExpired }).hasSize(0) + + userRoleService.updateUserRole(1, Role.ADMIN) + + verify(sessionRegistry, times(1)).getAllSessions(any<OidcUser>(), any<Boolean>()) + + assertThat(dummySessions.filter { it.isExpired }).hasSize(2) + } + + @Test + fun shouldDeleteUserRole() { + userRoleService.deleteUserRole(1) + + val userRoleCaptor = argumentCaptor<UserRole>() + verify(userRoleRepository, times(1)).delete(userRoleCaptor.capture()) + + assertThat(userRoleCaptor.firstValue.id).isEqualTo(1L) + assertThat(userRoleCaptor.firstValue.role).isEqualTo(Role.USER) + } + + @Test + fun shouldExpireSessionOnDelete() { + val dummySessions = dummySessions() + whenever(sessionRegistry.getAllSessions(any(), any<Boolean>())).thenReturn( + dummySessions + ) + + assertThat(dummySessions.filter { it.isExpired }).hasSize(0) + + userRoleService.deleteUserRole(1) + + verify(sessionRegistry, times(1)).getAllSessions(any<OidcUser>(), any<Boolean>()) + + assertThat(dummySessions.filter { it.isExpired }).hasSize(2) + } + } + + @Nested + inner class WithoutExistingUserRole { + + @BeforeEach + fun setup() { + doAnswer { _ -> + Optional.empty<UserRole>() + }.whenever(userRoleRepository).findById(any<Long>()) + } + + @Test + fun shouldNotUpdateUserRole() { + userRoleService.updateUserRole(1, Role.ADMIN) + + verify(userRoleRepository, never()).save(any<UserRole>()) + } + + @Test + fun shouldNotExpireSessionOnUpdate() { + userRoleService.updateUserRole(1, Role.ADMIN) + + verify(sessionRegistry, never()).getAllSessions(any<OidcUser>(), any<Boolean>()) + } + + @Test + fun shouldNotDeleteUserRole() { + userRoleService.deleteUserRole(1) + + verify(userRoleRepository, never()).delete(any<UserRole>()) + } + + @Test + fun shouldNotExpireSessionOnDelete() { + userRoleService.deleteUserRole(1) + + verify(sessionRegistry, never()).getAllSessions(any<OidcUser>(), any<Boolean>()) + } + + } + + + companion object { + private fun dummyPrincipal() = DefaultOidcUser( + listOf(), + OidcIdToken( + "anytokenvalue", + Instant.now(), + Instant.now().plusSeconds(10), + mapOf("sub" to "testsub", "preferred_username" to "patrick.tester") + ) + ) + + private fun dummySessions() = listOf( + SessionInformation( + dummyPrincipal(), + "SESSIONID1", + Date.from(Instant.now()), + ), + SessionInformation( + dummyPrincipal(), + "SESSIONID2", + Date.from(Instant.now()), + ) + ) + } +}
\ 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 611c0ff..d7354be 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/services/RequestProcessorTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/services/RequestProcessorTest.kt @@ -21,6 +21,8 @@ package dev.dnpm.etl.processor.services import com.fasterxml.jackson.databind.ObjectMapper import de.ukw.ccc.bwhc.dto.* +import dev.dnpm.etl.processor.Fingerprint +import dev.dnpm.etl.processor.randomRequestId import dev.dnpm.etl.processor.config.AppConfigProperties import dev.dnpm.etl.processor.monitoring.Request import dev.dnpm.etl.processor.monitoring.RequestStatus @@ -41,7 +43,6 @@ import org.mockito.kotlin.argumentCaptor import org.mockito.kotlin.whenever import org.springframework.context.ApplicationEventPublisher import java.time.Instant -import java.util.* @ExtendWith(MockitoExtension::class) @@ -88,24 +89,24 @@ class RequestProcessorTest { fun testShouldSendMtbFileDuplicationAndSaveUnknownRequestStatusAtFirst() { doAnswer { Request( - id = 1L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "zdlzv5s5ydmd4ktw2v5piohegc4jcyrm6j66bq6tv2uxuerndmga", - type = RequestType.MTB_FILE, - status = RequestStatus.SUCCESS, - processedAt = Instant.parse("2023-08-08T02:00:00Z") + 1L, + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("zdlzv5s5ydmd4ktw2v5piohegc4jcyrm6j66bq6tv2uxuerndmga"), + RequestType.MTB_FILE, + RequestStatus.SUCCESS, + Instant.parse("2023-08-08T02:00:00Z") ) - }.`when`(requestService).lastMtbFileRequestForPatientPseudonym(anyString()) + }.whenever(requestService).lastMtbFileRequestForPatientPseudonym(anyString()) doAnswer { false - }.`when`(requestService).isLastRequestWithKnownStatusDeletion(anyString()) + }.whenever(requestService).isLastRequestWithKnownStatusDeletion(anyString()) doAnswer { it.arguments[0] as String - }.`when`(pseudonymizeService).patientPseudonym(any()) + }.whenever(pseudonymizeService).patientPseudonym(any()) doAnswer { it.arguments[0] @@ -147,24 +148,24 @@ class RequestProcessorTest { fun testShouldDetectMtbFileDuplicationAndSendDuplicationEvent() { doAnswer { Request( - id = 1L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "zdlzv5s5ydmd4ktw2v5piohegc4jcyrm6j66bq6tv2uxuerndmga", - type = RequestType.MTB_FILE, - status = RequestStatus.SUCCESS, - processedAt = Instant.parse("2023-08-08T02:00:00Z") + 1L, + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("zdlzv5s5ydmd4ktw2v5piohegc4jcyrm6j66bq6tv2uxuerndmga"), + RequestType.MTB_FILE, + RequestStatus.SUCCESS, + Instant.parse("2023-08-08T02:00:00Z") ) - }.`when`(requestService).lastMtbFileRequestForPatientPseudonym(anyString()) + }.whenever(requestService).lastMtbFileRequestForPatientPseudonym(anyString()) doAnswer { false - }.`when`(requestService).isLastRequestWithKnownStatusDeletion(anyString()) + }.whenever(requestService).isLastRequestWithKnownStatusDeletion(anyString()) doAnswer { it.arguments[0] as String - }.`when`(pseudonymizeService).patientPseudonym(any()) + }.whenever(pseudonymizeService).patientPseudonym(any()) doAnswer { it.arguments[0] @@ -206,28 +207,28 @@ class RequestProcessorTest { fun testShouldSendMtbFileAndSendSuccessEvent() { doAnswer { Request( - id = 1L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "different", - type = RequestType.MTB_FILE, - status = RequestStatus.SUCCESS, - processedAt = Instant.parse("2023-08-08T02:00:00Z") + 1L, + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("different"), + RequestType.MTB_FILE, + RequestStatus.SUCCESS, + Instant.parse("2023-08-08T02:00:00Z") ) - }.`when`(requestService).lastMtbFileRequestForPatientPseudonym(anyString()) + }.whenever(requestService).lastMtbFileRequestForPatientPseudonym(anyString()) doAnswer { false - }.`when`(requestService).isLastRequestWithKnownStatusDeletion(anyString()) + }.whenever(requestService).isLastRequestWithKnownStatusDeletion(anyString()) doAnswer { MtbFileSender.Response(status = RequestStatus.SUCCESS) - }.`when`(sender).send(any<MtbFileSender.MtbFileRequest>()) + }.whenever(sender).send(any<MtbFileSender.MtbFileRequest>()) doAnswer { it.arguments[0] as String - }.`when`(pseudonymizeService).patientPseudonym(any()) + }.whenever(pseudonymizeService).patientPseudonym(any()) doAnswer { it.arguments[0] @@ -269,28 +270,28 @@ class RequestProcessorTest { fun testShouldSendMtbFileAndSendErrorEvent() { doAnswer { Request( - id = 1L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "different", - type = RequestType.MTB_FILE, - status = RequestStatus.SUCCESS, - processedAt = Instant.parse("2023-08-08T02:00:00Z") + 1L, + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("different"), + RequestType.MTB_FILE, + RequestStatus.SUCCESS, + Instant.parse("2023-08-08T02:00:00Z") ) - }.`when`(requestService).lastMtbFileRequestForPatientPseudonym(anyString()) + }.whenever(requestService).lastMtbFileRequestForPatientPseudonym(anyString()) doAnswer { false - }.`when`(requestService).isLastRequestWithKnownStatusDeletion(anyString()) + }.whenever(requestService).isLastRequestWithKnownStatusDeletion(anyString()) doAnswer { MtbFileSender.Response(status = RequestStatus.ERROR) - }.`when`(sender).send(any<MtbFileSender.MtbFileRequest>()) + }.whenever(sender).send(any<MtbFileSender.MtbFileRequest>()) doAnswer { it.arguments[0] as String - }.`when`(pseudonymizeService).patientPseudonym(any()) + }.whenever(pseudonymizeService).patientPseudonym(any()) doAnswer { it.arguments[0] @@ -332,11 +333,11 @@ class RequestProcessorTest { fun testShouldSendDeleteRequestAndSaveUnknownRequestStatusAtFirst() { doAnswer { "PSEUDONYM" - }.`when`(pseudonymizeService).patientPseudonym(anyString()) + }.whenever(pseudonymizeService).patientPseudonym(anyString()) doAnswer { MtbFileSender.Response(status = RequestStatus.UNKNOWN) - }.`when`(sender).send(any<MtbFileSender.DeleteRequest>()) + }.whenever(sender).send(any<MtbFileSender.DeleteRequest>()) this.requestProcessor.processDeletion("TEST_12345678901") @@ -350,11 +351,11 @@ class RequestProcessorTest { fun testShouldSendDeleteRequestAndSendSuccessEvent() { doAnswer { "PSEUDONYM" - }.`when`(pseudonymizeService).patientPseudonym(anyString()) + }.whenever(pseudonymizeService).patientPseudonym(anyString()) doAnswer { MtbFileSender.Response(status = RequestStatus.SUCCESS) - }.`when`(sender).send(any<MtbFileSender.DeleteRequest>()) + }.whenever(sender).send(any<MtbFileSender.DeleteRequest>()) this.requestProcessor.processDeletion("TEST_12345678901") @@ -368,11 +369,11 @@ class RequestProcessorTest { fun testShouldSendDeleteRequestAndSendErrorEvent() { doAnswer { "PSEUDONYM" - }.`when`(pseudonymizeService).patientPseudonym(anyString()) + }.whenever(pseudonymizeService).patientPseudonym(anyString()) doAnswer { MtbFileSender.Response(status = RequestStatus.ERROR) - }.`when`(sender).send(any<MtbFileSender.DeleteRequest>()) + }.whenever(sender).send(any<MtbFileSender.DeleteRequest>()) this.requestProcessor.processDeletion("TEST_12345678901") @@ -384,7 +385,7 @@ class RequestProcessorTest { @Test fun testShouldSendDeleteRequestWithPseudonymErrorAndSaveErrorRequestStatus() { - doThrow(RuntimeException()).`when`(pseudonymizeService).patientPseudonym(anyString()) + doThrow(RuntimeException()).whenever(pseudonymizeService).patientPseudonym(anyString()) this.requestProcessor.processDeletion("TEST_12345678901") @@ -400,7 +401,7 @@ class RequestProcessorTest { doAnswer { it.arguments[0] as String - }.`when`(pseudonymizeService).patientPseudonym(any()) + }.whenever(pseudonymizeService).patientPseudonym(any()) doAnswer { it.arguments[0] @@ -408,7 +409,7 @@ class RequestProcessorTest { doAnswer { MtbFileSender.Response(status = RequestStatus.SUCCESS) - }.`when`(sender).send(any<MtbFileSender.MtbFileRequest>()) + }.whenever(sender).send(any<MtbFileSender.MtbFileRequest>()) val mtbFile = MtbFile.builder() .withPatient( diff --git a/src/test/kotlin/dev/dnpm/etl/processor/services/RequestServiceTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/services/RequestServiceTest.kt index 3cf8804..93090ed 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/services/RequestServiceTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/services/RequestServiceTest.kt @@ -19,6 +19,8 @@ package dev.dnpm.etl.processor.services +import dev.dnpm.etl.processor.Fingerprint +import dev.dnpm.etl.processor.randomRequestId import dev.dnpm.etl.processor.monitoring.Request import dev.dnpm.etl.processor.monitoring.RequestRepository import dev.dnpm.etl.processor.monitoring.RequestStatus @@ -30,8 +32,8 @@ import org.junit.jupiter.api.extension.ExtendWith import org.mockito.Mock import org.mockito.Mockito.* import org.mockito.junit.jupiter.MockitoExtension +import org.mockito.kotlin.whenever import java.time.Instant -import java.util.* @ExtendWith(MockitoExtension::class) class RequestServiceTest { @@ -41,14 +43,14 @@ class RequestServiceTest { private lateinit var requestService: RequestService private fun anyRequest() = any(Request::class.java) ?: Request( - id = 0L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_dummy", - pid = "PX", - fingerprint = "dummy", - type = RequestType.MTB_FILE, - status = RequestStatus.SUCCESS, - processedAt = Instant.parse("2023-08-08T02:00:00Z") + 0L, + randomRequestId(), + "TEST_dummy", + "PX", + Fingerprint("dummy"), + RequestType.MTB_FILE, + RequestStatus.SUCCESS, + Instant.parse("2023-08-08T02:00:00Z") ) @BeforeEach @@ -63,34 +65,34 @@ class RequestServiceTest { fun shouldIndicateLastRequestIsDeleteRequest() { val requests = listOf( Request( - id = 1L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "0123456789abcdef1", - type = RequestType.MTB_FILE, - status = RequestStatus.WARNING, - processedAt = Instant.parse("2023-07-07T00:00:00Z") + 1L, + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("0123456789abcdef1"), + RequestType.MTB_FILE, + RequestStatus.WARNING, + Instant.parse("2023-07-07T00:00:00Z") ), Request( - id = 2L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "0123456789abcdefd", - type = RequestType.DELETE, - status = RequestStatus.WARNING, - processedAt = Instant.parse("2023-07-07T02:00:00Z") + 2L, + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("0123456789abcdefd"), + RequestType.DELETE, + RequestStatus.WARNING, + Instant.parse("2023-07-07T02:00:00Z") ), Request( - id = 3L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "0123456789abcdef1", - type = RequestType.MTB_FILE, - status = RequestStatus.UNKNOWN, - processedAt = Instant.parse("2023-08-11T00:00:00Z") + 3L, + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("0123456789abcdef1"), + RequestType.MTB_FILE, + RequestStatus.UNKNOWN, + Instant.parse("2023-08-11T00:00:00Z") ) ) @@ -103,34 +105,34 @@ class RequestServiceTest { fun shouldIndicateLastRequestIsNotDeleteRequest() { val requests = listOf( Request( - id = 1L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "0123456789abcdef1", - type = RequestType.MTB_FILE, - status = RequestStatus.WARNING, - processedAt = Instant.parse("2023-07-07T00:00:00Z") + 1L, + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("0123456789abcdef1"), + RequestType.MTB_FILE, + RequestStatus.WARNING, + Instant.parse("2023-07-07T00:00:00Z") ), Request( - id = 2L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "0123456789abcdef1", - type = RequestType.MTB_FILE, - status = RequestStatus.WARNING, - processedAt = Instant.parse("2023-07-07T02:00:00Z") + 2L, + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("0123456789abcdef1"), + RequestType.MTB_FILE, + RequestStatus.WARNING, + Instant.parse("2023-07-07T02:00:00Z") ), Request( - id = 3L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "0123456789abcdef1", - type = RequestType.MTB_FILE, - status = RequestStatus.UNKNOWN, - processedAt = Instant.parse("2023-08-11T00:00:00Z") + 3L, + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("0123456789abcdef1"), + RequestType.MTB_FILE, + RequestStatus.UNKNOWN, + Instant.parse("2023-08-11T00:00:00Z") ) ) @@ -143,31 +145,31 @@ class RequestServiceTest { fun shouldReturnPatientsLastRequest() { val requests = listOf( Request( - id = 1L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "0123456789abcdef1", - type = RequestType.DELETE, - status = RequestStatus.SUCCESS, - processedAt = Instant.parse("2023-07-07T02:00:00Z") + 1L, + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("0123456789abcdef1"), + RequestType.DELETE, + RequestStatus.SUCCESS, + Instant.parse("2023-07-07T02:00:00Z") ), Request( - id = 1L, - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678902", - pid = "P2", - fingerprint = "0123456789abcdef2", - type = RequestType.MTB_FILE, - status = RequestStatus.WARNING, - processedAt = Instant.parse("2023-08-08T00:00:00Z") + 1L, + randomRequestId(), + "TEST_12345678902", + "P2", + Fingerprint("0123456789abcdef2"), + RequestType.MTB_FILE, + RequestStatus.WARNING, + Instant.parse("2023-08-08T00:00:00Z") ) ) val actual = RequestService.lastMtbFileRequestForPatientPseudonym(requests) assertThat(actual).isInstanceOf(Request::class.java) - assertThat(actual?.fingerprint).isEqualTo("0123456789abcdef2") + assertThat(actual?.fingerprint).isEqualTo(Fingerprint("0123456789abcdef2")) } @Test @@ -184,16 +186,16 @@ class RequestServiceTest { doAnswer { val obj = it.arguments[0] as Request obj.copy(id = 1L) - }.`when`(requestRepository).save(anyRequest()) + }.whenever(requestRepository).save(anyRequest()) val request = Request( - uuid = UUID.randomUUID().toString(), - patientId = "TEST_12345678901", - pid = "P1", - fingerprint = "0123456789abcdef1", - type = RequestType.DELETE, - status = RequestStatus.SUCCESS, - processedAt = Instant.parse("2023-07-07T02:00:00Z") + randomRequestId(), + "TEST_12345678901", + "P1", + Fingerprint("0123456789abcdef1"), + RequestType.DELETE, + RequestStatus.SUCCESS, + Instant.parse("2023-07-07T02:00:00Z") ) requestService.save(request) diff --git a/src/test/kotlin/dev/dnpm/etl/processor/services/ResponseProcessorTest.kt b/src/test/kotlin/dev/dnpm/etl/processor/services/ResponseProcessorTest.kt index b9e4b7f..7204d7c 100644 --- a/src/test/kotlin/dev/dnpm/etl/processor/services/ResponseProcessorTest.kt +++ b/src/test/kotlin/dev/dnpm/etl/processor/services/ResponseProcessorTest.kt @@ -19,8 +19,10 @@ package dev.dnpm.etl.processor.services +import dev.dnpm.etl.processor.Fingerprint +import dev.dnpm.etl.processor.RequestId +import dev.dnpm.etl.processor.anyValueClass import dev.dnpm.etl.processor.monitoring.Request -import dev.dnpm.etl.processor.monitoring.RequestRepository import dev.dnpm.etl.processor.monitoring.RequestStatus import dev.dnpm.etl.processor.monitoring.RequestType import org.assertj.core.api.Assertions.assertThat @@ -29,7 +31,6 @@ 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.* @@ -40,64 +41,64 @@ import java.util.* @ExtendWith(MockitoExtension::class) class ResponseProcessorTest { - private lateinit var requestRepository: RequestRepository + private lateinit var requestService: RequestService private lateinit var statisticsUpdateProducer: Sinks.Many<Any> private lateinit var responseProcessor: ResponseProcessor private val testRequest = Request( 1L, - "TestID1234", + RequestId("TestID1234"), "PSEUDONYM-A", "1", - "dummyfingerprint", + Fingerprint("dummyfingerprint"), RequestType.MTB_FILE, RequestStatus.UNKNOWN ) @BeforeEach fun setup( - @Mock requestRepository: RequestRepository, + @Mock requestService: RequestService, @Mock statisticsUpdateProducer: Sinks.Many<Any> ) { - this.requestRepository = requestRepository + this.requestService = requestService this.statisticsUpdateProducer = statisticsUpdateProducer - this.responseProcessor = ResponseProcessor(requestRepository, statisticsUpdateProducer) + this.responseProcessor = ResponseProcessor(requestService, statisticsUpdateProducer) } @Test fun shouldNotSaveStatusForUnknownRequest() { doAnswer { Optional.empty<Request>() - }.whenever(requestRepository).findByUuidEquals(anyString()) + }.whenever(requestService).findByUuid(anyValueClass()) val event = ResponseEvent( - "TestID1234", + RequestId("TestID1234"), Instant.parse("2023-09-09T00:00:00Z"), RequestStatus.SUCCESS ) this.responseProcessor.handleResponseEvent(event) - verify(requestRepository, never()).save(any()) + verify(requestService, never()).save(any()) } @Test fun shouldNotSaveStatusWithUnknownState() { doAnswer { Optional.of(testRequest) - }.whenever(requestRepository).findByUuidEquals(anyString()) + }.whenever(requestService).findByUuid(anyValueClass()) val event = ResponseEvent( - "TestID1234", + RequestId("TestID1234"), Instant.parse("2023-09-09T00:00:00Z"), RequestStatus.UNKNOWN ) this.responseProcessor.handleResponseEvent(event) - verify(requestRepository, never()).save(any()) + verify(requestService, never()).save(any<Request>()) } @ParameterizedTest @@ -105,10 +106,10 @@ class ResponseProcessorTest { fun shouldSaveStatusForKnownRequest(requestStatus: RequestStatus) { doAnswer { Optional.of(testRequest) - }.whenever(requestRepository).findByUuidEquals(anyString()) + }.whenever(requestService).findByUuid(anyValueClass()) val event = ResponseEvent( - "TestID1234", + RequestId("TestID1234"), Instant.parse("2023-09-09T00:00:00Z"), requestStatus ) @@ -116,7 +117,7 @@ class ResponseProcessorTest { this.responseProcessor.handleResponseEvent(event) val captor = argumentCaptor<Request>() - verify(requestRepository, times(1)).save(captor.capture()) + verify(requestService, times(1)).save(captor.capture()) assertThat(captor.firstValue).isNotNull assertThat(captor.firstValue.status).isEqualTo(requestStatus) } |
