Skip to content

Commit

Permalink
Refacto the CassandraContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
vroyer committed Mar 23, 2021
1 parent 0cd4411 commit 9f01da8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
1 change: 1 addition & 0 deletions modules/cassandra/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/**
Expand All @@ -25,7 +29,7 @@
*
* @author Eugeny Karpov
*/
public class CassandraContainer<SELF extends CassandraContainer<SELF>> extends GenericContainer<SELF> {
public class CassandraContainer extends GenericContainer<CassandraContainer> {

private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("cassandra");
private static final String DEFAULT_TAG = "3.11.2";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.testcontainers.containers.delegate;
package org.testcontainers.cassandra.delegate;


import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.DriverException;
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +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.cql.ResultSet;
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;
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -107,23 +108,23 @@ 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());
assertNotNull("Result set has no release_version", resultSet.one().getString(0));
}
}

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();
assertEquals("Inserted row is not in expected state", 1, row.getLong(0));
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)
Expand Down

0 comments on commit 9f01da8

Please sign in to comment.