Skip to content

Commit 3eb7feb

Browse files
authored
es support for index #118 (#119)
1 parent ef73302 commit 3eb7feb

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

lib/stove-testing-e2e-elasticsearch/src/main/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/ElasticsearchSystem.kt

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
@file:Suppress("UNCHECKED_CAST")
2-
31
package com.trendyol.stove.testing.e2e.elasticsearch
42

5-
import arrow.core.*
3+
import arrow.core.getOrElse
4+
import arrow.core.orElse
5+
import arrow.core.toOption
66
import co.elastic.clients.elasticsearch.ElasticsearchClient
77
import co.elastic.clients.elasticsearch._types.Refresh
88
import co.elastic.clients.elasticsearch._types.query_dsl.Query
@@ -16,10 +16,10 @@ import com.trendyol.stove.testing.e2e.containers.ExposedCertificate
1616
import com.trendyol.stove.testing.e2e.containers.NoCertificate
1717
import com.trendyol.stove.testing.e2e.database.DocumentDatabaseSystem
1818
import com.trendyol.stove.testing.e2e.system.TestSystem
19-
import com.trendyol.stove.testing.e2e.system.abstractions.*
20-
import javax.net.ssl.SSLContext
21-
import kotlin.jvm.optionals.getOrElse
22-
import kotlin.reflect.KClass
19+
import com.trendyol.stove.testing.e2e.system.abstractions.AfterRunAware
20+
import com.trendyol.stove.testing.e2e.system.abstractions.ExposesConfiguration
21+
import com.trendyol.stove.testing.e2e.system.abstractions.RunAware
22+
import com.trendyol.stove.testing.e2e.system.abstractions.StateOfSystem
2323
import kotlinx.coroutines.Dispatchers
2424
import kotlinx.coroutines.runBlocking
2525
import org.apache.http.HttpHost
@@ -28,9 +28,13 @@ import org.apache.http.auth.UsernamePasswordCredentials
2828
import org.apache.http.client.CredentialsProvider
2929
import org.apache.http.impl.client.BasicCredentialsProvider
3030
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
31-
import org.elasticsearch.client.*
31+
import org.elasticsearch.client.RestClient
32+
import org.elasticsearch.client.RestClientBuilder
3233
import org.slf4j.Logger
3334
import org.slf4j.LoggerFactory
35+
import javax.net.ssl.SSLContext
36+
import kotlin.jvm.optionals.getOrElse
37+
import kotlin.reflect.KClass
3438

