Skip to content

Commit

Permalink
refactor: improve tests (#541)
Browse files Browse the repository at this point in the history
* Remove spring-test dependency
* Remove usage of ReflectionTestUtils
* Use assertj's extracting
  • Loading branch information
eddumelendez authored May 3, 2023
1 parent 145e546 commit 64c9538
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 34 deletions.
7 changes: 0 additions & 7 deletions spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@
*/
package com.influxdb.spring.influx;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;

import com.influxdb.client.InfluxDBClient;

import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.util.ReflectionTestUtils;
import retrofit2.Retrofit;

/**
* Tests for {@link InfluxDB2AutoConfiguration}.
Expand Down Expand Up @@ -69,8 +65,8 @@ public void influxDBClientCanBeCreatedWithoutCredentials() {
this.contextRunner.withPropertyValues("influx.url=http://localhost:8086/")
.run((context) -> {
Assertions.assertThat(context.getBeansOfType(InfluxDBClient.class)).hasSize(1);
int readTimeout = getReadTimeoutProperty(context);
Assertions.assertThat(readTimeout).isEqualTo(10_000);
InfluxDBClient influxDB = context.getBean(InfluxDBClient.class);
Assertions.assertThat(influxDB).extracting("retrofit.callFactory.readTimeoutMillis").isEqualTo(10_000);
});
}

Expand All @@ -81,8 +77,8 @@ public void influxDBClientWithOkHttpClientBuilderProvider() {
.withPropertyValues("influx.url=http://localhost:8086/", "influx.token:token")
.run((context) -> {
Assertions.assertThat(context.getBeansOfType(InfluxDBClient.class)).hasSize(1);
int readTimeout = getReadTimeoutProperty(context);
Assertions.assertThat(readTimeout).isEqualTo(40_000);
InfluxDBClient influxDB = context.getBean(InfluxDBClient.class);
Assertions.assertThat(influxDB).extracting("retrofit.callFactory.readTimeoutMillis").isEqualTo(40_000);
});
}

Expand All @@ -91,34 +87,20 @@ public void influxDBClientWithReadTimeout() {
this.contextRunner.withPropertyValues("influx.url=http://localhost:8086/", "influx.readTimeout=13s")
.run((context) -> {
Assertions.assertThat(context.getBeansOfType(InfluxDBClient.class)).hasSize(1);
int readTimeout = getReadTimeoutProperty(context);
Assertions.assertThat(readTimeout).isEqualTo(13_000);
InfluxDBClient influxDB = context.getBean(InfluxDBClient.class);
Assertions.assertThat(influxDB).extracting("retrofit.callFactory.readTimeoutMillis").isEqualTo(13_000);
});
}

@Test
public void protocolVersion() {
this.contextRunner.withPropertyValues("influx.url=http://localhost:8086/", "spring.influx2.token:token")
.run((context) -> {
List<Protocol> protocols = getOkHttpClient(context).protocols();
Assertions.assertThat(protocols).hasSize(1);
Assertions.assertThat(protocols).contains(Protocol.HTTP_1_1);
InfluxDBClient influxDB = context.getBean(InfluxDBClient.class);
Assertions.assertThat(influxDB).extracting("retrofit.callFactory.protocols", InstanceOfAssertFactories.LIST).contains(Protocol.HTTP_1_1);
});
}

private int getReadTimeoutProperty(AssertableApplicationContext context) {
OkHttpClient callFactory = getOkHttpClient(context);
return callFactory.readTimeoutMillis();
}

@Nonnull
private OkHttpClient getOkHttpClient(final AssertableApplicationContext context) {
InfluxDBClient influxDB = context.getBean(InfluxDBClient.class);
Retrofit retrofit = (Retrofit) ReflectionTestUtils.getField(influxDB, "retrofit");
OkHttpClient callFactory = (OkHttpClient) retrofit.callFactory();
return callFactory;
}

@Configuration
static class CustomOkHttpClientBuilderProviderConfig {

Expand Down

0 comments on commit 64c9538

Please sign in to comment.