/** * Get configured Cluster * * Can be used to obtain connections to Cassandra in the container */ public Cluster getCluster() { return getCluster(this); }
public CassandraContainer(String dockerImageName) { super(dockerImageName); addExposedPort(CQL_PORT); setStartupAttempts(3); }
@Override protected void configure() { optionallyMapResourceParameterAsVolume(CONTAINER_CONFIG_LOCATION, configLocation); }
@Test public void testCassandraGetCluster() { try (CassandraContainer cassandraContainer = new CassandraContainer<>()) { cassandraContainer.start(); ResultSet resultSet = performQuery(cassandraContainer.getCluster(), "SELECT release_version FROM system.local"); assertTrue("Query was not applied", resultSet.wasApplied()); assertNotNull("Result set has no release_version", resultSet.one().getString(0)); } }
@Test public void testInitScript() { try ( CassandraContainer cassandraContainer = new CassandraContainer<>() .withInitScript("initial.cql") ) { cassandraContainer.start(); testInitScript(cassandraContainer); } }
@Test(expected = ContainerLaunchException.class) public void testEmptyConfigurationOverride() { try ( CassandraContainer cassandraContainer = new CassandraContainer<>() .withConfigurationOverride("cassandra-empty-configuration") ) { cassandraContainer.start(); } }
@Test public void testCassandraQueryWaitStrategy() { try ( CassandraContainer cassandraContainer = new CassandraContainer<>() .waitingFor(new CassandraQueryWaitStrategy()) ) { cassandraContainer.start(); ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local"); assertTrue("Query was not applied", resultSet.wasApplied()); } }
@Test public void testSpecificVersion() { String cassandraVersion = "3.0.15"; try (CassandraContainer cassandraContainer = new CassandraContainer<>("cassandra:" + cassandraVersion)) { cassandraContainer.start(); ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local"); assertTrue("Query was not applied", resultSet.wasApplied()); assertEquals("Cassandra has wrong version", cassandraVersion, resultSet.one().getString(0)); } }
/** * Load init script content and apply it to the database if initScriptPath is set */ 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 ResultSet performQuery(CassandraContainer cassandraContainer, String cql) { Cluster explicitCluster = Cluster.builder() .addContactPoint(cassandraContainer.getContainerIpAddress()) .withPort(cassandraContainer.getMappedPort(CassandraContainer.CQL_PORT)) .build(); return performQuery(explicitCluster, cql); }
/** * Initialize Cassandra with init CQL script * <p> * CQL script will be applied after container is started (see using WaitStrategy) * * @param initScriptPath relative classpath resource */ public SELF withInitScript(String initScriptPath) { this.initScriptPath = initScriptPath; return self(); }
@Override protected void containerIsStarted(InspectContainerResponse containerInfo) { runInitScriptIfRequired(); }
@Test public void testInitScriptWithLegacyCassandra() { try ( CassandraContainer cassandraContainer = new CassandraContainer<>("cassandra:2.2.11") .withInitScript("initial.cql") ) { cassandraContainer.start(); testInitScript(cassandraContainer); } }
@Test public void testConfigurationOverride() { try ( CassandraContainer cassandraContainer = new CassandraContainer<>() .withConfigurationOverride("cassandra-test-configuration-example") ) { cassandraContainer.start(); ResultSet resultSet = performQuery(cassandraContainer, "SELECT cluster_name FROM system.local"); assertTrue("Query was not applied", resultSet.wasApplied()); assertEquals("Cassandra configuration is not overridden", TEST_CLUSTER_NAME_IN_CONF, resultSet.one().getString(0)); } }
@Test public void testSimple() { try (CassandraContainer cassandraContainer = new CassandraContainer<>()) { 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)); } }
/** * Initialize Cassandra with the custom overridden Cassandra configuration * <p> * Be aware, that Docker effectively replaces all /etc/cassandra content with the content of config location, so if * Cassandra.yaml in configLocation is absent or corrupted, then Cassandra just won't launch * * @param configLocation relative classpath with the directory that contains cassandra.yaml and other configuration files */ public SELF withConfigurationOverride(String configLocation) { this.configLocation = configLocation; return self(); }
@Override protected Session createNewConnection() { try { return CassandraContainer.getCluster(container) .newSession(); } catch (DriverException e) { log.error("Could not obtain cassandra connection"); throw new ConnectionCreationException("Could not obtain cassandra connection", e); } }