Skip to content
35 changes: 6 additions & 29 deletions docs/modules/databases/cassandra.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,12 @@

This example connects to the Cassandra Cluster, creates a keyspaces and asserts that is has been created.

=== "JUnit 4 example"
```java
public class SomeTest {

@Rule
public CassandraContainer cassandra = new CassandraContainer();


@Test
public void test(){
Cluster cluster = cassandra.getCluster();

try(Session session = cluster.connect()) {

session.execute("CREATE KEYSPACE IF NOT EXISTS test WITH replication = \n" +
"{'class':'SimpleStrategy','replication_factor':'1'};");

List<KeyspaceMetadata> keyspaces = session.getCluster().getMetadata().getKeyspaces();
List<KeyspaceMetadata> filteredKeyspaces = keyspaces
.stream()
.filter(km -> km.getName().equals("test"))
.collect(Collectors.toList());

assertEquals(1, filteredKeyspaces.size());
}
}

}
```
<!--codeinclude-->
[Building CqlSession](../../../modules/cassandra/src/test/java/org/testcontainers/containers/CassandraDriver3Test.java) inside_block:cassandra
<!--/codeinclude-->

!!! warning
All methods returning instances of the Cassandra Driver's Cluster object in `CassandraContainer` have been deprecated. Providing these methods unnecessarily couples the Container to the Driver and creates potential breaking changes if the driver is updated.

## Adding this module to your project dependencies

Expand Down
9 changes: 8 additions & 1 deletion modules/cassandra/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
description = "TestContainers :: Cassandra"

configurations.all {
resolutionStrategy {
force 'io.dropwizard.metrics:metrics-core:3.2.4'
}
}

