Skip to content

Commit c2cfa80

Browse files
IS-2720: Migrate tests to testApplication
1 parent 5d614e7 commit c2cfa80

20 files changed

+4222
-5378
lines changed

src/test/kotlin/no/nav/syfo/application/api/PodApiSpek.kt

+73-63
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package no.nav.syfo.application.api
22

3+
import io.ktor.client.request.*
4+
import io.ktor.client.statement.*
35
import io.ktor.http.*
46
import io.ktor.server.routing.*
57
import io.ktor.server.testing.*
68
import no.nav.syfo.application.ApplicationState
9+
import no.nav.syfo.application.database.DatabaseInterface
710
import no.nav.syfo.testhelper.TestDatabase
811
import no.nav.syfo.testhelper.TestDatabaseNotResponding
912
import org.amshove.kluent.shouldBeEqualTo
@@ -13,91 +16,98 @@ import org.spekframework.spek2.style.specification.describe
1316

1417
object PodApiSpek : Spek({
1518

16-
describe("Successful liveness and readiness checks") {
17-
with(TestApplicationEngine()) {
18-
start()
19-
val database = TestDatabase()
20-
application.routing {
19+
val database = TestDatabase()
20+
val databaseNotResponding = TestDatabaseNotResponding()
21+
22+
fun ApplicationTestBuilder.setupPodApi(database: DatabaseInterface, applicationState: ApplicationState) {
23+
application {
24+
routing {
2125
registerPodApi(
22-
ApplicationState(
23-
alive = true,
24-
ready = true
25-
),
26-
database,
26+
applicationState = applicationState,
27+
database = database,
2728
)
2829
}
30+
}
31+
}
32+
33+
describe("Successful liveness and readiness checks") {
34+
it("Returns ok on is_alive") {
35+
testApplication {
36+
setupPodApi(
37+
database = database,
38+
applicationState = ApplicationState(alive = true, ready = true)
39+
)
2940

30-
it("Returns ok on is_alive") {
31-
with(handleRequest(HttpMethod.Get, "/internal/is_alive")) {
32-
response.status()?.isSuccess() shouldBeEqualTo true
33-
response.content shouldNotBeEqualTo null
34-
}
41+
val response = client.get("/internal/is_alive")
42+
response.status.isSuccess() shouldBeEqualTo true
43+
response.bodyAsText() shouldNotBeEqualTo null
3544
}
36-
it("Returns ok on is_alive") {
37-
with(handleRequest(HttpMethod.Get, "/internal/is_ready")) {
38-
println(response.status())
39-
response.status()?.isSuccess() shouldBeEqualTo true
40-
response.content shouldNotBeEqualTo null
41-
}
45+
}
46+
it("Returns ok on is_alive") {
47+
testApplication {
48+
setupPodApi(
49+
database = database,
50+
applicationState = ApplicationState(alive = true, ready = true)
51+
)
52+
53+
val response = client.get("/internal/is_ready")
54+
response.status.isSuccess() shouldBeEqualTo true
55+
response.bodyAsText() shouldNotBeEqualTo null
4256
}
4357
}
4458
}
4559

4660
describe("Unsuccessful liveness and readiness checks") {
47-
with(TestApplicationEngine()) {
48-
start()
49-
val database = TestDatabase()
50-
application.routing {
51-
registerPodApi(
52-
ApplicationState(
53-
alive = false,
54-
ready = false
55-
),
56-
database,
61+
it("Returns internal server error when liveness check fails") {
62+
testApplication {
63+
setupPodApi(
64+
database = database,
65+
applicationState = ApplicationState(alive = false, ready = false)
5766
)
58-
}
5967

60-
it("Returns internal server error when liveness check fails") {
61-
with(handleRequest(HttpMethod.Get, "/internal/is_alive")) {
62-
response.status() shouldBeEqualTo HttpStatusCode.InternalServerError
63-
response.content shouldNotBeEqualTo null
64-
}
68+
val response = client.get("/internal/is_alive")
69+
response.status shouldBeEqualTo HttpStatusCode.InternalServerError
70+
response.bodyAsText() shouldNotBeEqualTo null
6571
}
72+
}
73+
74+
it("Returns internal server error when readiness check fails") {
75+
testApplication {
76+
setupPodApi(
77+
database = database,
78+
applicationState = ApplicationState(alive = false, ready = false)
79+
)
6680

67-
it("Returns internal server error when readiness check fails") {
68-
with(handleRequest(HttpMethod.Get, "/internal/is_ready")) {
69-
response.status() shouldBeEqualTo HttpStatusCode.InternalServerError
70-
response.content shouldNotBeEqualTo null
71-
}
81+
val response = client.get("/internal/is_ready")
82+
response.status shouldBeEqualTo HttpStatusCode.InternalServerError
83+
response.bodyAsText() shouldNotBeEqualTo null
7284
}
7385
}
7486
}
87+
7588
describe("Successful liveness and unsuccessful readiness checks when database not working") {
76-
with(TestApplicationEngine()) {
77-
start()
78-
val database = TestDatabaseNotResponding()
79-
application.routing {
80-
registerPodApi(
81-
ApplicationState(
82-
alive = true,
83-
ready = true
84-
),
85-
database,
89+
it("Returns ok on is_alive") {
90+
testApplication {
91+
setupPodApi(
92+
database = databaseNotResponding,
93+
applicationState = ApplicationState(alive = true, ready = true)
8694
)
87-
}
8895

89-
it("Returns ok on is_alive") {
90-
with(handleRequest(HttpMethod.Get, "/internal/is_alive")) {
91-
response.status()?.isSuccess() shouldBeEqualTo true
92-
response.content shouldNotBeEqualTo null
93-
}
96+
val response = client.get("/internal/is_alive")
97+
response.status.isSuccess() shouldBeEqualTo true
98+
response.bodyAsText() shouldNotBeEqualTo null
9499
}
100+
}
101+
it("Returns internal server error when readiness check fails") {
102+
testApplication {
103+
setupPodApi(
104+
database = databaseNotResponding,
105+
applicationState = ApplicationState(alive = true, ready = true)
106+
)
95107

96-
it("Returns internal server error when readiness check fails") {
97-
with(handleRequest(HttpMethod.Get, "/internal/is_ready")) {
98-
response.status() shouldBeEqualTo HttpStatusCode.InternalServerError
99-
response.content shouldNotBeEqualTo null
100-
}
108+
val response = client.get("/internal/is_ready")
109+
response.status shouldBeEqualTo HttpStatusCode.InternalServerError
110+
response.bodyAsText() shouldNotBeEqualTo null
101111
}
102112
}
103113
}

0 commit comments

Comments
 (0)