1
1
package no.java.conf
2
2
3
+ import io.kotest.core.spec.style.FunSpec
3
4
import io.ktor.client.request.get
4
5
import io.ktor.client.statement.bodyAsText
5
6
import io.ktor.http.HttpStatusCode
7
+ import io.ktor.server.testing.ApplicationTestBuilder
6
8
import io.ktor.server.testing.testApplication
7
9
import io.mockk.coEvery
8
10
import io.mockk.just
@@ -14,150 +16,127 @@ import no.java.conf.service.SearchService
14
16
import no.java.conf.service.search.ElasticIndexer
15
17
import no.java.conf.service.search.ElasticIngester
16
18
import no.java.conf.service.search.ElasticSearcher
17
- import kotlin.test.Test
18
19
import kotlin.test.assertEquals
19
20
20
- class StateTest {
21
- @Test
22
- fun testNew () {
23
- val indexer = mockk<ElasticIndexer >()
24
- val ingester = mockk<ElasticIngester >()
25
- val searcher = mockk<ElasticSearcher >()
21
+ class StateTest :
22
+ FunSpec ({
23
+ test(" test new" ) {
24
+ testApplication {
25
+ buildTestApplication(skipIndex = false )
26
26
27
- val service = SearchService (indexer, ingester, searcher, false )
28
-
29
- testApplication {
30
- application {
31
- configureSearchRouting(service)
32
- }
33
-
34
- client.get(" /api/search/state" ).apply {
35
- assertEquals(HttpStatusCode .OK , status)
36
- assertEquals(bodyAsText(), " NEW" )
27
+ client.get(" /api/search/state" ).apply {
28
+ assertEquals(HttpStatusCode .OK , status)
29
+ assertEquals(bodyAsText(), " NEW" )
30
+ }
37
31
}
38
32
}
39
- }
40
33
41
- @Test
42
- fun testMapped () {
43
- val indexer = mockk<ElasticIndexer >()
44
- val ingester = mockk<ElasticIngester >()
45
- val searcher = mockk<ElasticSearcher >()
34
+ test(" test mapped" ) {
35
+ val indexer = mockk<ElasticIndexer >()
46
36
47
- val service = SearchService (indexer, ingester, searcher, false )
37
+ coEvery { indexer.recreateIndex(any()) } just runs
48
38
49
- coEvery { indexer.recreateIndex(any()) } just runs
39
+ testApplication {
40
+ buildTestApplication(indexer = indexer, skipIndex = false ) { service ->
41
+ service.setup()
42
+ }
50
43
51
- runBlocking {
52
- service.setup()
53
- }
54
-
55
- testApplication {
56
- application {
57
- configureSearchRouting(service)
58
- }
59
-
60
- client.get(" /api/search/state" ).apply {
61
- assertEquals(HttpStatusCode .OK , status)
62
- assertEquals(bodyAsText(), " MAPPED" )
44
+ client.get(" /api/search/state" ).apply {
45
+ assertEquals(HttpStatusCode .OK , status)
46
+ assertEquals(bodyAsText(), " MAPPED" )
47
+ }
63
48
}
64
49
}
65
- }
66
50
67
- @Test
68
- fun testIndexed () {
69
- val indexer = mockk<ElasticIndexer >()
70
- val ingester = mockk<ElasticIngester >()
71
- val searcher = mockk<ElasticSearcher >()
51
+ test(" test indexed" ) {
52
+ val indexer = mockk<ElasticIndexer >()
53
+ val ingester = mockk<ElasticIngester >()
72
54
73
- val service = SearchService (indexer, ingester, searcher, false )
55
+ coEvery { indexer.recreateIndex(any()) } just runs
56
+ coEvery { ingester.ingest(any(), any()) } just runs
74
57
75
- coEvery { indexer.recreateIndex(any()) } just runs
76
- coEvery { ingester.ingest(any(), any()) } just runs
58
+ testApplication {
59
+ buildTestApplication(indexer = indexer, ingester = ingester, skipIndex = false ) { service ->
60
+ service.setup()
61
+ service.ingest(emptyList())
62
+ }
77
63
78
- runBlocking {
79
- service.setup()
80
- service.ingest(emptyList())
64
+ client.get(" /api/search/state" ).apply {
65
+ assertEquals(HttpStatusCode .OK , status)
66
+ assertEquals(bodyAsText(), " INDEXED" )
67
+ }
68
+ }
81
69
}
82
70
83
- testApplication {
84
- application {
85
- configureSearchRouting(service)
86
- }
71
+ test(" test skip index new" ) {
72
+ testApplication {
73
+ buildTestApplication(skipIndex = true )
87
74
88
- client.get(" /api/search/state" ).apply {
89
- assertEquals(HttpStatusCode .OK , status)
90
- assertEquals(bodyAsText(), " INDEXED" )
75
+ client.get(" /api/search/state" ).apply {
76
+ assertEquals(HttpStatusCode .OK , status)
77
+ assertEquals(bodyAsText(), " NEW" )
78
+ }
91
79
}
92
80
}
93
- }
94
81
95
- @Test
96
- fun testSkipIndexNew () {
97
- val indexer = mockk<ElasticIndexer >()
98
- val ingester = mockk<ElasticIngester >()
99
- val searcher = mockk<ElasticSearcher >()
82
+ test(" test skip index mapped" ) {
83
+ val indexer = mockk<ElasticIndexer >()
100
84
101
- val service = SearchService (indexer, ingester, searcher, true )
85
+ coEvery { indexer.recreateIndex(any()) } just runs
102
86
103
- testApplication {
104
- application {
105
- configureSearchRouting( service)
106
- }
87
+ testApplication {
88
+ buildTestApplication(indexer = indexer, skipIndex = false ) { service ->
89
+ service.setup( )
90
+ }
107
91
108
- client.get(" /api/search/state" ).apply {
109
- assertEquals(HttpStatusCode .OK , status)
110
- assertEquals(bodyAsText(), " NEW" )
92
+ client.get(" /api/search/state" ).apply {
93
+ assertEquals(HttpStatusCode .OK , status)
94
+ assertEquals(bodyAsText(), " MAPPED" )
95
+ }
111
96
}
112
97
}
113
- }
114
-
115
- @Test
116
- fun testSkipIndexMapped () {
117
- val indexer = mockk<ElasticIndexer >()
118
- val ingester = mockk<ElasticIngester >()
119
- val searcher = mockk<ElasticSearcher >()
120
98
121
- val service = SearchService (indexer, ingester, searcher, true )
99
+ test(" test skip index indexed" ) {
100
+ val indexer = mockk<ElasticIndexer >()
101
+ val ingester = mockk<ElasticIngester >()
122
102
123
- runBlocking {
124
- service.setup()
125
- }
103
+ coEvery { indexer.recreateIndex(any()) } just runs
104
+ coEvery { ingester.ingest(any(), any()) } just runs
126
105
127
- testApplication {
128
- application {
129
- configureSearchRouting(service)
130
- }
106
+ testApplication {
107
+ buildTestApplication(indexer = indexer, ingester = ingester, skipIndex = false ) { service ->
108
+ service.setup()
109
+ service.ingest(emptyList())
110
+ }
131
111
132
- client.get(" /api/search/state" ).apply {
133
- assertEquals(HttpStatusCode .OK , status)
134
- assertEquals(bodyAsText(), " MAPPED" )
112
+ client.get(" /api/search/state" ).apply {
113
+ assertEquals(HttpStatusCode .OK , status)
114
+ assertEquals(bodyAsText(), " INDEXED" )
115
+ }
135
116
}
136
117
}
118
+ })
119
+
120
+ private fun ApplicationTestBuilder.buildTestApplication (
121
+ indexer : ElasticIndexer = mockk(),
122
+ ingester : ElasticIngester = mockk(),
123
+ searcher : ElasticSearcher = mockk(),
124
+ skipIndex : Boolean = false,
125
+ block : (suspend (service: SearchService ) -> Unit )? = null,
126
+ ) {
127
+ val service =
128
+ SearchService (
129
+ indexer = indexer,
130
+ ingester = ingester,
131
+ searcher = searcher,
132
+ skipIndex = skipIndex
133
+ )
134
+
135
+ runBlocking {
136
+ block?.invoke(service)
137
137
}
138
138
139
- @Test
140
- fun testSkipIndexIndexed () {
141
- val indexer = mockk<ElasticIndexer >()
142
- val ingester = mockk<ElasticIngester >()
143
- val searcher = mockk<ElasticSearcher >()
144
-
145
- val service = SearchService (indexer, ingester, searcher, true )
146
-
147
- runBlocking {
148
- service.setup()
149
- service.ingest(emptyList())
150
- }
151
-
152
- testApplication {
153
- application {
154
- configureSearchRouting(service)
155
- }
156
-
157
- client.get(" /api/search/state" ).apply {
158
- assertEquals(HttpStatusCode .OK , status)
159
- assertEquals(bodyAsText(), " INDEXED" )
160
- }
161
- }
139
+ application {
140
+ configureSearchRouting(service)
162
141
}
163
142
}
0 commit comments