11package no.java.conf
22
3+ import io.kotest.core.spec.style.FunSpec
34import io.ktor.client.request.get
45import io.ktor.client.statement.bodyAsText
56import io.ktor.http.HttpStatusCode
7+ import io.ktor.server.testing.ApplicationTestBuilder
68import io.ktor.server.testing.testApplication
79import io.mockk.coEvery
810import io.mockk.just
@@ -14,150 +16,127 @@ import no.java.conf.service.SearchService
1416import no.java.conf.service.search.ElasticIndexer
1517import no.java.conf.service.search.ElasticIngester
1618import no.java.conf.service.search.ElasticSearcher
17- import kotlin.test.Test
1819import kotlin.test.assertEquals
1920
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 )
2626
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+ }
3731 }
3832 }
39- }
4033
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 >()
4636
47- val service = SearchService (indexer, ingester, searcher, false )
37+ coEvery { indexer.recreateIndex(any()) } just runs
4838
49- coEvery { indexer.recreateIndex(any()) } just runs
39+ testApplication {
40+ buildTestApplication(indexer = indexer, skipIndex = false ) { service ->
41+ service.setup()
42+ }
5043
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+ }
6348 }
6449 }
65- }
6650
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 >()
7254
73- val service = SearchService (indexer, ingester, searcher, false )
55+ coEvery { indexer.recreateIndex(any()) } just runs
56+ coEvery { ingester.ingest(any(), any()) } just runs
7457
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+ }
7763
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+ }
8169 }
8270
83- testApplication {
84- application {
85- configureSearchRouting(service)
86- }
71+ test(" test skip index new" ) {
72+ testApplication {
73+ buildTestApplication(skipIndex = true )
8774
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+ }
9179 }
9280 }
93- }
9481
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 >()
10084
101- val service = SearchService (indexer, ingester, searcher, true )
85+ coEvery { indexer.recreateIndex(any()) } just runs
10286
103- testApplication {
104- application {
105- configureSearchRouting( service)
106- }
87+ testApplication {
88+ buildTestApplication(indexer = indexer, skipIndex = false ) { service ->
89+ service.setup( )
90+ }
10791
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+ }
11196 }
11297 }
113- }
114-
115- @Test
116- fun testSkipIndexMapped () {
117- val indexer = mockk<ElasticIndexer >()
118- val ingester = mockk<ElasticIngester >()
119- val searcher = mockk<ElasticSearcher >()
12098
121- val service = SearchService (indexer, ingester, searcher, true )
99+ test(" test skip index indexed" ) {
100+ val indexer = mockk<ElasticIndexer >()
101+ val ingester = mockk<ElasticIngester >()
122102
123- runBlocking {
124- service.setup()
125- }
103+ coEvery { indexer.recreateIndex(any()) } just runs
104+ coEvery { ingester.ingest(any(), any()) } just runs
126105
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+ }
131111
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+ }
135116 }
136117 }
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)
137137 }
138138
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)
162141 }
163142}
0 commit comments