3539
class ElasticsearchSystem internal constructor(
3640
override val testSystem: TestSystem,
@@ -111,6 +115,21 @@ class ElasticsearchSystem internal constructor(
111115
.orElse { throw AssertionError("Resource with key ($key) is not found") }
112116
.let { this }
113117

118+
fun <T : Any> shouldGet(
119+
index: String,
120+
key: String,
121+
assertion: (T) -> Unit,
122+
clazz: KClass<T>,
123+
): ElasticsearchSystem {
124+
require(index.isNotBlank()) { "Index cannot be blank" }
125+
return esClient
126+
.get({ req -> req.index(index).id(key).refresh(true) }, clazz.java)
127+
.source().toOption()
128+
.map(assertion)
129+
.orElse { throw AssertionError("Resource with key ($key) is not found") }
130+
.let { this }
131+
}
132+
114133
override suspend fun shouldNotExist(key: String): ElasticsearchSystem {
115134
val exists = esClient.exists { req -> req.index(context.index).id(key) }
116135
if (exists.value()) {
@@ -200,5 +219,11 @@ class ElasticsearchSystem internal constructor(
200219
query: Query,
201220
noinline assertion: (List<T>) -> Unit,
202221
): ElasticsearchSystem = this.shouldQuery(query, assertion, T::class)
222+
223+
inline fun <reified T : Any> ElasticsearchSystem.shouldGet(
224+
index: String,
225+
key: String,
226+
noinline assertion: (T) -> Unit,
227+
): ElasticsearchSystem = this.shouldGet(index, key, assertion, T::class)
203228
}
204229
}

lib/stove-testing-e2e-elasticsearch/src/test/kotlin/com/trendyol/stove/testing/e2e/elasticsearch/ElasticsearchTestSystemTests.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties
88
import com.trendyol.stove.testing.e2e.database.DatabaseSystem.Companion.shouldQuery
99
import com.trendyol.stove.testing.e2e.database.DocumentDatabaseSystem.Companion.shouldGet
1010
import com.trendyol.stove.testing.e2e.database.migrations.DatabaseMigration
11+
import com.trendyol.stove.testing.e2e.elasticsearch.ElasticsearchSystem.Companion.shouldGet
1112
import com.trendyol.stove.testing.e2e.elasticsearch.ElasticsearchSystem.Companion.shouldQuery
1213
import com.trendyol.stove.testing.e2e.system.TestSystem
1314
import com.trendyol.stove.testing.e2e.system.abstractions.ApplicationUnderTest
1415
import com.trendyol.stove.testing.e2e.system.abstractions.ExperimentalStoveDsl
1516
import io.kotest.core.config.AbstractProjectConfig
1617
import io.kotest.core.spec.style.FunSpec
1718
import io.kotest.matchers.shouldBe
18-
import java.util.UUID
1919
import org.apache.http.HttpHost
2020
import org.elasticsearch.client.RestClient
2121
import org.junit.jupiter.api.assertThrows
2222
import org.slf4j.Logger
2323
import org.slf4j.LoggerFactory
24+
import java.util.*
2425

2526
const val testIndex = "stove-test-index"
27+
const val anotherIndex = "stove-another-index"
2628

2729
class TestIndexMigrator : DatabaseMigration<ElasticsearchClient> {
2830
private val logger: Logger = LoggerFactory.getLogger(javaClass)
@@ -35,6 +37,17 @@ class TestIndexMigrator : DatabaseMigration<ElasticsearchClient> {
3537
}
3638
}
3739

40+
class AnotherIndexMigrator : DatabaseMigration<ElasticsearchClient> {
41+
private val logger: Logger = LoggerFactory.getLogger(javaClass)
42+
override suspend fun execute(connection: ElasticsearchClient) {
43+
val createIndexRequest: CreateIndexRequest = CreateIndexRequest.Builder()
44+
.index(anotherIndex)
45+
.build()
46+
connection.indices().create(createIndexRequest)
47+
logger.info("$anotherIndex is created")
48+
}
49+
}
50+
3851
@ExperimentalStoveDsl
3952
class Setup : AbstractProjectConfig() {
4053
override suspend fun beforeProject(): Unit = TestSystem()
@@ -47,7 +60,7 @@ class Setup : AbstractProjectConfig() {
4760
RestClient.builder(HttpHost(cfg.host, cfg.port)).build()
4861
}
4962
)
50-
)
63+
).migrations { register<AnotherIndexMigrator>() }
5164
}
5265
applicationUnderTest(NoOpApplication())
5366
}.run()
@@ -82,6 +95,18 @@ class ElasticsearchTestSystemTests : FunSpec({
8295
}
8396
}
8497

98+
test("should save and get from another index") {
99+
val exampleInstance = ExampleInstance("1", "1312")
100+
TestSystem.validate {
101+
elasticsearch {
102+
save(anotherIndex, exampleInstance.id, exampleInstance)
103+
shouldGet<ExampleInstance>(anotherIndex, exampleInstance.id) {
104+
it.description shouldBe exampleInstance.description
105+
}
106+
}
107+
}
108+
}
109+
85110
test("should save 2 documents with the same description, then delete first one and query by description") {
86111
val desc = "some description"
87112
val exampleInstance1 = ExampleInstance("1", desc)

0 commit comments

Comments
 (0)