Polish
This commit is contained in:
parent
663ff05895
commit
ab43bf4643
|
@ -20,13 +20,13 @@ import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import org.influxdb.InfluxDB;
|
import org.influxdb.InfluxDB;
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
import org.springframework.beans.DirectFieldAccessor;
|
import org.springframework.beans.DirectFieldAccessor;
|
||||||
import org.springframework.boot.test.util.TestPropertyValues;
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@ -41,55 +41,57 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*/
|
*/
|
||||||
public class InfluxDbAutoConfigurationTests {
|
public class InfluxDbAutoConfigurationTests {
|
||||||
|
|
||||||
private AnnotationConfigApplicationContext context;
|
private final ApplicationContextTester context = new ApplicationContextTester()
|
||||||
|
.withConfiguration(AutoConfigurations.of(InfluxDbAutoConfiguration.class));
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
if (this.context != null) {
|
|
||||||
this.context.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void influxDbRequiresUrl() {
|
public void influxDbRequiresUrl() {
|
||||||
load();
|
this.context.run((loaded) ->
|
||||||
assertThat(this.context.getBeansOfType(InfluxDB.class)).isEmpty();
|
assertThat(loaded.getBeansOfType(InfluxDB.class)).isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void influxDbCanBeCustomized() {
|
public void influxDbCanBeCustomized() {
|
||||||
load("spring.influx.url=http://localhost", "spring.influx.password:password",
|
this.context.withPropertyValues("spring.influx.url=http://localhost",
|
||||||
"spring.influx.user:user");
|
"spring.influx.password:password", "spring.influx.user:user")
|
||||||
assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
.run((loaded ->
|
||||||
|
assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void influxDbCanBeCreatedWithoutCredentials() {
|
public void influxDbCanBeCreatedWithoutCredentials() {
|
||||||
load("spring.influx.url=http://localhost");
|
this.context.withPropertyValues("spring.influx.url=http://localhost")
|
||||||
assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
.run((loaded) -> {
|
||||||
int readTimeout = getReadTimeoutProperty();
|
assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||||
|
int readTimeout = getReadTimeoutProperty(loaded);
|
||||||
assertThat(readTimeout).isEqualTo(10_000);
|
assertThat(readTimeout).isEqualTo(10_000);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void influxDbWithoutCredentialsAndOkHttpClientBuilder() {
|
public void influxDbWithoutCredentialsAndOkHttpClientBuilder() {
|
||||||
load(CustomOkHttpClientBuilderConfig.class, "spring.influx.url=http://localhost");
|
this.context.withUserConfiguration(CustomOkHttpClientBuilderConfig.class)
|
||||||
assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
.withPropertyValues("spring.influx.url=http://localhost").run((loaded) -> {
|
||||||
int readTimeout = getReadTimeoutProperty();
|
assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||||
|
int readTimeout = getReadTimeoutProperty(loaded);
|
||||||
assertThat(readTimeout).isEqualTo(30_000);
|
assertThat(readTimeout).isEqualTo(30_000);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void influxDbWithOkHttpClientBuilder() {
|
public void influxDbWithOkHttpClientBuilder() {
|
||||||
load(CustomOkHttpClientBuilderConfig.class, "spring.influx.url=http://localhost",
|
this.context.withUserConfiguration(CustomOkHttpClientBuilderConfig.class)
|
||||||
"spring.influx.password:password", "spring.influx.user:user");
|
.withPropertyValues("spring.influx.url=http://localhost",
|
||||||
assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
"spring.influx.password:password", "spring.influx.user:user")
|
||||||
int readTimeout = getReadTimeoutProperty();
|
.run((loaded) -> {
|
||||||
|
assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||||
|
int readTimeout = getReadTimeoutProperty(loaded);
|
||||||
assertThat(readTimeout).isEqualTo(30_000);
|
assertThat(readTimeout).isEqualTo(30_000);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getReadTimeoutProperty() {
|
private int getReadTimeoutProperty(AssertableApplicationContext loaded) {
|
||||||
InfluxDB influxDB = this.context.getBean(InfluxDB.class);
|
InfluxDB influxDB = loaded.getBean(InfluxDB.class);
|
||||||
Retrofit retrofit = (Retrofit) new DirectFieldAccessor(influxDB)
|
Retrofit retrofit = (Retrofit) new DirectFieldAccessor(influxDB)
|
||||||
.getPropertyValue("retrofit");
|
.getPropertyValue("retrofit");
|
||||||
OkHttpClient callFactory = (OkHttpClient) new DirectFieldAccessor(retrofit)
|
OkHttpClient callFactory = (OkHttpClient) new DirectFieldAccessor(retrofit)
|
||||||
|
@ -97,21 +99,6 @@ public class InfluxDbAutoConfigurationTests {
|
||||||
return callFactory.readTimeoutMillis();
|
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
|
@Configuration
|
||||||
static class CustomOkHttpClientBuilderConfig {
|
static class CustomOkHttpClientBuilderConfig {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue