Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TestContainers to test Apache Cassandra backend #307

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}