DATACASS-101 - fixed AbstractSpringDataEmbeddedCassandraIntegrationTest & TestConfig

This commit is contained in:
Matthew Adams
2014-02-18 10:54:32 -06:00
parent 625b8afb0b
commit 7e91de62cb
15 changed files with 207 additions and 195 deletions

View File

@@ -212,6 +212,11 @@ public interface CassandraOperations extends CqlOperations {
*/
<T> void delete(List<T> entities, QueryOptions options);
/**
* Deletes all entities of a given class.
*/
<T> void deleteAll(Class<T> clazz);
/**
* Remove the given object from the table by id.
*

View File

@@ -753,4 +753,14 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
return batch;
}
@Override
public <T> void deleteAll(Class<T> clazz) {
if (!mappingContext.contains(clazz)) {
throw new IllegalArgumentException(String.format("unknown persistent entity class [%s]", clazz.getName()));
}
truncate(mappingContext.getPersistentEntity(clazz).getTableName());
}
}

View File

@@ -0,0 +1,164 @@
/*
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.collections;
import static org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification.createKeyspace;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.test.integration.simpletons.Book;
import org.springframework.data.cassandra.test.integration.simpletons.BookHistory;
import org.springframework.data.cassandra.test.integration.simpletons.BookReference;
import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
/**
* @author dwebb
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class CollectionsRowValueProviderTest extends AbstractSpringDataEmbeddedCassandraIntegrationTest {
@Configuration
public static class Config extends IntegrationTestConfig {
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.RECREATE;
}
@Override
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
return Arrays.asList(createKeyspace().name(getKeyspaceName()).withSimpleReplication());
}
@Override
public String[] getEntityBasePackages() {
return new String[] { Book.class.getPackage().getName() };
}
}
@Before
public void before() throws IOException {
deleteAllEntities();
}
@Test
public void mapTest() {
BookHistory b1 = new BookHistory();
b1.setIsbn("123456-1");
b1.setTitle("Spring Data Cassandra Guide");
b1.setAuthor("Cassandra Guru");
b1.setPages(521);
b1.setSaleDate(new Date());
b1.setInStock(true);
Map<String, Integer> checkOutMap = new HashMap<String, Integer>();
checkOutMap.put("dwebb", 50);
checkOutMap.put("madams", 100);
checkOutMap.put("jmcpeek", 150);
b1.setCheckOuts(checkOutMap);
template.insert(b1);
Select select = QueryBuilder.select().all().from("bookHistory");
select.where(QueryBuilder.eq("isbn", "123456-1"));
BookHistory b = template.selectOne(select.getQueryString(), BookHistory.class);
Assert.assertNotNull(b.getCheckOuts());
log.info("Checkouts map data");
for (String username : b.getCheckOuts().keySet()) {
log.info(username + " has " + b.getCheckOuts().get(username) + " checkouts of this book.");
}
Assert.assertEquals(b.getTitle(), "Spring Data Cassandra Guide");
Assert.assertEquals(b.getAuthor(), "Cassandra Guru");
}
@Test
public void listSetTest() {
BookReference b1 = new BookReference();
b1.setIsbn("123456-1");
b1.setTitle("Spring Data Cassandra Guide");
b1.setAuthor("Cassandra Guru");
b1.setPages(521);
b1.setSaleDate(new Date());
b1.setInStock(true);
Set<String> refs = new HashSet<String>();
refs.add("Spring Data by O'Reilly");
refs.add("Spring by Example");
refs.add("Spring Recipies");
b1.setReferences(refs);
List<Integer> marks = new LinkedList<Integer>();
marks.add(13);
marks.add(52);
marks.add(144);
b1.setBookmarks(marks);
template.insert(b1);
Select select = QueryBuilder.select().all().from("bookReference");
select.where(QueryBuilder.eq("isbn", "123456-1"));
BookReference b = template.selectOne(select.getQueryString(), BookReference.class);
Assert.assertNotNull(b.getReferences());
Assert.assertNotNull(b.getBookmarks());
log.info("Bookmark List<Integer> Data");
for (Integer mark : b.getBookmarks()) {
log.info("Bookmark set on page " + mark);
}
log.info("Reference Set<String> Data");
for (String ref : b.getReferences()) {
log.info("Reference -> " + ref);
}
Assert.assertEquals(b.getTitle(), "Spring Data Cassandra Guide");
Assert.assertEquals(b.getAuthor(), "Cassandra Guru");
}
}

View File

@@ -43,10 +43,6 @@ public class CommentRepositoryIntegrationTests {
repository.deleteAll();
}
public void after() {
repository.deleteAll();
}
public void testInsert() {
String author = "testAuthorInsert";

View File

@@ -20,7 +20,6 @@ import static org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecific
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -32,7 +31,7 @@ import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import org.springframework.data.cassandra.test.integration.repository.UserRepository;
import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.data.cassandra.test.integration.support.TestConfig;
import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -47,7 +46,7 @@ public class CommentRepositoryJavaConfigIntegrationTests extends AbstractSpringD
@Configuration
@EnableCassandraRepositories(basePackageClasses = CommentRepository.class)
public static class Config extends TestConfig {
public static class Config extends IntegrationTestConfig {
@Override
protected String getKeyspaceName() {
@@ -88,11 +87,6 @@ public class CommentRepositoryJavaConfigIntegrationTests extends AbstractSpringD
tests.before();
}
@After
public void after() {
tests.after();
}
@Test
public void testInsert() {
tests.testInsert();

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.data.cassandra.test.integration.composites;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -49,11 +48,6 @@ public class CommentRepositoryXmlConfigIntegrationTests extends AbstractSpringDa
tests.before();
}
@After
public void after() {
tests.after();
}
@Test
public void testInsert() {
tests.testInsert();

View File

@@ -36,6 +36,8 @@ import com.datastax.driver.core.DataType;
@PrimaryKeyClass
public class NotificationPK implements Serializable {
private static final long serialVersionUID = -5041736242014855732L;
/*
* Row ID
*/

View File

@@ -35,6 +35,8 @@ import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
@PrimaryKeyClass
public class PostPK implements Serializable {
private static final long serialVersionUID = 2390757126054483315L;
@PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private String author;

View File

@@ -35,6 +35,8 @@ import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
@PrimaryKeyClass
public class TimelinePK implements Serializable {
private static final long serialVersionUID = 1872200498229849025L;
/*
* Row ID
*/

View File

@@ -53,7 +53,7 @@ public class UserRepositoryIntegrationTests {
this.template = template;
}
public void setUp() throws InterruptedException {
public void setUp() {
repository.deleteAll();
@@ -88,8 +88,9 @@ public class UserRepositoryIntegrationTests {
all = template.insert(Arrays.asList(tom, bob, alice, scott));
}
public void after() {
public void before() {
repository.deleteAll();
setUp();
}
public void findsUserById() throws Exception {

View File

@@ -31,7 +31,7 @@ import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.data.cassandra.test.integration.support.TestConfig;
import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -46,7 +46,7 @@ public class UserRepositoryJavaConfigIntegrationTests extends AbstractSpringData
@Configuration
@EnableCassandraRepositories(basePackageClasses = UserRepository.class)
public static class Config extends TestConfig {
public static class Config extends IntegrationTestConfig {
@Override
protected String getKeyspaceName() {
@@ -82,14 +82,9 @@ public class UserRepositoryJavaConfigIntegrationTests extends AbstractSpringData
UserRepositoryIntegrationTests tests;
@Before
public void setUp() throws InterruptedException {
public void before() {
tests = new UserRepositoryIntegrationTests(repository, template);
tests.setUp();
}
@After
public void after() {
tests.after();
tests.before();
}
@Test

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.data.cassandra.test.integration.repository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -43,14 +42,9 @@ public class UserRepositoryXmlConfigIntegrationTests extends AbstractSpringDataE
UserRepositoryIntegrationTests tests;
@Before
public void setUp() throws InterruptedException {
public void before() {
tests = new UserRepositoryIntegrationTests(repository, template);
tests.setUp();
}
@After
public void after() {
tests.after();
tests.before();
}
@Test

View File

@@ -15,29 +15,15 @@
*/
package org.springframework.data.cassandra.test.integration.support;
import static org.springframework.cassandra.core.keyspace.DropTableSpecification.dropTable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.test.integration.template.CassandraDataOperationsTest.Config;
import org.springframework.util.Assert;
import com.datastax.driver.core.TableMetadata;
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
public class AbstractSpringDataEmbeddedCassandraIntegrationTest extends AbstractEmbeddedCassandraIntegrationTest {
public static List<String> SCRIPT;
public static List<TableMetadata> TABLES;
static {
SpringDataCassandraBuildProperties props = new SpringDataCassandraBuildProperties();
CASSANDRA_NATIVE_PORT = props.getCassandraPort();
@@ -48,51 +34,10 @@ public class AbstractSpringDataEmbeddedCassandraIntegrationTest extends Abstract
@Autowired
public CassandraOperations template;
/**
* Saves all table metadata, then drops & creates all tables.
*/
public void recreateAllTables() {
public void deleteAllEntities() {
saveAllTableMetadata();
for (TableMetadata table : TABLES) {
template.execute(dropTable(table.getName()));
template.execute(table.asCQLQuery());
for (CassandraPersistentEntity<?> entity : template.getConverter().getMappingContext().getPersistentEntities()) {
template.truncate(entity.getTableName());
}
}
public void saveAllTableMetadata() {
saveAllTableMetadata(false);
}
/**
* Saves all table metadata statically.
*/
public void saveAllTableMetadata(boolean force) {
if (TABLES != null && !force) {
return;
}
TABLES = new ArrayList<TableMetadata>(template.getSession().getCluster().getMetadata()
.getKeyspace(Config.KEYSPACE_NAME).getTables());
}
public List<String> readScriptLines(String resourceName) {
return readScriptLines(resourceName);
}
/**
* Reads the lines from the script referenced by {@link #RESOURCE}.
*/
public List<String> readScriptLines(String resourceName, boolean force) throws IOException {
if (SCRIPT != null && !force) {
return SCRIPT;
}
Assert.hasText(resourceName);
return SCRIPT = FileUtils.readLines(new ClassPathResource(resourceName).getFile());
}
}

View File

@@ -26,17 +26,17 @@ import org.springframework.data.cassandra.config.java.AbstractSpringDataCassandr
* @author Matthew T. Adams
*/
@Configuration
public class TestConfig extends AbstractSpringDataCassandraConfiguration {
public class IntegrationTestConfig extends AbstractSpringDataCassandraConfiguration {
public static final SpringDataCassandraBuildProperties PROPS = new SpringDataCassandraBuildProperties();
public static final int PORT = PROPS.getCassandraPort();
public static final int RPC_PORT = PROPS.getCassandraRpcPort();
public static final String KEYSPACE_NAME = Utils.randomKeyspaceName();
public String keyspaceName = Utils.randomKeyspaceName();
@Override
protected String getKeyspaceName() {
return KEYSPACE_NAME;
return keyspaceName;
}
@Override

View File

@@ -21,12 +21,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.junit.Assert;
@@ -40,10 +35,8 @@ import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.test.integration.simpletons.Book;
import org.springframework.data.cassandra.test.integration.simpletons.BookHistory;
import org.springframework.data.cassandra.test.integration.simpletons.BookReference;
import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.data.cassandra.test.integration.support.TestConfig;
import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -61,7 +54,7 @@ import com.datastax.driver.core.querybuilder.Select;
public class CassandraDataOperationsTest extends AbstractSpringDataEmbeddedCassandraIntegrationTest {
@Configuration
public static class Config extends TestConfig {
public static class Config extends IntegrationTestConfig {
@Override
public SchemaAction getSchemaAction() {
@@ -80,8 +73,8 @@ public class CassandraDataOperationsTest extends AbstractSpringDataEmbeddedCassa
}
@Before
public void before() throws IOException {
recreateAllTables();
public void before() {
deleteAllEntities();
}
@Test
@@ -668,89 +661,4 @@ public class CassandraDataOperationsTest extends AbstractSpringDataEmbeddedCassa
Assert.assertEquals(count, template.count(Book.class));
}
@Test
public void mapTest() {
BookHistory b1 = new BookHistory();
b1.setIsbn("123456-1");
b1.setTitle("Spring Data Cassandra Guide");
b1.setAuthor("Cassandra Guru");
b1.setPages(521);
b1.setSaleDate(new Date());
b1.setInStock(true);
Map<String, Integer> checkOutMap = new HashMap<String, Integer>();
checkOutMap.put("dwebb", 50);
checkOutMap.put("madams", 100);
checkOutMap.put("jmcpeek", 150);
b1.setCheckOuts(checkOutMap);
template.insert(b1);
Select select = QueryBuilder.select().all().from("bookHistory");
select.where(QueryBuilder.eq("isbn", "123456-1"));
BookHistory b = template.selectOne(select.getQueryString(), BookHistory.class);
Assert.assertNotNull(b.getCheckOuts());
log.info("Checkouts map data");
for (String username : b.getCheckOuts().keySet()) {
log.info(username + " has " + b.getCheckOuts().get(username) + " checkouts of this book.");
}
Assert.assertEquals(b.getTitle(), "Spring Data Cassandra Guide");
Assert.assertEquals(b.getAuthor(), "Cassandra Guru");
}
@Test
public void listSetTest() {
BookReference b1 = new BookReference();
b1.setIsbn("123456-1");
b1.setTitle("Spring Data Cassandra Guide");
b1.setAuthor("Cassandra Guru");
b1.setPages(521);
b1.setSaleDate(new Date());
b1.setInStock(true);
Set<String> refs = new HashSet<String>();
refs.add("Spring Data by O'Reilly");
refs.add("Spring by Example");
refs.add("Spring Recipies");
b1.setReferences(refs);
List<Integer> marks = new LinkedList<Integer>();
marks.add(13);
marks.add(52);
marks.add(144);
b1.setBookmarks(marks);
template.insert(b1);
Select select = QueryBuilder.select().all().from("bookReference");
select.where(QueryBuilder.eq("isbn", "123456-1"));
BookReference b = template.selectOne(select.getQueryString(), BookReference.class);
Assert.assertNotNull(b.getReferences());
Assert.assertNotNull(b.getBookmarks());
log.info("Bookmark List<Integer> Data");
for (Integer mark : b.getBookmarks()) {
log.info("Bookmark set on page + " + mark);
}
log.info("Reference Set<String> Data");
for (String ref : b.getReferences()) {
log.info("Reference -> " + ref);
}
Assert.assertEquals(b.getTitle(), "Spring Data Cassandra Guide");
Assert.assertEquals(b.getAuthor(), "Cassandra Guru");
}
}