DATACASS-33 - fixed test classes in preparation for composite primary key testing

This commit is contained in:
Matthew Adams
2014-02-06 17:35:50 -06:00
parent 708c52e342
commit b1f9d5bfc3
25 changed files with 166 additions and 207 deletions

View File

@@ -127,6 +127,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -68,7 +68,7 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC
* The base package to scan for entities annotated with {@link Table} annotations. By default, returns the package
* name of {@literal this} (<code>this.getClass().getPackage().getName()</code>).
*/
public String getMappingBasePackage() {
public String getEntityBasePackage() {
return getClass().getPackage().getName();
}
@@ -127,13 +127,13 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC
/**
* Scans the mapping base package for entity classes annotated with {@link Table} or {@link Persistent}.
*
* @see #getMappingBasePackage()
* @see #getEntityBasePackage()
* @return <code>Set&lt;Class&lt;?&gt;&gt;</code> representing the annotated entity classes found.
* @throws ClassNotFoundException
*/
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
String basePackage = getMappingBasePackage();
String basePackage = getEntityBasePackage();
Set<Class<?>> initialEntitySet = new HashSet<Class<?>>();
if (StringUtils.hasText(basePackage)) {

View File

@@ -249,7 +249,7 @@ public interface CassandraOperations extends CqlOperations {
void deleteById(Class<?> type, Object id);
<T> List<T> selectByIds(Class<T> type, Iterable<?> ids);
<T> List<T> selectBySimpleIds(Class<T> type, Iterable<?> ids);
<T> List<T> selectAll(Class<T> type);
}

View File

@@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.mortbay.log.Log;
import org.springframework.cassandra.core.CqlTemplate;
import org.springframework.cassandra.core.QueryOptions;
import org.springframework.cassandra.core.SessionCallback;
@@ -250,7 +251,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
}
@Override
public <T> List<T> selectByIds(Class<T> type, Iterable<?> ids) {
public <T> List<T> selectBySimpleIds(Class<T> type, Iterable<?> ids) {
CassandraPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(type);
@@ -479,7 +480,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
execute(query);
}
return entity; // TODO: fix this!
return entity;
}
protected <T> List<T> batchInsert(List<T> entities, QueryOptions options, boolean asychronously) {
@@ -497,8 +498,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
execute(query);
}
return entities; // TODO: fix this! You're not supposed to necessarily return the very same entities that went in
// because the database may assign values.
return entities;
}
/**
@@ -525,7 +525,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
execute(query);
}
return entities; // TODO: fix this!
return entities;
}
/**
@@ -574,7 +574,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
execute(query);
}
return entity; // TODO: fix this!
return entity;
}
/**

View File

@@ -21,6 +21,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.data.annotation.Persistent;
/**
* Annotates a type that represents the identity type of another class whose instances are stored in a table.
* <p/>
@@ -33,5 +35,6 @@ import java.lang.annotation.Target;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@Persistent
public @interface PrimaryKeyClass {
}

View File

@@ -106,7 +106,7 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
@Override
public Iterable<T> findAll(Iterable<ID> ids) {
return template.selectByIds(entityInformation.getJavaType(), ids);
return template.selectBySimpleIds(entityInformation.getJavaType(), ids);
}
protected List<T> findAll(Select query) {

View File

@@ -1,14 +1,84 @@
package org.springframework.data.cassandra.test.integration;
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.support.SpringDataBuildProperties;
import org.springframework.data.cassandra.test.integration.template.CassandraDataOperationsTest.Config;
import org.springframework.util.Assert;
import com.datastax.driver.core.TableMetadata;
public class AbstractSpringDataEmbeddedCassandraIntegrationTest extends AbstractEmbeddedCassandraIntegrationTest {
static {
// override necessary superclass statics
public static List<String> SCRIPT;
public static List<TableMetadata> TABLES;
static {
SpringDataBuildProperties props = new SpringDataBuildProperties();
CASSANDRA_NATIVE_PORT = props.getCassandraPort();
}
public Logger log = LoggerFactory.getLogger(getClass());
@Autowired
public CassandraOperations template;
/**
* Saves all table metadata, then drops & creates all tables.
*/
public void recreateAllTables() {
saveAllTableMetadata();
for (TableMetadata table : TABLES) {
template.execute(dropTable(table.getName()));
template.execute(table.asCQLQuery());
}
}
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

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.table;
package org.springframework.data.cassandra.test.integration.composites;
import java.util.Date;
import java.util.Set;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.table;
package org.springframework.data.cassandra.test.integration.composites;
import java.util.Date;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.table;
package org.springframework.data.cassandra.test.integration.composites;
import java.util.Date;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.table;
package org.springframework.data.cassandra.test.integration.composites;
import java.util.Date;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.table;
package org.springframework.data.cassandra.test.integration.composites;
import java.util.Date;
import java.util.Map;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.table;
package org.springframework.data.cassandra.test.integration.composites;
import java.util.Date;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.table;
package org.springframework.data.cassandra.test.integration.composites;
import java.util.Date;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.table;
package org.springframework.data.cassandra.test.integration.composites;
import java.util.Date;

