Added integration test for Create Keyspace.

This commit is contained in:
john-mcpeek
2013-12-23 22:48:31 -05:00
parent c3e67ee36e
commit 66f1ae2fc9
6 changed files with 298 additions and 9 deletions

View File

@@ -0,0 +1,153 @@
/*
* 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.cassandra.test.integration.core.cql.generator;
import static org.junit.Assert.assertEquals;
import org.springframework.cassandra.core.keyspace.KeyspaceDescriptor;
import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.Session;
public class CqlKeyspaceSpecificationAssertions {
public static double DELTA = 1e-6; // delta for comparisons of doubles
public static void assertKeyspace(KeyspaceDescriptor expected, String keyspace, Session session) {
KeyspaceMetadata kmd = session.getCluster().getMetadata().getKeyspace(keyspace.toLowerCase());
String cql = kmd.asCQLQuery();
System.out.println( "%%% " + cql );
assertEquals( expected.getName(), kmd.getName() );
// assertEquals(expected.getName().toLowerCase(), tmd.getName().toLowerCase());
// assertPartitionKeyColumns(expected, tmd);
// assertPrimaryKeyColumns(expected, tmd);
// assertColumns(expected.getColumns(), tmd.getColumns());
// assertOptions(expected.getOptions(), tmd.getOptions());
}
// public static void assertPartitionKeyColumns(TableDescriptor expected, TableMetadata actual) {
// assertColumns(expected.getPartitionKeyColumns(), actual.getPartitionKey());
// }
//
// public static void assertPrimaryKeyColumns(TableDescriptor expected, TableMetadata actual) {
// assertColumns(expected.getPrimaryKeyColumns(), actual.getPrimaryKey());
// }
//
// public static void assertOptions(Map<String, Object> expected, Options actual) {
//
// for (String key : expected.keySet()) {
//
// Object value = expected.get(key);
// TableOption tableOption = getTableOptionFor(key);
//
// if (tableOption == null && key.equalsIgnoreCase(TableOption.COMPACT_STORAGE.getName())) {
// // TODO: figure out how to tell if COMPACT STORAGE was used
// continue;
// }
//
// assertOption(tableOption, key, value, getOptionFor(tableOption, tableOption.getType(), actual));
// }
// }
//
// @SuppressWarnings({ "unchecked", "incomplete-switch" })
// public static void assertOption(TableOption tableOption, String key, Object expected, Object actual) {
//
// if (tableOption == null) { // then this is a string-only or unknown value
// key.equalsIgnoreCase(actual.toString()); // TODO: determine if this is the right test
// }
//
// switch (tableOption) {
//
// case BLOOM_FILTER_FP_CHANCE:
// case READ_REPAIR_CHANCE:
// case DCLOCAL_READ_REPAIR_CHANCE:
// assertEquals((Double) expected, (Double) actual, DELTA);
// return;
//
// case CACHING:
// assertEquals(CachingOption.valueOf((String) expected).getValue(), actual);
// return;
//
// case COMPACTION:
// assertCompaction((Map<String, Object>) expected, (Map<String, String>) actual);
// return;
//
// case COMPRESSION:
// assertCompression((Map<String, Object>) expected, (Map<String, String>) actual);
// return;
// }
//
// assertEquals(expected, actual);
// }
//
// public static void assertCompaction(Map<String, Object> expected, Map<String, String> actual) {
// // TODO
// }
//
// public static void assertCompression(Map<String, Object> expected, Map<String, String> actual) {
// // TODO
// }
//
// public static TableOption getTableOptionFor(String key) {
// try {
// return TableOption.valueOf(key);
// } catch (IllegalArgumentException x) {
// return null;
// }
// }
//
// @SuppressWarnings("unchecked")
// public static <T> T getOptionFor(TableOption option, Class<?> type, Options options) {
// switch (option) {
// case BLOOM_FILTER_FP_CHANCE:
// return (T) (Double) options.getBloomFilterFalsePositiveChance();
// case CACHING:
// return (T) options.getCaching();
// case COMMENT:
// return (T) options.getComment();
// case COMPACTION:
// return (T) options.getCompaction();
// case COMPACT_STORAGE:
// throw new Error(); // TODO: figure out
// case COMPRESSION:
// return (T) options.getCompression();
// case DCLOCAL_READ_REPAIR_CHANCE:
// return (T) (Double) options.getReadRepairChance();
// case GC_GRACE_SECONDS:
// return (T) new Long(options.getGcGraceInSeconds());
// case READ_REPAIR_CHANCE:
// return (T) (Double) options.getReadRepairChance();
// case REPLICATE_ON_WRITE:
// return (T) (Boolean) options.getReplicateOnWrite();
// }
// return null;
// }
//
// public static void assertColumns(List<ColumnSpecification> expected, List<ColumnMetadata> actual) {
// for (int i = 0; i < expected.size(); i++) {
// ColumnSpecification expectedColumn = expected.get(i);
// ColumnMetadata actualColumn = actual.get(i);
//
// assertColumn(expectedColumn, actualColumn);
// }
// }
//
// public static void assertColumn(ColumnSpecification expected, ColumnMetadata actual) {
// assertEquals(expected.getName().toLowerCase(), actual.getName().toLowerCase());
// assertEquals(expected.getType(), actual.getType());
// }
}

View File

@@ -0,0 +1,45 @@
package org.springframework.cassandra.test.integration.core.cql.generator;
import static org.springframework.cassandra.test.integration.core.cql.generator.CqlKeyspaceSpecificationAssertions.assertKeyspace;
import org.junit.Test;
import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest;
import org.springframework.cassandra.test.unit.core.cql.generator.CreateKeyspaceCqlGeneratorTests.CreateKeyspaceTest;
import org.springframework.cassandra.test.unit.core.cql.generator.CreateKeyspaceCqlGeneratorTests.BasicTest;
/**
* Integration tests that reuse unit tests.
*
* @author John McPeek
*/
public class CreateKeyspaceCqlGeneratorIntegrationTests {
/**
* Integration test base class that knows how to do everything except instantiate the concrete unit test type T.
*
* @param <T> The concrete unit test class to which this integration test corresponds.
*/
public static abstract class Base<T extends CreateKeyspaceTest> extends AbstractEmbeddedCassandraIntegrationTest {
T unit;
public abstract T unit();
@Test
public void test() {
unit = unit();
unit.prepare();
session.execute(unit.cql);
assertKeyspace(unit.specification, unit.keyspace, session);
}
}
public static class BasicIntegrationTest extends Base<BasicTest> {
@Override
public BasicTest unit() {
return new BasicTest();
}
}
}

