Commit ab43bf46 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish

parent 663ff058
......@@ -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();
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();
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();
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 {
......
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