group persistence modules (#2890)
* move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full * group persistence modules
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
dbeb5f8ba4
commit
26c50909be
@@ -0,0 +1,45 @@
|
||||
package org.baeldung.spring.data.cassandra.config;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
|
||||
import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration;
|
||||
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
|
||||
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
|
||||
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
|
||||
|
||||
@Configuration
|
||||
@PropertySource(value = { "classpath:cassandra.properties" })
|
||||
@EnableCassandraRepositories(basePackages = "org.baeldung.spring.data.cassandra.repository")
|
||||
public class CassandraConfig extends AbstractCassandraConfiguration {
|
||||
private static final Log LOGGER = LogFactory.getLog(CassandraConfig.class);
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Override
|
||||
protected String getKeyspaceName() {
|
||||
return environment.getProperty("cassandra.keyspace");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Bean
|
||||
public CassandraClusterFactoryBean cluster() {
|
||||
final CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
|
||||
cluster.setContactPoints(environment.getProperty("cassandra.contactpoints"));
|
||||
cluster.setPort(Integer.parseInt(environment.getProperty("cassandra.port")));
|
||||
LOGGER.info("Cluster created with contact points [" + environment.getProperty("cassandra.contactpoints") + "] " + "& port [" + Integer.parseInt(environment.getProperty("cassandra.port")) + "].");
|
||||
return cluster;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Bean
|
||||
public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
|
||||
return new BasicCassandraMappingContext();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.baeldung.spring.data.cassandra.model;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.cassandra.core.Ordering;
|
||||
import org.springframework.cassandra.core.PrimaryKeyType;
|
||||
import org.springframework.data.cassandra.mapping.Column;
|
||||
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
|
||||
import org.springframework.data.cassandra.mapping.Table;
|
||||
|
||||
@Table
|
||||
public class Book {
|
||||
|
||||
@PrimaryKeyColumn(name = "id", ordinal = 0, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
|
||||
private UUID id;
|
||||
|
||||
@PrimaryKeyColumn(name = "title", ordinal = 1, type = PrimaryKeyType.PARTITIONED)
|
||||
private String title;
|
||||
|
||||
@PrimaryKeyColumn(name = "publisher", ordinal = 2, type = PrimaryKeyType.PARTITIONED)
|
||||
private String publisher;
|
||||
|
||||
@Column
|
||||
private Set<String> tags = new HashSet<>();
|
||||
|
||||
public Book(final UUID id, final String title, final String publisher, final Set<String> tags) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.publisher = publisher;
|
||||
this.tags.addAll(tags);
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getPublisher() {
|
||||
return publisher;
|
||||
}
|
||||
|
||||
public Set getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setId(final UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setTitle(final String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public void setPublisher(final String publisher) {
|
||||
this.publisher = publisher;
|
||||
}
|
||||
|
||||
public void setTags(final Set<String> tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.spring.data.cassandra.repository;
|
||||
|
||||
import org.baeldung.spring.data.cassandra.model.Book;
|
||||
import org.springframework.data.cassandra.repository.CassandraRepository;
|
||||
import org.springframework.data.cassandra.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface BookRepository extends CassandraRepository<Book> {
|
||||
|
||||
@Query("select * from book where title = ?0 and publisher=?1")
|
||||
Iterable<Book> findByTitleAndPublisher(String title, String publisher);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
cassandra.contactpoints=127.0.0.1
|
||||
cassandra.port=9142
|
||||
cassandra.keyspace=testKeySpace
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 855 B |
@@ -0,0 +1,121 @@
|
||||
package org.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 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.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import org.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;
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package org.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 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.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import org.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.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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package org.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 org.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import org.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