From 0e87b9c163b239edbc7aa7a5f4b94ce7399670b4 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Thu, 11 Jul 2019 11:33:50 +0300 Subject: [PATCH 1/2] 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 ab70515838f..a0e22f7d744 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 0eff7db2636..74b9938ea95 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 00000000000..343cbcd86cd --- /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)); + } + +} From fb0fccf3c7e1b1290ef64a1d2c388d788a5a965e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 1 Aug 2019 11:58:38 +0200 Subject: [PATCH 2/2] Polish "Create RestClient from a RestHighLevelClient if available" This significantly rework the auto-configuration to reflect the order in which things are expected. Rather than keeping a conceptual cycle between the builder and the two inner classes that are processed first, the configuration is now split in three parts: * The builder that is required and common * The configuration when the HighLevelClient is available * The RestClient configuration when that's not the case See gh-17488 --- .../rest/RestClientAutoConfiguration.java | 88 +-------------- .../rest/RestClientConfigurations.java | 101 ++++++++++++++++++ .../RestClientAutoConfigurationTests.java | 23 ++-- ...rationWithoutRestHighLevelClientTests.java | 47 -------- 4 files changed, 122 insertions(+), 137 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientConfigurations.java delete 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 a0e22f7d744..ee0cc7f8eae 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 @@ -16,105 +16,27 @@ package org.springframework.boot.autoconfigure.elasticsearch.rest; -import org.apache.http.HttpHost; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.Credentials; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.impl.client.BasicCredentialsProvider; import org.elasticsearch.client.RestClient; -import org.elasticsearch.client.RestClientBuilder; -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; -import org.springframework.boot.context.properties.PropertyMapper; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; /** * {@link EnableAutoConfiguration Auto-configuration} for Elasticsearch REST clients. * * @author Brian Clozel + * @author Stephane Nicoll * @since 2.1.0 */ @Configuration @ConditionalOnClass(RestClient.class) @EnableConfigurationProperties(RestClientProperties.class) +@Import({ RestClientConfigurations.RestClientBuilderConfiguration.class, + RestClientConfigurations.RestHighLevelClientConfiguration.class, + RestClientConfigurations.RestClientFallbackConfiguration.class }) public class RestClientAutoConfiguration { - private final RestClientProperties properties; - - private final ObjectProvider builderCustomizers; - - public RestClientAutoConfiguration(RestClientProperties properties, - ObjectProvider builderCustomizers) { - this.properties = properties; - this.builderCustomizers = builderCustomizers; - } - - @Bean - @ConditionalOnMissingBean - public RestClient restClient(RestClientBuilder builder) { - return builder.build(); - } - - @Bean - @ConditionalOnMissingBean - public RestClientBuilder restClientBuilder() { - HttpHost[] hosts = this.properties.getUris().stream().map(HttpHost::create).toArray(HttpHost[]::new); - RestClientBuilder builder = RestClient.builder(hosts); - PropertyMapper map = PropertyMapper.get(); - map.from(this.properties::getUsername).whenHasText().to((username) -> { - CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - Credentials credentials = new UsernamePasswordCredentials(this.properties.getUsername(), - this.properties.getPassword()); - credentialsProvider.setCredentials(AuthScope.ANY, credentials); - builder.setHttpClientConfigCallback( - (httpClientBuilder) -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); - }); - this.builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); - return builder; - } - - @Configuration - @ConditionalOnClass(RestHighLevelClient.class) - public static class RestHighLevelClientConfiguration { - - @Bean - @ConditionalOnMissingBean - public RestHighLevelClient restHighLevelClient(RestClientBuilder restClientBuilder) { - return new RestHighLevelClient(restClientBuilder); - } - - } - - /** - * 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/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientConfigurations.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientConfigurations.java new file mode 100644 index 00000000000..735f27bd5ab --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientConfigurations.java @@ -0,0 +1,101 @@ +/* + * 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.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestClientBuilder; +import org.elasticsearch.client.RestHighLevelClient; + +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.PropertyMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Elasticsearch rest client infrastructure configurations. + * + * @author Brian Clozel + * @author Stephane Nicoll + */ +class RestClientConfigurations { + + @Configuration + static class RestClientBuilderConfiguration { + + @Bean + @ConditionalOnMissingBean + RestClientBuilder restClientBuilder(RestClientProperties properties, + ObjectProvider builderCustomizers) { + HttpHost[] hosts = properties.getUris().stream().map(HttpHost::create).toArray(HttpHost[]::new); + RestClientBuilder builder = RestClient.builder(hosts); + PropertyMapper map = PropertyMapper.get(); + map.from(properties::getUsername).whenHasText().to((username) -> { + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + Credentials credentials = new UsernamePasswordCredentials(properties.getUsername(), + properties.getPassword()); + credentialsProvider.setCredentials(AuthScope.ANY, credentials); + builder.setHttpClientConfigCallback( + (httpClientBuilder) -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); + }); + builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); + return builder; + } + + } + + @Configuration + @ConditionalOnClass(RestHighLevelClient.class) + static class RestHighLevelClientConfiguration { + + @Bean + @ConditionalOnMissingBean + RestHighLevelClient restHighLevelClient(RestClientBuilder restClientBuilder) { + return new RestHighLevelClient(restClientBuilder); + } + + @Bean + @ConditionalOnMissingBean + RestClient restClient(RestClientBuilder builder, ObjectProvider restHighLevelClient) { + RestHighLevelClient client = restHighLevelClient.getIfUnique(); + if (client != null) { + return client.getLowLevelClient(); + } + return builder.build(); + } + + } + + @Configuration + static class RestClientFallbackConfiguration { + + @Bean + @ConditionalOnMissingBean + RestClient restClient(RestClientBuilder builder) { + 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 74b9938ea95..233dcaea674 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 @@ -30,6 +30,7 @@ import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchNodeTemplate; +import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -52,23 +53,23 @@ public class RestClientAutoConfigurationTests { public void configureShouldCreateBothRestClientVariants() { 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)); + assertThat(context.getBean(RestClient.class)) + .isSameAs(context.getBean(RestHighLevelClient.class).getLowLevelClient()); }); } @Test public void configureWhenCustomClientShouldBackOff() { this.contextRunner.withUserConfiguration(CustomRestClientConfiguration.class) - .run((context) -> assertThat(context).hasSingleBean(RestClient.class).hasBean("customRestClient")); + .run((context) -> assertThat(context).getBeanNames(RestClient.class).containsOnly("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)); + assertThat(context.getBean(RestClient.class)) + .isSameAs(context.getBean(RestHighLevelClient.class).getLowLevelClient()); }); } @@ -76,14 +77,22 @@ public class RestClientAutoConfigurationTests { public void configureWhenDefaultRestClientShouldCreateWhenNoUniqueRestHighLevelClient() { this.contextRunner.withUserConfiguration(TwoCustomRestHighLevelClientConfiguration.class).run((context) -> { assertThat(context).hasSingleBean(RestClient.class); + RestClient restClient = context.getBean(RestClient.class); Map restHighLevelClients = context.getBeansOfType(RestHighLevelClient.class); - assertThat(restHighLevelClients).isNotEmpty(); + assertThat(restHighLevelClients).hasSize(2); for (RestHighLevelClient restHighLevelClient : restHighLevelClients.values()) { - assertThat(restHighLevelClient.getLowLevelClient()).isNotSameAs(context.getBean(RestClient.class)); + assertThat(restHighLevelClient.getLowLevelClient()).isNotSameAs(restClient); } }); } + @Test + public void configureWhenHighLevelClientIsNotAvailableShouldCreateRestClientOnly() { + this.contextRunner.withClassLoader(new FilteredClassLoader(RestHighLevelClient.class)) + .run((context) -> assertThat(context).hasSingleBean(RestClient.class) + .doesNotHaveBean(RestHighLevelClient.class)); + } + @Test public void configureWhenBuilderCustomizerShouldApply() { this.contextRunner.withUserConfiguration(BuilderCustomizerConfiguration.class).run((context) -> { 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 deleted file mode 100644 index 343cbcd86cd..00000000000 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationWithoutRestHighLevelClientTests.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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)); - } - -}