Skip to content

Commit

Permalink
elastic-search: remove default index migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Jun 24, 2024
1 parent d985a83 commit 90c2df7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import kotlin.jvm.optionals.getOrElse
@ElasticDsl
class ElasticsearchSystem internal constructor(
override val testSystem: TestSystem,
val context: ElasticsearchContext
private val context: ElasticsearchContext
) : PluggedSystem, RunAware, AfterRunAware, ExposesConfiguration {
@PublishedApi
internal lateinit var esClient: ElasticsearchClient
Expand Down Expand Up @@ -67,34 +67,35 @@ class ElasticsearchSystem internal constructor(
@ElasticDsl
inline fun <reified T : Any> shouldQuery(
query: String,
index: String,
assertion: (List<T>) -> Unit
): ElasticsearchSystem =
esClient.search(
SearchRequest.of { req ->
req.index(context.index).query { q -> q.withJson(query.reader()) }
},
): ElasticsearchSystem {
require(index.isNotBlank()) { "Index cannot be blank" }
require(query.isNotBlank()) { "Query cannot be blank" }
return esClient.search(
SearchRequest.of { req -> req.index(index).query { q -> q.withJson(query.reader()) } },
T::class.java
).hits().hits()
.mapNotNull { it.source() }
.also(assertion)
.let { this }
}

@ElasticDsl
inline fun <reified T : Any> shouldQuery(
query: Query,
assertion: (List<T>) -> Unit
): ElasticsearchSystem =
esClient.search(
SearchRequest.of { q -> q.query(query) },
T::class.java
).hits().hits()
.mapNotNull { it.source() }
.also(assertion)
.let { this }
): ElasticsearchSystem = esClient.search(
SearchRequest.of { q -> q.query(query) },
T::class.java
).hits().hits()
.mapNotNull { it.source() }
.also(assertion)
.let { this }

@ElasticDsl
inline fun <reified T : Any> shouldGet(
index: String = context.index,
index: String,
key: String,
assertion: (T) -> Unit
): ElasticsearchSystem {
Expand All @@ -110,9 +111,10 @@ class ElasticsearchSystem internal constructor(
@ElasticDsl
fun shouldNotExist(
key: String,
onIndex: String = context.index
index: String
): ElasticsearchSystem {
val exists = esClient.exists { req -> req.index(onIndex).id(key) }
require(index.isNotBlank()) { "Index cannot be blank" }
val exists = esClient.exists { req -> req.index(index).id(key) }
if (exists.value()) {
throw AssertionError("The document with the given id($key) was not expected, but found!")
}
Expand All @@ -122,20 +124,19 @@ class ElasticsearchSystem internal constructor(
@ElasticDsl
fun shouldDelete(
key: String,
fromIndex: String = context.index
): ElasticsearchSystem =
esClient
.delete(DeleteRequest.of { req -> req.index(fromIndex).id(key).refresh(Refresh.WaitFor) })
.let { this }
index: String
): ElasticsearchSystem = esClient
.delete(DeleteRequest.of { req -> req.index(index).id(key).refresh(Refresh.WaitFor) })
.let { this }

@ElasticDsl
fun <T : Any> save(
id: String,
instance: T,
toIndex: String = context.index
index: String
): ElasticsearchSystem =
esClient.index { req ->
req.index(toIndex)
req.index(index)
.id(id)
.document(instance)
.refresh(Refresh.WaitFor)
Expand All @@ -155,20 +156,14 @@ class ElasticsearchSystem internal constructor(
@ElasticDsl
fun unpause(): ElasticsearchSystem = context.container.unpause().let { this }

override fun close(): Unit =
runBlocking(context = Dispatchers.IO) {
Try {
esClient._transport().close()
executeWithReuseCheck { stop() }
}.recover { logger.warn("got an error while stopping elasticsearch: ${it.message}") }
}
override fun close(): Unit = runBlocking(context = Dispatchers.IO) {
Try {
esClient._transport().close()
executeWithReuseCheck { stop() }
}.recover { logger.warn("got an error while stopping elasticsearch: ${it.message}") }
}

override fun configuration(): List<String> =
context.options.configureExposedConfiguration(exposedConfiguration) +
listOf(
"elasticsearch.host=${exposedConfiguration.host}",
"elasticsearch.port=${exposedConfiguration.port}"
)
override fun configuration(): List<String> = context.options.configureExposedConfiguration(exposedConfiguration)

private fun createEsClient(exposedConfiguration: ElasticSearchExposedConfiguration): ElasticsearchClient =
context.options.clientConfigurer.restClientOverrideFn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import com.trendyol.stove.testing.e2e.system.annotations.StoveDsl
* You can configure it by changing the implementation of migrator.
*/
internal fun TestSystem.withElasticsearch(options: ElasticsearchSystemOptions): TestSystem {
options.migrations {
register<DefaultIndexMigrator> { options.defaultIndex.migrator }
}

options.objectMapper.registerArrowModule()

return withProvidedRegistry(
Expand All @@ -34,7 +30,7 @@ internal fun TestSystem.withElasticsearch(options: ElasticsearchSystemOptions):
withReuse(this@withElasticsearch.options.keepDependenciesRunning)
options.container.containerFn(this)
}
.let { getOrRegister(ElasticsearchSystem(this, ElasticsearchContext(options.defaultIndex.index, it, options))) }
.let { getOrRegister(ElasticsearchSystem(this, ElasticsearchContext(it, options))) }
.let { this }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import kotlin.time.Duration.Companion.minutes

@StoveDsl
data class ElasticsearchSystemOptions(
val defaultIndex: DefaultIndex,
val clientConfigurer: ElasticClientConfigurer = ElasticClientConfigurer(),
val container: ElasticContainerOptions = ElasticContainerOptions(),
val objectMapper: ObjectMapper = StoveObjectMapper.Default,
Expand Down Expand Up @@ -46,7 +45,6 @@ data class ElasticSearchExposedConfiguration(
) : ExposedConfiguration

data class ElasticsearchContext(
val index: String,
val container: StoveElasticSearchContainer,
val options: ElasticsearchSystemOptions
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.trendyol.stove.testing.e2e.database.migrations.DatabaseMigration
import com.trendyol.stove.testing.e2e.database.migrations.MigrationPriority
import com.trendyol.stove.testing.e2e.database.migrations.*
import com.trendyol.stove.testing.e2e.system.TestSystem
import com.trendyol.stove.testing.e2e.system.abstractions.ApplicationUnderTest
import io.kotest.core.config.AbstractProjectConfig
Expand All @@ -15,8 +14,7 @@ import io.kotest.matchers.shouldBe
import org.apache.http.HttpHost
import org.elasticsearch.client.RestClient
import org.junit.jupiter.api.assertThrows
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.slf4j.*
import java.util.*

const val TEST_INDEX = "stove-test-index"
Expand Down Expand Up @@ -56,7 +54,6 @@ class Setup : AbstractProjectConfig() {
.with {
elasticsearch {
ElasticsearchSystemOptions(
DefaultIndex(index = TEST_INDEX, migrator = TestIndexMigrator()),
clientConfigurer =
ElasticClientConfigurer(
restClientOverrideFn =
Expand All @@ -65,7 +62,10 @@ class Setup : AbstractProjectConfig() {
}
),
ElasticContainerOptions(tag = "8.9.0")
).migrations { register<AnotherIndexMigrator>() }
).migrations {
register<TestIndexMigrator>()
register<AnotherIndexMigrator>()
}
}
applicationUnderTest(NoOpApplication())
}.run()
Expand All @@ -90,8 +90,8 @@ class ElasticsearchTestSystemTests : FunSpec({
val exampleInstance = ExampleInstance("1", "1312")
TestSystem.validate {
elasticsearch {
save(exampleInstance.id, exampleInstance)
shouldGet<ExampleInstance>(key = exampleInstance.id) {
save(exampleInstance.id, exampleInstance, TEST_INDEX)
shouldGet<ExampleInstance>(key = exampleInstance.id, index = TEST_INDEX) {
it.description shouldBe exampleInstance.description
}
}
Expand All @@ -118,14 +118,14 @@ class ElasticsearchTestSystemTests : FunSpec({
val queryAsString = queryByDesc.asJsonString()
TestSystem.validate {
elasticsearch {
save(exampleInstance1.id, exampleInstance1)
save(exampleInstance2.id, exampleInstance2)
save(exampleInstance1.id, exampleInstance1, TEST_INDEX)
save(exampleInstance2.id, exampleInstance2, TEST_INDEX)
shouldQuery<ExampleInstance>(queryByDesc._toQuery()) {
it.size shouldBe 2
}
shouldDelete(exampleInstance1.id)
shouldGet<ExampleInstance>(key = exampleInstance2.id) {}
shouldQuery<ExampleInstance>(queryAsString) {
shouldDelete(exampleInstance1.id, TEST_INDEX)
shouldGet<ExampleInstance>(key = exampleInstance2.id, index = TEST_INDEX) {}
shouldQuery<ExampleInstance>(queryAsString, TEST_INDEX) {
it.size shouldBe 1
}
}
Expand All @@ -137,12 +137,12 @@ class ElasticsearchTestSystemTests : FunSpec({
val exampleInstance = ExampleInstance(existDocId, "1312")
TestSystem.validate {
elasticsearch {
save(exampleInstance.id, exampleInstance)
shouldGet<ExampleInstance>(key = exampleInstance.id) {
save(exampleInstance.id, exampleInstance, TEST_INDEX)
shouldGet<ExampleInstance>(key = exampleInstance.id, index = TEST_INDEX) {
it.description shouldBe exampleInstance.description
}

assertThrows<AssertionError> { shouldNotExist(existDocId) }
assertThrows<AssertionError> { shouldNotExist(existDocId, index = TEST_INDEX) }
}
}
}
Expand All @@ -151,7 +151,7 @@ class ElasticsearchTestSystemTests : FunSpec({
val notExistDocId = UUID.randomUUID().toString()
TestSystem.validate {
elasticsearch {
shouldNotExist(notExistDocId)
shouldNotExist(notExistDocId, index = TEST_INDEX)
}
}
}
Expand Down

0 comments on commit 90c2df7

Please sign in to comment.