-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
290 additions
and
144 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
...t-stopp-perioder/src/test/kotlin/no/nav/paw/arbeidssokerregisteret/ApiV1TestCaseRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package no.nav.paw.arbeidssokerregisteret | ||
|
||
import io.kotest.core.spec.style.FreeSpec | ||
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder | ||
import io.kotest.matchers.collections.shouldNotBeEmpty | ||
import io.kotest.matchers.nulls.shouldBeNull | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import io.kotest.matchers.should | ||
import io.kotest.matchers.shouldBe | ||
import io.ktor.client.call.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.testing.* | ||
import io.mockk.mockk | ||
import no.nav.paw.arbeidssoekerregisteret.api.startstopp.models.Feil | ||
import no.nav.paw.arbeidssoekerregisteret.api.startstopp.models.FeilV2 | ||
import no.nav.paw.arbeidssokerregisteret.auth.configureAuthentication | ||
import no.nav.paw.arbeidssokerregisteret.plugins.configureHTTP | ||
import no.nav.paw.arbeidssokerregisteret.plugins.configureSerialization | ||
import no.nav.paw.arbeidssokerregisteret.routes.arbeidssokerRoutes | ||
import no.nav.paw.arbeidssokerregisteret.routes.arbeidssokerRoutesV2 | ||
import no.nav.paw.arbeidssokerregisteret.testdata.TestCase | ||
import no.nav.paw.arbeidssokerregisteret.testdata.TestCaseBuilder | ||
import no.nav.security.mock.oauth2.MockOAuth2Server | ||
import org.slf4j.LoggerFactory | ||
|
||
|
||
class ApiV1TestCaseRunner : FreeSpec({ | ||
val mockOAuthServer = MockOAuth2Server() | ||
beforeSpec { | ||
mockOAuthServer.start() | ||
} | ||
afterSpec { | ||
mockOAuthServer.shutdown() | ||
} | ||
val testCases = TestCase::class.sealedSubclasses | ||
"Verifiserer oppsett av test caser" - { | ||
"Det må finnes minst en test" { | ||
testCases.shouldNotBeEmpty() | ||
} | ||
"Alle tester må ha 'objectInstance" - { | ||
testCases.forEach { case -> | ||
"${case.simpleName} må ha 'objectInstance'" { | ||
case.objectInstance.shouldNotBeNull() | ||
} | ||
} | ||
} | ||
} | ||
"Test cases V1" - { | ||
TestCase::class.sealedSubclasses | ||
.mapNotNull { it.objectInstance } | ||
.forEach { testCase -> | ||
val logger = LoggerFactory.getLogger(testCase::class.java) | ||
"Test ${testCase::class.simpleName?.readable()}" - { | ||
"Verifiser API V1" - { | ||
with(initTestCaseContext()) { | ||
"Verifiser API response" { | ||
testApplication { | ||
application { | ||
configureSerialization() | ||
configureHTTP() | ||
configureAuthentication(mockOAuthServer) | ||
} | ||
routing { | ||
authenticate("tokenx", "azure") { | ||
arbeidssokerRoutes(startStoppRequestHandler, mockk()) | ||
arbeidssokerRoutesV2(startStoppRequestHandler) | ||
} | ||
} | ||
val client = createClient { defaultConfig() } | ||
val id = testCase.id | ||
val person = testCase.person | ||
logger.info("Running test for $id") | ||
personInfoService.setPersonInfo(id, person) | ||
val testConfiguration = TestCaseBuilder(mockOAuthServer, autorisasjonService) | ||
.also { testCase.configure(it) } | ||
val status = | ||
client.startPeriode( | ||
id, | ||
testConfiguration.authToken, | ||
testCase.forhaandsGodkjent | ||
) | ||
status.status shouldBe testCase.producesHttpResponse | ||
testCase.producesError?.also { expectedErrorResponse -> | ||
val body = status.body<Feil>() | ||
body.feilKode.name shouldBe expectedErrorResponse.feilKode.name | ||
body.melding shouldBe expectedErrorResponse.melding | ||
body.aarsakTilAvvisning?.regel shouldBe expectedErrorResponse.aarsakTilAvvisning?.regel | ||
expectedErrorResponse.aarsakTilAvvisning?.detaljer?.also { expectedDetails -> | ||
body.aarsakTilAvvisning?.detaljer.shouldNotBeNull() | ||
body.aarsakTilAvvisning?.detaljer?.shouldContainExactlyInAnyOrder( | ||
expectedDetails | ||
) | ||
} | ||
} | ||
} | ||
} | ||
"Verifiser Kafka melding" { | ||
val expectedRecord = testCase.producesRecord(kafkaKeys) | ||
if (expectedRecord != null) { | ||
verify( | ||
actual = producer.next(), | ||
expected = expectedRecord | ||
) | ||
producer.next().shouldBeNull() | ||
} else { | ||
producer.next().shouldBeNull() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
121 changes: 121 additions & 0 deletions
121
...t-stopp-perioder/src/test/kotlin/no/nav/paw/arbeidssokerregisteret/ApiV2TestCaseRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package no.nav.paw.arbeidssokerregisteret | ||
|
||
import io.kotest.core.spec.style.FreeSpec | ||
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder | ||
import io.kotest.matchers.collections.shouldNotBeEmpty | ||
import io.kotest.matchers.nulls.shouldBeNull | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import io.kotest.matchers.should | ||
import io.kotest.matchers.shouldBe | ||
import io.ktor.client.call.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.testing.* | ||
import io.mockk.mockk | ||
import no.nav.paw.arbeidssoekerregisteret.api.startstopp.models.Feil | ||
import no.nav.paw.arbeidssoekerregisteret.api.startstopp.models.FeilV2 | ||
import no.nav.paw.arbeidssokerregisteret.auth.configureAuthentication | ||
import no.nav.paw.arbeidssokerregisteret.plugins.configureHTTP | ||
import no.nav.paw.arbeidssokerregisteret.plugins.configureSerialization | ||
import no.nav.paw.arbeidssokerregisteret.routes.arbeidssokerRoutes | ||
import no.nav.paw.arbeidssokerregisteret.routes.arbeidssokerRoutesV2 | ||
import no.nav.paw.arbeidssokerregisteret.testdata.TestCase | ||
import no.nav.paw.arbeidssokerregisteret.testdata.TestCaseBuilder | ||
import no.nav.security.mock.oauth2.MockOAuth2Server | ||
import org.slf4j.LoggerFactory | ||
|
||
|
||
class ApiV2TestCaseRunner : FreeSpec({ | ||
val mockOAuthServer = MockOAuth2Server() | ||
beforeSpec { | ||
mockOAuthServer.start() | ||
} | ||
afterSpec { | ||
mockOAuthServer.shutdown() | ||
} | ||
val testCases = TestCase::class.sealedSubclasses | ||
"Verifiserer oppsett av test caser" - { | ||
"Det må finnes minst en test" { | ||
testCases.shouldNotBeEmpty() | ||
} | ||
"Alle tester må ha 'objectInstance" - { | ||
testCases.forEach { case -> | ||
"${case.simpleName} må ha 'objectInstance'" { | ||
case.objectInstance.shouldNotBeNull() | ||
} | ||
} | ||
} | ||
} | ||
"Test cases V2" - { | ||
TestCase::class.sealedSubclasses | ||
.mapNotNull { it.objectInstance } | ||
.forEach { testCase -> | ||
val logger = LoggerFactory.getLogger(testCase::class.java) | ||
"Test API V2 ${testCase::class.simpleName?.readable()}" - { | ||
"Verifiser API V2" - { | ||
with(initTestCaseContext()) { | ||
"Verifiser API response" { | ||
testApplication { | ||
application { | ||
configureSerialization() | ||
configureHTTP() | ||
configureAuthentication(mockOAuthServer) | ||
} | ||
routing { | ||
authenticate("tokenx", "azure") { | ||
arbeidssokerRoutes(startStoppRequestHandler, mockk()) | ||
arbeidssokerRoutesV2(startStoppRequestHandler) | ||
} | ||
} | ||
val client = createClient { defaultConfig() } | ||
val id = testCase.id | ||
val person = testCase.person | ||
logger.info("Running test for $id") | ||
personInfoService.setPersonInfo(id, person) | ||
val testConfiguration = TestCaseBuilder(mockOAuthServer, autorisasjonService) | ||
.also { testCase.configure(it) } | ||
val statusV2 = | ||
client.startPeriodeV2( | ||
id, | ||
testConfiguration.authToken, | ||
testCase.forhaandsGodkjent | ||
) | ||
statusV2.status shouldBe testCase.producesHttpResponse | ||
testCase.producesError?.also { expectedErrorResponse -> | ||
val body = statusV2.body<FeilV2>() | ||
body.feilKode.name shouldBe expectedErrorResponse.feilKode.name | ||
val forventetMelding: String = | ||
if (expectedErrorResponse.feilKode == Feil.FeilKode.IKKE_TILGANG) expectedErrorResponse.melding else "Avvist, se 'aarsakTilAvvisning' for detaljer" | ||
body.melding shouldBe forventetMelding | ||
expectedErrorResponse.aarsakTilAvvisning?.regel?.should { aarsak -> | ||
body.aarsakTilAvvisning?.regler?.map { it.id?.name } shouldBe listOf( | ||
aarsak.name | ||
) | ||
} | ||
expectedErrorResponse.aarsakTilAvvisning?.detaljer?.also { expectedDetails -> | ||
body.aarsakTilAvvisning?.detaljer.shouldNotBeNull() | ||
body.aarsakTilAvvisning?.detaljer?.shouldContainExactlyInAnyOrder( | ||
expectedDetails | ||
) | ||
} | ||
} | ||
} | ||
} | ||
"Verifiser Kafka melding" { | ||
val expectedRecord = testCase.producesRecord(kafkaKeys) | ||
if (expectedRecord != null) { | ||
verify( | ||
actual = producer.next(), | ||
expected = expectedRecord | ||
) | ||
producer.next().shouldBeNull() | ||
} else { | ||
producer.next().shouldBeNull() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...start-stopp-perioder/src/test/kotlin/no/nav/paw/arbeidssokerregisteret/TestCaseContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package no.nav.paw.arbeidssokerregisteret | ||
|
||
import io.mockk.mockk | ||
import no.nav.paw.arbeidssokerregisteret.application.RequestValidator | ||
import no.nav.paw.arbeidssokerregisteret.application.StartStoppRequestHandler | ||
import no.nav.paw.arbeidssokerregisteret.intern.v1.Hendelse | ||
import no.nav.paw.arbeidssokerregisteret.services.AutorisasjonService | ||
import no.nav.paw.arbeidssokerregisteret.services.PersonInfoService | ||
import no.nav.paw.kafkakeygenerator.client.KafkaKeysClient | ||
import no.nav.paw.kafkakeygenerator.client.inMemoryKafkaKeysMock | ||
|
||
fun String.readable(): String = | ||
map { letter -> if (letter.isUpperCase()) " ${letter.lowercase()}" else "$letter" } | ||
.joinToString("") | ||
.replace("oe", "ø") | ||
.replace("aa", "å") | ||
|
||
data class TestCaseContext( | ||
val autorisasjonService: AutorisasjonService, | ||
val personInfoService: PersonInfoService, | ||
val producer: ProducerMock<Long, Hendelse>, | ||
val kafkaKeys: KafkaKeysClient, | ||
val startStoppRequestHandler: StartStoppRequestHandler | ||
) | ||
|
||
fun initTestCaseContext(): TestCaseContext { | ||
val autorisasjonService = mockk<AutorisasjonService>() | ||
val personInfoService = mockk<PersonInfoService>() | ||
val producer: ProducerMock<Long, Hendelse> = ProducerMock() | ||
val kafkaKeys = inMemoryKafkaKeysMock() | ||
val startStoppRequestHandler = StartStoppRequestHandler( | ||
hendelseTopic = "any", | ||
requestValidator = RequestValidator( | ||
autorisasjonService = autorisasjonService, | ||
personInfoService = personInfoService | ||
), | ||
producer = producer, | ||
kafkaKeysClient = kafkaKeys | ||
) | ||
return TestCaseContext( | ||
autorisasjonService = autorisasjonService, | ||
personInfoService = personInfoService, | ||
producer = producer, | ||
kafkaKeys = kafkaKeys, | ||
startStoppRequestHandler = startStoppRequestHandler | ||
) | ||
} |
Oops, something went wrong.