From 406f41740ef87b7dd2845dcd27580056668f23d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Sun, 2 Jul 2017 23:48:03 -0500 Subject: [PATCH 1/3] Allow to customize OkHttpClient.Builder This commit enables to auto-configure OkHttpClient.Builder in InfluxDbAutoConfiguration if it is available. See gh-9669 --- .../influx/InfluxDbAutoConfiguration.java | 30 ++++++++-- .../InfluxDbAutoConfigurationTests.java | 56 ++++++++++++++++++- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java index aa1610fc1e..5f009f8514 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java @@ -16,10 +16,11 @@ package org.springframework.boot.autoconfigure.influx; -import com.google.common.base.Strings; +import okhttp3.OkHttpClient; import org.influxdb.InfluxDB; import org.influxdb.InfluxDBFactory; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -27,12 +28,14 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.util.StringUtils; /** * {@link EnableAutoConfiguration Auto-configuration} for InfluxDB. * * @author Sergey Kuptsov * @author Stephane Nicoll + * @author Eddú Meléndez * @since 2.0.0 */ @Configuration @@ -42,19 +45,34 @@ public class InfluxDbAutoConfiguration { private final InfluxDbProperties properties; - public InfluxDbAutoConfiguration(InfluxDbProperties properties) { + private final OkHttpClient.Builder builder; + + public InfluxDbAutoConfiguration(InfluxDbProperties properties, + ObjectProvider builder) { this.properties = properties; + this.builder = builder.getIfAvailable(); } @Bean @ConditionalOnMissingBean @ConditionalOnProperty("spring.influx.url") public InfluxDB influxDb() { - if (Strings.isNullOrEmpty(this.properties.getUser())) { - return InfluxDBFactory.connect(this.properties.getUrl()); + if (hasCredentials() && this.builder != null) { + return InfluxDBFactory.connect(this.properties.getUrl(), + this.properties.getUser(), this.properties.getUrl(), this.builder); + } + else if (hasCredentials()) { + return InfluxDBFactory.connect(this.properties.getUrl(), + this.properties.getUser(), this.properties.getPassword()); } - return InfluxDBFactory.connect(this.properties.getUrl(), - this.properties.getUser(), this.properties.getPassword()); + else if (this.builder != null) { + return InfluxDBFactory.connect(this.properties.getUrl(), this.builder); + } + return InfluxDBFactory.connect(this.properties.getUrl()); } + private boolean hasCredentials() { + return StringUtils.hasText(this.properties.getUser()) + && StringUtils.hasText(this.properties.getPassword()); + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java index 1e3c34d93e..8a2839a604 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java @@ -16,12 +16,20 @@ package org.springframework.boot.autoconfigure.influx; +import java.util.concurrent.TimeUnit; + +import okhttp3.OkHttpClient; import org.influxdb.InfluxDB; +import org.influxdb.impl.InfluxDBImpl; import org.junit.After; import org.junit.Test; +import retrofit2.Retrofit; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -30,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Sergey Kuptsov * @author Stephane Nicoll + * @author Eddú Meléndez */ public class InfluxDbAutoConfigurationTests { @@ -59,14 +68,59 @@ public class InfluxDbAutoConfigurationTests { public void influxDbCanBeCreatedWithoutCredentials() { load("spring.influx.url=http://localhost"); assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(); + assertThat(readTimeout).isEqualTo(10_000); } - private void load(String... environment) { + @Test + public void influxDbWithoutCredentialsAndOkHttpClientBuilder() { + load(CustomOkHttpClientBuilderConfig.class, "spring.influx.url=http://localhost"); + assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(); + assertThat(readTimeout).isEqualTo(30_000); + } + + @Test + public void influxDbWithOkHttpClientBuilder() { + load(CustomOkHttpClientBuilderConfig.class, "spring.influx.url=http://localhost", + "spring.influx.password:password", "spring.influx.user:user"); + assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(); + assertThat(readTimeout).isEqualTo(30_000); + } + + private int getReadTimeoutProperty() { + InfluxDB influxDB = this.context.getBean(InfluxDB.class); + Retrofit retrofit = (Retrofit) ReflectionTestUtils.getField(influxDB, + InfluxDBImpl.class, "retrofit"); + OkHttpClient callFactory = (OkHttpClient) ReflectionTestUtils.getField(retrofit, + Retrofit.class, "callFactory"); + return callFactory.readTimeoutMillis(); + } + + private void load(Class clazz, String... environment) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); TestPropertyValues.of(environment).applyTo(ctx); ctx.register(InfluxDbAutoConfiguration.class); + if (clazz != null) { + ctx.register(clazz); + } ctx.refresh(); this.context = ctx; } + private void load(String... environment) { + load(null, environment); + } + + @Configuration + static class CustomOkHttpClientBuilderConfig { + + @Bean + public OkHttpClient.Builder builder() { + return new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS); + } + + } + } From 663ff0589554e97a40f0b7bc104f6c3f50020914 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 25 Jul 2017 08:37:04 +0200 Subject: [PATCH 2/3] Polish "Allow to customize OkHttpClient.Builder" Closes gh-9669 --- .../influx/InfluxDbAutoConfiguration.java | 23 ++++--------------- .../InfluxDbAutoConfigurationTests.java | 11 ++++----- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java index 5f009f8514..3f94e38175 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java @@ -18,7 +18,7 @@ package org.springframework.boot.autoconfigure.influx; import okhttp3.OkHttpClient; import org.influxdb.InfluxDB; -import org.influxdb.InfluxDBFactory; +import org.influxdb.impl.InfluxDBImpl; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -28,7 +28,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.util.StringUtils; /** * {@link EnableAutoConfiguration Auto-configuration} for InfluxDB. @@ -50,29 +49,15 @@ public class InfluxDbAutoConfiguration { public InfluxDbAutoConfiguration(InfluxDbProperties properties, ObjectProvider builder) { this.properties = properties; - this.builder = builder.getIfAvailable(); + this.builder = builder.getIfAvailable(OkHttpClient.Builder::new); } @Bean @ConditionalOnMissingBean @ConditionalOnProperty("spring.influx.url") public InfluxDB influxDb() { - if (hasCredentials() && this.builder != null) { - return InfluxDBFactory.connect(this.properties.getUrl(), - this.properties.getUser(), this.properties.getUrl(), this.builder); - } - else if (hasCredentials()) { - return InfluxDBFactory.connect(this.properties.getUrl(), - this.properties.getUser(), this.properties.getPassword()); - } - else if (this.builder != null) { - return InfluxDBFactory.connect(this.properties.getUrl(), this.builder); - } - return InfluxDBFactory.connect(this.properties.getUrl()); + return new InfluxDBImpl(this.properties.getUrl(), this.properties.getUser(), + this.properties.getPassword(), this.builder); } - private boolean hasCredentials() { - return StringUtils.hasText(this.properties.getUser()) - && StringUtils.hasText(this.properties.getPassword()); - } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java index 8a2839a604..38d2b18d72 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java @@ -20,16 +20,15 @@ import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import org.influxdb.InfluxDB; -import org.influxdb.impl.InfluxDBImpl; import org.junit.After; import org.junit.Test; import retrofit2.Retrofit; +import org.springframework.beans.DirectFieldAccessor; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -91,10 +90,10 @@ public class InfluxDbAutoConfigurationTests { private int getReadTimeoutProperty() { InfluxDB influxDB = this.context.getBean(InfluxDB.class); - Retrofit retrofit = (Retrofit) ReflectionTestUtils.getField(influxDB, - InfluxDBImpl.class, "retrofit"); - OkHttpClient callFactory = (OkHttpClient) ReflectionTestUtils.getField(retrofit, - Retrofit.class, "callFactory"); + Retrofit retrofit = (Retrofit) new DirectFieldAccessor(influxDB) + .getPropertyValue("retrofit"); + OkHttpClient callFactory = (OkHttpClient) new DirectFieldAccessor(retrofit) + .getPropertyValue("callFactory"); return callFactory.readTimeoutMillis(); } From ab43bf464308860d7ffb98feed707e7802d5f62a Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 25 Jul 2017 08:46:30 +0200 Subject: [PATCH 3/3] Polish --- .../InfluxDbAutoConfigurationTests.java | 79 ++++++++----------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java index 38d2b18d72..fa5cdfc538 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java @@ -20,13 +20,13 @@ import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import org.influxdb.InfluxDB; -import org.junit.After; import org.junit.Test; import retrofit2.Retrofit; import org.springframework.beans.DirectFieldAccessor; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.AssertableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -41,55 +41,57 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class InfluxDbAutoConfigurationTests { - private AnnotationConfigApplicationContext context; - - @After - public void tearDown() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextTester context = new ApplicationContextTester() + .withConfiguration(AutoConfigurations.of(InfluxDbAutoConfiguration.class)); @Test public void influxDbRequiresUrl() { - load(); - assertThat(this.context.getBeansOfType(InfluxDB.class)).isEmpty(); + this.context.run((loaded) -> + assertThat(loaded.getBeansOfType(InfluxDB.class)).isEmpty()); } @Test public void influxDbCanBeCustomized() { - load("spring.influx.url=http://localhost", "spring.influx.password:password", - "spring.influx.user:user"); - assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); + this.context.withPropertyValues("spring.influx.url=http://localhost", + "spring.influx.password:password", "spring.influx.user:user") + .run((loaded -> + assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1))); } @Test public void influxDbCanBeCreatedWithoutCredentials() { - load("spring.influx.url=http://localhost"); - assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); - int readTimeout = getReadTimeoutProperty(); - assertThat(readTimeout).isEqualTo(10_000); + this.context.withPropertyValues("spring.influx.url=http://localhost") + .run((loaded) -> { + assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(loaded); + assertThat(readTimeout).isEqualTo(10_000); + }); } @Test public void influxDbWithoutCredentialsAndOkHttpClientBuilder() { - load(CustomOkHttpClientBuilderConfig.class, "spring.influx.url=http://localhost"); - assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); - int readTimeout = getReadTimeoutProperty(); - assertThat(readTimeout).isEqualTo(30_000); + this.context.withUserConfiguration(CustomOkHttpClientBuilderConfig.class) + .withPropertyValues("spring.influx.url=http://localhost").run((loaded) -> { + assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(loaded); + assertThat(readTimeout).isEqualTo(30_000); + }); } @Test public void influxDbWithOkHttpClientBuilder() { - load(CustomOkHttpClientBuilderConfig.class, "spring.influx.url=http://localhost", - "spring.influx.password:password", "spring.influx.user:user"); - assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); - int readTimeout = getReadTimeoutProperty(); - assertThat(readTimeout).isEqualTo(30_000); + this.context.withUserConfiguration(CustomOkHttpClientBuilderConfig.class) + .withPropertyValues("spring.influx.url=http://localhost", + "spring.influx.password:password", "spring.influx.user:user") + .run((loaded) -> { + assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(loaded); + assertThat(readTimeout).isEqualTo(30_000); + }); } - private int getReadTimeoutProperty() { - InfluxDB influxDB = this.context.getBean(InfluxDB.class); + private int getReadTimeoutProperty(AssertableApplicationContext loaded) { + InfluxDB influxDB = loaded.getBean(InfluxDB.class); Retrofit retrofit = (Retrofit) new DirectFieldAccessor(influxDB) .getPropertyValue("retrofit"); OkHttpClient callFactory = (OkHttpClient) new DirectFieldAccessor(retrofit) @@ -97,21 +99,6 @@ public class InfluxDbAutoConfigurationTests { return callFactory.readTimeoutMillis(); } - private void load(Class clazz, String... environment) { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - TestPropertyValues.of(environment).applyTo(ctx); - ctx.register(InfluxDbAutoConfiguration.class); - if (clazz != null) { - ctx.register(clazz); - } - ctx.refresh(); - this.context = ctx; - } - - private void load(String... environment) { - load(null, environment); - } - @Configuration static class CustomOkHttpClientBuilderConfig {