dependencies {
api project(":database-commons")
api "com.datastax.cassandra:cassandra-driver-core:3.7.1"
api "com.datastax.cassandra:cassandra-driver-core:3.10.0"
testImplementation 'com.datastax.oss:java-driver-core:4.8.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.testcontainers.utility.MountableFile;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
Expand All @@ -35,6 +36,8 @@ public class CassandraContainer<SELF extends CassandraContainer<SELF>> extends G

public static final Integer CQL_PORT = 9042;

private static final String DEFAULT_LOCAL_DATACENTER = "datacenter1";

private static final String CONTAINER_CONFIG_LOCATION = "/etc/cassandra";

private static final String USERNAME = "cassandra";
Expand Down Expand Up @@ -70,6 +73,8 @@ public CassandraContainer(DockerImageName dockerImageName) {
withEnv("JVM_OPTS", "-Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0");
withEnv("HEAP_NEWSIZE", "128M");
withEnv("MAX_HEAP_SIZE", "1024M");
withEnv("CASSANDRA_ENDPOINT_SNITCH", "GossipingPropertyFileSnitch");
withEnv("CASSANDRA_DC", DEFAULT_LOCAL_DATACENTER);
}

@Override
Expand Down Expand Up @@ -187,11 +192,20 @@ public String getPassword() {
* Get configured Cluster
*
* Can be used to obtain connections to Cassandra in the container
*
* @deprecated For Cassandra driver 3.x, use {@link #getHost()} and {@link #getMappedPort(int)} with
* the driver's {@link Cluster#builder() Cluster.Builder} {@code addContactPoint(String)} and
* {@code withPort(int)} methods to create a Cluster object. For Cassandra driver 4.x, use
* {@link #getContactPoint()} and {@link #getLocalDatacenter()} with the driver's {@code CqlSession.builder()}
* {@code addContactPoint(InetSocketAddress)} and {@code withLocalDatacenter(String)} methods to create
* a Session Object. See https://docs.datastax.com/en/developer/java-driver/ for more on the driver.
*/
@Deprecated
public Cluster getCluster() {
return getCluster(this, enableJmxReporting);
}

@Deprecated
public static Cluster getCluster(ContainerState containerState, boolean enableJmxReporting) {
final Cluster.Builder builder = Cluster
.builder()
Expand All @@ -203,10 +217,30 @@ public static Cluster getCluster(ContainerState containerState, boolean enableJm
return builder.build();
}

@Deprecated
public static Cluster getCluster(ContainerState containerState) {
return getCluster(containerState, false);
}

/**
* Retrieve an {@link InetSocketAddress} for connecting to the Cassandra container via the driver.
*
* @return A InetSocketAddrss representation of this Cassandra container's host and port.
*/
public InetSocketAddress getContactPoint() {
return new InetSocketAddress(getHost(), getMappedPort(CQL_PORT));
}

/**
* Retrieve the Local Datacenter for connecting to the Cassandra container via the driver.
*
* @return The configured local Datacenter name.
*/
public String getLocalDatacenter() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice that you added it, it was missing before.

return getEnvMap().getOrDefault("CASSANDRA_DC", DEFAULT_LOCAL_DATACENTER);
}

@Deprecated
private DatabaseDelegate getDatabaseDelegate() {
return new CassandraDatabaseDelegate(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ public class CassandraContainerTest {

private static final String TEST_CLUSTER_NAME_IN_CONF = "Test Cluster Integration Test";

private static final String BASIC_QUERY = "SELECT release_version FROM system.local";

@Test
public void testSimple() {
try (CassandraContainer<?> cassandraContainer = new CassandraContainer<>(CASSANDRA_IMAGE)) {
cassandraContainer.start();
ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local");
ResultSet resultSet = performQuery(cassandraContainer, BASIC_QUERY);
assertTrue("Query was not applied", resultSet.wasApplied());
assertNotNull("Result set has no release_version", resultSet.one().getString(0));
}
Expand All @@ -42,7 +44,7 @@ public void testSpecificVersion() {
)
) {
cassandraContainer.start();
ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local");
ResultSet resultSet = performQuery(cassandraContainer, BASIC_QUERY);
assertTrue("Query was not applied", resultSet.wasApplied());
assertEquals("Cassandra has wrong version", cassandraVersion, resultSet.one().getString(0));
}
Expand Down Expand Up @@ -107,7 +109,7 @@ public void testCassandraQueryWaitStrategy() {
.waitingFor(new CassandraQueryWaitStrategy())
) {
cassandraContainer.start();
ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local");
ResultSet resultSet = performQuery(cassandraContainer, BASIC_QUERY);
assertTrue("Query was not applied", resultSet.wasApplied());
}
}
Expand All @@ -117,10 +119,7 @@ public void testCassandraQueryWaitStrategy() {
public void testCassandraGetCluster() {
try (CassandraContainer<?> cassandraContainer = new CassandraContainer<>()) {
cassandraContainer.start();
ResultSet resultSet = performQuery(
cassandraContainer.getCluster(),
"SELECT release_version FROM system.local"
);
ResultSet resultSet = performQuery(cassandraContainer.getCluster(), BASIC_QUERY);
assertTrue("Query was not applied", resultSet.wasApplied());
assertNotNull("Result set has no release_version", resultSet.one().getString(0));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.testcontainers.containers;

import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;

public class CassandraDriver3Test {

@Rule
public CassandraContainer<?> cassandra = new CassandraContainer<>("cassandra:3.11.2");

@Test
public void testCassandraGetContactPoint() {
try (
// cassandra {
CqlSession session = CqlSession
.builder()
.addContactPoint(this.cassandra.getContactPoint())
.withLocalDatacenter(this.cassandra.getLocalDatacenter())
.build()
// }
) {
session.execute(
"CREATE KEYSPACE IF NOT EXISTS test WITH replication = \n" +
"{'class':'SimpleStrategy','replication_factor':'1'};"
);

KeyspaceMetadata keyspace = session.getMetadata().getKeyspaces().get(CqlIdentifier.fromCql("test"));

assertNotNull("Failed to create test keyspace", keyspace);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.testcontainers.containers;

import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;

public class CassandraDriver4Test {

@Rule
public CassandraContainer<?> cassandra = new CassandraContainer<>("cassandra:3.11.2");

@Test
public void testCassandraGetContactPoint() {
try (
CqlSession session = CqlSession
.builder()
.addContactPoint(this.cassandra.getContactPoint())
.withLocalDatacenter(this.cassandra.getLocalDatacenter())
.build()
) {
session.execute(
"CREATE KEYSPACE IF NOT EXISTS test WITH replication = \n" +
"{'class':'SimpleStrategy','replication_factor':'1'};"
);

KeyspaceMetadata keyspace = session.getMetadata().getKeyspaces().get(CqlIdentifier.fromCql("test"));

assertNotNull("Failed to create test keyspace", keyspace);
}
}
}