pull/13117/head
Andy Wilkinson 7 years ago
parent a41c9eb736
commit d77c4c83a1

@ -39,8 +39,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* {@link EnableAutoConfiguration Auto-Configuration}
* for Elasticseach REST clients.
* {@link EnableAutoConfiguration Auto-Configuration} for Elasticseach REST clients.
*
* @author Brian Clozel
* @since 2.1.0
@ -50,7 +49,6 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(RestClientProperties.class)
public class RestClientAutoConfiguration {
private final RestClientProperties properties;
private final List<RestClientBuilderCustomizer> builderCustomizers;
@ -58,7 +56,8 @@ public class RestClientAutoConfiguration {
public RestClientAutoConfiguration(RestClientProperties properties,
ObjectProvider<List<RestClientBuilderCustomizer>> builderCustomizers) {
this.properties = properties;
this.builderCustomizers = builderCustomizers.getIfAvailable(Collections::emptyList);
this.builderCustomizers = builderCustomizers
.getIfAvailable(Collections::emptyList);
}
@Bean(destroyMethod = "close")
@ -69,8 +68,8 @@ public class RestClientAutoConfiguration {
}
protected RestClientBuilder configureBuilder() {
HttpHost[] hosts = this.properties.getUris().stream()
.map(HttpHost::create).toArray(HttpHost[]::new);
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) -> {
@ -78,8 +77,8 @@ public class RestClientAutoConfiguration {
Credentials credentials = new UsernamePasswordCredentials(
this.properties.getUsername(), this.properties.getPassword());
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
builder.setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
builder.setHttpClientConfigCallback((httpClientBuilder) -> httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider));
});
this.builderCustomizers.forEach((customizer) -> customizer.customize(builder));
return builder;

@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.elasticsearch.rest;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
@ -49,26 +48,21 @@ 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));
}
@Test
public void configureWhenCustomClientShouldBackOff() {
this.contextRunner
.withUserConfiguration(CustomRestClientConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(RestClient.class)
.hasBean("customRestClient");
});
this.contextRunner.withUserConfiguration(CustomRestClientConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(RestClient.class)
.hasBean("customRestClient"));
}
@Test
public void configureWhenBuilderCustomizerShouldApply() {
this.contextRunner
.withUserConfiguration(BuilderCustomizerConfiguration.class)
this.contextRunner.withUserConfiguration(BuilderCustomizerConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(RestClient.class);
RestClient restClient = context.getBean(RestClient.class);
@ -86,7 +80,8 @@ public class RestClientAutoConfigurationTests {
.withPropertyValues("spring.elasticsearch.rest.uris=http://localhost:"
+ node.getHttpPort())
.run((context) -> {
RestHighLevelClient client = context.getBean(RestHighLevelClient.class);
RestHighLevelClient client = context
.getBean(RestHighLevelClient.class);
Map<String, String> source = new HashMap<>();
source.put("a", "alpha");
source.put("b", "bravo");
@ -112,7 +107,7 @@ public class RestClientAutoConfigurationTests {
@Bean
public RestClientBuilderCustomizer myCustomizer() {
return builder -> builder.setMaxRetryTimeoutMillis(42);
return (builder) -> builder.setMaxRetryTimeoutMillis(42);
}
}

Loading…
Cancel
Save