Skip to content

Commit

Permalink
IS-2720: Remove unneccesary TestApplicationEngine (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersrognstad authored Dec 3, 2024
1 parent 0c95ae4 commit 9d2e052
Show file tree
Hide file tree
Showing 9 changed files with 808 additions and 917 deletions.

Large diffs are not rendered by default.

206 changes: 100 additions & 106 deletions src/test/kotlin/no/nav/syfo/client/azuread/AzureAdClientSpek.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,122 +24,117 @@ class AzureAdClientSpek : Spek({
val anyToken = "anyToken"

describe(AzureAdClientSpek::class.java.simpleName) {
val externalMockEnvironment = ExternalMockEnvironment.getInstance()
val cacheMock = mockk<RedisStore>()

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<RedisStore>()

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()) }
}
}
})
79 changes: 37 additions & 42 deletions src/test/kotlin/no/nav/syfo/client/dokarkiv/DokarkivClientSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,52 @@ import java.util.*
class DokarkivClientSpek : Spek({

describe("DokarkivClient") {
val azureAdV2ClientMock = mockk<AzureAdV2Client>(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<AzureAdV2Client>(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
}
}
}
Expand Down
Loading

0 comments on commit 9d2e052

Please sign in to comment.