|
| 1 | +package no.nav.paw.arbeidssokerregisteret |
| 2 | + |
| 3 | +import io.kotest.core.spec.style.FreeSpec |
| 4 | +import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder |
| 5 | +import io.kotest.matchers.collections.shouldNotBeEmpty |
| 6 | +import io.kotest.matchers.nulls.shouldBeNull |
| 7 | +import io.kotest.matchers.nulls.shouldNotBeNull |
| 8 | +import io.kotest.matchers.should |
| 9 | +import io.kotest.matchers.shouldBe |
| 10 | +import io.ktor.client.call.* |
| 11 | +import io.ktor.server.auth.* |
| 12 | +import io.ktor.server.testing.* |
| 13 | +import io.mockk.mockk |
| 14 | +import no.nav.paw.arbeidssoekerregisteret.api.startstopp.models.Feil |
| 15 | +import no.nav.paw.arbeidssoekerregisteret.api.startstopp.models.FeilV2 |
| 16 | +import no.nav.paw.arbeidssokerregisteret.auth.configureAuthentication |
| 17 | +import no.nav.paw.arbeidssokerregisteret.plugins.configureHTTP |
| 18 | +import no.nav.paw.arbeidssokerregisteret.plugins.configureSerialization |
| 19 | +import no.nav.paw.arbeidssokerregisteret.routes.arbeidssokerRoutes |
| 20 | +import no.nav.paw.arbeidssokerregisteret.routes.arbeidssokerRoutesV2 |
| 21 | +import no.nav.paw.arbeidssokerregisteret.testdata.TestCase |
| 22 | +import no.nav.paw.arbeidssokerregisteret.testdata.TestCaseBuilder |
| 23 | +import no.nav.security.mock.oauth2.MockOAuth2Server |
| 24 | +import org.slf4j.LoggerFactory |
| 25 | + |
| 26 | + |
| 27 | +class ApiV2TestCaseRunner : FreeSpec({ |
| 28 | + val mockOAuthServer = MockOAuth2Server() |
| 29 | + beforeSpec { |
| 30 | + mockOAuthServer.start() |
| 31 | + } |
| 32 | + afterSpec { |
| 33 | + mockOAuthServer.shutdown() |
| 34 | + } |
| 35 | + val testCases = TestCase::class.sealedSubclasses |
| 36 | + "Verifiserer oppsett av test caser" - { |
| 37 | + "Det må finnes minst en test" { |
| 38 | + testCases.shouldNotBeEmpty() |
| 39 | + } |
| 40 | + "Alle tester må ha 'objectInstance" - { |
| 41 | + testCases.forEach { case -> |
| 42 | + "${case.simpleName} må ha 'objectInstance'" { |
| 43 | + case.objectInstance.shouldNotBeNull() |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + "Test cases V2" - { |
| 49 | + TestCase::class.sealedSubclasses |
| 50 | + .mapNotNull { it.objectInstance } |
| 51 | + .forEach { testCase -> |
| 52 | + val logger = LoggerFactory.getLogger(testCase::class.java) |
| 53 | + "Test API V2 ${testCase::class.simpleName?.readable()}" - { |
| 54 | + "Verifiser API V2" - { |
| 55 | + with(initTestCaseContext()) { |
| 56 | + "Verifiser API response" { |
| 57 | + testApplication { |
| 58 | + application { |
| 59 | + configureSerialization() |
| 60 | + configureHTTP() |
| 61 | + configureAuthentication(mockOAuthServer) |
| 62 | + } |
| 63 | + routing { |
| 64 | + authenticate("tokenx", "azure") { |
| 65 | + arbeidssokerRoutes(startStoppRequestHandler, mockk()) |
| 66 | + arbeidssokerRoutesV2(startStoppRequestHandler) |
| 67 | + } |
| 68 | + } |
| 69 | + val client = createClient { defaultConfig() } |
| 70 | + val id = testCase.id |
| 71 | + val person = testCase.person |
| 72 | + logger.info("Running test for $id") |
| 73 | + personInfoService.setPersonInfo(id, person) |
| 74 | + val testConfiguration = TestCaseBuilder(mockOAuthServer, autorisasjonService) |
| 75 | + .also { testCase.configure(it) } |
| 76 | + val statusV2 = |
| 77 | + client.startPeriodeV2( |
| 78 | + id, |
| 79 | + testConfiguration.authToken, |
| 80 | + testCase.forhaandsGodkjent |
| 81 | + ) |
| 82 | + statusV2.status shouldBe testCase.producesHttpResponse |
| 83 | + testCase.producesError?.also { expectedErrorResponse -> |
| 84 | + val body = statusV2.body<FeilV2>() |
| 85 | + body.feilKode.name shouldBe expectedErrorResponse.feilKode.name |
| 86 | + val forventetMelding: String = |
| 87 | + if (expectedErrorResponse.feilKode == Feil.FeilKode.IKKE_TILGANG) expectedErrorResponse.melding else "Avvist, se 'aarsakTilAvvisning' for detaljer" |
| 88 | + body.melding shouldBe forventetMelding |
| 89 | + expectedErrorResponse.aarsakTilAvvisning?.regel?.should { aarsak -> |
| 90 | + body.aarsakTilAvvisning?.regler?.map { it.id?.name } shouldBe listOf( |
| 91 | + aarsak.name |
| 92 | + ) |
| 93 | + } |
| 94 | + expectedErrorResponse.aarsakTilAvvisning?.detaljer?.also { expectedDetails -> |
| 95 | + body.aarsakTilAvvisning?.detaljer.shouldNotBeNull() |
| 96 | + body.aarsakTilAvvisning?.detaljer?.shouldContainExactlyInAnyOrder( |
| 97 | + expectedDetails |
| 98 | + ) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + "Verifiser Kafka melding" { |
| 104 | + val expectedRecord = testCase.producesRecord(kafkaKeys) |
| 105 | + if (expectedRecord != null) { |
| 106 | + verify( |
| 107 | + actual = producer.next(), |
| 108 | + expected = expectedRecord |
| 109 | + ) |
| 110 | + producer.next().shouldBeNull() |
| 111 | + } else { |
| 112 | + producer.next().shouldBeNull() |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +}) |
| 121 | + |
0 commit comments