How to use CassandraContainer class of org.testcontainers.containers package

Best Testcontainers-java code snippet using org.testcontainers.containers.CassandraContainer

Source:CassandraDatabaseInitializer.java Github

copy

Full Screen

...17import org.springframework.data.cassandra.config.CqlSessionFactoryBean;18import org.springframework.lang.NonNull;19import org.slf4j.Logger;20import org.slf4j.LoggerFactory;21import org.testcontainers.containers.CassandraContainer;22/**23 * Abstract base class using Testcontainers to bootstrap, configure and initialize an Apache Cassandra database.24 *25 * @author John Blum26 * @see org.testcontainers.containers.CassandraContainer27 * @since 1.0.028 */29@SuppressWarnings("unused")30public abstract class CassandraDatabaseInitializer implements Runnable {31 public static final int CASSANDRA_DEFAULT_PORT = CqlSessionFactoryBean.DEFAULT_PORT;32 public static final String CASSANDRA_CONTACT_POINTS_PROPERTY = "spring.app.cassandra.contact-points";33 public static final String CASSANDRA_DEFAULT_HOSTNAME = "localhost";34 public static final String CASSANDRA_LOCAL_DATACENTER = "datacenter1";35 public static final String CASSANDRA_SCHEMA_CQL = "cassandra-schema.cql";36 public static final String CASSANDRA_SYSTEM_KEYSPACE_NAME = "system";37 public static final String CASSANDRA_TEST_KEYSPACE_NAME = "test";38 public static final String CASSANDRA_USERS_TABLE_NAME = "Users";39 protected static final String CASSANDRA_DOCKER_IMAGE_NAME = "cassandra:latest";40 public static CassandraDatabaseInitializer init() {41 CassandraDatabaseInitializer initializer = new CassandraDatabaseInitializer() { };42 initializer.run();43 return initializer;44 }45 private final CassandraContainer<?> cassandraContainer;46 private final Logger logger = LoggerFactory.getLogger(getClass());47 protected CassandraDatabaseInitializer() {48 this.cassandraContainer = newCassandraContainer();49 }50 protected CassandraContainer<?> newCassandraContainer() {51 return new CassandraContainer<>(CASSANDRA_DOCKER_IMAGE_NAME)52 .withExposedPorts(CASSANDRA_DEFAULT_PORT)53 .withInitScript(CASSANDRA_SCHEMA_CQL)54 .withReuse(true);55 }56 public @NonNull CassandraContainer<?> getCassandraContainer() {57 return this.cassandraContainer;58 }59 protected @NonNull Logger getLogger() {60 return this.logger;61 }62 @Override63 public void run() {64 getLogger().info("Bootstrapping Apache Cassandra database...");65 // Run the Apache Cassandra database.66 getCassandraContainer().start();67 configureEnvironment(getCassandraContainer());68 getLogger().info("Apache Cassandra databased initialized!");69 }70 private @NonNull CassandraContainer<?> configureEnvironment(@NonNull CassandraContainer<?> cassandraContainer) {71 String contactPoints = String.format("%s[%d]",72 CASSANDRA_DEFAULT_HOSTNAME, cassandraContainer.getMappedPort(CASSANDRA_DEFAULT_PORT));73 getLogger().info("Apache Cassandra database contact points [{}]", contactPoints);74 System.setProperty(CASSANDRA_CONTACT_POINTS_PROPERTY, contactPoints);75 getLogger().info("System-Property['{}'] = {}", CASSANDRA_CONTACT_POINTS_PROPERTY,76 System.getProperty(CASSANDRA_CONTACT_POINTS_PROPERTY));77 return cassandraContainer;78 }79}...

Full Screen

Full Screen

Source:CassandraLocalContainerService.java Github

copy

Full Screen

...18import org.apache.camel.test.infra.cassandra.common.CassandraProperties;19import org.apache.camel.test.infra.common.services.ContainerService;20import org.slf4j.Logger;21import org.slf4j.LoggerFactory;22import org.testcontainers.containers.CassandraContainer;23/**24 * A service for a local instance of Apache Cassandra running with TestContainers25 */26public class CassandraLocalContainerService implements CassandraService, ContainerService<CassandraContainer> {27 private static final Logger LOG = LoggerFactory.getLogger(CassandraLocalContainerService.class);28 private static final String IMAGE_NAME = "cassandra:3.11.2";29 private final CassandraContainer container;30 public CassandraLocalContainerService() {31 this(System.getProperty(CassandraProperties.CASSANDRA_CONTAINER, IMAGE_NAME));32 }33 public CassandraLocalContainerService(String imageName) {34 container = initContainer(IMAGE_NAME);35 }36 public CassandraLocalContainerService(CassandraContainer container) {37 this.container = container;38 }39 protected CassandraContainer initContainer(String imageName) {40 return new CassandraContainer(imageName);41 }42 @Override43 public int getCQL3Port() {44 return container.getMappedPort(CassandraContainer.CQL_PORT);45 }46 @Override47 public String getCassandraHost() {48 return container.getHost();49 }50 @Override51 public void registerProperties() {52 System.setProperty(CassandraProperties.CASSANDRA_HOST, getCassandraHost());53 System.setProperty(CassandraProperties.CASSANDRA_CQL3_PORT, String.valueOf(getCQL3Port()));54 }55 @Override56 public void initialize() {57 container.start();58 registerProperties();59 LOG.info("Cassandra server running at address {}", getCQL3Endpoint());60 }61 @Override62 public void shutdown() {63 LOG.info("Stopping the Cassandra container");64 container.stop();65 }66 @Override67 public CassandraContainer getContainer() {68 return container;69 }70}...

Full Screen

Full Screen

Source:TestContainersDemoApplicationTests.java Github

copy

Full Screen

...7import org.springframework.context.ApplicationContextInitializer;8import org.springframework.context.ConfigurableApplicationContext;9import org.springframework.context.annotation.Profile;10import org.springframework.test.context.ContextConfiguration;11import org.testcontainers.containers.CassandraContainer;12import org.testcontainers.junit.jupiter.Container;13import org.testcontainers.junit.jupiter.Testcontainers;14import com.example.testcontainers.domain.Car;15import com.example.testcontainers.repository.CarRepository;16import reactor.core.publisher.Flux;17import reactor.test.StepVerifier;18import static org.junit.jupiter.api.Assertions.assertEquals;19@Testcontainers20@SpringBootTest21@ContextConfiguration(initializers = TestContainersDemoApplicationTests.Initializer.class)22@Profile("testcontainer")23class TestContainersDemoApplicationTests {24 @Container25 private static final CassandraContainer cassandraContainer = new CassandraContainer<>()26 .withInitScript("insert.cql");27 @Autowired28 CarRepository testClassRepository;29 static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {30 public void initialize(ConfigurableApplicationContext configurableApplicationContext) {31 TestPropertyValues32 .of("spring.data.cassandra.contact-points=" + cassandraContainer.getContainerIpAddress(),33 "spring.data.cassandra.port=" + cassandraContainer.getMappedPort(9042),34 "spring.data.cassandra.keyspace-name=" + "test",35 "spring.data.cassandra.cluster-name=" + cassandraContainer.getCluster().getClusterName(),36 "spring.data.cassandra.username=" + cassandraContainer.getUsername(),37 "spring.data.cassandra.password=" + cassandraContainer.getPassword())38 .applyTo(configurableApplicationContext.getEnvironment());39 }...

Full Screen

Full Screen

CassandraContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.CassandraContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import org.testcontainers.utility.DockerImageName;4public class CassandraTestContainer {5 public static void main(String[] args) {6 try (CassandraContainer cassandraContainer = new CassandraContainer(DockerImageName.parse("cassandra:3.11.8"))) {7 cassandraContainer.start();8 System.out.println("Cassandra container started successfully");9 System.out.println("Cassandra container started successfully with following details");10 System.out.println("Cassandra container host: " + cassandraContainer.getHost());11 System.out.println("Cassandra container port: " + cassandraContainer.getFirstMappedPort());12 System.out.println("Cassandra container username: " + cassandraContainer.getUsername());13 System.out.println("Cassandra container password: " + cassandraContainer.getPassword());14 System.out.println("Cassandra container cluster name: " + cassandraContainer.getClusterName());15 System.out.println("Cassandra container container id: " + cassandraContainer.getContainerId());16 System.out.println("Cassandra container container name: " + cassandraContainer.getContainerName());17 System.out.println("Cassandra container image name: " + cassandraContainer.getDockerImageName());18 System.out.println("Cassandra container exposed ports: " + cassandraContainer.getExposedPorts());19 System.out.println("Cassandra container mapped ports: " + cassandraContainer.getMappedPort(9042));20 System.out.println("Cassandra container network: " + cassandraContainer.getNetwork());21 System.out.println("Cassandra container network aliases: " + cassandraContainer.getNetworkAliases());22 System.out.println("Cassandra container logs: " + cassandraContainer.getLogs());23 System.out.println("Cassandra container wait strategy: " + cassandraContainer.getWaitStrategy());24 System.out.println("Cassandra container internal host: " + cassandraContainer.getInternalHost());25 System.out.println("Cassandra container internal port: " + cassandraContainer.getInternalPort(9042));26 System.out.println("Cassandra container env: " + cassandraContainer.getEnvMap());27 System.out.println("Cassandra container labels: " + cassandraContainer.getLabels());28 System.out.println("Cassandra container cmd: " + cassandraContainer.getCmdParts());29 System.out.println("Cassandra container entrypoint: " + cassandraContainer.getEntrypoint());

Full Screen

Full Screen

CassandraContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.CassandraContainer;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.wait.strategy.Wait;4public class CassandraContainerDemo {5 public static void main(String[] args) {6 try (CassandraContainer cassandraContainer = new CassandraContainer()) {7 cassandraContainer.start();8 System.out.println("CassandraContainer started with IP Address: " + cassandraContainer.getContainerIpAddress());9 System.out.println("CassandraContainer started with Port: " + cassandraContainer.getMappedPort(9042));10 }11 }12}

Full Screen

Full Screen

CassandraContainer

Using AI Code Generation

copy

Full Screen

1package com.mycompany.app;2import java.util.*;3import org.testcontainers.containers.CassandraContainer;4public class App {5 public static void main( String[] args ) {6 CassandraContainer cassandraContainer = new CassandraContainer();7 cassandraContainer.start();8 System.out.println("CassandraContainer is started");9 System.out.println("CassandraContainer started with IP Address: " + cassandraContainer.getContainerIpAddress());10 System.out.println("CassandraContainer started with Port: " + cassandraContainer.getMappedPort(9042));11 System.out.println("CassandraContainer started with CQL Port: " + cassandraContainer.getMappedPort(9042));12 System.out.println("CassandraContainer started with JMX Port: " + cassandraContainer.getMappedPort(7199));13 System.out.println("CassandraContainer started with Storage Port: " + cassandraContainer.getMappedPort(7000));14 System.out.println("CassandraContainer started with SSL Storage Port: " + cassandraContainer.getMappedPort(7001));15 System.out.println("CassandraContainer started with Native Transport Port: " + cassandraContainer.getMappedPort(9042));16 System.out.println("CassandraContainer started with RPC Port: " + cassandraContainer.getMappedPort(9160));17 System.out.println("CassandraContainer started with Thrift Port: " + cassandraContainer.getMappedPort(9160));18 cassandraContainer.stop();19 System.out.println("CassandraContainer is stopped");20 }21}

Full Screen

Full Screen

CassandraContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.CassandraContainer;2import org.testcontainers.containers.GenericContainer;3public class CassandraContainerExample {4 public static void main(String[] args) {5 CassandraContainer cassandraContainer = new CassandraContainer("cassandra:3.11.2");6 cassandraContainer.start();7 System.out.println("CassandraContainer started");8 String ipAddress = cassandraContainer.getContainerIpAddress();9 Integer port = cassandraContainer.getMappedPort(9042);10 String containerId = cassandraContainer.getContainerId();11 String imageName = cassandraContainer.getDockerImageName();12 GenericContainer.ContainerInfo containerInfo = cassandraContainer.getContainerInfo();13 String containerState = cassandraContainer.getContainerState();14 String logs = cassandraContainer.getLogs();15 System.out.println("Container ID: " + containerId);16 System.out.println("Image Name: " + imageName);17 System.out.println("Container Info: " + containerInfo);18 System.out.println("Container State: " + containerState);19 System.out.println("Container Logs:

Full Screen

Full Screen

CassandraContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.CassandraContainer;2public class CassandraContainerDemo {3 public static void main(String[] args) {4 CassandraContainer cassandraContainer = new CassandraContainer();5 cassandraContainer.start();6 String jdbcUrl = cassandraContainer.getJdbcUrl();7 String username = cassandraContainer.getUsername();8 String password = cassandraContainer.getPassword();9 System.out.println("JDBC URL:" + jdbcUrl);10 System.out.println("Username:" + username);11 System.out.println("Password:" + password);12 cassandraContainer.stop();13 }14}

Full Screen

Full Screen

CassandraContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.CassandraContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import org.testcontainers.containers.wait.strategy.WaitAllStrategy;4import org.testcontainers.containers.wait.strategy.WaitStrategy;5public class CassandraContainerClass {6 public static CassandraContainer cassandraContainer = new CassandraContainer();7 public static void startCassandraContainer() {8 cassandraContainer.waitingFor(new WaitAllStrategy()9 .withStrategy(Wait.forLogMessage(".*Created default superuser role 'cassandra'.*\\s", 1))10 .withStrategy(Wait.forLogMessage(".*Starting listening for CQL clients.*\\s", 1)));11 cassandraContainer.start();12 }13 public static void stopCassandraContainer() {14 cassandraContainer.stop();15 }16}17import org.testcontainers.containers.CassandraContainer;18import org.testcontainers.containers.wait.strategy.Wait;19import org.testcontainers.containers.wait.strategy.WaitAllStrategy;20import org.testcontainers.containers.wait.strategy.WaitStrategy;21public class CassandraContainerClass {22 public static CassandraContainer cassandraContainer = new CassandraContainer();23 public static void startCassandraContainer() {24 cassandraContainer.waitingFor(new WaitAllStrategy()25 .withStrategy(Wait.forLogMessage(".*Created default superuser role 'cassandra'.*\\s", 1))26 .withStrategy(Wait.forLogMessage(".*Starting listening for CQL clients.*\\s", 1)));27 cassandraContainer.start();28 }29 public static void stopCassandraContainer() {30 cassandraContainer.stop();31 }32}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful