org.testcontainers.containers.delegate.CassandraDatabaseDelegate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra Show documentation
Show all versions of cassandra Show documentation
Isolated container management for Java code testing
package org.testcontainers.containers.delegate;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.DriverException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.testcontainers.containers.CassandraContainer;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.delegate.AbstractDatabaseDelegate;
import org.testcontainers.exception.ConnectionCreationException;
import org.testcontainers.ext.ScriptUtils.ScriptStatementFailedException;
/**
* Cassandra database delegate
*/
@Slf4j
@RequiredArgsConstructor
public class CassandraDatabaseDelegate extends AbstractDatabaseDelegate {
private final ContainerState container;
@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);
}
}
@Override
public void execute(
String statement,
String scriptPath,
int lineNumber,
boolean continueOnError,
boolean ignoreFailedDrops
) {
try {
ResultSet result = getConnection().execute(statement);
if (result.wasApplied()) {
log.debug("Statement {} was applied", statement);
} else {
throw new ScriptStatementFailedException(statement, lineNumber, scriptPath);
}
} catch (DriverException e) {
throw new ScriptStatementFailedException(statement, lineNumber, scriptPath, e);
}
}
@Override
protected void closeConnectionQuietly(Session session) {
try {
session.getCluster().close();
} catch (Exception e) {
log.error("Could not close cassandra connection", e);
}
}
}