View File

@@ -21,14 +21,27 @@ public class AlterKeyspaceCqlGeneratorTests {
assertTrue(cql.startsWith("ALTER KEYSPACE " + tableName + " "));
}
private static void assertReplicationMap(Map<Option, Object> replicationMap, String cql) {
assertTrue(cql.contains(" WITH replication = { "));
for (Map.Entry<Option, Object> entry : replicationMap.entrySet() ) {
String keyValuePair = "'" + entry.getKey().getName() + "' : '" + entry.getValue().toString() + "'";
assertTrue(cql.contains(keyValuePair));
}
}
public static void assertDurableWrites(Boolean durableWrites, String cql) {
assertTrue(cql.contains(" AND durable_writes = " + durableWrites));
}
/**
* Convenient base class that other test classes can use so as not to repeat the generics declarations.
*/
public static abstract class AlterTableTest extends
public static abstract class AlterKeyspaceTest extends
KeyspaceOperationCqlGeneratorTest<AlterKeyspaceSpecification, AlterKeyspaceCqlGenerator> {
}
public static class BasicTest extends AlterTableTest {
public static class CompleteTest extends AlterKeyspaceTest {
public String name = "mytable";
public Boolean durableWrites = true;
@@ -56,6 +69,39 @@ public class AlterKeyspaceCqlGeneratorTests {
prepare();
assertPreamble(name, cql);
assertReplicationMap(replicationMap, cql);
assertDurableWrites(durableWrites, cql);
}
}
public static class ReplicationMapOnlyTest extends AlterKeyspaceTest {
public String name = "mytable";
public Boolean durableWrites = true;
public Map<Option, Object> replicationMap = new HashMap<Option, Object>();
public AlterKeyspaceSpecification specification() {
replicationMap.put( new DefaultOption( "class", String.class, false, false, true ), "SimpleStrategy" );
replicationMap.put( new DefaultOption( "replication_factor", Long.class, false, false, true ), 1 );
replicationMap.put( new DefaultOption( "dc1", Long.class, false, false, true ), 2 );
replicationMap.put( new DefaultOption( "dc2", Long.class, false, false, true ), 3 );
return AlterKeyspaceSpecification.alterKeyspace()
.name(name)
.with(KeyspaceOption.REPLICATION, replicationMap);
}
public AlterKeyspaceCqlGenerator generator() {
return new AlterKeyspaceCqlGenerator(specification);
}
@Test
public void test() {
prepare();
assertPreamble(name, cql);
assertReplicationMap(replicationMap, cql);
}
}
}

