Skip to content

Commit 753ca88

Browse files
committed
Oppdaterte deprekerte testmetoder for ktor
1 parent 642ce2f commit 753ca88

File tree

7 files changed

+379
-297
lines changed

7 files changed

+379
-297
lines changed

dp-inntekt-api/src/main/kotlin/no/nav/dagpenger/inntekt/inntektskomponenten/v1/InntektkomponentResponse.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ enum class InntektType {
9999
YTELSE_FRA_OFFENTLIGE,
100100
}
101101

102-
/**
103-
* @Json = Moshi
104-
* @JsonProperty = Jackson
105-
*
106-
*/
107102
enum class SpesielleInntjeningsforhold {
108103
@JsonProperty("hyreTilMannskapPaaFiskeSmaahvalfangstOgSelfangstfartoey")
109104
HYRE_TIL_MANNSKAP_PAA_FISKE_SMAAHVALFANGST_OG_SELFANGSTFARTOEY,
@@ -131,11 +126,6 @@ enum class SpesielleInntjeningsforhold {
131126
UNKNOWN,
132127
}
133128

134-
/**
135-
* @Json == Moshi
136-
* @JsonProperty = Jackson
137-
*
138-
*/
139129
enum class InntektBeskrivelse {
140130
@JsonProperty("aksjerGrunnfondsbevisTilUnderkurs")
141131
AKSJER_GRUNNFONDSBEVIS_TIL_UNDERKURS,

dp-inntekt-api/src/test/kotlin/no/nav/dagpenger/inntekt/NaisChecksTest.kt

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package no.nav.dagpenger.inntekt
22

3-
import io.ktor.http.HttpMethod
3+
import io.ktor.client.request.get
44
import io.ktor.http.HttpStatusCode
5-
import io.ktor.server.testing.TestApplicationEngine
6-
import io.ktor.server.testing.handleRequest
7-
import io.ktor.server.testing.withTestApplication
85
import io.mockk.every
96
import io.mockk.mockk
107
import no.nav.dagpenger.inntekt.db.InntektStore
118
import no.nav.dagpenger.inntekt.inntektskomponenten.v1.InntektskomponentClient
129
import no.nav.dagpenger.inntekt.oppslag.PersonOppslag
1310
import no.nav.dagpenger.inntekt.v1.TestApplication.mockInntektApi
11+
import no.nav.dagpenger.inntekt.v1.TestApplication.withMockAuthServerAndTestApplication
1412
import org.junit.jupiter.api.Test
1513
import kotlin.test.assertEquals
1614

@@ -32,15 +30,7 @@ class NaisChecksTest {
3230

3331
@Test
3432
fun ` should get fault on isAlive endpoint `() {
35-
testApp {
36-
with(handleRequest(HttpMethod.Get, "isAlive")) {
37-
assertEquals(HttpStatusCode.ServiceUnavailable, response.status())
38-
}
39-
}
40-
}
41-
42-
private fun testApp(callback: TestApplicationEngine.() -> Unit) {
43-
withTestApplication(
33+
withMockAuthServerAndTestApplication(
4434
mockInntektApi(
4535
inntektskomponentClient = inntektskomponentClientMock,
4636
inntektStore = inntektStoreMock,
@@ -50,6 +40,9 @@ class NaisChecksTest {
5040
inntektStoreMockHealthCheck,
5141
),
5242
),
53-
) { callback() }
43+
) {
44+
val response = client.get("isAlive")
45+
assertEquals(HttpStatusCode.ServiceUnavailable, response.status)
46+
}
5447
}
5548
}
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
11
package no.nav.dagpenger.inntekt.v1
22

33
import io.kotest.matchers.shouldBe
4-
import io.ktor.http.HttpMethod
4+
import io.ktor.client.request.get
55
import io.ktor.http.HttpStatusCode
6-
import io.ktor.server.testing.handleRequest
7-
import io.ktor.server.testing.withTestApplication
86
import io.mockk.coEvery
97
import io.mockk.mockk
108
import no.nav.dagpenger.inntekt.oppslag.enhetsregister.EnhetsregisterClient
119
import no.nav.dagpenger.inntekt.v1.TestApplication.mockInntektApi
12-
import org.junit.Test
10+
import no.nav.dagpenger.inntekt.v1.TestApplication.withMockAuthServerAndTestApplication
11+
import org.junit.jupiter.api.Test
1312

1413
internal class EnhetsregistrerRouteTest {
1514
@Test
1615
fun `hent organisasjon `() {
1716
val enhetsregisterClient: EnhetsregisterClient = mockk()
1817
coEvery { enhetsregisterClient.hentEnhet("123456789") } returns "{}"
19-
withTestApplication(
18+
withMockAuthServerAndTestApplication(
2019
mockInntektApi(
2120
enhetsregisterClient = enhetsregisterClient,
2221
),
2322
) {
24-
handleRequest(HttpMethod.Get, "v1/enhetsregisteret/enhet/123456789") {
25-
}.apply {
26-
response.status() shouldBe HttpStatusCode.OK
27-
response.headers["Cache-Control"] shouldBe "max-age=86400"
28-
}
23+
val response = client.get("v1/enhetsregisteret/enhet/123456789")
24+
response.status shouldBe HttpStatusCode.OK
25+
response.headers["Cache-Control"] shouldBe "max-age=86400"
2926
}
3027
}
3128

3229
@Test
3330
fun `hent organisasjon på feil `() {
3431
val enhetsregisterClient: EnhetsregisterClient = mockk()
3532
coEvery { enhetsregisterClient.hentEnhet("123456789") } throws RuntimeException("Feilet")
36-
withTestApplication(
33+
withMockAuthServerAndTestApplication(
3734
mockInntektApi(
3835
enhetsregisterClient = enhetsregisterClient,
3936
),
4037
) {
41-
handleRequest(HttpMethod.Get, "v1/enhetsregisteret/enhet/123456789") {
42-
}.apply {
43-
response.status() shouldBe HttpStatusCode.BadGateway
44-
}
38+
val response = client.get("v1/enhetsregisteret/enhet/123456789")
39+
response.status shouldBe HttpStatusCode.BadGateway
4540
}
4641
}
4742
}

dp-inntekt-api/src/test/kotlin/no/nav/dagpenger/inntekt/v1/InntektRouteSpec.kt

Lines changed: 125 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import io.kotest.matchers.shouldBe
66
import io.ktor.client.request.HttpRequestBuilder
77
import io.ktor.client.request.get
88
import io.ktor.client.request.header
9+
import io.ktor.client.request.post
10+
import io.ktor.client.request.setBody
911
import io.ktor.client.statement.bodyAsText
1012
import io.ktor.http.HttpHeaders
1113
import io.ktor.http.HttpMethod
1214
import io.ktor.http.HttpStatusCode
13-
import io.ktor.server.testing.TestApplicationEngine
14-
import io.ktor.server.testing.handleRequest
1515
import io.ktor.server.testing.setBody
16-
import io.ktor.server.testing.withTestApplication
1716
import io.mockk.coEvery
1817
import io.mockk.coVerify
1918
import io.mockk.mockk
@@ -32,7 +31,7 @@ import no.nav.dagpenger.inntekt.oppslag.Person
3231
import no.nav.dagpenger.inntekt.oppslag.PersonNotFoundException
3332
import no.nav.dagpenger.inntekt.oppslag.PersonOppslag
3433
import no.nav.dagpenger.inntekt.serder.jacksonObjectMapper
35-
import no.nav.dagpenger.inntekt.v1.TestApplication.handleAuthenticatedAzureAdRequest
34+
import no.nav.dagpenger.inntekt.v1.TestApplication.autentisert
3635
import no.nav.dagpenger.inntekt.v1.TestApplication.mockInntektApi
3736
import no.nav.dagpenger.inntekt.v1.TestApplication.testOAuthToken
3837
import no.nav.dagpenger.inntekt.v1.TestApplication.withMockAuthServerAndTestApplication
@@ -188,105 +187,153 @@ internal class InntektRouteSpec {
188187

189188
@Test
190189
fun `skal ikke autentisere på v2 hvis ikke auth token er med `() =
191-
testApp {
192-
handleRequest(HttpMethod.Post, klassifisertInntektPathV2) {
193-
addHeader(HttpHeaders.ContentType, "application/json")
194-
setBody(validJson)
195-
}.apply {
196-
assertEquals(HttpStatusCode.Unauthorized, response.status())
197-
}
190+
withMockAuthServerAndTestApplication(
191+
mockInntektApi(
192+
behandlingsInntektsGetter = behandlingsInntektsGetterMock,
193+
personOppslag = personOppslagMock,
194+
),
195+
) {
196+
val response =
197+
client.post(klassifisertInntektPathV2) {
198+
header(HttpHeaders.ContentType, "application/json")
199+
setBody(validJson)
200+
}
201+
202+
assertEquals(HttpStatusCode.Unauthorized, response.status)
198203
}
199204

200205
@Test
201206
fun `skal autentisere på v2 hvis auth token er med `() =
202-
testApp {
203-
handleAuthenticatedAzureAdRequest(HttpMethod.Post, klassifisertInntektPathV2) {
204-
addHeader(HttpHeaders.ContentType, "application/json")
205-
setBody(validJson)
206-
}.apply {
207-
assertEquals(HttpStatusCode.OK, response.status())
208-
}
207+
withMockAuthServerAndTestApplication(
208+
mockInntektApi(
209+
behandlingsInntektsGetter = behandlingsInntektsGetterMock,
210+
personOppslag = personOppslagMock,
211+
),
212+
) {
213+
val response =
214+
autentisert(
215+
klassifisertInntektPathV2,
216+
httpMethod = HttpMethod.Post,
217+
body = validJson,
218+
callId = callId,
219+
)
220+
221+
assertEquals(HttpStatusCode.OK, response.status)
209222
}
210223

211224
@Test
212225
fun `Klassifisert inntekt API specification test - Should match json field names and formats`() =
213-
testApp {
214-
handleAuthenticatedAzureAdRequest(HttpMethod.Post, klassifisertInntektPathV2) {
215-
addHeader(HttpHeaders.ContentType, "application/json")
216-
addHeader("X-Request-Id", callId)
217-
setBody(validJson)
218-
}.apply {
219-
assertEquals(HttpStatusCode.OK, response.status())
220-
coVerify(exactly = 1) { behandlingsInntektsGetterMock.getBehandlingsInntekt(inntektParametre, callId) }
221-
coVerify(exactly = 1) { behandlingsInntektsGetterMock.getKlassifisertInntekt(inntektParametre, callId) }
222-
}
226+
withMockAuthServerAndTestApplication(
227+
mockInntektApi(
228+
behandlingsInntektsGetter = behandlingsInntektsGetterMock,
229+
personOppslag = personOppslagMock,
230+
),
231+
) {
232+
val response =
233+
autentisert(
234+
klassifisertInntektPathV2,
235+
httpMethod = HttpMethod.Post,
236+
body = validJson,
237+
callId = callId,
238+
)
239+
240+
assertEquals(HttpStatusCode.OK, response.status)
241+
coVerify(exactly = 1) { behandlingsInntektsGetterMock.getBehandlingsInntekt(inntektParametre, callId) }
242+
coVerify(exactly = 1) { behandlingsInntektsGetterMock.getKlassifisertInntekt(inntektParametre, callId) }
223243
}
224244

225245
@Test
226246
fun `Klassifisert Requests with fødselsnummer works and does store data`() =
227-
testApp {
228-
handleAuthenticatedAzureAdRequest(HttpMethod.Post, klassifisertInntektPathV2) {
229-
addHeader(HttpHeaders.ContentType, "application/json")
230-
addHeader("X-Request-Id", callId)
231-
setBody(validJsonWithFnr)
232-
}.apply {
233-
assertEquals(HttpStatusCode.OK, response.status())
234-
coVerify(exactly = 1) { behandlingsInntektsGetterMock.getBehandlingsInntekt(fnrParametre, callId) }
235-
coVerify(exactly = 1) { behandlingsInntektsGetterMock.getSpesifisertInntekt(fnrParametre, callId) }
236-
coVerify(exactly = 1) { behandlingsInntektsGetterMock.getKlassifisertInntekt(fnrParametre, callId) }
237-
}
247+
withMockAuthServerAndTestApplication(
248+
mockInntektApi(
249+
behandlingsInntektsGetter = behandlingsInntektsGetterMock,
250+
personOppslag = personOppslagMock,
251+
),
252+
) {
253+
val response =
254+
autentisert(
255+
klassifisertInntektPathV2,
256+
httpMethod = HttpMethod.Post,
257+
body = validJsonWithFnr,
258+
callId = callId,
259+
)
260+
assertEquals(HttpStatusCode.OK, response.status)
261+
coVerify(exactly = 1) { behandlingsInntektsGetterMock.getBehandlingsInntekt(fnrParametre, callId) }
262+
coVerify(exactly = 1) { behandlingsInntektsGetterMock.getSpesifisertInntekt(fnrParametre, callId) }
263+
coVerify(exactly = 1) { behandlingsInntektsGetterMock.getKlassifisertInntekt(fnrParametre, callId) }
238264
}
239265

240266
@Test
241267
fun `Klassifisert Requests with vedtakId as string works and does store data`() =
242-
testApp {
243-
handleAuthenticatedAzureAdRequest(HttpMethod.Post, klassifisertInntektPathV2) {
244-
addHeader(HttpHeaders.ContentType, "application/json")
245-
addHeader("X-Request-Id", callId)
246-
setBody(validJsonWithVedtakIdAsUlid)
247-
}.apply {
248-
assertEquals(HttpStatusCode.OK, response.status())
249-
coVerify(exactly = 1) {
250-
behandlingsInntektsGetterMock.getBehandlingsInntekt(
251-
vedtakIdUlidParametre,
252-
callId,
253-
)
254-
}
255-
coVerify(exactly = 1) {
256-
behandlingsInntektsGetterMock.getSpesifisertInntekt(
257-
vedtakIdUlidParametre,
258-
callId,
259-
)
260-
}
261-
coVerify(exactly = 1) {
262-
behandlingsInntektsGetterMock.getKlassifisertInntekt(
263-
vedtakIdUlidParametre,
264-
callId,
265-
)
266-
}
268+
withMockAuthServerAndTestApplication(
269+
mockInntektApi(
270+
behandlingsInntektsGetter = behandlingsInntektsGetterMock,
271+
personOppslag = personOppslagMock,
272+
),
273+
) {
274+
val response =
275+
autentisert(
276+
klassifisertInntektPathV2,
277+
httpMethod = HttpMethod.Post,
278+
body = validJsonWithVedtakIdAsUlid,
279+
callId = callId,
280+
)
281+
282+
assertEquals(HttpStatusCode.OK, response.status)
283+
coVerify(exactly = 1) {
284+
behandlingsInntektsGetterMock.getBehandlingsInntekt(
285+
vedtakIdUlidParametre,
286+
callId,
287+
)
288+
}
289+
coVerify(exactly = 1) {
290+
behandlingsInntektsGetterMock.getSpesifisertInntekt(
291+
vedtakIdUlidParametre,
292+
callId,
293+
)
294+
}
295+
coVerify(exactly = 1) {
296+
behandlingsInntektsGetterMock.getKlassifisertInntekt(
297+
vedtakIdUlidParametre,
298+
callId,
299+
)
267300
}
268301
}
269302

270303
@Test
271304
fun `Klassifisert request fails on post request with missing fields`() =
272-
testApp {
273-
handleAuthenticatedAzureAdRequest(HttpMethod.Post, klassifisertInntektPathV2) {
274-
addHeader(HttpHeaders.ContentType, "application/json")
275-
setBody(jsonMissingFields)
276-
}.apply {
277-
assertEquals(HttpStatusCode.BadRequest, response.status())
278-
}
305+
withMockAuthServerAndTestApplication(
306+
mockInntektApi(
307+
behandlingsInntektsGetter = behandlingsInntektsGetterMock,
308+
personOppslag = personOppslagMock,
309+
),
310+
) {
311+
val response =
312+
autentisert(
313+
klassifisertInntektPathV2,
314+
httpMethod = HttpMethod.Post,
315+
body = jsonMissingFields,
316+
callId = callId,
317+
)
318+
assertEquals(HttpStatusCode.BadRequest, response.status)
279319
}
280320

281321
@Test
282322
fun `Klassifisert request fails on post request with unknown person`() =
283-
testApp {
284-
handleAuthenticatedAzureAdRequest(HttpMethod.Post, klassifisertInntektPathV2) {
285-
addHeader(HttpHeaders.ContentType, "application/json")
286-
setBody(jsonUkjentPerson)
287-
}.apply {
288-
assertEquals(HttpStatusCode.BadRequest, response.status())
289-
}
323+
withMockAuthServerAndTestApplication(
324+
mockInntektApi(
325+
behandlingsInntektsGetter = behandlingsInntektsGetterMock,
326+
personOppslag = personOppslagMock,
327+
),
328+
) {
329+
val response =
330+
autentisert(
331+
klassifisertInntektPathV2,
332+
httpMethod = HttpMethod.Post,
333+
body = jsonUkjentPerson,
334+
callId = callId,
335+
)
336+
assertEquals(HttpStatusCode.BadRequest, response.status)
290337
}
291338

292339
@Test
@@ -324,15 +371,6 @@ internal class InntektRouteSpec {
324371
ikkeInntektIdResponse.status shouldBe HttpStatusCode.BadRequest
325372
}
326373

327-
private fun testApp(callback: TestApplicationEngine.() -> Unit) {
328-
withTestApplication(
329-
mockInntektApi(
330-
behandlingsInntektsGetter = behandlingsInntektsGetterMock,
331-
personOppslag = personOppslagMock,
332-
),
333-
) { callback() }
334-
}
335-
336374
private fun HttpRequestBuilder.autentisert() {
337375
header(HttpHeaders.Authorization, "Bearer $testOAuthToken")
338376
}

0 commit comments

Comments
 (0)