Feature/bael 5040 cassandra testcontainers (#11124)
* BAEL-5040: initial commit - spring data cassandra using boot-2 * BAEL-5040: update test example * BAEL-5040: add brackets * BAEL-5040: use singleton container * BAEL-5040: switch from singleton to nested classes approach * BAEL-5040: package private methods in junit5 * BAEL-5040: Move SpringBootTest annotation to parent class
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
package org.baeldung.springcassandra;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableCassandraRepositories(basePackages = "org.baeldung.springcassandra.repository")
|
||||
public class SpringCassandraApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringCassandraApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package org.baeldung.springcassandra.model;
|
||||
|
||||
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
|
||||
import org.springframework.data.cassandra.core.mapping.Table;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
@Table
|
||||
public class Car {
|
||||
|
||||
@PrimaryKey
|
||||
private UUID id;
|
||||
|
||||
private String make;
|
||||
|
||||
private String model;
|
||||
|
||||
private int year;
|
||||
|
||||
public Car(UUID id, String make, String model, int year) {
|
||||
this.id = id;
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public int getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
public void setYear(int year) {
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Car car = (Car) o;
|
||||
return year == car.year && Objects.equals(id, car.id) && Objects.equals(make, car.make) && Objects.equals(model, car.model);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, make, model, year);
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package org.baeldung.springcassandra.repository;
|
||||
|
||||
import org.baeldung.springcassandra.model.Car;
|
||||
import org.springframework.data.cassandra.repository.CassandraRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Repository
|
||||
public interface CarRepository extends CassandraRepository<Car, UUID> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
spring.data.cassandra.keyspace-name=${CASSANDRA_KEYSPACE_NAME}
|
||||
spring.data.cassandra.contact-points=${CASSANDRA_CONTACT_POINTS}
|
||||
spring.data.cassandra.port=${CASSANDRA_PORT}
|
||||
spring.data.cassandra.local-datacenter=datacenter1
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package org.baeldung.springcassandra;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import org.baeldung.springcassandra.model.Car;
|
||||
import org.baeldung.springcassandra.repository.CarRepository;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.testcontainers.containers.CassandraContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
@Testcontainers
|
||||
@SpringBootTest
|
||||
class CassandraNestedIntegrationTest {
|
||||
|
||||
private static final String KEYSPACE_NAME = "test";
|
||||
|
||||
@Container
|
||||
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.port", String.valueOf(cassandra.getMappedPort(9042)));
|
||||
|
||||
createKeyspace(cassandra.getCluster());
|
||||
}
|
||||
|
||||
static void createKeyspace(Cluster cluster) {
|
||||
try(Session session = cluster.connect()) {
|
||||
session.execute("CREATE KEYSPACE IF NOT EXISTS " + KEYSPACE_NAME + " WITH replication = \n" +
|
||||
"{'class':'SimpleStrategy','replication_factor':'1'};");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ApplicationContextIntegrationTest {
|
||||
|
||||
@Test
|
||||
void givenCassandraContainer_whenSpringContextIsBootstrapped_thenContainerIsRunningWithNoExceptions() {
|
||||
assertThat(cassandra.isRunning()).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
class CarRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private CarRepository carRepository;
|
||||
|
||||
@Test
|
||||
void givenValidCarRecord_whenSavingIt_thenRecordIsSaved() {
|
||||
UUID carId = UUIDs.timeBased();
|
||||
Car newCar = new Car(carId, "Nissan", "Qashqai", 2018);
|
||||
|
||||
carRepository.save(newCar);
|
||||
|
||||
List<Car> savedCars = carRepository.findAllById(List.of(carId));
|
||||
assertThat(savedCars.get(0)).isEqualTo(newCar);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenExistingCarRecord_whenUpdatingIt_thenRecordIsUpdated() {
|
||||
UUID carId = UUIDs.timeBased();
|
||||
Car existingCar = carRepository.save(new Car(carId, "Nissan", "Qashqai", 2018));
|
||||
|
||||
existingCar.setModel("X-Trail");
|
||||
carRepository.save(existingCar);
|
||||
|
||||
List<Car> savedCars = carRepository.findAllById(List.of(carId));
|
||||
assertThat(savedCars.get(0).getModel()).isEqualTo("X-Trail");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenExistingCarRecord_whenDeletingIt_thenRecordIsDeleted() {
|
||||
UUID carId = UUIDs.timeBased();
|
||||
Car existingCar = carRepository.save(new Car(carId, "Nissan", "Qashqai", 2018));
|
||||
|
||||
carRepository.delete(existingCar);
|
||||
|
||||
List<Car> savedCars = carRepository.findAllById(List.of(carId));
|
||||
assertThat(savedCars.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package org.baeldung.springcassandra;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import org.baeldung.springcassandra.model.Car;
|
||||
import org.baeldung.springcassandra.repository.CarRepository;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.testcontainers.containers.CassandraContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@Testcontainers
|
||||
@SpringBootTest
|
||||
class CassandraSimpleIntegrationTest {
|
||||
|
||||
private static final String KEYSPACE_NAME = "test";
|
||||
|
||||
@Container
|
||||
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.port", String.valueOf(cassandra.getMappedPort(9042)));
|
||||
|
||||
createKeyspace(cassandra.getCluster());
|
||||
}
|
||||
|
||||
static void createKeyspace(Cluster cluster) {
|
||||
try(Session session = cluster.connect()) {
|
||||
session.execute("CREATE KEYSPACE IF NOT EXISTS " + KEYSPACE_NAME + " WITH replication = \n" +
|
||||
"{'class':'SimpleStrategy','replication_factor':'1'};");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCassandraContainer_whenSpringContextIsBootstrapped_thenContainerIsRunningWithNoExceptions() {
|
||||
assertThat(cassandra.isRunning()).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
spring.data.cassandra.keyspace-name=${CASSANDRA_KEYSPACE_NAME}
|
||||
spring.data.cassandra.contact-points=${CASSANDRA_CONTACT_POINTS}
|
||||
spring.data.cassandra.port=${CASSANDRA_PORT}
|
||||
spring.data.cassandra.local-datacenter=datacenter1
|
||||
spring.data.cassandra.schema-action=create_if_not_exists
|
||||
Reference in New Issue
Block a user