View File

@@ -1,38 +1,21 @@
package org.springframework.data.cassandra.test.integration.config;
import java.io.IOException;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class CassandraNamespaceTests {
public class CassandraNamespaceTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest {
@Autowired
private ApplicationContext ctx;
@BeforeClass
public static void startCassandra() throws IOException, TTransportException, ConfigurationException,
InterruptedException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra("spring-cassandra.yaml");
}
@After
public void clearCassandra() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
@Test
public void test() {
Assert.notNull(ctx);

View File

@@ -1,5 +1,6 @@
package org.springframework.data.cassandra.test.integration.config;
import org.springframework.cassandra.test.unit.support.Utils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.java.AbstractSpringDataCassandraConfiguration;
@@ -23,7 +24,7 @@ public class TestConfig extends AbstractSpringDataCassandraConfiguration {
public static final int PORT = PROPS.getCassandraPort();
public static final int RPC_PORT = PROPS.getCassandraRpcPort();
public static final String KEYSPACE_NAME = "test";
public static final String KEYSPACE_NAME = Utils.randomKeyspaceName();
@Override
protected String getKeyspaceName() {

View File

@@ -19,13 +19,8 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import java.io.IOException;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -33,6 +28,7 @@ import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.data.cassandra.mapping.BasicCassandraPersistentEntity;
import org.springframework.data.cassandra.mapping.Table;
import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.data.util.ClassTypeInformation;
/**
@@ -41,17 +37,11 @@ import org.springframework.data.util.ClassTypeInformation;
* @author Alex Shvid
*/
@RunWith(MockitoJUnitRunner.class)
public class BasicCassandraPersistentEntityIntegrationTests {
public class BasicCassandraPersistentEntityIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest {
@Mock
ApplicationContext context;
@BeforeClass
public static void startCassandra() throws IOException, TTransportException, ConfigurationException,
InterruptedException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra("spring-cassandra.yaml");
}
@Test
public void subclassInheritsAtDocumentAnnotation() {
@@ -84,11 +74,6 @@ public class BasicCassandraPersistentEntityIntegrationTests {
assertThat(entity.getTableName(), is(bean.tableName));
}
@After
public void clearCassandra() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
@Table("messages")
static class Message {

View File

@@ -68,7 +68,7 @@ public class UserRepositoryJavaConfigIntegrationTests extends AbstractSpringData
}
@Override
public String getMappingBasePackage() {
public String getEntityBasePackage() {
return User.class.getPackage().getName();
}
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.table;
package org.springframework.data.cassandra.test.integration.simpletons;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.test.integration.table;
package org.springframework.data.cassandra.test.integration.simpletons;
import java.util.Date;

View File

@@ -0,0 +1,22 @@
package org.springframework.data.cassandra.test.integration.support;
import org.cassandraunit.dataset.commons.AbstractCommonsParserDataSet;
import org.cassandraunit.dataset.commons.ParsedKeyspace;
public class ManualDataSet extends AbstractCommonsParserDataSet {
private String keyspaceName;
private ParsedKeyspace pks;
public ManualDataSet(String keyspaceName) {
this.keyspaceName = keyspaceName;
pks = new ParsedKeyspace();
pks.setName(this.keyspaceName);
}
@Override
protected ParsedKeyspace getParsedKeyspace() {
return pks;
}
}

View File

@@ -1,102 +0,0 @@
/*
* Copyright 2011-2013 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.template;
import java.io.IOException;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.DataLoader;
import org.cassandraunit.dataset.yaml.ClassPathYamlDataSet;
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.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cassandra.core.CqlOperations;
import org.springframework.context.ApplicationContext;
import org.springframework.data.cassandra.test.integration.config.TestConfig;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
/**
* @author David Webb
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class CassandraAdminTest {
@Autowired
private CqlOperations cassandraTemplate;
@Mock
ApplicationContext context;
@BeforeClass
public static void startCassandra() throws IOException, TTransportException, ConfigurationException,
InterruptedException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra("spring-cassandra.yaml");
/*
* Load data file to creat the test keyspace before we init the template
*/
DataLoader dataLoader = new DataLoader("Test Cluster", "localhost:" + TestConfig.RPC_PORT);
dataLoader.load(new ClassPathYamlDataSet("cassandra-keyspace.yaml"));
}
@Before
public void setupKeyspace() {
/*
* Load data file to creat the test keyspace before we init the template
*/
DataLoader dataLoader = new DataLoader("Test Cluster", "localhost:" + TestConfig.RPC_PORT);
dataLoader.load(new ClassPathYamlDataSet("cassandra-keyspace.yaml"));
}
@Test
public void alterTableTest() {
// cassandraTemplate.alterTable(UserAlter.class);
}
@Test
public void dropTableTest() {
// cassandraTemplate.dropTable(User.class);
// cassandraTemplate.dropTable("comments");
}
@After
public void clearCassandra() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
@AfterClass
public static void stopCassandra() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
}

View File

@@ -15,22 +15,18 @@
*/
package org.springframework.data.cassandra.test.integration.template;
import static org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification.createKeyspace;
import static org.springframework.cassandra.core.keyspace.DropTableSpecification.dropTable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.CassandraCQLUnit;
import org.cassandraunit.DataLoader;
import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;
import org.cassandraunit.dataset.yaml.ClassPathYamlDataSet;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.After;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
@@ -39,14 +35,19 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cassandra.core.ConsistencyLevel;
import org.springframework.cassandra.core.QueryOptions;
import org.springframework.cassandra.core.RetryPolicy;
import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.data.cassandra.test.integration.config.TestConfig;
import org.springframework.data.cassandra.test.integration.support.SpringDataBuildProperties;
import org.springframework.data.cassandra.test.integration.table.Book;
import org.springframework.data.cassandra.test.integration.simpletons.Book;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.datastax.driver.core.TableMetadata;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
@@ -57,36 +58,31 @@ import com.datastax.driver.core.querybuilder.Select;
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class CassandraDataOperationsTest {
@ContextConfiguration
public class CassandraDataOperationsTest extends AbstractSpringDataEmbeddedCassandraIntegrationTest {
@Autowired
private CassandraOperations template;
@Configuration
public static class Config extends TestConfig {
private static Logger log = LoggerFactory.getLogger(CassandraDataOperationsTest.class);
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.RECREATE;
}
public static final SpringDataBuildProperties PROPS = new SpringDataBuildProperties();
private final static String CASSANDRA_CONFIG = "spring-cassandra.yaml";
private final static String KEYSPACE_NAME = "test";
private final static String CASSANDRA_HOST = "localhost";
private final static int CASSANDRA_NATIVE_PORT = PROPS.getCassandraPort();
private final static int CASSANDRA_THRIFT_PORT = PROPS.getCassandraRpcPort();
@Override
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
return Arrays.asList(createKeyspace().name(getKeyspaceName()).withSimpleReplication());
}
@Rule
public CassandraCQLUnit cassandraCQLUnit = new CassandraCQLUnit(new ClassPathCQLDataSet("cql-dataload.cql",
KEYSPACE_NAME), CASSANDRA_CONFIG, CASSANDRA_HOST, CASSANDRA_NATIVE_PORT);
@Override
public String getEntityBasePackage() {
return Book.class.getPackage().getName();
}
}
@BeforeClass
public static void startCassandra() throws IOException, TTransportException, ConfigurationException,
InterruptedException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra(CASSANDRA_CONFIG);
/*
* Load data file to creat the test keyspace before we init the template
*/
DataLoader dataLoader = new DataLoader("Test Cluster", CASSANDRA_HOST + ":" + CASSANDRA_THRIFT_PORT);
dataLoader.load(new ClassPathYamlDataSet("cassandra-keyspace.yaml"));
@Before
public void before() throws IOException {
recreateAllTables();
}
@Test
@@ -665,9 +661,4 @@ public class CassandraDataOperationsTest {
Assert.assertEquals(count, template.count(Book.class));
}
@After
public void clearCassandra() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
}

View File

@@ -1,3 +1,3 @@
name: test
name: ks9487fb2da1a541e39a05e5545d64411a
replicationFactor: 1
strategy: org.apache.cassandra.locator.SimpleStrategy