BAEL-21730: Migrate spring-data-cassandra to com.baeldung
This commit is contained in:
+67
@@ -0,0 +1,67 @@
|
||||
package com.baeldung;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import com.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import com.baeldung.spring.data.cassandra.model.Book;
|
||||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cassandra.core.cql.CqlIdentifier;
|
||||
import org.springframework.data.cassandra.core.CassandraAdminOperations;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = CassandraConfig.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };";
|
||||
|
||||
public static final String KEYSPACE_ACTIVATE_QUERY = "USE testKeySpace;";
|
||||
|
||||
public static final String DATA_TABLE_NAME = "book";
|
||||
|
||||
@Autowired
|
||||
private CassandraAdminOperations adminTemplate;
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
|
||||
final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
|
||||
final Session session = cluster.connect();
|
||||
session.execute(KEYSPACE_CREATION_QUERY);
|
||||
session.execute(KEYSPACE_ACTIVATE_QUERY);
|
||||
Thread.sleep(5000);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void dropTable() {
|
||||
adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopCassandraEmbedded() {
|
||||
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
|
||||
}
|
||||
}
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
package com.baeldung.spring.data.cassandra.repository;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.baeldung.spring.data.cassandra.model.Book;
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import com.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cassandra.core.cql.CqlIdentifier;
|
||||
import org.springframework.data.cassandra.core.CassandraAdminOperations;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = CassandraConfig.class)
|
||||
public class BookRepositoryIntegrationTest {
|
||||
private static final Log LOGGER = LogFactory.getLog(BookRepositoryIntegrationTest.class);
|
||||
|
||||
public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };";
|
||||
|
||||
public static final String KEYSPACE_ACTIVATE_QUERY = "USE testKeySpace;";
|
||||
|
||||
public static final String DATA_TABLE_NAME = "book";
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@Autowired
|
||||
private CassandraAdminOperations adminTemplate;
|
||||
|
||||
//
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
|
||||
final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
|
||||
LOGGER.info("Server Started at 127.0.0.1:9142... ");
|
||||
final Session session = cluster.connect();
|
||||
session.execute(KEYSPACE_CREATION_QUERY);
|
||||
session.execute(KEYSPACE_ACTIVATE_QUERY);
|
||||
LOGGER.info("KeySpace created and activated.");
|
||||
Thread.sleep(5000);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingBook_thenAvailableOnRetrieval() {
|
||||
final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
bookRepository.save(ImmutableSet.of(javaBook));
|
||||
final Iterable<Book> books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
|
||||
assertEquals(javaBook.getId(), books.iterator().next().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdatingBooks_thenAvailableOnRetrieval() {
|
||||
final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
bookRepository.save(ImmutableSet.of(javaBook));
|
||||
final Iterable<Book> books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
|
||||
javaBook.setTitle("Head First Java Second Edition");
|
||||
bookRepository.save(ImmutableSet.of(javaBook));
|
||||
final Iterable<Book> updateBooks = bookRepository.findByTitleAndPublisher("Head First Java Second Edition", "O'Reilly Media");
|
||||
assertEquals(javaBook.getTitle(), updateBooks.iterator().next().getTitle());
|
||||
}
|
||||
|
||||
@Test(expected = java.util.NoSuchElementException.class)
|
||||
public void whenDeletingExistingBooks_thenNotAvailableOnRetrieval() {
|
||||
final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
bookRepository.save(ImmutableSet.of(javaBook));
|
||||
bookRepository.delete(javaBook);
|
||||
final Iterable<Book> books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
|
||||
assertNotEquals(javaBook.getId(), books.iterator().next().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingBooks_thenAllShouldAvailableOnRetrieval() {
|
||||
final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
final Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
bookRepository.save(ImmutableSet.of(javaBook));
|
||||
bookRepository.save(ImmutableSet.of(dPatternBook));
|
||||
final Iterable<Book> books = bookRepository.findAll();
|
||||
int bookCount = 0;
|
||||
for (final Book book : books) {
|
||||
bookCount++;
|
||||
}
|
||||
assertEquals(bookCount, 2);
|
||||
}
|
||||
|
||||
@After
|
||||
public void dropTable() {
|
||||
adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopCassandraEmbedded() {
|
||||
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
|
||||
}
|
||||
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
package com.baeldung.spring.data.cassandra.repository;
|
||||
|
||||
import static junit.framework.TestCase.assertNull;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import com.baeldung.spring.data.cassandra.model.Book;
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cassandra.core.cql.CqlIdentifier;
|
||||
import org.springframework.data.cassandra.core.CassandraAdminOperations;
|
||||
import org.springframework.data.cassandra.core.CassandraOperations;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.datastax.driver.core.querybuilder.Select;
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = CassandraConfig.class)
|
||||
public class CassandraTemplateIntegrationTest {
|
||||
private static final Log LOGGER = LogFactory.getLog(CassandraTemplateIntegrationTest.class);
|
||||
|
||||
public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };";
|
||||
|
||||
public static final String KEYSPACE_ACTIVATE_QUERY = "USE testKeySpace;";
|
||||
|
||||
public static final String DATA_TABLE_NAME = "book";
|
||||
|
||||
@Autowired
|
||||
private CassandraAdminOperations adminTemplate;
|
||||
|
||||
@Autowired
|
||||
private CassandraOperations cassandraTemplate;
|
||||
|
||||
//
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
|
||||
final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
|
||||
LOGGER.info("Server Started at 127.0.0.1:9142... ");
|
||||
final Session session = cluster.connect();
|
||||
session.execute(KEYSPACE_CREATION_QUERY);
|
||||
session.execute(KEYSPACE_ACTIVATE_QUERY);
|
||||
LOGGER.info("KeySpace created and activated.");
|
||||
Thread.sleep(5000);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingBook_thenAvailableOnRetrieval() {
|
||||
final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
cassandraTemplate.insert(javaBook);
|
||||
final Select select = QueryBuilder.select().from("book").where(QueryBuilder.eq("title", "Head First Java")).and(QueryBuilder.eq("publisher", "O'Reilly Media")).limit(10);
|
||||
final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class);
|
||||
assertEquals(javaBook.getId(), retrievedBook.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingBooks_thenAllAvailableOnRetrieval() {
|
||||
final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
final Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
final List<Book> bookList = new ArrayList<>();
|
||||
bookList.add(javaBook);
|
||||
bookList.add(dPatternBook);
|
||||
cassandraTemplate.insert(bookList);
|
||||
|
||||
final Select select = QueryBuilder.select().from("book").limit(10);
|
||||
final List<Book> retrievedBooks = cassandraTemplate.select(select, Book.class);
|
||||
assertThat(retrievedBooks.size(), is(2));
|
||||
assertEquals(javaBook.getId(), retrievedBooks.get(0).getId());
|
||||
assertEquals(dPatternBook.getId(), retrievedBooks.get(1).getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdatingBook_thenShouldUpdatedOnRetrieval() {
|
||||
final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
cassandraTemplate.insert(javaBook);
|
||||
final Select select = QueryBuilder.select().from("book").limit(10);
|
||||
final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class);
|
||||
retrievedBook.setTags(ImmutableSet.of("Java", "Programming"));
|
||||
cassandraTemplate.update(retrievedBook);
|
||||
final Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class);
|
||||
assertEquals(retrievedBook.getTags(), retrievedUpdatedBook.getTags());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeletingASelectedBook_thenNotAvailableOnRetrieval() throws InterruptedException {
|
||||
final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "OReilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
cassandraTemplate.insert(javaBook);
|
||||
cassandraTemplate.delete(javaBook);
|
||||
final Select select = QueryBuilder.select().from("book").limit(10);
|
||||
final Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class);
|
||||
assertNull(retrievedUpdatedBook);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeletingAllBooks_thenNotAvailableOnRetrieval() {
|
||||
final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
final Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
cassandraTemplate.insert(javaBook);
|
||||
cassandraTemplate.insert(dPatternBook);
|
||||
cassandraTemplate.deleteAll(Book.class);
|
||||
final Select select = QueryBuilder.select().from("book").limit(10);
|
||||
final Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class);
|
||||
assertNull(retrievedUpdatedBook);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddingBooks_thenCountShouldBeCorrectOnRetrieval() {
|
||||
final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
final Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
cassandraTemplate.insert(javaBook);
|
||||
cassandraTemplate.insert(dPatternBook);
|
||||
final long bookCount = cassandraTemplate.count(Book.class);
|
||||
assertEquals(2, bookCount);
|
||||
}
|
||||
|
||||
@After
|
||||
public void dropTable() {
|
||||
adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopCassandraEmbedded() {
|
||||
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
|
||||
}
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
package com.baeldung.spring.data.cassandra.repository;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import com.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import com.baeldung.spring.data.cassandra.model.Book;
|
||||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cassandra.core.cql.CqlIdentifier;
|
||||
import org.springframework.data.cassandra.core.CassandraAdminOperations;
|
||||
import org.springframework.data.cassandra.core.CassandraOperations;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.querybuilder.Insert;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.datastax.driver.core.querybuilder.Select;
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = CassandraConfig.class)
|
||||
public class CqlQueriesIntegrationTest {
|
||||
private static final Log LOGGER = LogFactory.getLog(CqlQueriesIntegrationTest.class);
|
||||
|
||||
public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };";
|
||||
|
||||
public static final String KEYSPACE_ACTIVATE_QUERY = "USE testKeySpace;";
|
||||
|
||||
public static final String DATA_TABLE_NAME = "book";
|
||||
|
||||
@Autowired
|
||||
private CassandraAdminOperations adminTemplate;
|
||||
|
||||
@Autowired
|
||||
private CassandraOperations cassandraTemplate;
|
||||
|
||||
//
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra(25000);
|
||||
final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
|
||||
LOGGER.info("Server Started at 127.0.0.1:9142... ");
|
||||
final Session session = cluster.connect();
|
||||
session.execute(KEYSPACE_CREATION_QUERY);
|
||||
session.execute(KEYSPACE_ACTIVATE_QUERY);
|
||||
LOGGER.info("KeySpace created and activated.");
|
||||
Thread.sleep(5000);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingBook_thenAvailableOnRetrieval_usingQueryBuilder() {
|
||||
final UUID uuid = UUIDs.timeBased();
|
||||
final Insert insert = QueryBuilder.insertInto(DATA_TABLE_NAME).value("id", uuid).value("title", "Head First Java").value("publisher", "OReilly Media").value("tags", ImmutableSet.of("Software"));
|
||||
cassandraTemplate.execute(insert);
|
||||
final Select select = QueryBuilder.select().from("book").limit(10);
|
||||
final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class);
|
||||
assertEquals(uuid, retrievedBook.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingBook_thenAvailableOnRetrieval_usingCQLStatements() {
|
||||
final UUID uuid = UUIDs.timeBased();
|
||||
final String insertCql = "insert into book (id, title, publisher, tags) values " + "(" + uuid + ", 'Head First Java', 'OReilly Media', {'Software'})";
|
||||
cassandraTemplate.execute(insertCql);
|
||||
final Select select = QueryBuilder.select().from("book").limit(10);
|
||||
final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class);
|
||||
assertEquals(uuid, retrievedBook.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingBook_thenAvailableOnRetrieval_usingPreparedStatements() throws InterruptedException {
|
||||
final UUID uuid = UUIDs.timeBased();
|
||||
final String insertPreparedCql = "insert into book (id, title, publisher, tags) values (?, ?, ?, ?)";
|
||||
final List<Object> singleBookArgsList = new ArrayList<>();
|
||||
final List<List<?>> bookList = new ArrayList<>();
|
||||
singleBookArgsList.add(uuid);
|
||||
singleBookArgsList.add("Head First Java");
|
||||
singleBookArgsList.add("OReilly Media");
|
||||
singleBookArgsList.add(ImmutableSet.of("Software"));
|
||||
bookList.add(singleBookArgsList);
|
||||
cassandraTemplate.ingest(insertPreparedCql, bookList);
|
||||
// This may not be required, just added to avoid any transient issues
|
||||
Thread.sleep(5000);
|
||||
final Select select = QueryBuilder.select().from("book");
|
||||
final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class);
|
||||
assertEquals(uuid, retrievedBook.getId());
|
||||
}
|
||||
|
||||
@After
|
||||
public void dropTable() {
|
||||
adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopCassandraEmbedded() {
|
||||
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user