1
1
package no.nav.syfo.application.api
2
2
3
+ import io.ktor.client.request.*
4
+ import io.ktor.client.statement.*
3
5
import io.ktor.http.*
4
6
import io.ktor.server.routing.*
5
7
import io.ktor.server.testing.*
6
8
import no.nav.syfo.application.ApplicationState
9
+ import no.nav.syfo.application.database.DatabaseInterface
7
10
import no.nav.syfo.testhelper.TestDatabase
8
11
import no.nav.syfo.testhelper.TestDatabaseNotResponding
9
12
import org.amshove.kluent.shouldBeEqualTo
@@ -13,91 +16,98 @@ import org.spekframework.spek2.style.specification.describe
13
16
14
17
object PodApiSpek : Spek({
15
18
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 {
21
25
registerPodApi(
22
- ApplicationState (
23
- alive = true,
24
- ready = true
25
- ),
26
- database,
26
+ applicationState = applicationState,
27
+ database = database,
27
28
)
28
29
}
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
+ )
29
40
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
35
44
}
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
42
56
}
43
57
}
44
58
}
45
59
46
60
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)
57
66
)
58
- }
59
67
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
65
71
}
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
+ )
66
80
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
72
84
}
73
85
}
74
86
}
87
+
75
88
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)
86
94
)
87
- }
88
95
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
94
99
}
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
+ )
95
107
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
101
111
}
102
112
}
103
113
}
0 commit comments