Commit d77c4c83 authored by Andy Wilkinson's avatar Andy Wilkinson

Polish

parent a41c9eb7
...@@ -39,8 +39,7 @@ import org.springframework.context.annotation.Bean; ...@@ -39,8 +39,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
/** /**
* {@link EnableAutoConfiguration Auto-Configuration} * {@link EnableAutoConfiguration Auto-Configuration} for Elasticseach REST clients.
* for Elasticseach REST clients.
* *
* @author Brian Clozel * @author Brian Clozel
* @since 2.1.0 * @since 2.1.0
...@@ -50,7 +49,6 @@ import org.springframework.context.annotation.Configuration; ...@@ -50,7 +49,6 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(RestClientProperties.class) @EnableConfigurationProperties(RestClientProperties.class)
public class RestClientAutoConfiguration { public class RestClientAutoConfiguration {
private final RestClientProperties properties; private final RestClientProperties properties;
private final List<RestClientBuilderCustomizer> builderCustomizers; private final List<RestClientBuilderCustomizer> builderCustomizers;
...@@ -58,7 +56,8 @@ public class RestClientAutoConfiguration { ...@@ -58,7 +56,8 @@ public class RestClientAutoConfiguration {
public RestClientAutoConfiguration(RestClientProperties properties, public RestClientAutoConfiguration(RestClientProperties properties,
ObjectProvider<List<RestClientBuilderCustomizer>> builderCustomizers) { ObjectProvider<List<RestClientBuilderCustomizer>> builderCustomizers) {
this.properties = properties; this.properties = properties;
this.builderCustomizers = builderCustomizers.getIfAvailable(Collections::emptyList); this.builderCustomizers = builderCustomizers
.getIfAvailable(Collections::emptyList);
} }
@Bean(destroyMethod = "close") @Bean(destroyMethod = "close")
...@@ -69,8 +68,8 @@ public class RestClientAutoConfiguration { ...@@ -69,8 +68,8 @@ public class RestClientAutoConfiguration {
} }
protected RestClientBuilder configureBuilder() { protected RestClientBuilder configureBuilder() {
HttpHost[] hosts = this.properties.getUris().stream() HttpHost[] hosts = this.properties.getUris().stream().map(HttpHost::create)
.map(HttpHost::create).toArray(HttpHost[]::new); .toArray(HttpHost[]::new);
RestClientBuilder builder = RestClient.builder(hosts); RestClientBuilder builder = RestClient.builder(hosts);
PropertyMapper map = PropertyMapper.get(); PropertyMapper map = PropertyMapper.get();
map.from(this.properties::getUsername).whenHasText().to((username) -> { map.from(this.properties::getUsername).whenHasText().to((username) -> {
...@@ -78,8 +77,8 @@ public class RestClientAutoConfiguration { ...@@ -78,8 +77,8 @@ public class RestClientAutoConfiguration {
Credentials credentials = new UsernamePasswordCredentials( Credentials credentials = new UsernamePasswordCredentials(
this.properties.getUsername(), this.properties.getPassword()); this.properties.getUsername(), this.properties.getPassword());
credentialsProvider.setCredentials(AuthScope.ANY, credentials); credentialsProvider.setCredentials(AuthScope.ANY, credentials);
builder.setHttpClientConfigCallback(httpClientBuilder -> builder.setHttpClientConfigCallback((httpClientBuilder) -> httpClientBuilder
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); .setDefaultCredentialsProvider(credentialsProvider));
}); });
this.builderCustomizers.forEach((customizer) -> customizer.customize(builder)); this.builderCustomizers.forEach((customizer) -> customizer.customize(builder));
return builder; return builder;
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.elasticsearch.rest; package org.springframework.boot.autoconfigure.elasticsearch.rest;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -49,26 +48,21 @@ public class RestClientAutoConfigurationTests { ...@@ -49,26 +48,21 @@ public class RestClientAutoConfigurationTests {
@Test @Test
public void configureShouldCreateBothRestClientVariants() { public void configureShouldCreateBothRestClientVariants() {
this.contextRunner.run((context) -> { this.contextRunner
assertThat(context).hasSingleBean(RestClient.class) .run((context) -> assertThat(context).hasSingleBean(RestClient.class)
.hasSingleBean(RestHighLevelClient.class); .hasSingleBean(RestHighLevelClient.class));
});
} }
@Test @Test
public void configureWhenCustomClientShouldBackOff() { public void configureWhenCustomClientShouldBackOff() {
this.contextRunner this.contextRunner.withUserConfiguration(CustomRestClientConfiguration.class)
.withUserConfiguration(CustomRestClientConfiguration.class) .run((context) -> assertThat(context).hasSingleBean(RestClient.class)
.run((context) -> { .hasBean("customRestClient"));
assertThat(context).hasSingleBean(RestClient.class)
.hasBean("customRestClient");
});
} }
@Test @Test
public void configureWhenBuilderCustomizerShouldApply() { public void configureWhenBuilderCustomizerShouldApply() {
this.contextRunner this.contextRunner.withUserConfiguration(BuilderCustomizerConfiguration.class)
.withUserConfiguration(BuilderCustomizerConfiguration.class)
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(RestClient.class); assertThat(context).hasSingleBean(RestClient.class);
RestClient restClient = context.getBean(RestClient.class); RestClient restClient = context.getBean(RestClient.class);
...@@ -86,7 +80,8 @@ public class RestClientAutoConfigurationTests { ...@@ -86,7 +80,8 @@ public class RestClientAutoConfigurationTests {
.withPropertyValues("spring.elasticsearch.rest.uris=http://localhost:" .withPropertyValues("spring.elasticsearch.rest.uris=http://localhost:"
+ node.getHttpPort()) + node.getHttpPort())
.run((context) -> { .run((context) -> {
RestHighLevelClient client = context.getBean(RestHighLevelClient.class); RestHighLevelClient client = context
.getBean(RestHighLevelClient.class);
Map<String, String> source = new HashMap<>(); Map<String, String> source = new HashMap<>();
source.put("a", "alpha"); source.put("a", "alpha");
source.put("b", "bravo"); source.put("b", "bravo");
...@@ -112,7 +107,7 @@ public class RestClientAutoConfigurationTests { ...@@ -112,7 +107,7 @@ public class RestClientAutoConfigurationTests {
@Bean @Bean
public RestClientBuilderCustomizer myCustomizer() { public RestClientBuilderCustomizer myCustomizer() {
return builder -> builder.setMaxRetryTimeoutMillis(42); return (builder) -> builder.setMaxRetryTimeoutMillis(42);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment