JAVA-25344 fix spring-data-cassandra-2 tests (#14799)

This commit is contained in:
Kasra Madadipouya 2023-10-02 19:07:03 +02:00 committed by GitHub
parent dc512cc408
commit 99437e5c52
2 changed files with 27 additions and 27 deletions

View File

@ -23,7 +23,7 @@ import static org.junit.jupiter.api.Assertions.*;
@Testcontainers @Testcontainers
@SpringBootTest @SpringBootTest
class ProductRepositoryIntegrationTest { class ProductRepositoryNestedLiveTest {
private static final String KEYSPACE_NAME = "mynamespace"; private static final String KEYSPACE_NAME = "mynamespace";

View File

@ -1,7 +1,8 @@
package org.baeldung.objectmapper; package org.baeldung.objectmapper;
import com.datastax.oss.driver.api.core.CqlIdentifier; import java.net.InetSocketAddress;
import com.datastax.oss.driver.api.core.CqlSession; import java.util.List;
import org.baeldung.objectmapper.dao.CounterDao; import org.baeldung.objectmapper.dao.CounterDao;
import org.baeldung.objectmapper.dao.UserDao; import org.baeldung.objectmapper.dao.UserDao;
import org.baeldung.objectmapper.entity.Counter; import org.baeldung.objectmapper.entity.Counter;
@ -14,7 +15,8 @@ import org.testcontainers.containers.CassandraContainer;
import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers; import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.List; import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
@Testcontainers @Testcontainers
@SpringBootTest @SpringBootTest
@ -23,35 +25,30 @@ public class MapperLiveTest {
private static final String KEYSPACE_NAME = "baeldung"; private static final String KEYSPACE_NAME = "baeldung";
@Container @Container
private static final CassandraContainer cassandra = (CassandraContainer) new CassandraContainer("cassandra:3.11.2") private static final CassandraContainer cassandra = (CassandraContainer) new CassandraContainer("cassandra:3.11.2").withExposedPorts(9042);
.withExposedPorts(9042);
@BeforeAll
static void setupCassandraConnectionProperties() { static void setupCassandraConnectionProperties() {
System.setProperty("spring.data.cassandra.keyspace-name", KEYSPACE_NAME); System.setProperty("spring.data.cassandra.keyspace-name", KEYSPACE_NAME);
System.setProperty("spring.data.cassandra.contact-points", cassandra.getContainerIpAddress()); System.setProperty("spring.data.cassandra.contact-points", cassandra.getHost());
System.setProperty("spring.data.cassandra.port", String.valueOf(cassandra.getMappedPort(9042))); System.setProperty("spring.data.cassandra.port", String.valueOf(cassandra.getMappedPort(9042)));
setupCassandra(new InetSocketAddress(cassandra.getHost(), cassandra.getMappedPort(9042)), cassandra.getLocalDatacenter());
} }
static UserDao userDao; static UserDao userDao;
static CounterDao counterDao; static CounterDao counterDao;
@BeforeAll static void setupCassandra(InetSocketAddress cassandraEndpoint, String localDataCenter) {
static void setup() { CqlSession session = CqlSession.builder()
setupCassandraConnectionProperties(); .withLocalDatacenter(localDataCenter)
CqlSession session = CqlSession.builder().build(); .addContactPoint(cassandraEndpoint)
.build();
String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS baeldung " + String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS baeldung " + "WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};";
"WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};";
String useKeyspace = "USE baeldung;"; String useKeyspace = "USE baeldung;";
String createUserTable = "CREATE TABLE IF NOT EXISTS user_profile " + String createUserTable = "CREATE TABLE IF NOT EXISTS user_profile " + "(id int, username text, user_age int, writetime bigint, PRIMARY KEY (id, user_age)) " + "WITH CLUSTERING ORDER BY (user_age DESC);";
"(id int, username text, user_age int, writetime bigint, PRIMARY KEY (id, user_age)) " + String createAdminTable = "CREATE TABLE IF NOT EXISTS admin_profile " + "(id int, username text, user_age int, role text, writetime bigint, department text, " + "PRIMARY KEY (id, user_age)) " + "WITH CLUSTERING ORDER BY (user_age DESC);";
"WITH CLUSTERING ORDER BY (user_age DESC);"; String createCounter = "CREATE TABLE IF NOT EXISTS counter " + "(id text, count counter, PRIMARY KEY (id));";
String createAdminTable = "CREATE TABLE IF NOT EXISTS admin_profile " +
"(id int, username text, user_age int, role text, writetime bigint, department text, " +
"PRIMARY KEY (id, user_age)) " +
"WITH CLUSTERING ORDER BY (user_age DESC);";
String createCounter = "CREATE TABLE IF NOT EXISTS counter " +
"(id text, count counter, PRIMARY KEY (id));";
session.execute(createKeyspace); session.execute(createKeyspace);
session.execute(useKeyspace); session.execute(useKeyspace);
@ -87,10 +84,13 @@ public class MapperLiveTest {
@Test @Test
void givenUser_whenGetUsersOlderThan_thenRetrieved() { void givenUser_whenGetUsersOlderThan_thenRetrieved() {
User user = new User(2, "JaneDoe", 20); User user = new User(2, "JaneDoe", 32);
User userTwo = new User(3, "JohnDoe", 20);
userDao.insertUser(user); userDao.insertUser(user);
List<User> retrievedUsers = userDao.getUsersOlderThanAge(30).all(); userDao.insertUser(userTwo);
Assertions.assertEquals(retrievedUsers.size(), 1); List<User> retrievedUsers = userDao.getUsersOlderThanAge(30)
.all();
Assertions.assertEquals(1, retrievedUsers.size());
} }
} }