22
33package com.trendyol.stove.testing.e2e.elasticsearch
44
5+ import arrow.core.Option
56import arrow.core.getOrElse
7+ import arrow.core.none
68import arrow.core.orElse
79import arrow.core.toOption
810import co.elastic.clients.elasticsearch.ElasticsearchClient
@@ -36,7 +38,6 @@ import kotlin.reflect.KClass
3638import kotlin.time.Duration.Companion.seconds
3739
3840data class ElasticClientConfigurer (
39- val restClientBuilder : RestClientBuilder .() -> Unit = {},
4041 val httpClientBuilder : HttpAsyncClientBuilder .() -> Unit = {
4142 setDefaultRequestConfig(
4243 RequestConfig .custom()
@@ -46,6 +47,7 @@ data class ElasticClientConfigurer(
4647 .build()
4748 )
4849 },
50+ val restClientOverrideFn : Option <(cfg: ElasticSearchExposedConfiguration ) -> RestClient > = none(),
4951)
5052
5153data class ElasticsearchSystemOptions (
@@ -215,7 +217,16 @@ class ElasticsearchSystem internal constructor(
215217 private fun createEsClient (
216218 exposedConfiguration : ElasticSearchExposedConfiguration ,
217219 sslContext : SSLContext ,
218- ): ElasticsearchClient {
220+ ): ElasticsearchClient =
221+ context.options.clientConfigurer.restClientOverrideFn
222+ .getOrElse { { cfg -> secureRestClient(cfg, sslContext) } }
223+ .let { RestClientTransport (it(exposedConfiguration), JacksonJsonpMapper (jacksonObjectMapper())) }
224+ .let { ElasticsearchClient (it) }
225+
226+ private fun secureRestClient (
227+ exposedConfiguration : ElasticSearchExposedConfiguration ,
228+ sslContext : SSLContext ,
229+ ): RestClient {
219230 val credentialsProvider: CredentialsProvider = BasicCredentialsProvider ()
220231 credentialsProvider.setCredentials(
221232 AuthScope .ANY ,
@@ -229,10 +240,7 @@ class ElasticsearchSystem internal constructor(
229240 clientBuilder.setDefaultCredentialsProvider(credentialsProvider)
230241 context.options.clientConfigurer.httpClientBuilder(clientBuilder)
231242 clientBuilder
232- }.also (context.options.clientConfigurer.restClientBuilder)
233- .build()
234- .let { RestClientTransport (it, JacksonJsonpMapper (jacksonObjectMapper())) }
235- .let { ElasticsearchClient (it) }
243+ }.build()
236244 }
237245
238246 companion object {
0 commit comments