Skip to content

Commit

Permalink
es support for index #118 (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan authored May 26, 2023
1 parent ef73302 commit 3eb7feb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@file:Suppress("UNCHECKED_CAST")

package com.trendyol.stove.testing.e2e.elasticsearch

import arrow.core.*
import arrow.core.getOrElse
import arrow.core.orElse
import arrow.core.toOption
import co.elastic.clients.elasticsearch.ElasticsearchClient
import co.elastic.clients.elasticsearch._types.Refresh
import co.elastic.clients.elasticsearch._types.query_dsl.Query
Expand All @@ -16,10 +16,10 @@ import com.trendyol.stove.testing.e2e.containers.ExposedCertificate
import com.trendyol.stove.testing.e2e.containers.NoCertificate
import com.trendyol.stove.testing.e2e.database.DocumentDatabaseSystem
import com.trendyol.stove.testing.e2e.system.TestSystem
import com.trendyol.stove.testing.e2e.system.abstractions.*
import javax.net.ssl.SSLContext
import kotlin.jvm.optionals.getOrElse
import kotlin.reflect.KClass
import com.trendyol.stove.testing.e2e.system.abstractions.AfterRunAware
import com.trendyol.stove.testing.e2e.system.abstractions.ExposesConfiguration
import com.trendyol.stove.testing.e2e.system.abstractions.RunAware
import com.trendyol.stove.testing.e2e.system.abstractions.StateOfSystem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.apache.http.HttpHost
Expand All @@ -28,9 +28,13 @@ import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.client.CredentialsProvider
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
import org.elasticsearch.client.*
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestClientBuilder
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import javax.net.ssl.SSLContext
import kotlin.jvm.optionals.getOrElse
import kotlin.reflect.KClass

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

fun <T : Any> shouldGet(
index: String,
key: String,
assertion: (T) -> Unit,
clazz: KClass<T>,
): ElasticsearchSystem {
require(index.isNotBlank()) { "Index cannot be blank" }
return esClient
.get({ req -> req.index(index).id(key).refresh(true) }, clazz.java)
.source().toOption()
.map(assertion)
.orElse { throw AssertionError("Resource with key ($key) is not found") }
.let { this }
}

override suspend fun shouldNotExist(key: String): ElasticsearchSystem {
val exists = esClient.exists { req -> req.index(context.index).id(key) }
if (exists.value()) {
Expand Down Expand Up @@ -200,5 +219,11 @@ class ElasticsearchSystem internal constructor(
query: Query,
noinline assertion: (List<T>) -> Unit,
): ElasticsearchSystem = this.shouldQuery(query, assertion, T::class)

inline fun <reified T : Any> ElasticsearchSystem.shouldGet(
index: String,
key: String,
noinline assertion: (T) -> Unit,
): ElasticsearchSystem = this.shouldGet(index, key, assertion, T::class)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.trendyol.stove.testing.e2e.database.DatabaseSystem.Companion.shouldQuery
import com.trendyol.stove.testing.e2e.database.DocumentDatabaseSystem.Companion.shouldGet
import com.trendyol.stove.testing.e2e.database.migrations.DatabaseMigration
import com.trendyol.stove.testing.e2e.elasticsearch.ElasticsearchSystem.Companion.shouldGet
import com.trendyol.stove.testing.e2e.elasticsearch.ElasticsearchSystem.Companion.shouldQuery
import com.trendyol.stove.testing.e2e.system.TestSystem
import com.trendyol.stove.testing.e2e.system.abstractions.ApplicationUnderTest
import com.trendyol.stove.testing.e2e.system.abstractions.ExperimentalStoveDsl
import io.kotest.core.config.AbstractProjectConfig
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.util.UUID
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 java.util.*

const val testIndex = "stove-test-index"
const val anotherIndex = "stove-another-index"

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

class AnotherIndexMigrator : DatabaseMigration<ElasticsearchClient> {
private val logger: Logger = LoggerFactory.getLogger(javaClass)
override suspend fun execute(connection: ElasticsearchClient) {
val createIndexRequest: CreateIndexRequest = CreateIndexRequest.Builder()
.index(anotherIndex)
.build()
connection.indices().create(createIndexRequest)
logger.info("$anotherIndex is created")
}
}

@ExperimentalStoveDsl
class Setup : AbstractProjectConfig() {
override suspend fun beforeProject(): Unit = TestSystem()
Expand All @@ -47,7 +60,7 @@ class Setup : AbstractProjectConfig() {
RestClient.builder(HttpHost(cfg.host, cfg.port)).build()
}
)
)
).migrations { register<AnotherIndexMigrator>() }
}
applicationUnderTest(NoOpApplication())
}.run()
Expand Down Expand Up @@ -82,6 +95,18 @@ class ElasticsearchTestSystemTests : FunSpec({
}
}

test("should save and get from another index") {
val exampleInstance = ExampleInstance("1", "1312")
TestSystem.validate {
elasticsearch {
save(anotherIndex, exampleInstance.id, exampleInstance)
shouldGet<ExampleInstance>(anotherIndex, exampleInstance.id) {
it.description shouldBe exampleInstance.description
}
}
}
}

test("should save 2 documents with the same description, then delete first one and query by description") {
val desc = "some description"
val exampleInstance1 = ExampleInstance("1", desc)
Expand Down

0 comments on commit 3eb7feb

Please sign in to comment.