|
| 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.utils.UUIDs; |
| 6 | +import com.google.common.collect.ImmutableSet; |
| 7 | +import org.apache.cassandra.exceptions.ConfigurationException; |
| 8 | +import org.apache.commons.logging.Log; |
| 9 | +import org.apache.commons.logging.LogFactory; |
| 10 | +import org.apache.thrift.transport.TTransportException; |
| 11 | +import org.baeldung.spring.data.cassandra.config.CassandraConfig; |
| 12 | +import org.baeldung.spring.data.cassandra.model.Book; |
| 13 | +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; |
| 14 | +import org.junit.*; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.cassandra.core.cql.CqlIdentifier; |
| 18 | +import org.springframework.data.cassandra.core.CassandraAdminOperations; |
| 19 | +import org.springframework.test.context.ContextConfiguration; |
| 20 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.HashMap; |
| 24 | + |
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | +import static org.junit.Assert.assertNotEquals; |
| 27 | + |
| 28 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 29 | +@ContextConfiguration(classes = CassandraConfig.class) |
| 30 | +public class BookRepositoryIntegrationTest { |
| 31 | + |
| 32 | + private static final Log LOGGER = LogFactory.getLog(BookRepositoryIntegrationTest.class); |
| 33 | + |
| 34 | + public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };"; |
| 35 | + |
| 36 | + public static final String KEYSPACE_ACTIVATE_QUERY = "USE testKeySpace;"; |
| 37 | + |
| 38 | + public static final String DATA_TABLE_NAME = "book"; |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private BookRepository bookRepository; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private CassandraAdminOperations adminTemplate; |
| 45 | + |
| 46 | + @BeforeClass |
| 47 | + public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException { |
| 48 | + EmbeddedCassandraServerHelper.startEmbeddedCassandra(); |
| 49 | + Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1") |
| 50 | + .withPort(9142).build(); |
| 51 | + LOGGER.info("Server Started at 127.0.0.1:9142... "); |
| 52 | + Session session = cluster.connect(); |
| 53 | + session.execute(KEYSPACE_CREATION_QUERY); |
| 54 | + session.execute(KEYSPACE_ACTIVATE_QUERY); |
| 55 | + LOGGER.info(session.execute("Select * from Book").all().toArray()); |
| 56 | + Thread.sleep(5000); |
| 57 | + LOGGER.info("KeySpace created and activated."); |
| 58 | + } |
| 59 | + |
| 60 | + @Before |
| 61 | + public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException { |
| 62 | + adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void whenSavingBook_thenAvailableOnRetrieval() { |
| 67 | + Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", |
| 68 | + "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 69 | + bookRepository.save(ImmutableSet.of(javaBook)); |
| 70 | + Iterable<Book> books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media"); |
| 71 | + assertEquals(javaBook.getId(), books.iterator().next().getId()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void whenUpdatingBooks_thenAvailableOnRetrieval() { |
| 76 | + Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 77 | + bookRepository.save(ImmutableSet.of(javaBook)); |
| 78 | + Iterable<Book> books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media"); |
| 79 | + javaBook.setTitle("Head First Java Second Edition"); |
| 80 | + bookRepository.save(ImmutableSet.of(javaBook)); |
| 81 | + Iterable<Book> updateBooks = bookRepository.findByTitleAndPublisher("Head First Java Second Edition", "O'Reilly Media"); |
| 82 | + assertEquals(javaBook.getTitle(), updateBooks.iterator().next().getTitle()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test(expected = java.util.NoSuchElementException.class) |
| 86 | + public void whenDeletingExistingBooks_thenNotAvailableOnRetrieval() { |
| 87 | + Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 88 | + bookRepository.save(ImmutableSet.of(javaBook)); |
| 89 | + bookRepository.delete(javaBook); |
| 90 | + Iterable<Book> books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media"); |
| 91 | + assertNotEquals(javaBook.getId(), books.iterator().next().getId()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void whenSavingBooks_thenAllShouldAvailableOnRetrieval() { |
| 96 | + Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", |
| 97 | + "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 98 | + Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", |
| 99 | + "O'Reilly Media", ImmutableSet.of("Computer", "Software")); |
| 100 | + bookRepository.save(ImmutableSet.of(javaBook)); |
| 101 | + bookRepository.save(ImmutableSet.of(dPatternBook)); |
| 102 | + Iterable<Book> books = bookRepository.findAll(); |
| 103 | + int bookCount = 0; |
| 104 | + for (Book book : books) bookCount++; |
| 105 | + assertEquals(bookCount, 2); |
| 106 | + } |
| 107 | + |
| 108 | + @After |
| 109 | + public void dropTable() { |
| 110 | + adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME)); |
| 111 | + } |
| 112 | + |
| 113 | + @AfterClass |
| 114 | + public static void stopCassandraEmbedded() { |
| 115 | + EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(); |
| 116 | + } |
| 117 | +} |
0 commit comments