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
@SpringBootTest
class ProductRepositoryIntegrationTest {
class ProductRepositoryNestedLiveTest {
private static final String KEYSPACE_NAME = "mynamespace";

View File

@ -1,7 +1,8 @@
package org.baeldung.objectmapper;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
import java.net.InetSocketAddress;
import java.util.List;
import org.baeldung.objectmapper.dao.CounterDao;
import org.baeldung.objectmapper.dao.UserDao;
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.Testcontainers;
import java.util.List;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
@Testcontainers
@SpringBootTest
@ -23,35 +25,30 @@ public class MapperLiveTest {
private static final String KEYSPACE_NAME = "baeldung";
@Container
private static final CassandraContainer cassandra = (CassandraContainer) new CassandraContainer("cassandra:3.11.2")
.withExposedPorts(9042);
private static final CassandraContainer cassandra = (CassandraContainer) new CassandraContainer("cassandra:3.11.2").withExposedPorts(9042);
@BeforeAll
static void setupCassandraConnectionProperties() {
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)));
setupCassandra(new InetSocketAddress(cassandra.getHost(), cassandra.getMappedPort(9042)), cassandra.getLocalDatacenter());
}
static UserDao userDao;
static CounterDao counterDao;
@BeforeAll
static void setup() {
setupCassandraConnectionProperties();
CqlSession session = CqlSession.builder().build();
static void setupCassandra(InetSocketAddress cassandraEndpoint, String localDataCenter) {
CqlSession session = CqlSession.builder()
.withLocalDatacenter(localDataCenter)
.addContactPoint(cassandraEndpoint)
.build();
String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS baeldung " +
"WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};";
String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS baeldung " + "WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};";
String useKeyspace = "USE baeldung;";
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);";
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));";
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);";
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(useKeyspace);
@ -75,22 +72,25 @@ public class MapperLiveTest {
@Test
void givenCounter_whenIncrement_thenIncremented() {
Counter users = counterDao.getCounterById("users");
long initialCount = users != null ? users.getCount(): 0;
long initialCount = users != null ? users.getCount() : 0;
counterDao.incrementCounter("users", 1);
users = counterDao.getCounterById("users");
long finalCount = users != null ? users.getCount(): 0;
long finalCount = users != null ? users.getCount() : 0;
Assertions.assertEquals(finalCount - initialCount, 1);
}
@Test
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);
List<User> retrievedUsers = userDao.getUsersOlderThanAge(30).all();
Assertions.assertEquals(retrievedUsers.size(), 1);
userDao.insertUser(userTwo);
List<User> retrievedUsers = userDao.getUsersOlderThanAge(30)
.all();
Assertions.assertEquals(1, retrievedUsers.size());
}
}