Package no.mnemonic.commons.junit.docker
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
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Builder to create a CassandraDockerResource which extendsDockerResource.Builder
. -
Method Summary
Modifier and TypeMethodDescriptionprotected org.mandas.docker.client.messages.HostConfig
additionalHostConfig
(org.mandas.docker.client.messages.HostConfig config) Adds Cassandra specific host configuration to default configuration fromDockerResource
.builder()
Create builder for CassandraDockerResource.protected boolean
Verifies that Cassandra is reachable by issuing a simple cqlsh command inside the Cassandra Docker container.protected void
Initializes Cassandra by executing the set up CQL script.void
truncate()
Truncate data stored inside Cassandra by executing the truncate CQL script.Methods inherited from class no.mnemonic.commons.junit.docker.DockerResource
additionalContainerConfig, after, before, getContainerID, getDockerClient, getExposedHost, getExposedHostPort
Methods inherited from class org.junit.rules.ExternalResource
apply
-
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
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 fromDockerResource
.- Overrides:
additionalHostConfig
in classDockerResource
- 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 classDockerResource
- 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 classDockerResource
- Throws:
IllegalStateException
- If CQL script could not be executed
-