Class CassandraDockerResource

java.lang.Object
org.junit.rules.ExternalResource
no.mnemonic.commons.junit.docker.DockerResource
no.mnemonic.commons.junit.docker.CassandraDockerResource
All Implemented Interfaces:
org.junit.rules.TestRule

public class CassandraDockerResource extends DockerResource
CassandraDockerResource is a JUnit resource which can be used to write integration tests against a Cassandra server executed inside an isolated Docker container. It extends the basic DockerResource and makes sure that the container initialization waits until Cassandra is available. It is also possible to initialize Cassandra with a schema and data by providing a CQL start up script. In addition, data stored into Cassandra by tests can be truncated by providing a truncate CQL script and calling truncate().

Initialize CassandraDockerResource in the following way as a ClassRule:

 @ClassRule
  public static CassandraDockerResource cassandra = CassandraDockerResource.builder()
     .setImageName("cassandra")
     .addApplicationPort(9042)
     .setSetupScript("setup.cql")
     .setTruncateScript("truncate.cql")
     .build();
 
See DockerResource.Builder and CassandraDockerResource.Builder for more information on the configuration properties.
  • Method Details

    • truncate

      public void truncate()
      Truncate data stored inside Cassandra by executing the truncate CQL script.
      Throws:
      IllegalStateException - If CQL script could not be executed
    • builder

      public static CassandraDockerResource.Builder builder()
      Create builder for CassandraDockerResource.
      Returns:
      Builder object
    • additionalHostConfig

      protected org.mandas.docker.client.messages.HostConfig additionalHostConfig(org.mandas.docker.client.messages.HostConfig config)
      Adds Cassandra specific host configuration to default configuration from DockerResource.
      Overrides:
      additionalHostConfig in class DockerResource
      Parameters:
      config - Default configuration as set up by DockerResource
      Returns:
      Modified host configuration
    • isContainerReachable

      protected boolean isContainerReachable()
      Verifies that Cassandra is reachable by issuing a simple cqlsh command inside the Cassandra Docker container.
      Overrides:
      isContainerReachable in class DockerResource
      Returns:
      True if cqlsh command returns successfully
      Throws:
      IllegalStateException - If cqlsh command could not be executed
    • prepareContainer

      protected void prepareContainer()
      Initializes Cassandra by executing the set up CQL script.
      Overrides:
      prepareContainer in class DockerResource
      Throws:
      IllegalStateException - If CQL script could not be executed