Skip to content

Commit

Permalink
Add TestContainers to test Apache Cassandra backend (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximthomas authored Oct 3, 2023
1 parent 7b24f6a commit 623bc6b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
7 changes: 7 additions & 0 deletions opendj-server-legacy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@
</exclusion>
</exclusions>
</dependency>
<!-- TestContainers-->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>cassandra</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build><finalName>${project.groupId}.${project.artifactId}</finalName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,35 @@

import org.forgerock.opendj.server.config.server.CASBackendCfg;
import org.opends.server.backends.pluggable.PluggableBackendImplTestCase;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.CassandraContainer;
import org.testng.SkipException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

import com.datastax.oss.driver.api.core.AllNodesFailedException;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;

import java.net.InetSocketAddress;

//docker run --rm -it -p 9042:9042 --name cassandra cassandra

@Test
public class EncryptedTestCase extends PluggableBackendImplTestCase<CASBackendCfg>
{
public class EncryptedTestCase extends PluggableBackendImplTestCase<CASBackendCfg> {
CassandraContainer cassandraContainer;
@Override
protected Backend createBackend()
{
protected Backend createBackend() {
if(DockerClientFactory.instance().isDockerAvailable()) {
cassandraContainer = new CassandraContainer<>("cassandra:latest").withExposedPorts(9042);
cassandraContainer.start();
InetSocketAddress contactPoint = cassandraContainer.getContactPoint();
final String contactPointString = String.format("%s:%s", contactPoint.getHostName(), contactPoint.getPort());
System.setProperty("datastax-java-driver.basic.contact-points.0", contactPointString);
System.setProperty("datastax-java-driver.basic.load-balancing-policy.local-datacenter", cassandraContainer.getLocalDatacenter());
}
System.setProperty("datastax-java-driver.basic.request.timeout", "30 seconds"); //for docker slow start

//test allow cassandra
try(CqlSession session=CqlSession.builder()
.withConfigLoader(DriverConfigLoader.fromDefaults(Storage.class.getClassLoader()))
Expand All @@ -48,8 +61,7 @@ protected Backend createBackend()
}

@Override
protected CASBackendCfg createBackendCfg()
{
protected CASBackendCfg createBackendCfg() {
CASBackendCfg backendCfg = mockCfg(CASBackendCfg.class);
when(backendCfg.getBackendId()).thenReturn("EncCASTestCase"+System.currentTimeMillis());
when(backendCfg.getDBDirectory()).thenReturn("EncCASTestCase");
Expand All @@ -59,4 +71,12 @@ protected CASBackendCfg createBackendCfg()
when(backendCfg.getCipherTransformation()).thenReturn("AES/CBC/PKCS5Padding");
return backendCfg;
}
@AfterClass
@Override
public void cleanUp() throws Exception {
super.cleanUp();
if(cassandraContainer != null) {
cassandraContainer.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,35 @@

import org.forgerock.opendj.server.config.server.CASBackendCfg;
import org.opends.server.backends.pluggable.PluggableBackendImplTestCase;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.CassandraContainer;
import org.testng.SkipException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

import com.datastax.oss.driver.api.core.AllNodesFailedException;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;

import java.net.InetSocketAddress;

//docker run --rm -it -p 9042:9042 --name cassandra cassandra

@Test
public class TestCase extends PluggableBackendImplTestCase<CASBackendCfg> {

CassandraContainer cassandraContainer;
@Override
protected Backend createBackend() {
System.setProperty("datastax-java-driver.basic.request.timeout", "30 seconds"); //for docker slow start
if(DockerClientFactory.instance().isDockerAvailable()) {
cassandraContainer = new CassandraContainer<>("cassandra:latest").withExposedPorts(9042);
cassandraContainer.start();
InetSocketAddress contactPoint = cassandraContainer.getContactPoint();
final String contactPointString = String.format("%s:%s", contactPoint.getHostName(), contactPoint.getPort());
System.setProperty("datastax-java-driver.basic.contact-points.0", contactPointString);
System.setProperty("datastax-java-driver.basic.load-balancing-policy.local-datacenter", cassandraContainer.getLocalDatacenter());
}

//test allow cassandra
try(CqlSession session=CqlSession.builder()
.withConfigLoader(DriverConfigLoader.fromDefaults(Storage.class.getClassLoader()))
Expand All @@ -53,4 +67,13 @@ protected CASBackendCfg createBackendCfg() {
when(backendCfg.getDBDirectory()).thenReturn("CASTestCase");
return backendCfg;
}

@AfterClass
@Override
public void cleanUp() throws Exception {
super.cleanUp();
if(cassandraContainer != null) {
cassandraContainer.close();
}
}
}

0 comments on commit 623bc6b

Please sign in to comment.