|
1 | 1 | package com.baeldung.spring.data.cassandra.repository;
|
2 | 2 |
|
3 |
| -import com.baeldung.spring.data.cassandra.model.Book; |
4 |
| -import com.datastax.oss.driver.api.core.CqlSession; |
5 |
| -import com.datastax.oss.driver.api.core.cql.SimpleStatement; |
6 |
| -import com.datastax.oss.driver.api.core.uuid.Uuids; |
7 |
| -import com.datastax.oss.driver.api.querybuilder.QueryBuilder; |
8 |
| -import org.junit.After; |
9 |
| -import org.junit.AfterClass; |
10 |
| -import org.junit.Before; |
11 |
| -import org.junit.BeforeClass; |
12 |
| -import org.junit.Test; |
| 3 | +import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal; |
| 4 | +import static org.hamcrest.CoreMatchers.is; |
| 5 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
13 | 7 |
|
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.AfterAll; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
14 | 17 | import org.springframework.beans.factory.annotation.Autowired;
|
15 | 18 | import org.springframework.boot.test.context.SpringBootTest;
|
16 | 19 | import org.springframework.data.cassandra.core.CassandraAdminOperations;
|
17 |
| -import org.springframework.data.cassandra.core.CassandraOperations; |
18 |
| -import org.springframework.data.cassandra.core.cql.CqlIdentifier; |
19 |
| -import org.springframework.test.context.ContextConfiguration; |
20 |
| -import org.springframework.test.context.DynamicPropertyRegistry; |
21 |
| -import org.springframework.test.context.DynamicPropertySource; |
22 | 20 | import org.testcontainers.containers.CassandraContainer;
|
| 21 | +import org.testcontainers.junit.jupiter.Container; |
23 | 22 | import org.testcontainers.junit.jupiter.Testcontainers;
|
24 |
| -import org.testcontainers.utility.DockerImageName; |
25 | 23 |
|
26 |
| -import java.util.Collections; |
27 |
| -import java.util.List; |
28 |
| -import java.util.Set; |
29 |
| - |
30 |
| -import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal; |
31 |
| -import static org.hamcrest.CoreMatchers.is; |
32 |
| -import static org.hamcrest.MatcherAssert.assertThat; |
33 |
| -import static org.junit.Assert.assertEquals; |
| 24 | +import com.baeldung.spring.data.cassandra.model.Book; |
| 25 | +import com.datastax.oss.driver.api.core.CqlIdentifier; |
| 26 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 27 | +import com.datastax.oss.driver.api.core.cql.SimpleStatement; |
| 28 | +import com.datastax.oss.driver.api.core.uuid.Uuids; |
| 29 | +import com.datastax.oss.driver.api.querybuilder.QueryBuilder; |
34 | 30 |
|
35 | 31 | @Testcontainers
|
36 |
| -@SpringBootTest |
37 |
| -public class CassandraTemplateLiveTest { |
| 32 | +@SpringBootTest(classes = CassandraTestConfiguration.class) |
| 33 | +class CassandraTemplateLiveTest { |
| 34 | + |
| 35 | + private static final String KEYSPACE_NAME = "testKeySpace"; |
| 36 | + private static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '1' };"; |
| 37 | + private static final String KEYSPACE_ACTIVATE_QUERY = "USE " + KEYSPACE_NAME + ";"; |
38 | 38 |
|
39 | 39 | private static final String DATA_TABLE_NAME = "book";
|
40 | 40 |
|
41 |
| - static CassandraContainer<?> cassandraContainer = |
42 |
| - new CassandraContainer<>("cassandra:4.1.8").withExposedPorts(9042); |
| 41 | + @Container |
| 42 | + private static final CassandraContainer<?> cassandraContainer = new CassandraContainer<>("cassandra:4.1.8").withExposedPorts(9042); |
43 | 43 |
|
44 |
| - @DynamicPropertySource |
45 |
| - static void cassandraProperties(DynamicPropertyRegistry registry) { |
46 |
| - registry.add("cassandra.contactpoints", () -> cassandraContainer.getHost()); |
47 |
| - registry.add("cassandra.port", () -> cassandraContainer.getMappedPort(9042)); |
48 |
| - registry.add("cassandra.keyspace", () -> "testKeySpace"); |
49 |
| - registry.add("cassandra.localdatacenter", () -> cassandraContainer.getLocalDatacenter()); |
50 |
| - } |
| 44 | + @BeforeAll |
| 45 | + static void setupCassandraConnectionProperties() { |
| 46 | + System.setProperty("spring.cassandra.keyspace-name", KEYSPACE_NAME); |
| 47 | + System.setProperty("spring.cassandra.contact-points", cassandraContainer.getContainerIpAddress()); |
| 48 | + System.setProperty("spring.cassandra.port", String.valueOf(cassandraContainer.getMappedPort(9042))); |
51 | 49 |
|
52 |
| - @BeforeClass |
53 |
| - public static void startContainerAndCreateKeyspace() { |
54 |
| - cassandraContainer.start(); |
55 | 50 | try (CqlSession session = CqlSession.builder()
|
56 |
| - .addContactPoint(cassandraContainer.getContactPoint()) |
57 |
| - .withLocalDatacenter(cassandraContainer.getLocalDatacenter()) |
58 |
| - .build()) { |
59 |
| - session.execute("CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': 1 };"); |
| 51 | + .addContactPoint(cassandraContainer.getContactPoint()) |
| 52 | + .withLocalDatacenter(cassandraContainer.getLocalDatacenter()) |
| 53 | + .build()) { |
| 54 | + |
| 55 | + session.execute(KEYSPACE_CREATION_QUERY); |
| 56 | + session.execute(KEYSPACE_ACTIVATE_QUERY); |
60 | 57 | }
|
61 | 58 | }
|
62 | 59 |
|
63 | 60 | @Autowired
|
64 |
| - private CassandraOperations cassandraTemplate; |
| 61 | + private CassandraAdminOperations cassandraTemplate; |
65 | 62 |
|
66 |
| - @Autowired |
67 |
| - private CassandraAdminOperations adminTemplate; |
68 |
| - |
69 |
| - @Before |
70 |
| - public void createTable() { |
71 |
| - adminTemplate.createTable( |
72 |
| - true, |
73 |
| - CqlIdentifier.of(DATA_TABLE_NAME).toCqlIdentifier(), |
74 |
| - Book.class, |
75 |
| - Collections.emptyMap() |
76 |
| - ); |
| 63 | + @BeforeEach |
| 64 | + void createTable() { |
| 65 | + cassandraTemplate.createTable(true, CqlIdentifier.fromCql(DATA_TABLE_NAME), Book.class, Collections.emptyMap()); |
77 | 66 | }
|
78 | 67 |
|
79 | 68 | @Test
|
80 |
| - public void whenSavingBook_thenAvailableOnRetrieval() { |
81 |
| - Book javaBook = new Book( |
82 |
| - Uuids.timeBased(), |
83 |
| - "Head First Java", |
84 |
| - "O'Reilly Media", |
85 |
| - Collections.singleton("Computer") |
86 |
| - ); |
| 69 | + void whenSavingBook_thenAvailableOnRetrieval() { |
| 70 | + Book javaBook = new Book(Uuids.timeBased(), "Head First Java", "O'Reilly Media", Collections.singleton("Computer")); |
87 | 71 |
|
88 | 72 | cassandraTemplate.insert(javaBook);
|
89 | 73 |
|
90 | 74 | SimpleStatement select = QueryBuilder.selectFrom(DATA_TABLE_NAME)
|
91 |
| - .all() |
92 |
| - .whereColumn("title").isEqualTo(literal("Head First Java")) |
93 |
| - .build(); |
| 75 | + .all() |
| 76 | + .whereColumn("title") |
| 77 | + .isEqualTo(literal("Head First Java")) |
| 78 | + .allowFiltering() |
| 79 | + .build(); |
94 | 80 |
|
95 | 81 | Book retrievedBook = cassandraTemplate.selectOne(select, Book.class);
|
96 | 82 | assertEquals(javaBook.getId(), retrievedBook.getId());
|
97 | 83 | }
|
98 | 84 |
|
99 | 85 | @Test
|
100 |
| - public void whenSavingBooks_thenAllAvailableOnRetrieval() { |
101 |
| - List<Book> books = List.of( |
102 |
| - new Book(Uuids.timeBased(), "Head First Java", "O'Reilly Media", Set.of("Computer")), |
103 |
| - new Book(Uuids.timeBased(), "Clean Code", "Pearson", Set.of("Software")) |
104 |
| - ); |
| 86 | + void whenSavingBooks_thenAllAvailableOnRetrieval() { |
| 87 | + List<Book> books = List.of(new Book(Uuids.timeBased(), "Head First Java", "O'Reilly Media", Set.of("Computer")), new Book(Uuids.timeBased(), "Clean Code", "Pearson", Set.of("Software"))); |
105 | 88 |
|
106 |
| - cassandraTemplate.insert(books); |
| 89 | + for (Book book : books) { |
| 90 | + cassandraTemplate.insert(book); |
| 91 | + } |
107 | 92 |
|
108 | 93 | SimpleStatement select = QueryBuilder.selectFrom(DATA_TABLE_NAME)
|
109 |
| - .all() |
110 |
| - .build(); |
| 94 | + .all() |
| 95 | + .build(); |
111 | 96 |
|
112 | 97 | List<Book> retrieved = cassandraTemplate.select(select, Book.class);
|
113 | 98 | assertThat(retrieved.size(), is(2));
|
114 | 99 | }
|
115 | 100 |
|
116 |
| - @After |
117 |
| - public void dropTable() { |
118 |
| - adminTemplate.dropTable(CqlIdentifier.of(DATA_TABLE_NAME).getClass()); |
| 101 | + @AfterEach |
| 102 | + void dropTable() { |
| 103 | + cassandraTemplate.dropTable(Book.class); |
119 | 104 | }
|
120 | 105 |
|
121 |
| - @AfterClass |
122 |
| - public static void tearDown() { |
| 106 | + @AfterAll |
| 107 | + static void tearDown() { |
123 | 108 | if (cassandraContainer != null) {
|
124 | 109 | cassandraContainer.stop();
|
125 | 110 | }
|
|
0 commit comments