From 206176425a834b7095546a070bdc75e51664c0c1 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 9 Sep 2021 15:11:54 +0100 Subject: [PATCH] Broaden testing of ReactiveElasticsearchRestClientAutoConfiguration Closes gh-27919 --- ...earchRestClientAutoConfigurationTests.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java index d46fa13dc4..1de73382c0 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java @@ -16,6 +16,11 @@ package org.springframework.boot.autoconfigure.data.elasticsearch; +import java.net.InetSocketAddress; +import java.time.Duration; +import java.util.List; + +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -24,6 +29,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; +import org.springframework.http.HttpHeaders; +import org.springframework.http.codec.CodecConfigurer.DefaultCodecConfig; +import org.springframework.web.reactive.function.client.WebClient; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -57,6 +65,86 @@ class ReactiveElasticsearchRestClientAutoConfigurationTests { .hasSingleBean(ClientConfiguration.class).hasBean("customClientConfiguration")); } + @Test + void whenEndpointIsCustomizedThenClientConfigurationHasCustomEndpoint() { + this.contextRunner.withPropertyValues("spring.data.elasticsearch.client.reactive.endpoints=localhost:9876") + .run((context) -> { + List endpoints = context.getBean(ClientConfiguration.class).getEndpoints(); + assertThat(endpoints).hasSize(1); + assertThat(endpoints.get(0).getHostString()).isEqualTo("localhost"); + assertThat(endpoints.get(0).getPort()).isEqualTo(9876); + }); + } + + @Test + void whenMultipleEndpointsAreConfiguredThenClientConfigurationHasMultipleEndpoints() { + this.contextRunner + .withPropertyValues("spring.data.elasticsearch.client.reactive.endpoints=localhost:9876,localhost:8765") + .run((context) -> { + List endpoints = context.getBean(ClientConfiguration.class).getEndpoints(); + assertThat(endpoints).hasSize(2); + assertThat(endpoints.get(0).getHostString()).isEqualTo("localhost"); + assertThat(endpoints.get(0).getPort()).isEqualTo(9876); + assertThat(endpoints.get(1).getHostString()).isEqualTo("localhost"); + assertThat(endpoints.get(1).getPort()).isEqualTo(8765); + }); + } + + @Test + void whenConfiguredToUseSslThenClientConfigurationUsesSsl() { + this.contextRunner.withPropertyValues("spring.data.elasticsearch.client.reactive.use-ssl=true") + .run((context) -> assertThat(context.getBean(ClientConfiguration.class).useSsl()).isTrue()); + } + + @Test + void whenSocketTimeoutIsNotConfiguredThenClientConfigurationUsesDefault() { + this.contextRunner.run((context) -> assertThat(context.getBean(ClientConfiguration.class).getSocketTimeout()) + .isEqualTo(Duration.ofSeconds(5))); + } + + @Test + void whenConnectionTimeoutIsNotConfiguredThenClientConfigurationUsesDefault() { + this.contextRunner.run((context) -> assertThat(context.getBean(ClientConfiguration.class).getConnectTimeout()) + .isEqualTo(Duration.ofSeconds(10))); + } + + @Test + void whenSocketTimeoutIsConfiguredThenClientConfigurationHasCustomSocketTimeout() { + this.contextRunner.withPropertyValues("spring.data.elasticsearch.client.reactive.socket-timeout=2s") + .run((context) -> assertThat(context.getBean(ClientConfiguration.class).getSocketTimeout()) + .isEqualTo(Duration.ofSeconds(2))); + } + + @Test + void whenConnectionTimeoutIsConfiguredThenClientConfigurationHasCustomConnectTimeout() { + this.contextRunner.withPropertyValues("spring.data.elasticsearch.client.reactive.connection-timeout=2s") + .run((context) -> assertThat(context.getBean(ClientConfiguration.class).getConnectTimeout()) + .isEqualTo(Duration.ofSeconds(2))); + } + + @Test + void whenCredentialsAreConfiguredThenClientConfigurationHasDefaultAuthorizationHeader() { + this.contextRunner + .withPropertyValues("spring.data.elasticsearch.client.reactive.username=alice", + "spring.data.elasticsearch.client.reactive.password=secret") + .run((context) -> assertThat( + context.getBean(ClientConfiguration.class).getDefaultHeaders().get(HttpHeaders.AUTHORIZATION)) + .containsExactly("Basic YWxpY2U6c2VjcmV0")); + } + + @Test + void whenMaxInMemorySizeIsConfiguredThenUnderlyingWebClientHasCustomMaxInMemorySize() { + this.contextRunner.withPropertyValues("spring.data.elasticsearch.client.reactive.max-in-memory-size=1MB") + .run((context) -> { + WebClient client = context.getBean(ClientConfiguration.class).getWebClientConfigurer() + .apply(WebClient.create()); + assertThat(client).extracting("exchangeFunction").extracting("strategies") + .extracting("codecConfigurer").extracting("defaultCodecs") + .asInstanceOf(InstanceOfAssertFactories.type(DefaultCodecConfig.class)) + .extracting(DefaultCodecConfig::maxInMemorySize).isEqualTo(1024 * 1024); + }); + } + @Configuration(proxyBeanMethods = false) static class CustomClientConfiguration {