Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import com.datastax.driver.core.Cluster;
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.apache.commons.io.IOUtils;
import org.testcontainers.containers.delegate.CassandraDatabaseDelegate;
import org.testcontainers.delegate.DatabaseDelegate;
import org.testcontainers.ext.ScriptUtils;
import org.testcontainers.ext.ScriptUtils.ScriptLoadException;
import org.testcontainers.utility.MountableFile;

import javax.script.ScriptException;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
Expand All @@ -31,7 +28,7 @@ public class CassandraContainer<SELF extends CassandraContainer<SELF>> extends G
private static final String PASSWORD = "cassandra";

private String configLocation;
private String initScriptPath;
private List<String> initScriptPaths;
private boolean enableJmxReporting;

public CassandraContainer() {
Expand All @@ -43,6 +40,7 @@ public CassandraContainer(String dockerImageName) {
addExposedPort(CQL_PORT);
setStartupAttempts(3);
this.enableJmxReporting = false;
this.initScriptPaths = new ArrayList<>();
}

@Override
Expand All @@ -52,30 +50,15 @@ protected void configure() {

@Override
protected void containerIsStarted(InspectContainerResponse containerInfo) {
runInitScriptIfRequired();
runInitScriptsIfRequired();
}

/**
* Load init script content and apply it to the database if initScriptPath is set
* Apply all configured init scripts to the running database
*/
private void runInitScriptIfRequired() {
if (initScriptPath != null) {
try {
URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
if (resource == null) {
logger().warn("Could not load classpath init script: {}", initScriptPath);
throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath + ". Resource not found.");
}
String cql = IOUtils.toString(resource, StandardCharsets.UTF_8);
DatabaseDelegate databaseDelegate = getDatabaseDelegate();
ScriptUtils.executeDatabaseScript(databaseDelegate, initScriptPath, cql);
} catch (IOException e) {
logger().warn("Could not load classpath init script: {}", initScriptPath);
throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath, e);
} catch (ScriptException e) {
logger().error("Error while executing init script: {}", initScriptPath, e);
throw new ScriptUtils.UncategorizedScriptException("Error while executing init script: " + initScriptPath, e);
}
private void runInitScriptsIfRequired() {
for (String initScriptPath : initScriptPaths) {
ScriptUtils.runInitScript(getDatabaseDelegate(), initScriptPath);
}
}

Expand Down Expand Up @@ -107,14 +90,16 @@ public SELF withConfigurationOverride(String configLocation) {
}

/**
* Initialize Cassandra with init CQL script
* Initialize Cassandra with one or more init CQL scripts.
* <p>
* CQL script will be applied after container is started (see using WaitStrategy)
* The CQL scripts will be applied after container is started (see using WaitStrategy)
*
* @param initScriptPath relative classpath resource
*/
public SELF withInitScript(String initScriptPath) {
this.initScriptPath = initScriptPath;
public SELF withInitScript(String initScriptPath, String... extraInitScriptPaths) {
this.initScriptPaths = new ArrayList<>();
this.initScriptPaths.add(initScriptPath);
this.initScriptPaths.addAll(Arrays.asList(extraInitScriptPaths));
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,34 @@ public void testEmptyConfigurationOverride() {

@Test
public void testInitScript() {
try (
CassandraContainer cassandraContainer = new CassandraContainer<>().withInitScript("initial-1.cql")
) {
cassandraContainer.start();
testInitScript1(cassandraContainer);
}
}

@Test
public void testMultipleInitScripts() {
try (
CassandraContainer cassandraContainer = new CassandraContainer<>()
.withInitScript("initial.cql")
.withInitScript("initial-1.cql", "initial-2.cql")
) {
cassandraContainer.start();
testInitScript(cassandraContainer);
testInitScript1(cassandraContainer);
testInitScript2(cassandraContainer);
}
}

@Test
public void testInitScriptWithLegacyCassandra() {
try (
CassandraContainer cassandraContainer = new CassandraContainer<>("cassandra:2.2.11")
.withInitScript("initial.cql")
.withInitScript("initial-1.cql")
) {
cassandraContainer.start();
testInitScript(cassandraContainer);
testInitScript1(cassandraContainer);
}
}

Expand All @@ -106,14 +117,22 @@ public void testCassandraGetCluster() {
}
}

private void testInitScript(CassandraContainer cassandraContainer) {
ResultSet resultSet = performQuery(cassandraContainer, "SELECT * FROM keySpaceTest.catalog_category");
private void testInitScript1(CassandraContainer cassandraContainer) {
ResultSet resultSet = performQuery(cassandraContainer, "SELECT * FROM keySpaceTest.catalog_category WHERE id = 1");
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 void testInitScript2(CassandraContainer cassandraContainer) {
ResultSet resultSet = performQuery(cassandraContainer, "SELECT * FROM keySpaceTest.catalog_category WHERE id = 2");
assertTrue("Query was not applied", resultSet.wasApplied());
Row row = resultSet.one();
assertEquals("Inserted row is not in expected state", 2, row.getLong(0));
assertEquals("Inserted row is not in expected state", "another_test_category", row.getString(1));
}

private ResultSet performQuery(CassandraContainer cassandraContainer, String cql) {
Cluster explicitCluster = Cluster.builder()
.addContactPoint(cassandraContainer.getHost())
Expand Down
2 changes: 2 additions & 0 deletions modules/cassandra/src/test/resources/initial-2.cql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
USE keySpaceTest;
INSERT INTO catalog_category (id, name) VALUES (2, 'another_test_category');