1- package org .testcontainers .containers ;
1+ package org .testcontainers .influxdb ;
22
33import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import com .github .dockerjava .api .command .InspectContainerResponse ;
45import com .github .dockerjava .zerodep .shaded .org .apache .hc .client5 .http .HttpResponseException ;
56import com .github .dockerjava .zerodep .shaded .org .apache .hc .client5 .http .classic .methods .HttpPost ;
67import com .github .dockerjava .zerodep .shaded .org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
78import com .github .dockerjava .zerodep .shaded .org .apache .hc .client5 .http .impl .classic .HttpClients ;
89import com .github .dockerjava .zerodep .shaded .org .apache .hc .core5 .http .HttpStatus ;
10+ import lombok .Getter ;
11+ import org .testcontainers .containers .GenericContainer ;
912import org .testcontainers .containers .wait .strategy .HttpWaitStrategy ;
1013import org .testcontainers .utility .DockerImageName ;
1114
1215import java .io .IOException ;
1316import java .io .InputStream ;
14- import java .util .Collections ;
15- import java .util .Set ;
1617
1718import 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();
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}
0 commit comments