Polish "Bring back Elasticsearch RestClient auto-configuration"

See gh-28496
This commit is contained in:
Andy Wilkinson
2022-04-12 19:31:23 +01:00
parent eb3bf40bdb
commit a7a71da9ef
13 changed files with 88 additions and 384 deletions

View File

@@ -23,8 +23,8 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientBuilderConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientFromRestHighLevelClientConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientSnifferConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientWithRestHighLevelClientConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestHighLevelClientConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;
@@ -42,7 +42,7 @@ import org.springframework.context.annotation.Import;
@EnableConfigurationProperties({ ElasticsearchProperties.class, ElasticsearchRestClientProperties.class,
DeprecatedElasticsearchRestClientProperties.class })
@Import({ RestClientBuilderConfiguration.class, RestHighLevelClientConfiguration.class,
RestClientWithRestHighLevelClientConfiguration.class, RestClientConfiguration.class,
RestClientFromRestHighLevelClientConfiguration.class, RestClientConfiguration.class,
RestClientSnifferConfiguration.class })
public class ElasticsearchRestClientAutoConfiguration {

View File

@@ -131,7 +131,7 @@ class ElasticsearchRestClientConfigurations {
@ConditionalOnClass(org.elasticsearch.client.RestHighLevelClient.class)
@ConditionalOnSingleCandidate(org.elasticsearch.client.RestHighLevelClient.class)
@ConditionalOnMissingBean(RestClient.class)
static class RestClientWithRestHighLevelClientConfiguration {
static class RestClientFromRestHighLevelClientConfiguration {
@Bean
RestClient elasticsearchRestClient(org.elasticsearch.client.RestHighLevelClient restHighLevelClient) {

View File

@@ -35,7 +35,6 @@ import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
@@ -97,24 +96,4 @@ class ElasticsearchRestClientAutoConfigurationIntegrationTests {
});
}
@Test
@SuppressWarnings("deprecation")
void restClientCanQueryElasticsearchNodeWithoutHighLevelClient() {
this.contextRunner.withClassLoader(new FilteredClassLoader(org.elasticsearch.client.RestHighLevelClient.class))
.withPropertyValues("spring.elasticsearch.uris=" + elasticsearch.getHttpHostAddress(),
"spring.elasticsearch.connection-timeout=120s", "spring.elasticsearch.socket-timeout=120s")
.run((context) -> {
RestClient client = context.getBean(RestClient.class);
Request index = new Request("PUT", "/test/_doc/3");
index.setJsonEntity("{" + " \"a\": \"alpha\"," + " \"b\": \"bravo\"" + "}");
client.performRequest(index);
Request getRequest = new Request("GET", "/test/_doc/3");
Response response = client.performRequest(getRequest);
try (InputStream input = response.getEntity().getContent()) {
JsonNode result = new ObjectMapper().readTree(input);
assertThat(result.path("found").asBoolean()).isTrue();
}
});
}
}

View File

@@ -21,7 +21,6 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.time.Duration;
import java.util.Map;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
@@ -55,6 +54,7 @@ import static org.mockito.Mockito.mock;
* @author Vedran Pavic
* @author Evgeniy Cheban
* @author Filip Hrisafov
* @author Andy Wilkinson
*/
@SuppressWarnings("deprecation")
class ElasticsearchRestClientAutoConfigurationTests {
@@ -63,7 +63,7 @@ class ElasticsearchRestClientAutoConfigurationTests {
.withConfiguration(AutoConfigurations.of(ElasticsearchRestClientAutoConfiguration.class));
@Test
void configureShouldCreateHighLevelAndLowLevelRestClient() {
void configureShouldCreateHighLevelAndLowLevelRestClients() {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(RestClient.class)
.hasSingleBean(org.elasticsearch.client.RestHighLevelClient.class)
@@ -77,8 +77,8 @@ class ElasticsearchRestClientAutoConfigurationTests {
void configureWithoutRestHighLevelClientShouldOnlyCreateRestClientBuilderAndRestClient() {
this.contextRunner.withClassLoader(new FilteredClassLoader(org.elasticsearch.client.RestHighLevelClient.class))
.run((context) -> assertThat(context).hasSingleBean(RestClient.class)
.doesNotHaveBean(org.elasticsearch.client.RestHighLevelClient.class)
.hasSingleBean(RestClientBuilder.class));
.hasSingleBean(RestClientBuilder.class)
.doesNotHaveBean(org.elasticsearch.client.RestHighLevelClient.class));
}
@Test
@@ -91,44 +91,26 @@ class ElasticsearchRestClientAutoConfigurationTests {
}
@Test
void configureWhenCustomRestHighLevelClientIsNotPresent() {
this.contextRunner.withClassLoader(new FilteredClassLoader(org.elasticsearch.client.RestHighLevelClient.class))
.run((context) -> assertThat(context)
.doesNotHaveBean(org.elasticsearch.client.RestHighLevelClient.class)
.hasSingleBean(RestClient.class).hasSingleBean(RestClientBuilder.class));
}
@Test
void configureWhenCustomRestHighLevelClientShouldBackOff() {
void configureWhenCustomRestHighLevelClientShouldDefineRestClientFromCustomHighLevelClient() {
this.contextRunner.withUserConfiguration(CustomRestHighLevelClientConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(org.elasticsearch.client.RestHighLevelClient.class)
.hasSingleBean(RestClient.class).hasBean("elasticsearchRestClient"));
.hasSingleBean(RestClient.class).hasBean("elasticsearchRestClient").getBean(RestClient.class)
.isEqualTo(context.getBean(org.elasticsearch.client.RestHighLevelClient.class)
.getLowLevelClient()));
}
@Test
void configureWhenCustomRestHighLevelClientAndRestClientWithRestHighLevelClientShouldBackOff() {
void configureWhenCustomRestHighLevelClientAndRestClientShouldBackOff() {
this.contextRunner.withUserConfiguration(CustomRestHighLevelClientWithRestClientConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(org.elasticsearch.client.RestHighLevelClient.class)
.hasSingleBean(RestClient.class).hasBean("customRestClient"));
.hasBean("customRestHighLevelClient").hasSingleBean(RestClient.class)
.hasBean("customRestClient"));
}
@Test
void configureWhenDefaultRestClientShouldCreateWhenNoUniqueRestHighLevelClient() {
this.contextRunner.withUserConfiguration(TwoCustomRestHighLevelClientConfiguration.class).run((context) -> {
assertThat(context).doesNotHaveBean(RestClient.class);
Map<String, org.elasticsearch.client.RestHighLevelClient> restHighLevelClients = context
.getBeansOfType(org.elasticsearch.client.RestHighLevelClient.class);
assertThat(restHighLevelClients).hasSize(2);
});
}
@Test
void configureWhenDefaultRestClientShouldCreateWhenNoUniqueRestClient() {
this.contextRunner.withClassLoader(new FilteredClassLoader(org.elasticsearch.client.RestHighLevelClient.class))
.withUserConfiguration(TwoCustomRestClientConfiguration.class).run((context) -> {
Map<String, RestClient> restClients = context.getBeansOfType(RestClient.class);
assertThat(restClients).hasSize(2);
});
void configureWhenNoUniqueRestHighLevelClientShouldNotDefineRestClient() {
this.contextRunner.withUserConfiguration(TwoCustomRestHighLevelClientsConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(RestClient.class));
}
@Test
@@ -352,7 +334,7 @@ class ElasticsearchRestClientAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class TwoCustomRestHighLevelClientConfiguration {
static class TwoCustomRestHighLevelClientsConfiguration {
@Bean
org.elasticsearch.client.RestHighLevelClient customRestHighLevelClient(RestClientBuilder builder) {
@@ -360,7 +342,7 @@ class ElasticsearchRestClientAutoConfigurationTests {
}
@Bean
org.elasticsearch.client.RestHighLevelClient customoRestHighLevelClient1(RestClientBuilder builder) {
org.elasticsearch.client.RestHighLevelClient anotherCustomRestHighLevelClient(RestClientBuilder builder) {
return new org.elasticsearch.client.RestHighLevelClient(builder);
}