|
| 1 | +package org.baeldung.spring.data.cassandra.repository; |
| 2 | + |
| 3 | +import com.datastax.driver.core.Cluster; |
| 4 | +import com.datastax.driver.core.Session; |
| 5 | +import com.datastax.driver.core.querybuilder.QueryBuilder; |
| 6 | +import com.datastax.driver.core.querybuilder.Select; |
| 7 | +import com.datastax.driver.core.utils.UUIDs; |
| 8 | +import com.google.common.collect.ImmutableSet; |
| 9 | +import org.apache.cassandra.exceptions.ConfigurationException; |
| 10 | +import org.apache.commons.logging.Log; |
| 11 | +import org.apache.commons.logging.LogFactory; |
| 12 | +import org.apache.thrift.transport.TTransportException; |
| 13 | +import org.baeldung.spring.data.cassandra.config.CassandraConfig; |
| 14 | +import org.baeldung.spring.data.cassandra.model.Book; |
| 15 | +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; |
| 16 | +import org.junit.*; |
| 17 | +import org.junit.runner.RunWith; |
| 18 | +import org.springframework.beans.factory.annotation.Autowired; |
| 19 | +import org.springframework.cassandra.core.cql.CqlIdentifier; |
| 20 | +import org.springframework.data.cassandra.core.CassandraAdminOperations; |
| 21 | +import org.springframework.data.cassandra.core.CassandraOperations; |
| 22 | +import org.springframework.test.context.ContextConfiguration; |
| 23 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static junit.framework.TestCase.assertNull; |
| 31 | +import static org.hamcrest.CoreMatchers.is; |
| 32 | +import static org.junit.Assert.assertEquals; |
| 33 | +import static org.junit.Assert.assertThat; |
| 34 | + |
| 35 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 36 | +@ContextConfiguration(classes = CassandraConfig.class) |
| 37 | +public class CassandraTemplateIntegrationTest { |
| 38 | + |
| 39 | + private static final Log LOGGER = LogFactory.getLog(CassandraTemplateIntegrationTest.class); |
| 40 | + |
| 41 | + public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };"; |
| 42 | + |
| 43 | + public static final String KEYSPACE_ACTIVATE_QUERY = "USE testKeySpace;"; |
| 44 | + |
| 45 | + public static final String DATA_TABLE_NAME = "book"; |
| 46 | + |
| 47 | + @Autowired |
| 48 | + private CassandraAdminOperations adminTemplate; |
| 49 | + |
| 50 | + @Autowired |
| 51 | + private CassandraOperations cassandraTemplate; |
| 52 | + |
| 53 | + @BeforeClass |
| 54 | + public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException { |
| 55 | + EmbeddedCassandraServerHelper.startEmbeddedCassandra(); |
| 56 | + Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build(); |
| 57 | + LOGGER.info("Server Started at 127.0.0.1:9142... "); |
| 58 | + Session session = cluster.connect(); |
| 59 | + session.execute(KEYSPACE_CREATION_QUERY); |
| 60 | + session.execute(KEYSPACE_ACTIVATE_QUERY); |
| 61 | + LOGGER.info("KeySpace created and activated."); |
| 62 | + Thread.sleep(5000); |
| 63 | + } |
| 64 | + |
| 65 | + @Before |
| 66 | + public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException { |
| 67 | + adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void whenSavingBook_thenAvailableOnRetrieval() { |
| 72 | + Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 73 | + cassandraTemplate.insert(javaBook); |
| 74 | + Select select = QueryBuilder.select().from("book").where(QueryBuilder.eq("title", "Head First Java")).and(QueryBuilder.eq("publisher", "O'Reilly Media")).limit(10); |
| 75 | + Book retrievedBook = cassandraTemplate.selectOne(select, Book.class); |
| 76 | + assertEquals(javaBook.getId(), retrievedBook.getId()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void whenSavingBooks_thenAllAvailableOnRetrieval() { |
| 81 | + Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 82 | + Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 83 | + List<Book> bookList = new ArrayList<>(); |
| 84 | + bookList.add(javaBook); |
| 85 | + bookList.add(dPatternBook); |
| 86 | + cassandraTemplate.insert(bookList); |
| 87 | + |
| 88 | + Select select = QueryBuilder.select().from("book").limit(10); |
| 89 | + List<Book> retrievedBooks = cassandraTemplate.select(select, Book.class); |
| 90 | + assertThat(retrievedBooks.size(), is(2)); |
| 91 | + assertEquals(javaBook.getId(), retrievedBooks.get(0).getId()); |
| 92 | + assertEquals(dPatternBook.getId(), retrievedBooks.get(1).getId()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void whenUpdatingBook_thenShouldUpdatedOnRetrieval() { |
| 97 | + Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 98 | + cassandraTemplate.insert(javaBook); |
| 99 | + Select select = QueryBuilder.select().from("book").limit(10); |
| 100 | + Book retrievedBook = cassandraTemplate.selectOne(select, Book.class); |
| 101 | + retrievedBook.setTags(ImmutableSet.of("Java", "Programming")); |
| 102 | + cassandraTemplate.update(retrievedBook); |
| 103 | + Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class); |
| 104 | + assertEquals(retrievedBook.getTags(), retrievedUpdatedBook.getTags()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void whenDeletingASelectedBook_thenNotAvailableOnRetrieval() throws InterruptedException { |
| 109 | + Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "OReilly Media", ImmutableSet.of("Computer", "Software")); |
| 110 | + cassandraTemplate.insert(javaBook); |
| 111 | + cassandraTemplate.delete(javaBook); |
| 112 | + Select select = QueryBuilder.select().from("book").limit(10); |
| 113 | + Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class); |
| 114 | + assertNull(retrievedUpdatedBook); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void whenDeletingAllBooks_thenNotAvailableOnRetrieval() { |
| 119 | + Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 120 | + Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 121 | + cassandraTemplate.insert(javaBook); |
| 122 | + cassandraTemplate.insert(dPatternBook); |
| 123 | + cassandraTemplate.deleteAll(Book.class); |
| 124 | + Select select = QueryBuilder.select().from("book").limit(10); |
| 125 | + Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class); |
| 126 | + assertNull(retrievedUpdatedBook); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void whenAddingBooks_thenCountShouldBeCorrectOnRetrieval() { |
| 131 | + Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 132 | + Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 133 | + cassandraTemplate.insert(javaBook); |
| 134 | + cassandraTemplate.insert(dPatternBook); |
| 135 | + long bookCount = cassandraTemplate.count(Book.class); |
| 136 | + assertEquals(2, bookCount); |
| 137 | + } |
| 138 | + |
| 139 | + @After |
| 140 | + public void dropTable() { |
| 141 | + adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME)); |
| 142 | + } |
| 143 | + |
| 144 | + @AfterClass |
| 145 | + public static void stopCassandraEmbedded() { |
| 146 | + EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(); |
| 147 | + } |
| 148 | +} |
0 commit comments