From 0e87b9c163b239edbc7aa7a5f4b94ce7399670b4 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Thu, 11 Jul 2019 11:33:50 +0300 Subject: [PATCH] Create RestClient from a RestHighLevelClient if available See gh-17488 --- .../rest/RestClientAutoConfiguration.java | 25 +++++++++ .../RestClientAutoConfigurationTests.java | 54 ++++++++++++++++++- ...rationWithoutRestHighLevelClientTests.java | 47 ++++++++++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationWithoutRestHighLevelClientTests.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfiguration.java index ab70515838..a0e22f7d74 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfiguration.java @@ -28,6 +28,7 @@ import org.elasticsearch.client.RestHighLevelClient; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -92,4 +93,28 @@ public class RestClientAutoConfiguration { } + /** + * Configuration to configure a {@link RestClient} bean from a + * {@link RestHighLevelClient} if such a bean has been registered by the application. + * If {@link RestHighLevelClient} is not unique or does not exist then + * {@link RestClientBuilder#build()} will be used. + */ + @Configuration(proxyBeanMethods = false) + @ConditionalOnClass(RestHighLevelClient.class) + @ConditionalOnBean(RestHighLevelClient.class) + public static class RestClientConfiguration { + + @Bean + @ConditionalOnMissingBean + public RestClient restClient(ObjectProvider restHighLevelClient, + RestClientBuilder builder) { + RestHighLevelClient client = restHighLevelClient.getIfUnique(); + if (client != null) { + return client.getLowLevelClient(); + } + return builder.build(); + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationTests.java index 0eff7db263..74b9938ea9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.junit.Test; @@ -49,8 +50,11 @@ public class RestClientAutoConfigurationTests { @Test public void configureShouldCreateBothRestClientVariants() { - this.contextRunner.run((context) -> assertThat(context).hasSingleBean(RestClient.class) - .hasSingleBean(RestHighLevelClient.class)); + this.contextRunner.run((context) -> { + assertThat(context).hasSingleBean(RestClient.class).hasSingleBean(RestHighLevelClient.class); + RestHighLevelClient restHighLevelClient = context.getBean(RestHighLevelClient.class); + assertThat(restHighLevelClient.getLowLevelClient()).isNotSameAs(context.getBean(RestClient.class)); + }); } @Test @@ -59,6 +63,27 @@ public class RestClientAutoConfigurationTests { .run((context) -> assertThat(context).hasSingleBean(RestClient.class).hasBean("customRestClient")); } + @Test + public void configureWhenCustomRestHighLevelClientShouldBackOff() { + this.contextRunner.withUserConfiguration(CustomRestHighLevelClientConfiguration.class).run((context) -> { + assertThat(context).hasSingleBean(RestClient.class).hasSingleBean(RestHighLevelClient.class); + RestHighLevelClient restHighLevelClient = context.getBean(RestHighLevelClient.class); + assertThat(restHighLevelClient.getLowLevelClient()).isSameAs(context.getBean(RestClient.class)); + }); + } + + @Test + public void configureWhenDefaultRestClientShouldCreateWhenNoUniqueRestHighLevelClient() { + this.contextRunner.withUserConfiguration(TwoCustomRestHighLevelClientConfiguration.class).run((context) -> { + assertThat(context).hasSingleBean(RestClient.class); + Map restHighLevelClients = context.getBeansOfType(RestHighLevelClient.class); + assertThat(restHighLevelClients).isNotEmpty(); + for (RestHighLevelClient restHighLevelClient : restHighLevelClients.values()) { + assertThat(restHighLevelClient.getLowLevelClient()).isNotSameAs(context.getBean(RestClient.class)); + } + }); + } + @Test public void configureWhenBuilderCustomizerShouldApply() { this.contextRunner.withUserConfiguration(BuilderCustomizerConfiguration.class).run((context) -> { @@ -106,4 +131,29 @@ public class RestClientAutoConfigurationTests { } + @Configuration + static class CustomRestHighLevelClientConfiguration { + + @Bean + RestHighLevelClient customRestHighLevelClient(RestClientBuilder builder) { + return new RestHighLevelClient(builder); + } + + } + + @Configuration + static class TwoCustomRestHighLevelClientConfiguration { + + @Bean + RestHighLevelClient customRestHighLevelClient(RestClientBuilder builder) { + return new RestHighLevelClient(builder); + } + + @Bean + RestHighLevelClient customRestHighLevelClient1(RestClientBuilder builder) { + return new RestHighLevelClient(builder); + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationWithoutRestHighLevelClientTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationWithoutRestHighLevelClientTests.java new file mode 100644 index 0000000000..343cbcd86c --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationWithoutRestHighLevelClientTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.elasticsearch.rest; + +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; +import org.junit.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.FilteredClassLoader; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link RestClientAutoConfiguration} when {@link RestHighLevelClient} is not + * on the classpath. + * + * @author Dmytro Nosan + */ +public class RestClientAutoConfigurationWithoutRestHighLevelClientTests { + + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class)) + .withClassLoader(new FilteredClassLoader(RestHighLevelClient.class)); + + @Test + public void shouldCreateRestClientOnly() { + this.contextRunner.run((context) -> assertThat(context).hasSingleBean(RestClient.class) + .doesNotHaveBean(RestHighLevelClient.class)); + } + +}