diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index ad71c07e8d5..8055f6aed57 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -43,7 +43,7 @@ Or create a container with custom username, password, bucket, organization, and The following code snippet shows how you can create an InfluxDB Java client: -[Create an InfluxDB Java client](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) inside_block:createInfluxDB2Client +[Create an InfluxDB Java client](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java) inside_block:createInfluxDBClient !!! hint @@ -77,7 +77,7 @@ For instance, creating an InfluxDB container with a custom username, password, a In the following example you will find a snippet to create an InfluxDB client using the official Java client: -[Create an InfluxDB Java client](../../../modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java) inside_block:createInfluxDBClient +[Create an InfluxDB Java client](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) inside_block:createInfluxDBClient !!! hint diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 94fd694c3f0..2af4ed1254d 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -1,8 +1,5 @@ package org.testcontainers.containers; -import java.util.Collections; -import java.util.Optional; -import java.util.Set; import lombok.Getter; import org.influxdb.InfluxDB; import org.influxdb.InfluxDBFactory; @@ -10,6 +7,10 @@ import org.testcontainers.utility.ComparableVersion; import org.testcontainers.utility.DockerImageName; +import java.util.Collections; +import java.util.Optional; +import java.util.Set; + /** * Testcontainers implementation for InfluxDB. */ @@ -41,6 +42,7 @@ public class InfluxDBContainer> extends Gen private String adminPassword = "password"; + @Getter private String database; /** @@ -80,16 +82,15 @@ public InfluxDBContainer(final DockerImageName dockerImageName) { super(dockerImageName); dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - this.logger().info("Starting an InfluxDB container using [{}]", dockerImageName); - - this.waitStrategy = new HttpWaitStrategy() - .forPath("/ping") - .withBasicCredentials(this.username, this.password) - .forStatusCode(NO_CONTENT_STATUS_CODE); + this.waitStrategy = + new HttpWaitStrategy() + .forPath("/ping") + .withBasicCredentials(this.username, this.password) + .forStatusCode(NO_CONTENT_STATUS_CODE); this.isAtLeastMajorVersion2 = new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("2.0.0"); - this.addExposedPort(INFLUXDB_PORT); + addExposedPort(INFLUXDB_PORT); } /** @@ -98,9 +99,9 @@ public InfluxDBContainer(final DockerImageName dockerImageName) { @Override protected void configure() { if (this.isAtLeastMajorVersion2) { - this.setInfluxDBV2Envs(); + configureInfluxDBV2(); } else { - this.setInfluxDBV1Envs(); + configureInfluxDBV1(); } } @@ -110,37 +111,37 @@ protected void configure() { * @see InfluxDB Dockerhub for full documentation on InfluxDB's * envrinoment variables */ - private void setInfluxDBV2Envs() { - this.addEnv("DOCKER_INFLUXDB_INIT_MODE", "setup"); + private void configureInfluxDBV2() { + addEnv("DOCKER_INFLUXDB_INIT_MODE", "setup"); - this.addEnv("DOCKER_INFLUXDB_INIT_USERNAME", this.username); - this.addEnv("DOCKER_INFLUXDB_INIT_PASSWORD", this.password); + addEnv("DOCKER_INFLUXDB_INIT_USERNAME", this.username); + addEnv("DOCKER_INFLUXDB_INIT_PASSWORD", this.password); - this.addEnv("DOCKER_INFLUXDB_INIT_ORG", this.organization); - this.addEnv("DOCKER_INFLUXDB_INIT_BUCKET", this.bucket); + addEnv("DOCKER_INFLUXDB_INIT_ORG", this.organization); + addEnv("DOCKER_INFLUXDB_INIT_BUCKET", this.bucket); - this.retention.ifPresent(ret -> this.addEnv("DOCKER_INFLUXDB_INIT_RETENTION", ret)); - this.adminToken.ifPresent(token -> this.addEnv("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", token)); + this.retention.ifPresent(ret -> addEnv("DOCKER_INFLUXDB_INIT_RETENTION", ret)); + this.adminToken.ifPresent(token -> addEnv("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", token)); } /** * Sets the InfluxDB 1.x environment variables */ - private void setInfluxDBV1Envs() { - this.addEnv("INFLUXDB_USER", this.username); - this.addEnv("INFLUXDB_USER_PASSWORD", this.password); + private void configureInfluxDBV1() { + addEnv("INFLUXDB_USER", this.username); + addEnv("INFLUXDB_USER_PASSWORD", this.password); - this.addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(this.authEnabled)); + addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(this.authEnabled)); - this.addEnv("INFLUXDB_ADMIN_USER", this.admin); - this.addEnv("INFLUXDB_ADMIN_PASSWORD", this.adminPassword); + addEnv("INFLUXDB_ADMIN_USER", this.admin); + addEnv("INFLUXDB_ADMIN_PASSWORD", this.adminPassword); - this.addEnv("INFLUXDB_DB", this.database); + addEnv("INFLUXDB_DB", this.database); } @Override public Set getLivenessCheckPortNumbers() { - return Collections.singleton(this.getMappedPort(INFLUXDB_PORT)); + return Collections.singleton(getMappedPort(INFLUXDB_PORT)); } /** @@ -266,11 +267,9 @@ public String getUrl() { * @deprecated Use the new InfluxDB client library. */ @Deprecated - // createInfluxDBClient { public InfluxDB getNewInfluxDB() { final InfluxDB influxDB = InfluxDBFactory.connect(getUrl(), this.username, this.password); influxDB.setDatabase(this.database); return influxDB; } - // } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java index d72aa15385e..fc80e41ff09 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java @@ -1,7 +1,8 @@ package org.testcontainers.containers; - import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.InfluxDBClientFactory; +import com.influxdb.client.InfluxDBClientOptions; import com.influxdb.client.QueryApi; import com.influxdb.client.WriteApi; import com.influxdb.client.domain.Bucket; @@ -10,13 +11,14 @@ import com.influxdb.client.write.Point; import com.influxdb.query.FluxRecord; import com.influxdb.query.FluxTable; -import java.time.Instant; -import java.util.List; -import java.util.Optional; import org.assertj.core.api.Assertions; import org.junit.Test; import org.testcontainers.utility.DockerImageName; +import java.time.Instant; +import java.util.List; +import java.util.Optional; + public class InfluxDBContainerTest { private static final String USERNAME = "new-test-user"; @@ -38,12 +40,13 @@ public void getInfluxDBClient() { try ( // constructorWithDefaultVariables { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - DockerImageName.parse("influxdb:2.0.7")) + DockerImageName.parse("influxdb:2.0.7") + ) // } ) { influxDBContainer.start(); - try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClient(influxDBContainer)) { + try (final InfluxDBClient influxDBClient = createClient(influxDBContainer)) { Assertions.assertThat(influxDBClient).isNotNull(); Assertions.assertThat(influxDBClient.ping()).isTrue(); } @@ -55,15 +58,21 @@ public void getInfluxDBClientWithAdminToken() { try ( // constructorWithAdminToken { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - DockerImageName.parse("influxdb:2.0.7")).withAdminToken(ADMIN_TOKEN) + DockerImageName.parse("influxdb:2.0.7") + ) + .withAdminToken(ADMIN_TOKEN) // } ) { influxDBContainer.start(); final Optional adminToken = influxDBContainer.getAdminToken(); Assertions.assertThat(adminToken).isNotEmpty(); - try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClientWithToken( - influxDBContainer.getUrl(), adminToken.get())) { + try ( + final InfluxDBClient influxDBClient = createClientWithToken( + influxDBContainer.getUrl(), + adminToken.get() + ) + ) { Assertions.assertThat(influxDBClient).isNotNull(); Assertions.assertThat(influxDBClient.ping()).isTrue(); } @@ -75,7 +84,8 @@ public void getBucket() { try ( // constructorWithCustomVariables { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - DockerImageName.parse("influxdb:2.0.7")) + DockerImageName.parse("influxdb:2.0.7") + ) .withUsername(USERNAME) .withPassword(PASSWORD) .withOrganization(ORG) @@ -83,16 +93,16 @@ public void getBucket() { .withRetention(RETENTION); // } ) { - influxDBContainer.start(); - try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClient(influxDBContainer)) { + try (final InfluxDBClient influxDBClient = createClient(influxDBContainer)) { final Bucket bucket = influxDBClient.getBucketsApi().findBucketByName(BUCKET); Assertions.assertThat(bucket).isNotNull(); Assertions.assertThat(bucket.getName()).isEqualTo(BUCKET); - Assertions.assertThat(bucket.getRetentionRules()). - hasSize(1) + Assertions + .assertThat(bucket.getRetentionRules()) + .hasSize(1) .first() .extracting(BucketRetentionRules::getEverySeconds) .isEqualTo(SECONDS_IN_WEEK); @@ -104,7 +114,8 @@ public void getBucket() { public void queryForWriteAndRead() { try ( final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) + InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE + ) .withUsername(USERNAME) .withPassword(PASSWORD) .withOrganization(ORG) @@ -113,7 +124,7 @@ public void queryForWriteAndRead() { ) { influxDBContainer.start(); - try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClient(influxDBContainer)) { + try (final InfluxDBClient influxDBClient = createClient(influxDBContainer)) { try (final WriteApi writeApi = influxDBClient.makeWriteApi()) { final Point point = Point .measurement("temperature") @@ -134,4 +145,22 @@ public void queryForWriteAndRead() { } } } + + // createInfluxDBClient { + public static InfluxDBClient createClient(final InfluxDBContainer influxDBContainer) { + final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions + .builder() + .url(influxDBContainer.getUrl()) + .authenticate(influxDBContainer.getUsername(), influxDBContainer.getPassword().toCharArray()) + .bucket(influxDBContainer.getBucket()) + .org(influxDBContainer.getOrganization()) + .build(); + return InfluxDBClientFactory.create(influxDBClientOptions); + } + + // } + + public static InfluxDBClient createClientWithToken(final String url, final String token) { + return InfluxDBClientFactory.create(url, token.toCharArray()); + } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index 4b981ac99a8..c9e292c5675 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -1,14 +1,17 @@ package org.testcontainers.containers; -import java.util.concurrent.TimeUnit; -import org.assertj.core.api.Assertions; import org.influxdb.InfluxDB; +import org.influxdb.InfluxDBFactory; import org.influxdb.dto.Point; import org.influxdb.dto.Query; import org.influxdb.dto.QueryResult; import org.junit.Test; import org.testcontainers.utility.DockerImageName; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; + public class InfluxDBContainerV1Test { private static final String TEST_VERSION = InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE.getVersionPart(); @@ -24,18 +27,17 @@ public void createInfluxDBOnlyWithUrlAndCorrectVersion() { try ( // constructorWithDefaultVariables { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - DockerImageName.parse("influxdb:1.4.3")) + DockerImageName.parse("influxdb:1.4.3") + ) // } ) { // Start the container. This step might take some time... influxDBContainer.start(); - final String actual = influxDBContainer.getUrl(); - - try (final InfluxDB influxDBClient = InfluxDBTestUtils.createInfluxDBWithUrl(actual)) { - Assertions.assertThat(influxDBClient).isNotNull(); - Assertions.assertThat(influxDBClient.ping().isGood()).isTrue(); - Assertions.assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); + try (final InfluxDB influxDBClient = createInfluxDBWithUrl(influxDBContainer)) { + assertThat(influxDBClient).isNotNull(); + assertThat(influxDBClient.ping().isGood()).isTrue(); + assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); } } } @@ -44,15 +46,16 @@ public void createInfluxDBOnlyWithUrlAndCorrectVersion() { public void getNewInfluxDBWithCorrectVersion() { try ( final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE + ) ) { // Start the container. This step might take some time... influxDBContainer.start(); - try (final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB()) { - Assertions.assertThat(influxDBClient).isNotNull(); - Assertions.assertThat(influxDBClient.ping().isGood()).isTrue(); - Assertions.assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); + try (final InfluxDB influxDBClient = createInfluxDBWithUrl(influxDBContainer)) { + assertThat(influxDBClient).isNotNull(); + assertThat(influxDBClient.ping().isGood()).isTrue(); + assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); } } } @@ -62,7 +65,8 @@ public void describeDatabases() { try ( // constructorWithUserPassword { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - DockerImageName.parse("influxdb:1.4.3")) + DockerImageName.parse("influxdb:1.4.3") + ) .withDatabase(DATABASE) .withUsername(USER) .withPassword(PASSWORD) @@ -71,8 +75,8 @@ public void describeDatabases() { // Start the container. This step might take some time... influxDBContainer.start(); - try (final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB()) { - Assertions.assertThat(influxDBClient.describeDatabases()).contains(DATABASE); + try (final InfluxDB influxDBClient = createInfluxDBWithUrl(influxDBContainer)) { + assertThat(influxDBClient.describeDatabases()).contains(DATABASE); } } } @@ -81,7 +85,8 @@ public void describeDatabases() { public void queryForWriteAndRead() { try ( final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE + ) .withDatabase(DATABASE) .withUsername(USER) .withPassword(PASSWORD) @@ -89,7 +94,7 @@ public void queryForWriteAndRead() { // Start the container. This step might take some time... influxDBContainer.start(); - try (final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB()) { + try (final InfluxDB influxDBClient = createInfluxDBWithUrl(influxDBContainer)) { final Point point = Point .measurement("cpu") .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) @@ -102,11 +107,23 @@ public void queryForWriteAndRead() { final Query query = new Query("SELECT idle FROM cpu", DATABASE); final QueryResult actual = influxDBClient.query(query); - Assertions.assertThat(actual).isNotNull(); - Assertions.assertThat(actual.getError()).isNull(); - Assertions.assertThat(actual.getResults()).isNotNull(); - Assertions.assertThat(actual.getResults()).hasSize(1); + assertThat(actual).isNotNull(); + assertThat(actual.getError()).isNull(); + assertThat(actual.getResults()).isNotNull(); + assertThat(actual.getResults()).hasSize(1); } } } + + // createInfluxDBClient { + public static InfluxDB createInfluxDBWithUrl(final InfluxDBContainer container) { + InfluxDB influxDB = InfluxDBFactory.connect( + container.getUrl(), + container.getUsername(), + container.getPassword() + ); + influxDB.setDatabase(container.getDatabase()); + return influxDB; + } + // } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java index 0cdec9a95d4..db9ae2bf79f 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java @@ -1,10 +1,5 @@ package org.testcontainers.containers; -import com.influxdb.client.InfluxDBClient; -import com.influxdb.client.InfluxDBClientFactory; -import com.influxdb.client.InfluxDBClientOptions; -import org.influxdb.InfluxDB; -import org.influxdb.InfluxDBFactory; import org.testcontainers.utility.DockerImageName; public final class InfluxDBTestUtils { @@ -12,27 +7,4 @@ public final class InfluxDBTestUtils { static final DockerImageName INFLUXDB_V1_TEST_IMAGE = DockerImageName.parse("influxdb:1.4.3"); static final DockerImageName INFLUXDB_V2_TEST_IMAGE = DockerImageName.parse("influxdb:2.0.7"); - - private InfluxDBTestUtils() {} - - // createInfluxDB2Client { - public static InfluxDBClient createInfluxDBClient(final InfluxDBContainer influxDBContainer) { - final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions - .builder() - .url(influxDBContainer.getUrl()) - .authenticate(influxDBContainer.getUsername(), influxDBContainer.getPassword().toCharArray()) - .bucket(influxDBContainer.getBucket()) - .org(influxDBContainer.getOrganization()) - .build(); - return InfluxDBClientFactory.create(influxDBClientOptions); - } - // } - - public static InfluxDBClient createInfluxDBClientWithToken(final String url, final String token) { - return InfluxDBClientFactory.create(url, token.toCharArray()); - } - - public static InfluxDB createInfluxDBWithUrl(final String url) { - return InfluxDBFactory.connect(url); - } }