From 9f01da8bf57d3222b5f2371e54088d610a61303f Mon Sep 17 00:00:00 2001 From: vroyer Date: Tue, 23 Mar 2021 23:59:00 +0100 Subject: [PATCH] Refacto the CassandraContainer --- modules/cassandra/build.gradle | 1 + .../CassandraContainer.java | 26 ++++++++++-------- .../delegate/CassandraDatabaseDelegate.java | 4 +-- .../wait/CassandraQueryWaitStrategy.java | 4 +-- .../CassandraContainerTest.java | 27 ++++++++++--------- 5 files changed, 34 insertions(+), 28 deletions(-) rename modules/cassandra/src/main/java/org/testcontainers/{containers => cassandra}/CassandraContainer.java (89%) rename modules/cassandra/src/main/java/org/testcontainers/{containers => cassandra}/delegate/CassandraDatabaseDelegate.java (94%) rename modules/cassandra/src/main/java/org/testcontainers/{containers => cassandra}/wait/CassandraQueryWaitStrategy.java (92%) rename modules/cassandra/src/test/java/org/testcontainers/{containers => cassandra}/CassandraContainerTest.java (79%) diff --git a/modules/cassandra/build.gradle b/modules/cassandra/build.gradle index 55c03dda4e8..590512e3434 100644 --- a/modules/cassandra/build.gradle +++ b/modules/cassandra/build.gradle @@ -1,6 +1,7 @@ description = "TestContainers :: Cassandra" dependencies { + implementation project(':testcontainers') implementation project(":database-commons") implementation "com.datastax.oss:java-driver-core:4.10.0" } diff --git a/modules/cassandra/src/main/java/org/testcontainers/containers/CassandraContainer.java b/modules/cassandra/src/main/java/org/testcontainers/cassandra/CassandraContainer.java similarity index 89% rename from modules/cassandra/src/main/java/org/testcontainers/containers/CassandraContainer.java rename to modules/cassandra/src/main/java/org/testcontainers/cassandra/CassandraContainer.java index 7523d5873bf..c6313179c25 100644 --- a/modules/cassandra/src/main/java/org/testcontainers/containers/CassandraContainer.java +++ b/modules/cassandra/src/main/java/org/testcontainers/cassandra/CassandraContainer.java @@ -1,10 +1,13 @@ -package org.testcontainers.containers; +package org.testcontainers.cassandra; import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.CqlSessionBuilder; import com.github.dockerjava.api.command.InspectContainerResponse; import org.apache.commons.io.IOUtils; -import org.testcontainers.containers.delegate.CassandraDatabaseDelegate; +import org.testcontainers.containers.ContainerState; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.cassandra.delegate.CassandraDatabaseDelegate; +import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy; import org.testcontainers.delegate.DatabaseDelegate; import org.testcontainers.ext.ScriptUtils; import org.testcontainers.ext.ScriptUtils.ScriptLoadException; @@ -16,6 +19,7 @@ import java.net.InetSocketAddress; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.time.Duration; import java.util.Optional; /** @@ -25,7 +29,7 @@ * * @author Eugeny Karpov */ -public class CassandraContainer> extends GenericContainer { +public class CassandraContainer extends GenericContainer { private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("cassandra"); private static final String DEFAULT_TAG = "3.11.2"; @@ -58,9 +62,9 @@ public CassandraContainer(DockerImageName dockerImageName) { super(dockerImageName); dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - + logger().info("Starting an cassandra container using [{}]", dockerImageName); addExposedPort(CQL_PORT); - setStartupAttempts(3); + setWaitStrategy(new HostPortWaitStrategy().withStartupTimeout(Duration.ofMinutes(2))); } @Override @@ -119,9 +123,9 @@ protected void optionallyMapResourceParameterAsVolume(String pathNameInContainer * * @param configLocation relative classpath with the directory that contains cassandra.yaml and other configuration files */ - public SELF withConfigurationOverride(String configLocation) { + public CassandraContainer withConfigurationOverride(String configLocation) { this.configLocation = configLocation; - return self(); + return this; } /** @@ -131,9 +135,9 @@ public SELF withConfigurationOverride(String configLocation) { * * @param initScriptPath relative classpath resource */ - public SELF withInitScript(String initScriptPath) { + public CassandraContainer withInitScript(String initScriptPath) { this.initScriptPath = initScriptPath; - return self(); + return this; } /** @@ -142,9 +146,9 @@ public SELF withInitScript(String initScriptPath) { * * @param metricRegistry */ - public SELF withMetricRegistry(Object metricRegistry) { + public CassandraContainer withMetricRegistry(Object metricRegistry) { this.metricRegistry = metricRegistry; - return self(); + return this; } /** diff --git a/modules/cassandra/src/main/java/org/testcontainers/containers/delegate/CassandraDatabaseDelegate.java b/modules/cassandra/src/main/java/org/testcontainers/cassandra/delegate/CassandraDatabaseDelegate.java similarity index 94% rename from modules/cassandra/src/main/java/org/testcontainers/containers/delegate/CassandraDatabaseDelegate.java rename to modules/cassandra/src/main/java/org/testcontainers/cassandra/delegate/CassandraDatabaseDelegate.java index f5eb566a042..9346ea8c99e 100644 --- a/modules/cassandra/src/main/java/org/testcontainers/containers/delegate/CassandraDatabaseDelegate.java +++ b/modules/cassandra/src/main/java/org/testcontainers/cassandra/delegate/CassandraDatabaseDelegate.java @@ -1,4 +1,4 @@ -package org.testcontainers.containers.delegate; +package org.testcontainers.cassandra.delegate; import com.datastax.oss.driver.api.core.CqlSession; @@ -6,7 +6,7 @@ import com.datastax.oss.driver.api.core.cql.ResultSet; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.testcontainers.containers.CassandraContainer; +import org.testcontainers.cassandra.CassandraContainer; import org.testcontainers.containers.ContainerState; import org.testcontainers.delegate.AbstractDatabaseDelegate; import org.testcontainers.exception.ConnectionCreationException; diff --git a/modules/cassandra/src/main/java/org/testcontainers/containers/wait/CassandraQueryWaitStrategy.java b/modules/cassandra/src/main/java/org/testcontainers/cassandra/wait/CassandraQueryWaitStrategy.java similarity index 92% rename from modules/cassandra/src/main/java/org/testcontainers/containers/wait/CassandraQueryWaitStrategy.java rename to modules/cassandra/src/main/java/org/testcontainers/cassandra/wait/CassandraQueryWaitStrategy.java index cf7f3769985..0668089bef6 100644 --- a/modules/cassandra/src/main/java/org/testcontainers/containers/wait/CassandraQueryWaitStrategy.java +++ b/modules/cassandra/src/main/java/org/testcontainers/cassandra/wait/CassandraQueryWaitStrategy.java @@ -1,8 +1,8 @@ -package org.testcontainers.containers.wait; +package org.testcontainers.cassandra.wait; import org.rnorth.ducttape.TimeoutException; import org.testcontainers.containers.ContainerLaunchException; -import org.testcontainers.containers.delegate.CassandraDatabaseDelegate; +import org.testcontainers.cassandra.delegate.CassandraDatabaseDelegate; import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy; import org.testcontainers.delegate.DatabaseDelegate; diff --git a/modules/cassandra/src/test/java/org/testcontainers/containers/CassandraContainerTest.java b/modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraContainerTest.java similarity index 79% rename from modules/cassandra/src/test/java/org/testcontainers/containers/CassandraContainerTest.java rename to modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraContainerTest.java index e5061b31627..a0789075657 100644 --- a/modules/cassandra/src/test/java/org/testcontainers/containers/CassandraContainerTest.java +++ b/modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraContainerTest.java @@ -1,4 +1,4 @@ -package org.testcontainers.containers; +package org.testcontainers.cassandra; import com.datastax.oss.driver.api.core.CqlSession; @@ -6,7 +6,8 @@ import com.datastax.oss.driver.api.core.cql.Row; import lombok.extern.slf4j.Slf4j; import org.junit.Test; -import org.testcontainers.containers.wait.CassandraQueryWaitStrategy; +import org.testcontainers.cassandra.wait.CassandraQueryWaitStrategy; +import org.testcontainers.containers.ContainerLaunchException; import org.testcontainers.utility.DockerImageName; import java.net.InetSocketAddress; @@ -21,13 +22,13 @@ @Slf4j public class CassandraContainerTest { - private static final DockerImageName CASSANDRA_IMAGE = DockerImageName.parse("cassandra:3.11.9"); + private static final DockerImageName CASSANDRA_IMAGE = DockerImageName.parse("cassandra:3.11.2"); private static final String TEST_CLUSTER_NAME_IN_CONF = "Test Cluster Integration Test"; @Test public void testSimple() { - try (CassandraContainer cassandraContainer = new CassandraContainer<>(CASSANDRA_IMAGE)) { + try (CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE)) { cassandraContainer.start(); ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local"); assertTrue("Query was not applied", resultSet.wasApplied()); @@ -38,7 +39,7 @@ public void testSimple() { @Test public void testSpecificVersion() { String cassandraVersion = "3.0.15"; - try (CassandraContainer cassandraContainer = new CassandraContainer<>(CASSANDRA_IMAGE.withTag(cassandraVersion))) { + try (CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE.withTag(cassandraVersion))) { cassandraContainer.start(); ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local"); assertTrue("Query was not applied", resultSet.wasApplied()); @@ -49,7 +50,7 @@ public void testSpecificVersion() { @Test public void testConfigurationOverride() { try ( - CassandraContainer cassandraContainer = new CassandraContainer<>(CASSANDRA_IMAGE) + CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE) .withConfigurationOverride("cassandra-test-configuration-example") ) { cassandraContainer.start(); @@ -62,7 +63,7 @@ public void testConfigurationOverride() { @Test(expected = ContainerLaunchException.class) public void testEmptyConfigurationOverride() { try ( - CassandraContainer cassandraContainer = new CassandraContainer<>(CASSANDRA_IMAGE) + CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE) .withConfigurationOverride("cassandra-empty-configuration") ) { cassandraContainer.start(); @@ -72,7 +73,7 @@ public void testEmptyConfigurationOverride() { @Test public void testInitScript() { try ( - CassandraContainer cassandraContainer = new CassandraContainer<>(CASSANDRA_IMAGE) + CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE) .withInitScript("initial.cql") ) { cassandraContainer.start(); @@ -83,7 +84,7 @@ public void testInitScript() { @Test public void testInitScriptWithLegacyCassandra() { try ( - CassandraContainer cassandraContainer = new CassandraContainer<>(DockerImageName.parse("cassandra:2.2.11")) + CassandraContainer cassandraContainer = new CassandraContainer(DockerImageName.parse("cassandra:2.2.11")) .withInitScript("initial.cql") ) { cassandraContainer.start(); @@ -95,7 +96,7 @@ public void testInitScriptWithLegacyCassandra() { @Test public void testCassandraQueryWaitStrategy() { try ( - CassandraContainer cassandraContainer = new CassandraContainer<>() + CassandraContainer cassandraContainer = new CassandraContainer() .waitingFor(new CassandraQueryWaitStrategy()) ) { cassandraContainer.start(); @@ -107,7 +108,7 @@ public void testCassandraQueryWaitStrategy() { @SuppressWarnings("deprecation") // Using deprecated constructor for verification of backwards compatibility @Test public void testCassandraGetCluster() { - try (CassandraContainer cassandraContainer = new CassandraContainer<>()) { + try (CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE)) { cassandraContainer.start(); ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local"); assertTrue("Query was not applied", resultSet.wasApplied()); @@ -115,7 +116,7 @@ public void testCassandraGetCluster() { } } - private void testInitScript(CassandraContainer cassandraContainer) { + private void testInitScript(CassandraContainer cassandraContainer) { ResultSet resultSet = performQuery(cassandraContainer, "SELECT * FROM keySpaceTest.catalog_category"); assertTrue("Query was not applied", resultSet.wasApplied()); Row row = resultSet.one(); @@ -123,7 +124,7 @@ private void testInitScript(CassandraContainer cassandraContainer) { assertEquals("Inserted row is not in expected state", "test_category", row.getString(1)); } - private ResultSet performQuery(CassandraContainer cassandraContainer, String cql) { + private ResultSet performQuery(CassandraContainer cassandraContainer, String cql) { InetSocketAddress endpoint = new InetSocketAddress(cassandraContainer.getHost(), CassandraContainer.CQL_PORT); CqlSession cqlSession = CqlSession.builder() .addContactPoint(endpoint)