View File

@@ -21,6 +21,19 @@ public class CreateKeyspaceCqlGeneratorTests {
assertTrue(cql.startsWith("CREATE KEYSPACE " + keyspaceName + " "));
}
private static void assertReplicationMap(Map<Option, Object> replicationMap, String cql) {
assertTrue(cql.contains(" WITH replication = { "));
for (Map.Entry<Option, Object> entry : replicationMap.entrySet() ) {
String keyValuePair = "'" + entry.getKey().getName() + "' : '" + entry.getValue().toString() + "'";
assertTrue(cql.contains(keyValuePair));
}
}
public static void assertDurableWrites(Boolean durableWrites, String cql) {
assertTrue(cql.contains(" AND durable_writes = " + durableWrites));
}
/**
* Convenient base class that other test classes can use so as not to repeat the generics declarations or
* {@link #generator()} method.
@@ -35,20 +48,20 @@ public class CreateKeyspaceCqlGeneratorTests {
public static class BasicTest extends CreateKeyspaceTest {
public String name = "mytable";
public String name = "mykeyspace";
public Boolean durableWrites = true;
public Map<Option, Object> replicationMap = new HashMap<Option, Object>();
@Override
public CreateKeyspaceSpecification specification() {
keyspace = name;
replicationMap.put( new DefaultOption( "class", String.class, false, false, true ), "SimpleStrategy" );
replicationMap.put( new DefaultOption( "replication_factor", Long.class, false, false, true ), 1 );
replicationMap.put( new DefaultOption( "dc1", Long.class, false, false, true ), 2 );
replicationMap.put( new DefaultOption( "dc2", Long.class, false, false, true ), 3 );
return (CreateKeyspaceSpecification) CreateKeyspaceSpecification.createKeyspace()
.name(name)
.name(keyspace)
.with(KeyspaceOption.REPLICATION, replicationMap)
.with(KeyspaceOption.DURABLE_WRITES, durableWrites);
}
@@ -57,7 +70,40 @@ public class CreateKeyspaceCqlGeneratorTests {
public void test() {
prepare();
assertPreamble(name, cql);
assertPreamble(keyspace, cql);
assertReplicationMap(replicationMap, cql);
assertDurableWrites(durableWrites, cql);
}
}
public static class NetworkTopologyTest extends CreateKeyspaceTest {
public String name = "mykeyspace";
public Boolean durableWrites = false;
public Map<Option, Object> replicationMap = new HashMap<Option, Object>();
@Override
public CreateKeyspaceSpecification specification() {
keyspace = name;
replicationMap.put( new DefaultOption( "class", String.class, false, false, true ), "NetworkTopologyStrategy" );
replicationMap.put( new DefaultOption( "dc1", Long.class, false, false, true ), 2 );
replicationMap.put( new DefaultOption( "dc2", Long.class, false, false, true ), 3 );
return (CreateKeyspaceSpecification) CreateKeyspaceSpecification.createKeyspace()
.name(keyspace)
.with(KeyspaceOption.REPLICATION, replicationMap)
.with(KeyspaceOption.DURABLE_WRITES, durableWrites);
}
@Test
public void test() {
prepare();
assertPreamble(keyspace, cql);
assertReplicationMap(replicationMap, cql);
assertDurableWrites(durableWrites, cql);
}
}
}

View File

@@ -12,7 +12,6 @@ public class DropKeyspaceCqlGeneratorTests {
* Asserts that the preamble is first & correctly formatted in the given CQL string.
*/
public static void assertStatement(String tableName, String cql) {
System.out.println( "'" + cql + "'" );
assertTrue(cql.equals("DROP KEYSPACE " + tableName + ";"));
}

View File

@@ -22,7 +22,7 @@ public abstract class KeyspaceOperationCqlGeneratorTest<S extends KeyspaceNameSp
public abstract G generator();
public String tableName;
public String keyspace;
public S specification;
public G generator;
public String cql;