Best Testcontainers-java code snippet using org.testcontainers.containers.CassandraContainerTest.performQuery
Source:CassandraContainerTest.java
...15 @Test16 public void testSimple() {17 try (CassandraContainer cassandraContainer = new CassandraContainer()) {18 cassandraContainer.start();19 ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local");20 Assert.assertTrue("Query was not applied", resultSet.wasApplied());21 Assert.assertNotNull("Result set has no release_version", resultSet.one().getString(0));22 }23 }24 @Test25 public void testSpecificVersion() {26 String cassandraVersion = "3.0.15";27 try (CassandraContainer cassandraContainer = new CassandraContainer(("cassandra:" + cassandraVersion))) {28 cassandraContainer.start();29 ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local");30 Assert.assertTrue("Query was not applied", resultSet.wasApplied());31 Assert.assertEquals("Cassandra has wrong version", cassandraVersion, resultSet.one().getString(0));32 }33 }34 @Test35 public void testConfigurationOverride() {36 try (CassandraContainer cassandraContainer = new CassandraContainer().withConfigurationOverride("cassandra-test-configuration-example")) {37 cassandraContainer.start();38 ResultSet resultSet = performQuery(cassandraContainer, "SELECT cluster_name FROM system.local");39 Assert.assertTrue("Query was not applied", resultSet.wasApplied());40 Assert.assertEquals("Cassandra configuration is not overridden", CassandraContainerTest.TEST_CLUSTER_NAME_IN_CONF, resultSet.one().getString(0));41 }42 }43 @Test(expected = ContainerLaunchException.class)44 public void testEmptyConfigurationOverride() {45 try (CassandraContainer cassandraContainer = new CassandraContainer().withConfigurationOverride("cassandra-empty-configuration")) {46 cassandraContainer.start();47 }48 }49 @Test50 public void testInitScript() {51 try (CassandraContainer cassandraContainer = new CassandraContainer().withInitScript("initial.cql")) {52 cassandraContainer.start();53 testInitScript(cassandraContainer);54 }55 }56 @Test57 public void testInitScriptWithLegacyCassandra() {58 try (CassandraContainer cassandraContainer = new CassandraContainer("cassandra:2.2.11").withInitScript("initial.cql")) {59 cassandraContainer.start();60 testInitScript(cassandraContainer);61 }62 }63 @Test64 public void testCassandraQueryWaitStrategy() {65 try (CassandraContainer cassandraContainer = new CassandraContainer().waitingFor(new CassandraQueryWaitStrategy())) {66 cassandraContainer.start();67 ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local");68 Assert.assertTrue("Query was not applied", resultSet.wasApplied());69 }70 }71 @Test72 public void testCassandraGetCluster() {73 try (CassandraContainer cassandraContainer = new CassandraContainer()) {74 cassandraContainer.start();75 ResultSet resultSet = performQuery(cassandraContainer.getCluster(), "SELECT release_version FROM system.local");76 Assert.assertTrue("Query was not applied", resultSet.wasApplied());77 Assert.assertNotNull("Result set has no release_version", resultSet.one().getString(0));78 }79 }80}...
performQuery
Using AI Code Generation
1import org.testcontainers.containers.CassandraContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.testcontainers.containers.wait.strategy.Wait;4import java.io.IOException;5import java.nio.file.Files;6import java.nio.file.Path;7import java.nio.file.Paths;8import java.util.Arrays;9import java.util.stream.Collectors;10import static org.junit.Assert.assertEquals;11import static org.junit.Assert.assertTrue;12public class CassandraContainerTest {13 private static final String CASSANDRA_VERSION = "3.11.2";14 private static final String KEYSPACE = "test";15 private static final String TABLE = "testtable";16 private static final String QUERY_CREATE_KEYSPACE = "CREATE KEYSPACE IF NOT EXISTS " + KEYSPACE + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};";17 private static final String QUERY_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + KEYSPACE + "." + TABLE + " (id int PRIMARY KEY, value text);";18 private static final String QUERY_INSERT_DATA = "INSERT INTO " + KEYSPACE + "." + TABLE + " (id, value) VALUES (1, 'test');";19 private static final String QUERY_SELECT_DATA = "SELECT * FROM " + KEYSPACE + "." + TABLE + ";";20 private static CassandraContainer cassandraContainer;21 public static void setUp() {22 cassandraContainer = new CassandraContainer(CASSANDRA_VERSION)23 .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(CassandraContainerTest.class)))24 .waitingFor(Wait.forLogMessage(".*Created default superuser role 'cassandra'.*", 1));25 cassandraContainer.start();26 performQuery(QUERY_CREATE_KEYSPACE);27 performQuery(QUERY_CREATE_TABLE);28 performQuery(QUERY_INSERT_DATA);29 }30 public static void tearDown() {31 cassandraContainer.stop();32 }33 public void testCassandra() {34 String result = performQuery(QUERY_SELECT_DATA);35 assertTrue(result.contains("test"));36 }37 private static String performQuery(String query) {38 try {39 Path tempFile = Files.createTempFile(Paths.get("/tmp"), "cql", ".cql");40 Files.write(tempFile, Arrays.asList(query));41 String output = cassandraContainer.execInContainer("cqlsh", "-f", tempFile.toAbsolutePath().toString
performQuery
Using AI Code Generation
1@Step("Execute query: {query}")2public ResultSet performQuery(String query) {3 return session.execute(query);4}5@Step("Execute query: {query} with parameters: {params}")6public ResultSet performQuery(String query, Object... params) {7 return session.execute(query, params);8}9@Step("Execute query: {query} with parameters: {params}")10public ResultSet performQuery(String query, Map<String, Object> params) {11 BoundStatement boundStatement = new BoundStatement(prepareQuery(query));12 boundStatement.bind(params);13 return session.execute(boundStatement);14}15private PreparedStatement prepareQuery(String query) {16 return session.prepare(query);17}18@Step("Execute batch query: {query}")19public ResultSet performBatchQuery(String query) {20 return session.execute(query);21}22@Step("Execute batch query: {query} with parameters: {params}")23public ResultSet performBatchQuery(String query, Object... params) {24 return session.execute(query, params);25}26@Step("Execute batch query: {query} with parameters: {params}")27public ResultSet performBatchQuery(String query, Map<String, Object> params) {28 BoundStatement boundStatement = new BoundStatement(prepareQuery(query));29 boundStatement.bind(params);30 return session.execute(boundStatement);31}32@Step("Execute batch query: {query}")33public ResultSet performBatchQuery(List<String> query) {34 BatchStatement batchStatement = new BatchStatement();35 for (String q : query) {36 batchStatement.add(new SimpleStatement(q));37 }38 return session.execute(batchStatement);39}40@Step("Execute batch query: {query} with parameters: {params}")41public ResultSet performBatchQuery(List<String> query, Object... params) {42 BatchStatement batchStatement = new BatchStatement();43 for (String q : query) {44 batchStatement.add(new SimpleStatement(q));45 }46 return session.execute(batchStatement);47}48@Step("Execute batch query: {query} with parameters: {params}")49public ResultSet performBatchQuery(List<String> query, Map<String, Object> params) {50 BatchStatement batchStatement = new BatchStatement();51 for (String q : query) {52 BoundStatement boundStatement = new BoundStatement(prepareQuery(q));53 boundStatement.bind(params);54 batchStatement.add(boundStatement);55 }56 return session.execute(batchStatement);57}58@Step("Execute batch query: {query}")59public ResultSet performBatchQuery(BatchStatement query)
performQuery
Using AI Code Generation
1CREATE KEYSPACE IF NOT EXISTS test WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};2CREATE TABLE IF NOT EXISTS test.person (id int PRIMARY KEY, name text, age int);3INSERT INTO test.person (id, name, age) VALUES (1, 'John', 30);4INSERT INTO test.person (id, name, age) VALUES (2, 'Mary', 25);5INSERT INTO test.person (id, name, age) VALUES (3, 'Peter', 21);6SELECT * FROM test.person;
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!