diff --git a/src/test/kotlin/no/nav/syfo/brev/narmesteleder/NarmesteLederAccessServiceSpek.kt b/src/test/kotlin/no/nav/syfo/brev/narmesteleder/NarmesteLederAccessServiceSpek.kt index b80fd0d8..68c14698 100644 --- a/src/test/kotlin/no/nav/syfo/brev/narmesteleder/NarmesteLederAccessServiceSpek.kt +++ b/src/test/kotlin/no/nav/syfo/brev/narmesteleder/NarmesteLederAccessServiceSpek.kt @@ -22,270 +22,266 @@ import java.time.LocalDate object NarmesteLederAccessServiceSpek : Spek({ describe(NarmesteLederAccessServiceSpek::class.java.simpleName) { - with(TestApplicationEngine()) { - start() - - val anyToken = "any-token" - val anyCallId = "any-call-id" - - val dialogmotedeltakerArbeidsgiver = generateDialogmotedeltakerArbeidsgiver() - val dialogmotedeltakerArbeidstaker = generateDialogmotedeltakerArbeidstaker() - val dialogmoteList = listOf(generateDialogmote()) - - val dialogmotedeltakerServiceMock = mockk() - val narmesteLederClientMock = mockk() - val oppfolgingstilfelleClientMock = mockk() - val referatCreated6MonthsPrior = generateReferat(6L) - val referatCreated3MonthsPrior = generateReferat(3L) - - val narmesteLederAccessService = NarmesteLederAccessService( - dialogmotedeltakerServiceMock, - narmesteLederClientMock, - oppfolgingstilfelleClientMock - ) - - beforeEachTest { - clearMocks(dialogmotedeltakerServiceMock) - clearMocks(narmesteLederClientMock) - clearMocks(oppfolgingstilfelleClientMock) + val anyToken = "any-token" + val anyCallId = "any-call-id" + + val dialogmotedeltakerArbeidsgiver = generateDialogmotedeltakerArbeidsgiver() + val dialogmotedeltakerArbeidstaker = generateDialogmotedeltakerArbeidstaker() + val dialogmoteList = listOf(generateDialogmote()) + + val dialogmotedeltakerServiceMock = mockk() + val narmesteLederClientMock = mockk() + val oppfolgingstilfelleClientMock = mockk() + val referatCreated6MonthsPrior = generateReferat(6L) + val referatCreated3MonthsPrior = generateReferat(3L) + + val narmesteLederAccessService = NarmesteLederAccessService( + dialogmotedeltakerServiceMock, + narmesteLederClientMock, + oppfolgingstilfelleClientMock + ) + + beforeEachTest { + clearMocks(dialogmotedeltakerServiceMock) + clearMocks(narmesteLederClientMock) + clearMocks(oppfolgingstilfelleClientMock) + + coEvery { + dialogmotedeltakerServiceMock.getDialogmoteDeltakerArbeidsgiverById( + any(), + ) + } returns dialogmotedeltakerArbeidsgiver + + coEvery { + dialogmotedeltakerServiceMock.getDialogmoteDeltakerArbeidstaker( + any(), + ) + } returns dialogmotedeltakerArbeidstaker + } + describe("removeExpiredBrevInDialogmoter removes brev created before a validity period") { + it("when validity period is extended by oppfolgingstilfelle") { coEvery { - dialogmotedeltakerServiceMock.getDialogmoteDeltakerArbeidsgiverById( + oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( + any(), + any(), + any(), any(), + any() ) - } returns dialogmotedeltakerArbeidsgiver + } returns listOf( + Oppfolgingstilfelle( + start = LocalDate.now().minusMonths(5), + end = LocalDate.now().minusMonths(2) + ), + Oppfolgingstilfelle( + start = LocalDate.now().minusMonths(6), + end = LocalDate.now().minusMonths(2) + ) + ) + + val moteList = runBlocking { + narmesteLederAccessService.removeExpiredBrevInDialogmoter( + moteList = dialogmoteList, + narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, + arbeidstakerPersonIdentNumber = UserConstants.ARBEIDSTAKER_FNR, + tokenx = anyToken, + callId = anyCallId + ) + } + val brev = moteList.toNarmesteLederBrevDTOList() + + brev.size shouldBe 4 + } + it("when oppfolgingstilfelle does not overlap with grace period") { coEvery { - dialogmotedeltakerServiceMock.getDialogmoteDeltakerArbeidstaker( + oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( any(), + any(), + any(), + any(), + any() + ) + } returns listOf( + Oppfolgingstilfelle( + start = LocalDate.now().minusMonths(10), + end = LocalDate.now().minusMonths(6) ) - } returns dialogmotedeltakerArbeidstaker + ) + + val moteList = runBlocking { + narmesteLederAccessService.removeExpiredBrevInDialogmoter( + moteList = dialogmoteList, + narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, + arbeidstakerPersonIdentNumber = UserConstants.ARBEIDSTAKER_FNR, + tokenx = anyToken, + callId = anyCallId + ) + } + val brev = moteList.toNarmesteLederBrevDTOList() + + brev.size shouldBe 2 } - describe("removeExpiredBrevInDialogmoter removes brev created before a validity period") { - it("when validity period is extended by oppfolgingstilfelle") { - coEvery { - oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( - any(), - any(), - any(), - any(), - any() - ) - } returns listOf( - Oppfolgingstilfelle( - start = LocalDate.now().minusMonths(5), - end = LocalDate.now().minusMonths(2) - ), - Oppfolgingstilfelle( - start = LocalDate.now().minusMonths(6), - end = LocalDate.now().minusMonths(2) - ) + it("when there is no oppfolgingstilfelle") { + coEvery { + oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( + any(), + any(), + any(), + any(), + any() + ) + } returns emptyList() + + val moteList = runBlocking { + narmesteLederAccessService.removeExpiredBrevInDialogmoter( + moteList = dialogmoteList, + narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, + arbeidstakerPersonIdentNumber = UserConstants.ARBEIDSTAKER_FNR, + tokenx = anyToken, + callId = anyCallId ) + } + val brev = moteList.toNarmesteLederBrevDTOList() - val moteList = runBlocking { - narmesteLederAccessService.removeExpiredBrevInDialogmoter( - moteList = dialogmoteList, - narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, - arbeidstakerPersonIdentNumber = UserConstants.ARBEIDSTAKER_FNR, - tokenx = anyToken, - callId = anyCallId - ) - } - val brev = moteList.toNarmesteLederBrevDTOList() + brev.size shouldBe 2 + } - brev.size shouldBe 4 + it("when oppfolgingstilfelle client fails") { + coEvery { + oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( + any(), + any(), + any(), + any(), + any() + ) + } returns null + + val moteList = runBlocking { + narmesteLederAccessService.removeExpiredBrevInDialogmoter( + moteList = dialogmoteList, + narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, + arbeidstakerPersonIdentNumber = UserConstants.ARBEIDSTAKER_FNR, + tokenx = anyToken, + callId = anyCallId + ) } + val brev = moteList.toNarmesteLederBrevDTOList() - it("when oppfolgingstilfelle does not overlap with grace period") { - coEvery { - oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( - any(), - any(), - any(), - any(), - any() - ) - } returns listOf( - Oppfolgingstilfelle( - start = LocalDate.now().minusMonths(10), - end = LocalDate.now().minusMonths(6) - ) - ) + brev.size shouldBe 2 + } - val moteList = runBlocking { - narmesteLederAccessService.removeExpiredBrevInDialogmoter( - moteList = dialogmoteList, - narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, - arbeidstakerPersonIdentNumber = UserConstants.ARBEIDSTAKER_FNR, - tokenx = anyToken, - callId = anyCallId + describe("isBrevExpired") { + describe("returns false when brev is created in a validity period") { + it("and validity period is extended by oppfolgingstilfelle") { + coEvery { + oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( + any(), + any(), + any(), + any(), + any() + ) + } returns listOf( + Oppfolgingstilfelle( + start = LocalDate.now().minusMonths(5), + end = LocalDate.now().minusMonths(2) + ), + Oppfolgingstilfelle( + start = LocalDate.now().minusMonths(6), + end = LocalDate.now().minusMonths(2) + ) ) - } - val brev = moteList.toNarmesteLederBrevDTOList() - brev.size shouldBe 2 - } + val isBrevExpired = runBlocking { + narmesteLederAccessService.isBrevExpired( + brev = referatCreated6MonthsPrior, + narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, + tokenx = anyToken, + callId = anyCallId + ) + } - it("when there is no oppfolgingstilfelle") { - coEvery { - oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( - any(), - any(), - any(), - any(), - any() - ) - } returns emptyList() - - val moteList = runBlocking { - narmesteLederAccessService.removeExpiredBrevInDialogmoter( - moteList = dialogmoteList, - narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, - arbeidstakerPersonIdentNumber = UserConstants.ARBEIDSTAKER_FNR, - tokenx = anyToken, - callId = anyCallId - ) + isBrevExpired shouldBe false } - val brev = moteList.toNarmesteLederBrevDTOList() - brev.size shouldBe 2 - } + it("when there is no oppfolgingstilfelle") { + coEvery { + oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( + any(), + any(), + any(), + any(), + any() + ) + } returns emptyList() + + val isBrevExpired = runBlocking { + narmesteLederAccessService.isBrevExpired( + brev = referatCreated3MonthsPrior, + narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, + tokenx = anyToken, + callId = anyCallId + ) + } - it("when oppfolgingstilfelle client fails") { - coEvery { - oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( - any(), - any(), - any(), - any(), - any() - ) - } returns null - - val moteList = runBlocking { - narmesteLederAccessService.removeExpiredBrevInDialogmoter( - moteList = dialogmoteList, - narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, - arbeidstakerPersonIdentNumber = UserConstants.ARBEIDSTAKER_FNR, - tokenx = anyToken, - callId = anyCallId - ) + isBrevExpired shouldBe false } - val brev = moteList.toNarmesteLederBrevDTOList() - - brev.size shouldBe 2 } - describe("isBrevExpired") { - describe("returns false when brev is created in a validity period") { - it("and validity period is extended by oppfolgingstilfelle") { - coEvery { - oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( - any(), - any(), - any(), - any(), - any() - ) - } returns listOf( - Oppfolgingstilfelle( - start = LocalDate.now().minusMonths(5), - end = LocalDate.now().minusMonths(2) - ), - Oppfolgingstilfelle( - start = LocalDate.now().minusMonths(6), - end = LocalDate.now().minusMonths(2) - ) + describe("returns true when brev is created before a validity period") { + it("when there is no oppfolgingstilfelle") { + coEvery { + oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( + any(), + any(), + any(), + any(), + any() + ) + } returns emptyList() + + val isBrevExpired = runBlocking { + narmesteLederAccessService.isBrevExpired( + brev = referatCreated6MonthsPrior, + narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, + tokenx = anyToken, + callId = anyCallId ) - - val isBrevExpired = runBlocking { - narmesteLederAccessService.isBrevExpired( - brev = referatCreated6MonthsPrior, - narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, - tokenx = anyToken, - callId = anyCallId - ) - } - - isBrevExpired shouldBe false } - it("when there is no oppfolgingstilfelle") { - coEvery { - oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( - any(), - any(), - any(), - any(), - any() - ) - } returns emptyList() - - val isBrevExpired = runBlocking { - narmesteLederAccessService.isBrevExpired( - brev = referatCreated3MonthsPrior, - narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, - tokenx = anyToken, - callId = anyCallId - ) - } - - isBrevExpired shouldBe false - } + isBrevExpired shouldBe true } - describe("returns true when brev is created before a validity period") { - it("when there is no oppfolgingstilfelle") { - coEvery { - oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( - any(), - any(), - any(), - any(), - any() - ) - } returns emptyList() - - val isBrevExpired = runBlocking { - narmesteLederAccessService.isBrevExpired( - brev = referatCreated6MonthsPrior, - narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, - tokenx = anyToken, - callId = anyCallId - ) - } - - isBrevExpired shouldBe true - } - - it("when oppfolgingstilfelle ended before grace period") { - coEvery { - oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( - any(), - any(), - any(), - any(), - any() - ) - } returns listOf( - Oppfolgingstilfelle( - start = LocalDate.now().minusMonths(10), - end = LocalDate.now().minusMonths(6) - ) + it("when oppfolgingstilfelle ended before grace period") { + coEvery { + oppfolgingstilfelleClientMock.oppfolgingstilfelleTokenx( + any(), + any(), + any(), + any(), + any() ) + } returns listOf( + Oppfolgingstilfelle( + start = LocalDate.now().minusMonths(10), + end = LocalDate.now().minusMonths(6) + ) + ) - val isBrevExpired = runBlocking { - narmesteLederAccessService.isBrevExpired( - brev = referatCreated6MonthsPrior, - narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, - tokenx = anyToken, - callId = anyCallId - ) - } - - isBrevExpired shouldBe true + val isBrevExpired = runBlocking { + narmesteLederAccessService.isBrevExpired( + brev = referatCreated6MonthsPrior, + narmesteLederPersonIdentNumber = UserConstants.NARMESTELEDER_FNR, + tokenx = anyToken, + callId = anyCallId + ) } + + isBrevExpired shouldBe true } } } diff --git a/src/test/kotlin/no/nav/syfo/client/azuread/AzureAdClientSpek.kt b/src/test/kotlin/no/nav/syfo/client/azuread/AzureAdClientSpek.kt index e809db98..a588c956 100644 --- a/src/test/kotlin/no/nav/syfo/client/azuread/AzureAdClientSpek.kt +++ b/src/test/kotlin/no/nav/syfo/client/azuread/AzureAdClientSpek.kt @@ -1,6 +1,5 @@ package no.nav.syfo.client.azuread -import io.ktor.server.testing.TestApplicationEngine import io.mockk.clearMocks import io.mockk.every import io.mockk.justRun @@ -25,122 +24,117 @@ class AzureAdClientSpek : Spek({ val anyToken = "anyToken" describe(AzureAdClientSpek::class.java.simpleName) { + val externalMockEnvironment = ExternalMockEnvironment.getInstance() + val cacheMock = mockk() + + val azureAdClient = AzureAdV2Client( + aadAppClient = externalMockEnvironment.environment.aadAppClient, + aadAppSecret = externalMockEnvironment.environment.aadAppSecret, + aadTokenEndpoint = externalMockEnvironment.environment.aadTokenEndpoint, + redisStore = cacheMock, + ) + val systemTokenCacheKey = + "${AzureAdV2Client.CACHE_AZUREAD_TOKEN_SYSTEM_KEY_PREFIX}${externalMockEnvironment.environment.pdlClientId}" + val cachedToken = AzureAdV2Token(anyToken, LocalDateTime.now().plusHours(1)) + val cachedTokenString = mapper.writeValueAsString(cachedToken) + + beforeEachTest { + clearMocks(cacheMock) + } - with(TestApplicationEngine()) { - start() - - val externalMockEnvironment = ExternalMockEnvironment.getInstance() - val cacheMock = mockk() - - val azureAdClient = AzureAdV2Client( - aadAppClient = externalMockEnvironment.environment.aadAppClient, - aadAppSecret = externalMockEnvironment.environment.aadAppSecret, - aadTokenEndpoint = externalMockEnvironment.environment.aadTokenEndpoint, - redisStore = cacheMock, - ) - val systemTokenCacheKey = - "${AzureAdV2Client.CACHE_AZUREAD_TOKEN_SYSTEM_KEY_PREFIX}${externalMockEnvironment.environment.pdlClientId}" - val cachedToken = AzureAdV2Token(anyToken, LocalDateTime.now().plusHours(1)) - val cachedTokenString = mapper.writeValueAsString(cachedToken) - - beforeEachTest { - clearMocks(cacheMock) - } + it("azureAdClient returns cached system token") { + every { cacheMock.mapper } returns mapper + every { cacheMock.get(systemTokenCacheKey) } returns cachedTokenString - it("azureAdClient returns cached system token") { - every { cacheMock.mapper } returns mapper - every { cacheMock.get(systemTokenCacheKey) } returns cachedTokenString - - runBlocking { - azureAdClient.getSystemToken( - scopeClientId = externalMockEnvironment.environment.pdlClientId, - ) shouldBeEqualTo cachedToken - } - verify(exactly = 1) { cacheMock.get(systemTokenCacheKey) } - verify(exactly = 0) { cacheMock.set(any(), any(), any()) } + runBlocking { + azureAdClient.getSystemToken( + scopeClientId = externalMockEnvironment.environment.pdlClientId, + ) shouldBeEqualTo cachedToken } + verify(exactly = 1) { cacheMock.get(systemTokenCacheKey) } + verify(exactly = 0) { cacheMock.set(any(), any(), any()) } + } - it("azureAdClient returns new token when cached token missing") { - every { cacheMock.mapper } returns mapper - every { cacheMock.get(systemTokenCacheKey) } returns null - justRun { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } - - runBlocking { - azureAdClient.getSystemToken( - scopeClientId = externalMockEnvironment.environment.pdlClientId, - )?.accessToken shouldBeEqualTo AZUREAD_TOKEN - } - verify(exactly = 1) { cacheMock.get(systemTokenCacheKey) } - verify(exactly = 1) { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } + it("azureAdClient returns new token when cached token missing") { + every { cacheMock.mapper } returns mapper + every { cacheMock.get(systemTokenCacheKey) } returns null + justRun { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } + + runBlocking { + azureAdClient.getSystemToken( + scopeClientId = externalMockEnvironment.environment.pdlClientId, + )?.accessToken shouldBeEqualTo AZUREAD_TOKEN } - val expiredToken = AzureAdV2Token(anyToken, LocalDateTime.now()) - val expiredTokenString = mapper.writeValueAsString(expiredToken) - - it("azureAdClient returns new token when cached token is expired") { - every { cacheMock.mapper } returns mapper - every { cacheMock.get(systemTokenCacheKey) } returns expiredTokenString - justRun { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } - - runBlocking { - azureAdClient.getSystemToken( - scopeClientId = externalMockEnvironment.environment.pdlClientId, - )?.accessToken shouldBeEqualTo AZUREAD_TOKEN - } - verify(exactly = 1) { cacheMock.get(systemTokenCacheKey) } - verify(exactly = 1) { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } + verify(exactly = 1) { cacheMock.get(systemTokenCacheKey) } + verify(exactly = 1) { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } + } + val expiredToken = AzureAdV2Token(anyToken, LocalDateTime.now()) + val expiredTokenString = mapper.writeValueAsString(expiredToken) + + it("azureAdClient returns new token when cached token is expired") { + every { cacheMock.mapper } returns mapper + every { cacheMock.get(systemTokenCacheKey) } returns expiredTokenString + justRun { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } + + runBlocking { + azureAdClient.getSystemToken( + scopeClientId = externalMockEnvironment.environment.pdlClientId, + )?.accessToken shouldBeEqualTo AZUREAD_TOKEN } - val validToken = generateJWTNavIdent( - externalMockEnvironment.environment.aadAppClient, - externalMockEnvironment.wellKnownVeilederV2.issuer, - VEILEDER_IDENT, - ) - - val scopeClientId = externalMockEnvironment.environment.syfobehandlendeenhetClientId - val oboTokenCacheKey = "$VEILEDER_IDENT-$JWT_AZP-$scopeClientId" - - it("azureAdClient returns cached obo token") { - every { cacheMock.mapper } returns mapper - every { cacheMock.get(oboTokenCacheKey) } returns cachedTokenString - - runBlocking { - azureAdClient.getOnBehalfOfToken( - scopeClientId = scopeClientId, - validToken, - ) shouldBeEqualTo cachedToken - } - verify(exactly = 1) { cacheMock.get(oboTokenCacheKey) } - verify(exactly = 0) { cacheMock.set(any(), any(), any()) } + verify(exactly = 1) { cacheMock.get(systemTokenCacheKey) } + verify(exactly = 1) { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } + } + val validToken = generateJWTNavIdent( + externalMockEnvironment.environment.aadAppClient, + externalMockEnvironment.wellKnownVeilederV2.issuer, + VEILEDER_IDENT, + ) + + val scopeClientId = externalMockEnvironment.environment.syfobehandlendeenhetClientId + val oboTokenCacheKey = "$VEILEDER_IDENT-$JWT_AZP-$scopeClientId" + + it("azureAdClient returns cached obo token") { + every { cacheMock.mapper } returns mapper + every { cacheMock.get(oboTokenCacheKey) } returns cachedTokenString + + runBlocking { + azureAdClient.getOnBehalfOfToken( + scopeClientId = scopeClientId, + validToken, + ) shouldBeEqualTo cachedToken } + verify(exactly = 1) { cacheMock.get(oboTokenCacheKey) } + verify(exactly = 0) { cacheMock.set(any(), any(), any()) } + } + + it("azureAdClient returns new obo token when cached token missing") { + every { cacheMock.mapper } returns mapper + every { cacheMock.get(oboTokenCacheKey) } returns null + justRun { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } - it("azureAdClient returns new obo token when cached token missing") { - every { cacheMock.mapper } returns mapper - every { cacheMock.get(oboTokenCacheKey) } returns null - justRun { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } - - runBlocking { - azureAdClient.getOnBehalfOfToken( - scopeClientId = scopeClientId, - validToken, - )?.accessToken shouldBeEqualTo AZUREAD_TOKEN - } - verify(exactly = 1) { cacheMock.get(oboTokenCacheKey) } - verify(exactly = 1) { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } + runBlocking { + azureAdClient.getOnBehalfOfToken( + scopeClientId = scopeClientId, + validToken, + )?.accessToken shouldBeEqualTo AZUREAD_TOKEN } + verify(exactly = 1) { cacheMock.get(oboTokenCacheKey) } + verify(exactly = 1) { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } + } + + it("azureAdClient returns new obo token when cached token is expired") { + every { cacheMock.mapper } returns mapper + every { cacheMock.get(oboTokenCacheKey) } returns expiredTokenString + justRun { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } - it("azureAdClient returns new obo token when cached token is expired") { - every { cacheMock.mapper } returns mapper - every { cacheMock.get(oboTokenCacheKey) } returns expiredTokenString - justRun { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } - - runBlocking { - azureAdClient.getOnBehalfOfToken( - scopeClientId = scopeClientId, - validToken, - )?.accessToken shouldBeEqualTo AZUREAD_TOKEN - } - verify(exactly = 1) { cacheMock.get(oboTokenCacheKey) } - verify(exactly = 1) { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } + runBlocking { + azureAdClient.getOnBehalfOfToken( + scopeClientId = scopeClientId, + validToken, + )?.accessToken shouldBeEqualTo AZUREAD_TOKEN } + verify(exactly = 1) { cacheMock.get(oboTokenCacheKey) } + verify(exactly = 1) { cacheMock.setObject(any(), any() as AzureAdV2Token, any()) } } } }) diff --git a/src/test/kotlin/no/nav/syfo/client/dokarkiv/DokarkivClientSpek.kt b/src/test/kotlin/no/nav/syfo/client/dokarkiv/DokarkivClientSpek.kt index b1a9d135..3dda3740 100644 --- a/src/test/kotlin/no/nav/syfo/client/dokarkiv/DokarkivClientSpek.kt +++ b/src/test/kotlin/no/nav/syfo/client/dokarkiv/DokarkivClientSpek.kt @@ -17,57 +17,52 @@ import java.util.* class DokarkivClientSpek : Spek({ describe("DokarkivClient") { + val azureAdV2ClientMock = mockk(relaxed = true) + val dokarkivMock = DokarkivMock() + val dokarkivClient = DokarkivClient( + azureAdV2Client = azureAdV2ClientMock, + dokarkivClientId = "dokarkivClientId", + dokarkivBaseUrl = dokarkivMock.url, + ) + val pdf = byteArrayOf(23) - with(TestApplicationEngine()) { - start() - - val azureAdV2ClientMock = mockk(relaxed = true) - val dokarkivMock = DokarkivMock() - val dokarkivClient = DokarkivClient( - azureAdV2Client = azureAdV2ClientMock, - dokarkivClientId = "dokarkivClientId", - dokarkivBaseUrl = dokarkivMock.url, - ) - val pdf = byteArrayOf(23) - - beforeGroup { - dokarkivMock.server.start() - } + beforeGroup { + dokarkivMock.server.start() + } - afterGroup { - dokarkivMock.server.stop(1L, 10L) - } + afterGroup { + dokarkivMock.server.stop(1L, 10L) + } - it("journalfører referat") { - val journalpostRequestReferat = generateJournalpostRequest( - tittel = "Referat fra dialogmøte", - brevkodeType = BrevkodeType.DIALOGMOTE_REFERAT_AT, - pdf = pdf, - kanal = JournalpostKanal.SENTRAL_UTSKRIFT.value, - varselId = UUID.randomUUID() - ) + it("journalfører referat") { + val journalpostRequestReferat = generateJournalpostRequest( + tittel = "Referat fra dialogmøte", + brevkodeType = BrevkodeType.DIALOGMOTE_REFERAT_AT, + pdf = pdf, + kanal = JournalpostKanal.SENTRAL_UTSKRIFT.value, + varselId = UUID.randomUUID() + ) - runBlocking { - val response = dokarkivClient.journalfor(journalpostRequest = journalpostRequestReferat) + runBlocking { + val response = dokarkivClient.journalfor(journalpostRequest = journalpostRequestReferat) - response?.journalpostId shouldBeEqualTo UserConstants.JOURNALPOSTID_JOURNALFORING - } + response?.journalpostId shouldBeEqualTo UserConstants.JOURNALPOSTID_JOURNALFORING } + } - it("handles conflict from api when eksternRefeanseId exists by returning journalpostid") { - val journalpostRequestReferat = generateJournalpostRequest( - tittel = "Referat fra dialogmøte", - brevkodeType = BrevkodeType.DIALOGMOTE_REFERAT_AG, - pdf = pdf, - kanal = JournalpostKanal.SENTRAL_UTSKRIFT.value, - varselId = UserConstants.EXISTING_EKSTERN_REFERANSE_UUID, - ) + it("handles conflict from api when eksternRefeanseId exists by returning journalpostid") { + val journalpostRequestReferat = generateJournalpostRequest( + tittel = "Referat fra dialogmøte", + brevkodeType = BrevkodeType.DIALOGMOTE_REFERAT_AG, + pdf = pdf, + kanal = JournalpostKanal.SENTRAL_UTSKRIFT.value, + varselId = UserConstants.EXISTING_EKSTERN_REFERANSE_UUID, + ) - runBlocking { - val response = dokarkivClient.journalfor(journalpostRequest = journalpostRequestReferat) + runBlocking { + val response = dokarkivClient.journalfor(journalpostRequest = journalpostRequestReferat) - response?.journalpostId shouldBeEqualTo UserConstants.JOURNALPOSTID_JOURNALFORING - } + response?.journalpostId shouldBeEqualTo UserConstants.JOURNALPOSTID_JOURNALFORING } } } diff --git a/src/test/kotlin/no/nav/syfo/client/narmesteleder/NarmesteLederClientSpek.kt b/src/test/kotlin/no/nav/syfo/client/narmesteleder/NarmesteLederClientSpek.kt index 8a196fe6..5f276b31 100644 --- a/src/test/kotlin/no/nav/syfo/client/narmesteleder/NarmesteLederClientSpek.kt +++ b/src/test/kotlin/no/nav/syfo/client/narmesteleder/NarmesteLederClientSpek.kt @@ -1,6 +1,5 @@ package no.nav.syfo.client.narmesteleder -import io.ktor.server.testing.TestApplicationEngine import io.mockk.* import kotlinx.coroutines.runBlocking import no.nav.syfo.application.cache.RedisStore @@ -26,91 +25,86 @@ class NarmesteLederClientSpek : Spek({ val anyCallId = "callId" describe(NarmesteLederClientSpek::class.java.simpleName) { + val externalMockEnvironment = ExternalMockEnvironment.getInstance() + val azureAdV2ClientMock = mockk() + val tokendingsClientMock = mockk() + val cacheMock = mockk() + val client = NarmesteLederClient( + narmesteLederBaseUrl = externalMockEnvironment.environment.narmestelederUrl, + narmestelederClientId = externalMockEnvironment.environment.narmestelederClientId, + azureAdV2Client = azureAdV2ClientMock, + tokendingsClient = tokendingsClientMock, + cache = cacheMock, + ) + val cacheKey = + "${NarmesteLederClient.CACHE_NARMESTE_LEDER_AKTIVE_ANSATTE_KEY_PREFIX}${NARMESTELEDER_FNR.value}" + val cachedValue = listOf( + NarmesteLederRelasjonDTO( + uuid = UUID.randomUUID().toString(), + arbeidstakerPersonIdentNumber = ARBEIDSTAKER_FNR.value, + virksomhetsnummer = VIRKSOMHETSNUMMER_HAS_NARMESTELEDER.value, + virksomhetsnavn = "", + narmesteLederPersonIdentNumber = NARMESTELEDER_FNR.value, + narmesteLederTelefonnummer = "", + narmesteLederEpost = "", + aktivFom = LocalDate.now(), + aktivTom = null, + timestamp = LocalDateTime.now(), + narmesteLederNavn = "", + arbeidsgiverForskutterer = true, + status = NarmesteLederRelasjonStatus.INNMELDT_AKTIV.name, + ) + ) - with(TestApplicationEngine()) { - start() + coEvery { + azureAdV2ClientMock.getSystemToken(externalMockEnvironment.environment.narmestelederClientId) + } returns AzureAdV2Token( + accessToken = anyToken, + expires = LocalDateTime.now().plusDays(1) + ) - val externalMockEnvironment = ExternalMockEnvironment.getInstance() - val azureAdV2ClientMock = mockk() - val tokendingsClientMock = mockk() - val cacheMock = mockk() - val client = NarmesteLederClient( - narmesteLederBaseUrl = externalMockEnvironment.environment.narmestelederUrl, - narmestelederClientId = externalMockEnvironment.environment.narmestelederClientId, - azureAdV2Client = azureAdV2ClientMock, - tokendingsClient = tokendingsClientMock, - cache = cacheMock, - ) - val cacheKey = - "${NarmesteLederClient.CACHE_NARMESTE_LEDER_AKTIVE_ANSATTE_KEY_PREFIX}${NARMESTELEDER_FNR.value}" - val cachedValue = listOf( - NarmesteLederRelasjonDTO( - uuid = UUID.randomUUID().toString(), - arbeidstakerPersonIdentNumber = ARBEIDSTAKER_FNR.value, - virksomhetsnummer = VIRKSOMHETSNUMMER_HAS_NARMESTELEDER.value, - virksomhetsnavn = "", - narmesteLederPersonIdentNumber = NARMESTELEDER_FNR.value, - narmesteLederTelefonnummer = "", - narmesteLederEpost = "", - aktivFom = LocalDate.now(), - aktivTom = null, - timestamp = LocalDateTime.now(), - narmesteLederNavn = "", - arbeidsgiverForskutterer = true, - status = NarmesteLederRelasjonStatus.INNMELDT_AKTIV.name, - ) - ) + coEvery { + tokendingsClientMock.getOnBehalfOfToken(externalMockEnvironment.environment.narmestelederClientId, anyToken) + } returns TokenendingsToken( + accessToken = anyToken, + expires = LocalDateTime.now().plusDays(1) + ) - coEvery { - azureAdV2ClientMock.getSystemToken(externalMockEnvironment.environment.narmestelederClientId) - } returns AzureAdV2Token( - accessToken = anyToken, - expires = LocalDateTime.now().plusDays(1) - ) + beforeEachTest { + clearMocks(cacheMock) + } - coEvery { - tokendingsClientMock.getOnBehalfOfToken(externalMockEnvironment.environment.narmestelederClientId, anyToken) - } returns TokenendingsToken( - accessToken = anyToken, - expires = LocalDateTime.now().plusDays(1) - ) + it("aktive ledere returns cached value") { + every { cacheMock.mapper } returns mapper + every { cacheMock.get(cacheKey) } returns mapper.writeValueAsString(cachedValue) - beforeEachTest { - clearMocks(cacheMock) + runBlocking { + client.getAktiveAnsatte( + narmesteLederIdent = NARMESTELEDER_FNR, + tokenx = anyToken, + callId = anyCallId, + ).size shouldBeEqualTo 1 } + verify(exactly = 1) { cacheMock.get(cacheKey) } + verify(exactly = 0) { cacheMock.setObject(any(), any() as List?, any()) } + } - it("aktive ledere returns cached value") { - every { cacheMock.mapper } returns mapper - every { cacheMock.get(cacheKey) } returns mapper.writeValueAsString(cachedValue) + it("aktive ledere when no cached value") { + every { cacheMock.mapper } returns mapper + every { cacheMock.get(cacheKey) } returns null + justRun { cacheMock.setObject(any(), any() as List, any()) } + runBlocking { runBlocking { client.getAktiveAnsatte( narmesteLederIdent = NARMESTELEDER_FNR, tokenx = anyToken, callId = anyCallId, - ).size shouldBeEqualTo 1 - } - verify(exactly = 1) { cacheMock.get(cacheKey) } - verify(exactly = 0) { cacheMock.setObject(any(), any() as List?, any()) } - } - - it("aktive ledere when no cached value") { - every { cacheMock.mapper } returns mapper - every { cacheMock.get(cacheKey) } returns null - justRun { cacheMock.setObject(any(), any() as List, any()) } - - runBlocking { - runBlocking { - client.getAktiveAnsatte( - narmesteLederIdent = NARMESTELEDER_FNR, - tokenx = anyToken, - callId = anyCallId, - ).size shouldBeEqualTo 2 - } + ).size shouldBeEqualTo 2 } - verify(exactly = 1) { cacheMock.get(cacheKey) } - verify(exactly = 1) { cacheMock.setObject(any(), any() as List?, any()) } } + verify(exactly = 1) { cacheMock.get(cacheKey) } + verify(exactly = 1) { cacheMock.setObject(any(), any() as List?, any()) } } } }) diff --git a/src/test/kotlin/no/nav/syfo/client/person/kontaktinfo/KontaktinformasjonClientSpek.kt b/src/test/kotlin/no/nav/syfo/client/person/kontaktinfo/KontaktinformasjonClientSpek.kt index 57218057..8e615c66 100644 --- a/src/test/kotlin/no/nav/syfo/client/person/kontaktinfo/KontaktinformasjonClientSpek.kt +++ b/src/test/kotlin/no/nav/syfo/client/person/kontaktinfo/KontaktinformasjonClientSpek.kt @@ -23,78 +23,73 @@ class KontaktinformasjonClientSpek : Spek({ val anyCallId = "callId" describe(KontaktinformasjonClientSpek::class.java.simpleName) { + val azureAdV2ClientMock = mockk(relaxed = true) + val krrMock = KrrMock() + val cacheMock = mockk(relaxed = true) + val client = KontaktinformasjonClient( + azureAdV2Client = azureAdV2ClientMock, + cache = cacheMock, + clientId = "krrClientId", + baseUrl = krrMock.url, + ) + + beforeGroup { + krrMock.server.start() + } - with(TestApplicationEngine()) { - start() + afterGroup { + krrMock.server.stop(1L, 10L) + } - val azureAdV2ClientMock = mockk(relaxed = true) - val krrMock = KrrMock() - val cacheMock = mockk(relaxed = true) - val client = KontaktinformasjonClient( - azureAdV2Client = azureAdV2ClientMock, - cache = cacheMock, - clientId = "krrClientId", - baseUrl = krrMock.url, - ) + beforeEachTest { + clearMocks(cacheMock) + } - beforeGroup { - krrMock.server.start() - } + it("returns cached kontaktinformasjon") { + every { + cacheMock.getObject( + digitalKontaktInfoCacheKey + ) + } returns digitalKontaktInfo - afterGroup { - krrMock.server.stop(1L, 10L) + runBlocking { + client.isDigitalVarselEnabled(personIdent, anyToken, anyCallId) shouldBeEqualTo true } - - beforeEachTest { - clearMocks(cacheMock) + verify(exactly = 1) { + cacheMock.getObject( + digitalKontaktInfoCacheKey + ) } + verify(exactly = 0) { cacheMock.set(any(), any(), any()) } + } - it("returns cached kontaktinformasjon") { - every { - cacheMock.getObject( - digitalKontaktInfoCacheKey - ) - } returns digitalKontaktInfo + it("caches kontaktinformasjon") { + every { + cacheMock.getObject( + digitalKontaktInfoCacheKey + ) + } returns null - runBlocking { - client.isDigitalVarselEnabled(personIdent, anyToken, anyCallId) shouldBeEqualTo true - } - verify(exactly = 1) { - cacheMock.getObject( - digitalKontaktInfoCacheKey - ) - } - verify(exactly = 0) { cacheMock.set(any(), any(), any()) } + runBlocking { + client.isDigitalVarselEnabled(personIdent, anyToken, anyCallId) shouldBeEqualTo true } - it("caches kontaktinformasjon") { - every { - cacheMock.getObject( - digitalKontaktInfoCacheKey - ) - } returns null - - runBlocking { - client.isDigitalVarselEnabled(personIdent, anyToken, anyCallId) shouldBeEqualTo true - } - - verify(exactly = 1) { - cacheMock.getObject( - digitalKontaktInfoCacheKey - ) - } - verify(exactly = 1) { cacheMock.setObject(digitalKontaktInfoCacheKey, digitalKontaktInfo, 600) } + verify(exactly = 1) { + cacheMock.getObject( + digitalKontaktInfoCacheKey + ) } - it("handles errors from dkif") { - every { - cacheMock.getObject( - digitalKontaktInfoCacheKey - ) - } returns null - - runBlocking { - client.isDigitalVarselEnabled(UserConstants.ARBEIDSTAKER_DKIF_FEIL, anyToken, anyCallId) shouldBeEqualTo false - } + verify(exactly = 1) { cacheMock.setObject(digitalKontaktInfoCacheKey, digitalKontaktInfo, 600) } + } + it("handles errors from dkif") { + every { + cacheMock.getObject( + digitalKontaktInfoCacheKey + ) + } returns null + + runBlocking { + client.isDigitalVarselEnabled(UserConstants.ARBEIDSTAKER_DKIF_FEIL, anyToken, anyCallId) shouldBeEqualTo false } } } diff --git a/src/test/kotlin/no/nav/syfo/dialogmote/database/repository/MoteRepositorySpek.kt b/src/test/kotlin/no/nav/syfo/dialogmote/database/repository/MoteRepositorySpek.kt index 2012942c..6b501652 100644 --- a/src/test/kotlin/no/nav/syfo/dialogmote/database/repository/MoteRepositorySpek.kt +++ b/src/test/kotlin/no/nav/syfo/dialogmote/database/repository/MoteRepositorySpek.kt @@ -15,61 +15,58 @@ import java.util.* class MoteRepositorySpek : Spek({ describe(MoteRepositorySpek::class.java.simpleName) { - with(TestApplicationEngine()) { - start() - val externalMockEnvironment = ExternalMockEnvironment.getInstance() - val database = externalMockEnvironment.database - val moteRepository = MoteRepository(database = database) - val newDialogmote = generateNewDialogmote(UserConstants.ARBEIDSTAKER_FNR) - val newDialogmoteNotBelongingToArbeidstaker = generateNewDialogmote(UserConstants.ARBEIDSTAKER_ANNEN_FNR) - val moteTilhorendeArbeidstaker = newDialogmote.arbeidstaker.personIdent - val otherArbeidstakerNoMoter = UserConstants.ARBEIDSTAKER_ANNEN_FNR + val externalMockEnvironment = ExternalMockEnvironment.getInstance() + val database = externalMockEnvironment.database + val moteRepository = MoteRepository(database = database) + val newDialogmote = generateNewDialogmote(UserConstants.ARBEIDSTAKER_FNR) + val newDialogmoteNotBelongingToArbeidstaker = generateNewDialogmote(UserConstants.ARBEIDSTAKER_ANNEN_FNR) + val moteTilhorendeArbeidstaker = newDialogmote.arbeidstaker.personIdent + val otherArbeidstakerNoMoter = UserConstants.ARBEIDSTAKER_ANNEN_FNR - afterEachTest { - database.dropData() - } - - describe("Get dialogmote with UUID") { - - it("Successfully get mote with uuid") { - val createdDialogmote = database.connection.use { connection -> - connection.createNewDialogmoteWithReferences(newDialogmote = newDialogmote) - connection.createNewDialogmoteWithReferences(newDialogmoteNotBelongingToArbeidstaker) - } + afterEachTest { + database.dropData() + } - val retrievedMote = moteRepository.getMote(createdDialogmote.dialogmoteIdPair.second) + describe("Get dialogmote with UUID") { - retrievedMote.size shouldBe 1 - retrievedMote.first().opprettetAv shouldBeEqualTo newDialogmote.opprettetAv - retrievedMote.first().status shouldBeEqualTo newDialogmote.status.name - retrievedMote.first().tildeltEnhet shouldBeEqualTo newDialogmote.tildeltEnhet - retrievedMote.first().tildeltVeilederIdent shouldBeEqualTo newDialogmote.tildeltVeilederIdent + it("Successfully get mote with uuid") { + val createdDialogmote = database.connection.use { connection -> + connection.createNewDialogmoteWithReferences(newDialogmote = newDialogmote) + connection.createNewDialogmoteWithReferences(newDialogmoteNotBelongingToArbeidstaker) } - it("Successfully get moter belonging to person with person ident") { - database.connection.use { connection -> - connection.createNewDialogmoteWithReferences(newDialogmote = newDialogmote) - connection.createNewDialogmoteWithReferences(newDialogmoteNotBelongingToArbeidstaker) - } + val retrievedMote = moteRepository.getMote(createdDialogmote.dialogmoteIdPair.second) - val retrievedMoter = moteRepository.getMoterFor(moteTilhorendeArbeidstaker) + retrievedMote.size shouldBe 1 + retrievedMote.first().opprettetAv shouldBeEqualTo newDialogmote.opprettetAv + retrievedMote.first().status shouldBeEqualTo newDialogmote.status.name + retrievedMote.first().tildeltEnhet shouldBeEqualTo newDialogmote.tildeltEnhet + retrievedMote.first().tildeltVeilederIdent shouldBeEqualTo newDialogmote.tildeltVeilederIdent + } - retrievedMoter.size shouldBe 1 - retrievedMoter.first().opprettetAv shouldBeEqualTo newDialogmote.opprettetAv - retrievedMoter.first().status shouldBeEqualTo newDialogmote.status.name - retrievedMoter.first().tildeltEnhet shouldBeEqualTo newDialogmote.tildeltEnhet - retrievedMoter.first().tildeltVeilederIdent shouldBeEqualTo newDialogmote.tildeltVeilederIdent + it("Successfully get moter belonging to person with person ident") { + database.connection.use { connection -> + connection.createNewDialogmoteWithReferences(newDialogmote = newDialogmote) + connection.createNewDialogmoteWithReferences(newDialogmoteNotBelongingToArbeidstaker) } - it("Returns empty list when no uuid exists") { - val retrievedMote = moteRepository.getMote(UUID.randomUUID()) - retrievedMote shouldBeEqualTo emptyList() - } + val retrievedMoter = moteRepository.getMoterFor(moteTilhorendeArbeidstaker) - it("Returns empty list when person ident does not exist") { - val retrievedMote = moteRepository.getMoterFor(otherArbeidstakerNoMoter) - retrievedMote shouldBeEqualTo emptyList() - } + retrievedMoter.size shouldBe 1 + retrievedMoter.first().opprettetAv shouldBeEqualTo newDialogmote.opprettetAv + retrievedMoter.first().status shouldBeEqualTo newDialogmote.status.name + retrievedMoter.first().tildeltEnhet shouldBeEqualTo newDialogmote.tildeltEnhet + retrievedMoter.first().tildeltVeilederIdent shouldBeEqualTo newDialogmote.tildeltVeilederIdent + } + + it("Returns empty list when no uuid exists") { + val retrievedMote = moteRepository.getMote(UUID.randomUUID()) + retrievedMote shouldBeEqualTo emptyList() + } + + it("Returns empty list when person ident does not exist") { + val retrievedMote = moteRepository.getMoterFor(otherArbeidstakerNoMoter) + retrievedMote shouldBeEqualTo emptyList() } } } diff --git a/src/test/kotlin/no/nav/syfo/identhendelse/IdenthendelseServiceSpek.kt b/src/test/kotlin/no/nav/syfo/identhendelse/IdenthendelseServiceSpek.kt index bfccd2c0..9bd13e52 100644 --- a/src/test/kotlin/no/nav/syfo/identhendelse/IdenthendelseServiceSpek.kt +++ b/src/test/kotlin/no/nav/syfo/identhendelse/IdenthendelseServiceSpek.kt @@ -25,170 +25,166 @@ object IdenthendelseServiceSpek : Spek({ describe(IdenthendelseServiceSpek::class.java.simpleName) { - with(TestApplicationEngine()) { - start() - - val externalMockEnvironment = ExternalMockEnvironment.getInstance() - val database = externalMockEnvironment.database - val cacheMock = mockk() - val redisConfig = externalMockEnvironment.environment.redisConfig - val pdlClient = PdlClient( - azureAdV2Client = AzureAdV2Client( - aadAppClient = externalMockEnvironment.environment.aadAppClient, - aadAppSecret = externalMockEnvironment.environment.aadAppSecret, - aadTokenEndpoint = externalMockEnvironment.environment.aadTokenEndpoint, - redisStore = RedisStore( - JedisPool( - JedisPoolConfig(), - HostAndPort(redisConfig.host, redisConfig.port), - DefaultJedisClientConfig.builder() - .ssl(redisConfig.ssl) - .password(redisConfig.redisPassword) - .build() - ) - ), + val externalMockEnvironment = ExternalMockEnvironment.getInstance() + val database = externalMockEnvironment.database + val cacheMock = mockk() + val redisConfig = externalMockEnvironment.environment.redisConfig + val pdlClient = PdlClient( + azureAdV2Client = AzureAdV2Client( + aadAppClient = externalMockEnvironment.environment.aadAppClient, + aadAppSecret = externalMockEnvironment.environment.aadAppSecret, + aadTokenEndpoint = externalMockEnvironment.environment.aadTokenEndpoint, + redisStore = RedisStore( + JedisPool( + JedisPoolConfig(), + HostAndPort(redisConfig.host, redisConfig.port), + DefaultJedisClientConfig.builder() + .ssl(redisConfig.ssl) + .password(redisConfig.redisPassword) + .build() + ) ), - pdlClientId = externalMockEnvironment.environment.pdlClientId, - pdlUrl = externalMockEnvironment.environment.pdlUrl, - redisStore = cacheMock, - ) - - val identhendelseService = IdenthendelseService( - database = database, - pdlClient = pdlClient, - ) - - afterEachTest { - database.dropData() - } - - describe("Happy path") { - it("Skal oppdatere database når arbeidstaker har fått ny ident") { - val kafkaIdenthendelseDTO = generateKafkaIdenthendelseDTOGenerator(hasOldPersonident = true) - val newIdent = kafkaIdenthendelseDTO.getActivePersonident()!! - val oldIdent = kafkaIdenthendelseDTO.getInactivePersonidenter().first() - - // Populate database with new dialogmote using old ident for arbeidstaker - val newDialogmote = generateNewDialogmote(personIdent = oldIdent) - database.connection.use { connection -> - connection.createNewDialogmoteWithReferences( - newDialogmote = newDialogmote, - commit = true, - ) - } - - // Check that arbeidstaker with old/current personident exist in db before update - val currentMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(oldIdent) - currentMotedeltakerArbeidstaker.size shouldBeEqualTo 1 - val initialUpdatedAt = currentMotedeltakerArbeidstaker.first().updatedAt + ), + pdlClientId = externalMockEnvironment.environment.pdlClientId, + pdlUrl = externalMockEnvironment.environment.pdlUrl, + redisStore = cacheMock, + ) + + val identhendelseService = IdenthendelseService( + database = database, + pdlClient = pdlClient, + ) + + afterEachTest { + database.dropData() + } - // Check that arbeidstaker with new personident do not exist in db before update - val newMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) - newMotedeltakerArbeidstaker.size shouldBeEqualTo 0 + describe("Happy path") { + it("Skal oppdatere database når arbeidstaker har fått ny ident") { + val kafkaIdenthendelseDTO = generateKafkaIdenthendelseDTOGenerator(hasOldPersonident = true) + val newIdent = kafkaIdenthendelseDTO.getActivePersonident()!! + val oldIdent = kafkaIdenthendelseDTO.getInactivePersonidenter().first() + + // Populate database with new dialogmote using old ident for arbeidstaker + val newDialogmote = generateNewDialogmote(personIdent = oldIdent) + database.connection.use { connection -> + connection.createNewDialogmoteWithReferences( + newDialogmote = newDialogmote, + commit = true, + ) + } - runBlocking { - identhendelseService.handleIdenthendelse(kafkaIdenthendelseDTO) - } + // Check that arbeidstaker with old/current personident exist in db before update + val currentMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(oldIdent) + currentMotedeltakerArbeidstaker.size shouldBeEqualTo 1 + val initialUpdatedAt = currentMotedeltakerArbeidstaker.first().updatedAt - // Check that arbeidstaker with new personident exist in db after update - val updatedMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) - updatedMotedeltakerArbeidstaker.size shouldBeEqualTo 1 - updatedMotedeltakerArbeidstaker.first().updatedAt shouldBeAfter initialUpdatedAt + // Check that arbeidstaker with new personident do not exist in db before update + val newMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) + newMotedeltakerArbeidstaker.size shouldBeEqualTo 0 - // Check that arbeidstaker with old personident do not exist in db after update - val oldMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(oldIdent) - oldMotedeltakerArbeidstaker.size shouldBeEqualTo 0 + runBlocking { + identhendelseService.handleIdenthendelse(kafkaIdenthendelseDTO) } - it("Skal ikke oppdatere database når arbeidstaker ikke finnes i databasen") { - val kafkaIdenthendelseDTO = generateKafkaIdenthendelseDTOGenerator(hasOldPersonident = true) - val newIdent = kafkaIdenthendelseDTO.getActivePersonident()!! - val oldIdent = PersonIdent("12333378910") + // Check that arbeidstaker with new personident exist in db after update + val updatedMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) + updatedMotedeltakerArbeidstaker.size shouldBeEqualTo 1 + updatedMotedeltakerArbeidstaker.first().updatedAt shouldBeAfter initialUpdatedAt - // Check that arbeidstaker with old/current personident do not exist in db before update - val currentMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(oldIdent) - currentMotedeltakerArbeidstaker.size shouldBeEqualTo 0 + // Check that arbeidstaker with old personident do not exist in db after update + val oldMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(oldIdent) + oldMotedeltakerArbeidstaker.size shouldBeEqualTo 0 + } - // Check that arbeidstaker with new personident do not exist in db before update - val newMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) - newMotedeltakerArbeidstaker.size shouldBeEqualTo 0 + it("Skal ikke oppdatere database når arbeidstaker ikke finnes i databasen") { + val kafkaIdenthendelseDTO = generateKafkaIdenthendelseDTOGenerator(hasOldPersonident = true) + val newIdent = kafkaIdenthendelseDTO.getActivePersonident()!! + val oldIdent = PersonIdent("12333378910") - runBlocking { - identhendelseService.handleIdenthendelse(kafkaIdenthendelseDTO) - } + // Check that arbeidstaker with old/current personident do not exist in db before update + val currentMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(oldIdent) + currentMotedeltakerArbeidstaker.size shouldBeEqualTo 0 - // Check that arbeidstaker with new personident still do not exist in db after update - val updatedMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) - updatedMotedeltakerArbeidstaker.size shouldBeEqualTo 0 + // Check that arbeidstaker with new personident do not exist in db before update + val newMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) + newMotedeltakerArbeidstaker.size shouldBeEqualTo 0 + + runBlocking { + identhendelseService.handleIdenthendelse(kafkaIdenthendelseDTO) } - it("Skal ikke oppdatere database når arbeidstaker ikke har gamle identer") { - val kafkaIdenthendelseDTO = generateKafkaIdenthendelseDTOGenerator(hasOldPersonident = false) - val newIdent = kafkaIdenthendelseDTO.getActivePersonident()!! + // Check that arbeidstaker with new personident still do not exist in db after update + val updatedMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) + updatedMotedeltakerArbeidstaker.size shouldBeEqualTo 0 + } - // Check that arbeidstaker with new personident do not exist in db before update - val newMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) - newMotedeltakerArbeidstaker.size shouldBeEqualTo 0 + it("Skal ikke oppdatere database når arbeidstaker ikke har gamle identer") { + val kafkaIdenthendelseDTO = generateKafkaIdenthendelseDTOGenerator(hasOldPersonident = false) + val newIdent = kafkaIdenthendelseDTO.getActivePersonident()!! - runBlocking { - identhendelseService.handleIdenthendelse(kafkaIdenthendelseDTO) - } + // Check that arbeidstaker with new personident do not exist in db before update + val newMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) + newMotedeltakerArbeidstaker.size shouldBeEqualTo 0 - // Check that arbeidstaker with new personident still do not exist in db after update - val updatedMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) - updatedMotedeltakerArbeidstaker.size shouldBeEqualTo 0 + runBlocking { + identhendelseService.handleIdenthendelse(kafkaIdenthendelseDTO) } + + // Check that arbeidstaker with new personident still do not exist in db after update + val updatedMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(newIdent) + updatedMotedeltakerArbeidstaker.size shouldBeEqualTo 0 } + } - describe("Unhappy path") { - it("Skal kaste feil hvis PDL ikke har oppdatert identen") { - val kafkaIdenthendelseDTO = generateKafkaIdenthendelseDTOGenerator( - personident = UserConstants.ARBEIDSTAKER_IKKE_AKTIVT_FNR, - hasOldPersonident = true, + describe("Unhappy path") { + it("Skal kaste feil hvis PDL ikke har oppdatert identen") { + val kafkaIdenthendelseDTO = generateKafkaIdenthendelseDTOGenerator( + personident = UserConstants.ARBEIDSTAKER_IKKE_AKTIVT_FNR, + hasOldPersonident = true, + ) + val oldIdent = kafkaIdenthendelseDTO.getInactivePersonidenter().first() + + // Populate database with new dialogmote using old ident for arbeidstaker + val newDialogmote = generateNewDialogmote(personIdent = oldIdent) + database.connection.use { connection -> + connection.createNewDialogmoteWithReferences( + newDialogmote = newDialogmote, + commit = true, ) - val oldIdent = kafkaIdenthendelseDTO.getInactivePersonidenter().first() - - // Populate database with new dialogmote using old ident for arbeidstaker - val newDialogmote = generateNewDialogmote(personIdent = oldIdent) - database.connection.use { connection -> - connection.createNewDialogmoteWithReferences( - newDialogmote = newDialogmote, - commit = true, - ) - } + } - // Check that arbeidstaker with old/current personident exist in db before update - val currentMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(oldIdent) - currentMotedeltakerArbeidstaker.size shouldBeEqualTo 1 + // Check that arbeidstaker with old/current personident exist in db before update + val currentMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(oldIdent) + currentMotedeltakerArbeidstaker.size shouldBeEqualTo 1 - runBlocking { - assertFailsWith(IllegalStateException::class) { - identhendelseService.handleIdenthendelse(kafkaIdenthendelseDTO) - } + runBlocking { + assertFailsWith(IllegalStateException::class) { + identhendelseService.handleIdenthendelse(kafkaIdenthendelseDTO) } } - it("Skal kaste RuntimeException hvis PDL gir en not_found ved henting av identer") { - val kafkaIdenthendelseDTO = generateKafkaIdenthendelseDTOGenerator( - personident = PersonIdent(UserConstants.ARBEIDSTAKER_WITH_ERROR_FNR.value), - hasOldPersonident = true, + } + it("Skal kaste RuntimeException hvis PDL gir en not_found ved henting av identer") { + val kafkaIdenthendelseDTO = generateKafkaIdenthendelseDTOGenerator( + personident = PersonIdent(UserConstants.ARBEIDSTAKER_WITH_ERROR_FNR.value), + hasOldPersonident = true, + ) + val oldIdent = kafkaIdenthendelseDTO.getInactivePersonidenter().first() + + val newDialogmote = generateNewDialogmote(personIdent = oldIdent) + database.connection.use { connection -> + connection.createNewDialogmoteWithReferences( + newDialogmote = newDialogmote, + commit = true, ) - val oldIdent = kafkaIdenthendelseDTO.getInactivePersonidenter().first() - - val newDialogmote = generateNewDialogmote(personIdent = oldIdent) - database.connection.use { connection -> - connection.createNewDialogmoteWithReferences( - newDialogmote = newDialogmote, - commit = true, - ) - } + } - val oldMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(oldIdent) - oldMotedeltakerArbeidstaker.size shouldBeEqualTo 1 + val oldMotedeltakerArbeidstaker = database.getMotedeltakerArbeidstakerByIdent(oldIdent) + oldMotedeltakerArbeidstaker.size shouldBeEqualTo 1 - runBlocking { - assertFailsWith(RuntimeException::class) { - identhendelseService.handleIdenthendelse(kafkaIdenthendelseDTO) - } + runBlocking { + assertFailsWith(RuntimeException::class) { + identhendelseService.handleIdenthendelse(kafkaIdenthendelseDTO) } } } diff --git a/src/test/kotlin/no/nav/syfo/janitor/JanitorServiceSpek.kt b/src/test/kotlin/no/nav/syfo/janitor/JanitorServiceSpek.kt index 0abf6948..e7da3bb0 100644 --- a/src/test/kotlin/no/nav/syfo/janitor/JanitorServiceSpek.kt +++ b/src/test/kotlin/no/nav/syfo/janitor/JanitorServiceSpek.kt @@ -1,29 +1,20 @@ package no.nav.syfo.janitor import com.fasterxml.jackson.databind.ObjectMapper -import io.ktor.http.* -import io.ktor.http.HttpHeaders.Authorization -import io.ktor.server.testing.* import io.mockk.* import kotlinx.coroutines.runBlocking -import no.altinn.schemas.services.intermediary.receipt._2009._10.ReceiptExternal -import no.altinn.schemas.services.intermediary.receipt._2009._10.ReceiptStatusEnum -import no.altinn.services.serviceengine.correspondence._2009._10.ICorrespondenceAgencyExternalBasic -import no.nav.syfo.brev.esyfovarsel.EsyfovarselProducer import no.nav.syfo.dialogmote.DialogmotedeltakerService import no.nav.syfo.dialogmote.DialogmoterelasjonService import no.nav.syfo.dialogmote.DialogmotestatusService -import no.nav.syfo.dialogmote.api.v2.dialogmoteApiMoteFerdigstillPath -import no.nav.syfo.dialogmote.api.v2.dialogmoteApiPersonIdentUrlPath -import no.nav.syfo.dialogmote.api.v2.dialogmoteApiV2Basepath +import no.nav.syfo.dialogmote.api.domain.toNewDialogmote +import no.nav.syfo.dialogmote.database.createNewDialogmoteWithReferences +import no.nav.syfo.dialogmote.database.domain.PDialogmote import no.nav.syfo.dialogmote.database.getDialogmoteList import no.nav.syfo.dialogmote.database.repository.MoteStatusEndretRepository import no.nav.syfo.dialogmote.domain.DialogmoteStatus import no.nav.syfo.janitor.kafka.* import no.nav.syfo.testhelper.* import no.nav.syfo.testhelper.generator.generateNewDialogmoteDTO -import no.nav.syfo.testhelper.generator.generateNewReferatDTO -import no.nav.syfo.util.bearerHeader import no.nav.syfo.util.configuredJacksonMapper import org.amshove.kluent.shouldBeEmpty import org.amshove.kluent.shouldBeEqualTo @@ -45,190 +36,127 @@ val objectMapper: ObjectMapper = configuredJacksonMapper() class JanitorServiceSpek : Spek({ describe(JanitorService::class.java.simpleName) { - with(TestApplicationEngine()) { - start() - - val externalMockEnvironment = ExternalMockEnvironment.getInstance() - val database = externalMockEnvironment.database - val altinnMock = mockk() - val esyfovarselProducerMock = mockk(relaxed = true) - justRun { esyfovarselProducerMock.sendVarselToEsyfovarsel(any()) } - val eventStatusProducerMock = mockk(relaxed = true) - justRun { eventStatusProducerMock.sendEventStatus(any()) } - - val moteStatusEndretRepository = MoteStatusEndretRepository(database) - val dialogmotestatusService = DialogmotestatusService( - oppfolgingstilfelleClient = mockk(relaxed = true), - moteStatusEndretRepository = moteStatusEndretRepository, - ) - val dialogmotedeltakerService = - DialogmotedeltakerService(database = database, arbeidstakerVarselService = mockk()) - val dialogmoterelasjonService = DialogmoterelasjonService( - database = database, - dialogmotedeltakerService = dialogmotedeltakerService, - ) - - val janitorService = JanitorService( - database = database, - dialogmotestatusService = dialogmotestatusService, - dialogmoterelasjonService = dialogmoterelasjonService, - janitorEventStatusProducer = eventStatusProducerMock, - ) - - application.testApiModule( - externalMockEnvironment = externalMockEnvironment, - behandlerVarselService = mockk(), - altinnMock = altinnMock, - esyfovarselProducer = esyfovarselProducerMock, - ) - - beforeEachTest { - val altinnResponse = ReceiptExternal() - altinnResponse.receiptStatusCode = ReceiptStatusEnum.OK - - clearMocks(altinnMock) - clearMocks(eventStatusProducerMock) - - every { - altinnMock.insertCorrespondenceBasicV2(any(), any(), any(), any(), any()) - } returns altinnResponse - } + val externalMockEnvironment = ExternalMockEnvironment.getInstance() + val database = externalMockEnvironment.database + val eventStatusProducerMock = mockk(relaxed = true) + justRun { eventStatusProducerMock.sendEventStatus(any()) } + + val moteStatusEndretRepository = MoteStatusEndretRepository(database) + val dialogmotestatusService = DialogmotestatusService( + oppfolgingstilfelleClient = mockk(relaxed = true), + moteStatusEndretRepository = moteStatusEndretRepository, + ) + val dialogmotedeltakerService = + DialogmotedeltakerService(database = database, arbeidstakerVarselService = mockk()) + val dialogmoterelasjonService = DialogmoterelasjonService( + database = database, + dialogmotedeltakerService = dialogmotedeltakerService, + ) + + val janitorService = JanitorService( + database = database, + dialogmotestatusService = dialogmotestatusService, + dialogmoterelasjonService = dialogmoterelasjonService, + janitorEventStatusProducer = eventStatusProducerMock, + ) + + beforeEachTest { + clearMocks(eventStatusProducerMock) + } + + afterEachTest { + database.dropData() + } + + describe("Handles lukk møte event") { + it("updates motestatus to LUKKET and produces event status OK") { + val mote = database.createDialogmote() + + val event = generateJanitorEventDTO( + action = JanitorAction.LUKK_DIALOGMOTE.name, + referenceUuid = mote.uuid.toString() + ) + runBlocking { janitorService.handle(event) } - afterEachTest { - database.dropData() + val motestatusList = moteStatusEndretRepository.getMoteStatusEndretNotPublished() + motestatusList.shouldNotBeEmpty() + val motestatus = motestatusList.last() + motestatus.status shouldBeEqualTo DialogmoteStatus.LUKKET + motestatus.opprettetAv shouldBeEqualTo UserConstants.VEILEDER_IDENT + motestatus.moteId shouldBeEqualTo mote.id + + verify { eventStatusProducerMock.sendEventStatus(JanitorEventStatusDTO(eventUUID = event.eventUUID, status = JanitorEventStatus.OK)) } } - describe("Handles lukk møte event") { - val validToken = generateJWTNavIdent( - externalMockEnvironment.environment.aadAppClient, - externalMockEnvironment.wellKnownVeilederV2.issuer, - UserConstants.VEILEDER_IDENT, + it("produces event status FAILED if mote not found") { + val event = generateJanitorEventDTO( + action = JanitorAction.LUKK_DIALOGMOTE.name, + referenceUuid = UUID.randomUUID().toString() ) - val urlMote = "$dialogmoteApiV2Basepath$dialogmoteApiPersonIdentUrlPath" + runBlocking { janitorService.handle(event) } - it("updates motestatus to LUKKET and produces event status OK") { - val newDialogmoteDTO = generateNewDialogmoteDTO( - personIdent = UserConstants.ARBEIDSTAKER_FNR, - ) - with( - handleRequest(HttpMethod.Post, urlMote) { - addHeader(Authorization, bearerHeader(validToken)) - addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString()) - setBody(objectMapper.writeValueAsString(newDialogmoteDTO)) - } - ) { - response.status() shouldBeEqualTo HttpStatusCode.OK - } - - val mote = database.getDialogmoteList(UserConstants.ARBEIDSTAKER_FNR).first() - - val event = generateJanitorEventDTO( - action = JanitorAction.LUKK_DIALOGMOTE.name, - referenceUuid = mote.uuid.toString() - ) - runBlocking { janitorService.handle(event) } + verify { eventStatusProducerMock.sendEventStatus(JanitorEventStatusDTO(eventUUID = event.eventUUID, status = JanitorEventStatus.FAILED)) } + } - val motestatusList = moteStatusEndretRepository.getMoteStatusEndretNotPublished() - motestatusList.shouldNotBeEmpty() - val motestatus = motestatusList.last() - motestatus.status shouldBeEqualTo DialogmoteStatus.LUKKET - motestatus.opprettetAv shouldBeEqualTo UserConstants.VEILEDER_IDENT - motestatus.moteId shouldBeEqualTo mote.id + it("produces event status FAILED if mote finished") { + val mote = database.createDialogmote() + val moteUuid = mote.uuid + database.updateMoteStatus(moteUUID = moteUuid, newMoteStatus = DialogmoteStatus.FERDIGSTILT) - verify { eventStatusProducerMock.sendEventStatus(JanitorEventStatusDTO(eventUUID = event.eventUUID, status = JanitorEventStatus.OK)) } - } + val event = generateJanitorEventDTO( + action = JanitorAction.LUKK_DIALOGMOTE.name, + referenceUuid = moteUuid.toString() + ) - it("produces event status FAILED if mote not found") { - val event = generateJanitorEventDTO( - action = JanitorAction.LUKK_DIALOGMOTE.name, - referenceUuid = UUID.randomUUID().toString() - ) - runBlocking { janitorService.handle(event) } + runBlocking { janitorService.handle(event) } - verify { eventStatusProducerMock.sendEventStatus(JanitorEventStatusDTO(eventUUID = event.eventUUID, status = JanitorEventStatus.FAILED)) } - } + verify { eventStatusProducerMock.sendEventStatus(JanitorEventStatusDTO(eventUUID = event.eventUUID, status = JanitorEventStatus.FAILED)) } + } - it("produces event status FAILED if mote finished") { - val newDialogmoteDTO = generateNewDialogmoteDTO( - personIdent = UserConstants.ARBEIDSTAKER_FNR, - ) - with( - handleRequest(HttpMethod.Post, urlMote) { - addHeader(Authorization, bearerHeader(validToken)) - addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString()) - setBody(objectMapper.writeValueAsString(newDialogmoteDTO)) - } - ) { - response.status() shouldBeEqualTo HttpStatusCode.OK - } - - val mote = database.getDialogmoteList(UserConstants.ARBEIDSTAKER_FNR).first() - val moteUuid = mote.uuid - - with( - handleRequest( - HttpMethod.Post, - "$dialogmoteApiV2Basepath/${moteUuid}$dialogmoteApiMoteFerdigstillPath" - ) { - addHeader(Authorization, bearerHeader(validToken)) - addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString()) - setBody(objectMapper.writeValueAsString(generateNewReferatDTO())) - } - ) { - response.status() shouldBeEqualTo HttpStatusCode.OK - } - - val event = generateJanitorEventDTO( - action = JanitorAction.LUKK_DIALOGMOTE.name, - referenceUuid = moteUuid.toString() - ) + it("produces event status FAILED if mote on wrong person") { + val moteUuid = database.createDialogmote() - runBlocking { janitorService.handle(event) } + val event = generateJanitorEventDTO( + action = JanitorAction.LUKK_DIALOGMOTE.name, + referenceUuid = moteUuid.toString() + ).copy(personident = UserConstants.ARBEIDSTAKER_ANNEN_FNR.value) - verify { eventStatusProducerMock.sendEventStatus(JanitorEventStatusDTO(eventUUID = event.eventUUID, status = JanitorEventStatus.FAILED)) } - } + runBlocking { janitorService.handle(event) } - it("produces event status FAILED if mote on wrong person") { - val newDialogmoteDTO = generateNewDialogmoteDTO( - personIdent = UserConstants.ARBEIDSTAKER_FNR, - ) - with( - handleRequest(HttpMethod.Post, urlMote) { - addHeader(Authorization, bearerHeader(validToken)) - addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString()) - setBody(objectMapper.writeValueAsString(newDialogmoteDTO)) - } - ) { - response.status() shouldBeEqualTo HttpStatusCode.OK - } - - val mote = database.getDialogmoteList(UserConstants.ARBEIDSTAKER_FNR).first() - val event = generateJanitorEventDTO( - action = JanitorAction.LUKK_DIALOGMOTE.name, - referenceUuid = mote.uuid.toString() - ).copy(personident = UserConstants.ARBEIDSTAKER_ANNEN_FNR.value) - - runBlocking { janitorService.handle(event) } - - verify { eventStatusProducerMock.sendEventStatus(JanitorEventStatusDTO(eventUUID = event.eventUUID, status = JanitorEventStatus.FAILED)) } - } + verify { eventStatusProducerMock.sendEventStatus(JanitorEventStatusDTO(eventUUID = event.eventUUID, status = JanitorEventStatus.FAILED)) } } + } - describe("Handles irrelevant event") { - it("does not update mote status or produce event status") { - runBlocking { - janitorService.handle( - generateJanitorEventDTO( - "IRRELEVANT_ACTION", - UUID.randomUUID().toString() - ) + describe("Handles irrelevant event") { + it("does not update mote status or produce event status") { + runBlocking { + janitorService.handle( + generateJanitorEventDTO( + "IRRELEVANT_ACTION", + UUID.randomUUID().toString() ) - } - - moteStatusEndretRepository.getMoteStatusEndretNotPublished().shouldBeEmpty() - verify(exactly = 0) { eventStatusProducerMock.sendEventStatus(any()) } + ) } + + moteStatusEndretRepository.getMoteStatusEndretNotPublished().shouldBeEmpty() + verify(exactly = 0) { eventStatusProducerMock.sendEventStatus(any()) } } } } }) + +private fun TestDatabase.createDialogmote(): PDialogmote { + val newDialogmoteDTO = generateNewDialogmoteDTO( + personIdent = UserConstants.ARBEIDSTAKER_FNR, + ) + this.connection.use { connection -> + connection.createNewDialogmoteWithReferences( + newDialogmote = newDialogmoteDTO.toNewDialogmote( + requestByNAVIdent = UserConstants.VEILEDER_IDENT, + navEnhet = UserConstants.ENHET_NR + ), + ) + } + + return this.getDialogmoteList(UserConstants.ARBEIDSTAKER_FNR).first() +} diff --git a/src/test/kotlin/no/nav/syfo/testdata.reset/TestdataResetServiceSpek.kt b/src/test/kotlin/no/nav/syfo/testdata.reset/TestdataResetServiceSpek.kt index 0ee8c0d6..6ca312c8 100644 --- a/src/test/kotlin/no/nav/syfo/testdata.reset/TestdataResetServiceSpek.kt +++ b/src/test/kotlin/no/nav/syfo/testdata.reset/TestdataResetServiceSpek.kt @@ -17,63 +17,59 @@ object TestdataResetServiceSpek : Spek({ describe(TestdataResetServiceSpek::class.java.simpleName) { - with(TestApplicationEngine()) { - start() + val externalMockEnvironment = ExternalMockEnvironment.getInstance() + val database = externalMockEnvironment.database - val externalMockEnvironment = ExternalMockEnvironment.getInstance() - val database = externalMockEnvironment.database + val testdataResetService = TestdataResetService( + database = database, + ) - val testdataResetService = TestdataResetService( - database = database, - ) - - afterEachTest { - database.dropData() - } - - describe("Happy path") { - it("Skal slette dialogmøte fra database for oppgitt arbeidstaker") { - // Populate database with new dialogmote for arbeidstaker - val newDialogmote = generateNewDialogmote(personIdent = ARBEIDSTAKER_FNR) - database.connection.use { connection -> - connection.createNewDialogmoteWithReferences( - newDialogmote = newDialogmote, - commit = true, - ) - } + afterEachTest { + database.dropData() + } - // Check that arbeidstaker exist in db before update - database.getMotedeltakerArbeidstakerByIdent(ARBEIDSTAKER_FNR).size shouldBeEqualTo 1 + describe("Happy path") { + it("Skal slette dialogmøte fra database for oppgitt arbeidstaker") { + // Populate database with new dialogmote for arbeidstaker + val newDialogmote = generateNewDialogmote(personIdent = ARBEIDSTAKER_FNR) + database.connection.use { connection -> + connection.createNewDialogmoteWithReferences( + newDialogmote = newDialogmote, + commit = true, + ) + } - runBlocking { - testdataResetService.resetTestdata(ARBEIDSTAKER_FNR) - } + // Check that arbeidstaker exist in db before update + database.getMotedeltakerArbeidstakerByIdent(ARBEIDSTAKER_FNR).size shouldBeEqualTo 1 - // Check that arbeidstaker do not exist in db after update - database.getMotedeltakerArbeidstakerByIdent(ARBEIDSTAKER_FNR).size shouldBeEqualTo 0 + runBlocking { + testdataResetService.resetTestdata(ARBEIDSTAKER_FNR) } - it("Skal ikke slette dialogmøte på annen arbeidstaker") { - // Populate database with new dialogmote for arbeidstaker - val newDialogmote = generateNewDialogmote(personIdent = ARBEIDSTAKER_FNR) - database.connection.use { connection -> - connection.createNewDialogmoteWithReferences( - newDialogmote = newDialogmote, - commit = true, - ) - } + // Check that arbeidstaker do not exist in db after update + database.getMotedeltakerArbeidstakerByIdent(ARBEIDSTAKER_FNR).size shouldBeEqualTo 0 + } - // Check that arbeidstaker exist in db before update - database.getMotedeltakerArbeidstakerByIdent(ARBEIDSTAKER_FNR).size shouldBeEqualTo 1 + it("Skal ikke slette dialogmøte på annen arbeidstaker") { + // Populate database with new dialogmote for arbeidstaker + val newDialogmote = generateNewDialogmote(personIdent = ARBEIDSTAKER_FNR) + database.connection.use { connection -> + connection.createNewDialogmoteWithReferences( + newDialogmote = newDialogmote, + commit = true, + ) + } - // Delete other arbeidstaker - runBlocking { - testdataResetService.resetTestdata(ARBEIDSTAKER_ANNEN_FNR) - } + // Check that arbeidstaker exist in db before update + database.getMotedeltakerArbeidstakerByIdent(ARBEIDSTAKER_FNR).size shouldBeEqualTo 1 - // Check that arbeidstaker still exist in db after update - database.getMotedeltakerArbeidstakerByIdent(ARBEIDSTAKER_FNR).size shouldBeEqualTo 1 + // Delete other arbeidstaker + runBlocking { + testdataResetService.resetTestdata(ARBEIDSTAKER_ANNEN_FNR) } + + // Check that arbeidstaker still exist in db after update + database.getMotedeltakerArbeidstakerByIdent(ARBEIDSTAKER_FNR).size shouldBeEqualTo 1 } } }