|
| 1 | +import org.junit.jupiter.api.AfterEach; |
| 2 | +import org.junit.jupiter.api.BeforeEach; |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.testcontainers.scylladb.ScyllaDBContainer; |
| 5 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 6 | +import com.datastax.oss.driver.api.core.cql.ResultSet; |
| 7 | +import com.datastax.oss.driver.api.core.cql.Row; |
| 8 | + |
| 9 | +import java.net.InetSocketAddress; |
| 10 | +import java.util.UUID; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 14 | + |
| 15 | +public class ScyllaDBExampleTest { |
| 16 | + |
| 17 | + private ScyllaDBContainer scylladb; |
| 18 | + private CqlSession session; |
| 19 | + |
| 20 | + @BeforeEach |
| 21 | + public void setUp() { |
| 22 | + scylladb = new ScyllaDBContainer("scylladb/scylla:2025.1") |
| 23 | + .withExposedPorts(9042, 19042); |
| 24 | + scylladb.start(); |
| 25 | + |
| 26 | + session = CqlSession.builder() |
| 27 | + .addContactPoint(new InetSocketAddress(scylladb.getHost(), scylladb.getMappedPort(9042))) |
| 28 | + .withLocalDatacenter("datacenter1") |
| 29 | + .build(); |
| 30 | + |
| 31 | + session.execute("CREATE KEYSPACE IF NOT EXISTS test_keyspace WITH replication = " |
| 32 | + + "{'class': 'NetworkTopologyStrategy', 'datacenter1': 1}"); |
| 33 | + session.execute("USE test_keyspace"); |
| 34 | + session.execute("CREATE TABLE IF NOT EXISTS users (id UUID PRIMARY KEY, name text, age int)"); |
| 35 | + } |
| 36 | + |
| 37 | + @AfterEach |
| 38 | + public void tearDown() { |
| 39 | + if (session != null) { |
| 40 | + session.close(); |
| 41 | + } |
| 42 | + if (scylladb != null) { |
| 43 | + scylladb.stop(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testScyllaDBOperations() { |
| 49 | + // Insert sample data |
| 50 | + UUID user1Id = UUID.randomUUID(); |
| 51 | + UUID user2Id = UUID.randomUUID(); |
| 52 | + |
| 53 | + session.execute("INSERT INTO users (id, name, age) VALUES (?, ?, ?)", user1Id, "John Doe", 30); |
| 54 | + session.execute("INSERT INTO users (id, name, age) VALUES (?, ?, ?)", user2Id, "Jane Doe", 27); |
| 55 | + |
| 56 | + // Retrieve and verify the inserted data |
| 57 | + ResultSet results = session.execute("SELECT * FROM users"); |
| 58 | + int count = 0; |
| 59 | + for (Row row : results) { |
| 60 | + assertNotNull(row.getString("name")); |
| 61 | + assertNotNull(row.getInt("age")); |
| 62 | + count++; |
| 63 | + } |
| 64 | + |
| 65 | + assertEquals(2, count); // Ensure two users are present |
| 66 | + } |
| 67 | +} |
0 commit comments