1- @file:Suppress(" UNCHECKED_CAST" )
2-
31package 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
66import co.elastic.clients.elasticsearch.ElasticsearchClient
77import co.elastic.clients.elasticsearch._types.Refresh
88import co.elastic.clients.elasticsearch._types.query_dsl.Query
@@ -16,10 +16,10 @@ import com.trendyol.stove.testing.e2e.containers.ExposedCertificate
1616import com.trendyol.stove.testing.e2e.containers.NoCertificate
1717import com.trendyol.stove.testing.e2e.database.DocumentDatabaseSystem
1818import 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
2323import kotlinx.coroutines.Dispatchers
2424import kotlinx.coroutines.runBlocking
2525import org.apache.http.HttpHost
@@ -28,9 +28,13 @@ import org.apache.http.auth.UsernamePasswordCredentials
2828import org.apache.http.client.CredentialsProvider
2929import org.apache.http.impl.client.BasicCredentialsProvider
3030import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
31- import org.elasticsearch.client.*
31+ import org.elasticsearch.client.RestClient
32+ import org.elasticsearch.client.RestClientBuilder
3233import org.slf4j.Logger
3334import org.slf4j.LoggerFactory
35+ import javax.net.ssl.SSLContext
36+ import kotlin.jvm.optionals.getOrElse
37+ import kotlin.reflect.KClass
3438
3539class 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}
0 commit comments