Skip to content

Commit fe09508

Browse files
committed
Corrected the comments for org.testcontainers.influxdb.InfluxDBContainer
1 parent ac59b23 commit fe09508

File tree

2 files changed

+33
-54
lines changed

2 files changed

+33
-54
lines changed

modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV3.java renamed to modules/influxdb/src/main/java/org/testcontainers/influxdb/InfluxDBContainer.java

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
1-
package org.testcontainers.containers;
1+
package org.testcontainers.influxdb;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.github.dockerjava.api.command.InspectContainerResponse;
45
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.HttpResponseException;
56
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.classic.methods.HttpPost;
67
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
78
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.HttpClients;
89
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpStatus;
10+
import lombok.Getter;
11+
import org.testcontainers.containers.GenericContainer;
912
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
1013
import org.testcontainers.utility.DockerImageName;
1114

1215
import java.io.IOException;
1316
import java.io.InputStream;
14-
import java.util.Collections;
15-
import java.util.Set;
1617

1718
import static java.lang.String.format;
1819

1920
/**
2021
* Testcontainers implementation for InfluxDB 3 (InfluxDB IOx).
2122
* <p>
23+
* Supported image: {@code influxdb}
24+
* <p>
25+
* Exposed ports: 8181
26+
* <p>
2227
* This container provides a instance of InfluxDB 3.x for integration testing.
2328
* It supports both authenticated and non-authenticated modes.
2429
* </p>
2530
*
2631
* <p>
2732
* <strong>Example usage:</strong>
2833
* <pre>{@code
29-
* try (InfluxDBContainerV3<?> influxDB = new InfluxDBContainerV3<>("influxdb:3-core")) {
34+
* try (InfluxDBContainer influxDB = new InfluxDBContainer("influxdb:3-core")) {
3035
* influxDB.start();
3136
* String url = influxDB.getUrl();
3237
* String token = influxDB.getToken();
@@ -35,31 +40,29 @@
3540
* }</pre>
3641
* </p>
3742
*/
38-
public class InfluxDBContainerV3<SELF extends InfluxDBContainerV3<SELF>> extends GenericContainer<SELF> {
43+
public class InfluxDBContainer extends GenericContainer<InfluxDBContainer> {
3944

4045
/**
4146
* The default port exposed by InfluxDB 3.
4247
*/
43-
public static final Integer INFLUXDB_PORT = 8181;
48+
private static final Integer INFLUXDB_PORT = 8181;
4449

4550
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb");
4651

4752
/**
48-
* The authentication token for InfluxDB 3. Lazily initialized and thread-safe.
53+
* The authentication token for InfluxDB 3.
4954
*/
50-
private volatile String token;
55+
@Getter
56+
private String token;
5157

52-
/**
53-
* Flag indicating whether authentication is disabled.
54-
*/
5558
private boolean isAuthDisable;
5659

5760
/**
5861
* Creates a new InfluxDB 3 container using the specified Docker image.
5962
*
6063
* @param dockerImageName the name of the Docker image
6164
*/
62-
public InfluxDBContainerV3(final DockerImageName dockerImageName) {
65+
public InfluxDBContainer(final DockerImageName dockerImageName) {
6366
super(dockerImageName);
6467
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
6568

@@ -78,7 +81,7 @@ public InfluxDBContainerV3(final DockerImageName dockerImageName) {
7881
*
7982
* @return the generated authentication token
8083
* @throws IllegalArgumentException if the token cannot be created due to HTTP or IO errors
81-
* @throws HttpResponseException if the InfluxDB server returns a non-201 status code
84+
* @throws HttpResponseException if the InfluxDB server returns a non-201 status code
8285
*/
8386
private String createToken() {
8487
HttpPost httpPost = new HttpPost(format("%s/api/v3/configure/token/admin", getUrl()));
@@ -114,14 +117,6 @@ protected void configure() {
114117
addEnv("INFLUXDB3_START_WITHOUT_AUTH", Boolean.toString(isAuthDisable));
115118
}
116119

117-
/**
118-
* @return a singleton set containing the mapped InfluxDB port
119-
*/
120-
@Override
121-
public Set<Integer> getLivenessCheckPortNumbers() {
122-
return Collections.singleton(getMappedPort(INFLUXDB_PORT));
123-
}
124-
125120
/**
126121
* Disables authentication for this InfluxDB instance.
127122
* <p>
@@ -130,8 +125,8 @@ public Set<Integer> getLivenessCheckPortNumbers() {
130125
*
131126
* @return this container instance for method chaining
132127
*/
133-
public InfluxDBContainerV3<SELF> withDisableAuth() {
134-
isAuthDisable = true;
128+
public InfluxDBContainer withAuthDisabled() {
129+
this.isAuthDisable = true;
135130
return this;
136131
}
137132

@@ -147,26 +142,10 @@ public String getUrl() {
147142
return "http://" + getHost() + ":" + getMappedPort(INFLUXDB_PORT);
148143
}
149144

150-
/**
151-
* Gets the authentication token for this InfluxDB instance.
152-
* <p>
153-
* The token is lazily initialized on first use and cached for subsequent calls.
154-
* This method is thread-safe.
155-
* </p>
156-
*
157-
* @return the authentication token
158-
* @throws IllegalArgumentException if authentication is disabled or token creation fails
159-
*/
160-
public String getToken() {
161-
String localToken = token;
162-
if (localToken == null) {
163-
synchronized (this) {
164-
localToken = token;
165-
if (localToken == null) {
166-
token = localToken = createToken();
167-
}
168-
}
145+
@Override
146+
protected void containerIsStarted(InspectContainerResponse containerInfo) {
147+
if (!isAuthDisable) {
148+
this.token = createToken();
169149
}
170-
return localToken;
171150
}
172151
}

modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV3Test.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.influxdb.v3.client.InfluxDBClient;
44
import com.influxdb.v3.client.Point;
55
import org.junit.Test;
6+
import org.testcontainers.influxdb.InfluxDBContainer;
67

78
import java.math.BigInteger;
89
import java.time.Instant;
@@ -11,16 +12,15 @@
1112
import java.util.stream.Stream;
1213

1314
import static org.assertj.core.api.Assertions.assertThat;
14-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1515
import static org.assertj.core.api.Assertions.fail;
1616

1717
public class InfluxDBContainerV3Test {
1818

1919
@Test
2020
public void createInfluxDBContainerV3WithAuthTokenTest() {
21-
try (InfluxDBContainerV3<?> container = new InfluxDBContainerV3<>(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE)) {
21+
try (final InfluxDBContainer container = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE)) {
2222
container.start();
23-
try (InfluxDBClient client = InfluxDBClient.getInstance(container.getUrl(), container.getToken().toCharArray(), "test")) {
23+
try (final InfluxDBClient client = InfluxDBClient.getInstance(container.getUrl(), container.getToken().toCharArray(), "test")) {
2424
assertThat(client).isNotNull();
2525
assertThat(client.getServerVersion()).isEqualTo("3.3.0");
2626
} catch (Exception e) {
@@ -31,9 +31,9 @@ public void createInfluxDBContainerV3WithAuthTokenTest() {
3131

3232
@Test
3333
public void createInfluxDBContainerV3WithDisableAuthTokenTest() {
34-
try (final InfluxDBContainerV3<?> container = new InfluxDBContainerV3<>(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE).withDisableAuth()) {
34+
try (final InfluxDBContainer container = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE).withAuthDisabled()) {
3535
container.start();
36-
try (InfluxDBClient client = InfluxDBClient.getInstance(container.getUrl(), null, "test")) {
36+
try (final InfluxDBClient client = InfluxDBClient.getInstance(container.getUrl(), null, "test")) {
3737
assertThat(client).isNotNull();
3838
assertThat(client.getServerVersion()).isEqualTo("3.3.0");
3939
} catch (Exception e) {
@@ -43,18 +43,18 @@ public void createInfluxDBContainerV3WithDisableAuthTokenTest() {
4343
}
4444

4545
@Test
46-
public void tryToGetTokenWithAuthDisableTest() {
47-
try (final InfluxDBContainerV3<?> container = new InfluxDBContainerV3<>(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE).withDisableAuth()) {
46+
public void getTokenWithAuthDisableTest() {
47+
try (final InfluxDBContainer container = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE).withAuthDisabled()) {
4848
container.start();
49-
assertThatThrownBy(container::getToken).isInstanceOf(IllegalArgumentException.class);
49+
assertThat(container.getToken()).isNull();
5050
}
5151
}
5252

5353
@Test
5454
public void writeAndReadResultTest() {
55-
try (InfluxDBContainerV3<?> container = new InfluxDBContainerV3<>(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE)) {
55+
try (final InfluxDBContainer container = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE)) {
5656
container.start();
57-
try (InfluxDBClient client = InfluxDBClient.getInstance(container.getUrl(), container.getToken().toCharArray(), "test")) {
57+
try (final InfluxDBClient client = InfluxDBClient.getInstance(container.getUrl(), container.getToken().toCharArray(), "test")) {
5858
String location = "west";
5959
Double value = 55.15;
6060
Point point = Point.measurement("temperature")
@@ -63,7 +63,7 @@ public void writeAndReadResultTest() {
6363
.setTimestamp(Instant.now());
6464
client.writePoint(point);
6565
String query = "select time,location,value from temperature";
66-
try (Stream<Object[]> result = client.query(query)) {
66+
try (final Stream<Object[]> result = client.query(query)) {
6767
List<Object[]> rows = result.collect(Collectors.toList());
6868
assertThat(rows).hasSize(1);
6969
Object[] row = rows.get(0);

0 commit comments

Comments
 (0)