diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/AlterTableCqlGenerator.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/AlterTableCqlGenerator.java index 9b15cdd5f..ad8404d2f 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/AlterTableCqlGenerator.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/AlterTableCqlGenerator.java @@ -25,6 +25,7 @@ import org.springframework.cassandra.core.keyspace.AlterTableSpecification; import org.springframework.cassandra.core.keyspace.ColumnChangeSpecification; import org.springframework.cassandra.core.keyspace.DropColumnSpecification; import org.springframework.cassandra.core.keyspace.Option; +import org.springframework.cassandra.core.keyspace.TableOption; /** * CQL generator for generating ALTER TABLE statements. @@ -94,6 +95,16 @@ public class AlterTableCqlGenerator extends TableOptionsCqlGenerator changes = new ArrayList(); - /** + /* * Adds a DROP to the list of column changes. + * + * DW Removed as this only works in C* 2.0 */ - public AlterTableSpecification drop(String column) { - changes.add(new DropColumnSpecification(column)); - return this; - } + // public AlterTableSpecification drop(String column) { + // changes.add(new DropColumnSpecification(column)); + // return this; + // } /** * Adds an ADD to the list of column changes. diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/DropTableSpecification.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/DropTableSpecification.java index f9f7afd52..be0df1982 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/DropTableSpecification.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/DropTableSpecification.java @@ -22,20 +22,22 @@ package org.springframework.cassandra.core.keyspace; */ public class DropTableSpecification extends TableNameSpecification { - private boolean ifExists; + // private boolean ifExists; - public DropTableSpecification ifExists() { - return ifExists(true); - } + // Added in Cassandra 2.0. - public DropTableSpecification ifExists(boolean ifExists) { - this.ifExists = ifExists; - return this; - } - - public boolean getIfExists() { - return ifExists; - } + // public DropTableSpecification ifExists() { + // return ifExists(true); + // } + // + // public DropTableSpecification ifExists(boolean ifExists) { + // this.ifExists = ifExists; + // return this; + // } + // + // public boolean getIfExists() { + // return ifExists; + // } /** * Entry point into the {@link DropTableSpecification}'s fluent API to drop a table. Convenient if imported diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/CqlTableSpecificationAssertions.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/CqlTableSpecificationAssertions.java index 89d682537..70b8b8adc 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/CqlTableSpecificationAssertions.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/CqlTableSpecificationAssertions.java @@ -16,6 +16,7 @@ package org.springframework.cassandra.test.integration.core.cql.generator; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import java.util.List; import java.util.Map; @@ -24,6 +25,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cassandra.core.cql.CqlStringUtils; import org.springframework.cassandra.core.keyspace.ColumnSpecification; +import org.springframework.cassandra.core.keyspace.DropTableSpecification; import org.springframework.cassandra.core.keyspace.TableDescriptor; import org.springframework.cassandra.core.keyspace.TableOption; @@ -49,6 +51,13 @@ public class CqlTableSpecificationAssertions { assertOptions(expected.getOptions(), tmd.getOptions()); } + public static void assertNoTable(DropTableSpecification expected, String keyspace, Session session) { + TableMetadata tmd = session.getCluster().getMetadata().getKeyspace(keyspace.toLowerCase()) + .getTable(expected.getName()); + + assertNull(tmd); + } + public static void assertPartitionKeyColumns(TableDescriptor expected, TableMetadata actual) { assertColumns(expected.getPartitionKeyColumns(), actual.getPartitionKey()); } diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/TableLifecycleIntegrationTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/TableLifecycleIntegrationTest.java new file mode 100644 index 000000000..60e55c9a8 --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/TableLifecycleIntegrationTest.java @@ -0,0 +1,105 @@ +/* + * 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.springframework.cassandra.test.integration.core.cql.generator.CqlTableSpecificationAssertions.assertNoTable; +import static org.springframework.cassandra.test.integration.core.cql.generator.CqlTableSpecificationAssertions.assertTable; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cassandra.core.cql.generator.DropTableCqlGenerator; +import org.springframework.cassandra.core.keyspace.DropTableSpecification; +import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest; +import org.springframework.cassandra.test.unit.core.cql.generator.AlterTableCqlGeneratorTests; +import org.springframework.cassandra.test.unit.core.cql.generator.CreateTableCqlGeneratorTests; +import org.springframework.cassandra.test.unit.core.cql.generator.DropTableCqlGeneratorTests; + +/** + * Test CREATE TABLE / ALTER TABLE / DROP TABLE + * + * @author David Webb + */ +public class TableLifecycleIntegrationTest extends AbstractEmbeddedCassandraIntegrationTest { + + private final static Logger log = LoggerFactory.getLogger(TableLifecycleIntegrationTest.class); + + CreateTableCqlGeneratorTests.MultipleOptionsTest createTableTest = new CreateTableCqlGeneratorTests.MultipleOptionsTest(); + + @Test + public void testDrop() { + + createTableTest.prepare(); + + log.info(createTableTest.cql); + + session.execute(createTableTest.cql); + + assertTable(createTableTest.specification, keyspace, session); + + DropTableTest dropTest = new DropTableTest(); + dropTest.prepare(); + + log.info(dropTest.cql); + + session.execute(dropTest.cql); + + assertNoTable(dropTest.specification, keyspace, session); + } + + @Test + public void testAlter() { + + createTableTest.prepare(); + + log.info(createTableTest.cql); + + session.execute(createTableTest.cql); + + assertTable(createTableTest.specification, keyspace, session); + + AlterTableCqlGeneratorTests.MultipleOptionsTest alterTest = new AlterTableCqlGeneratorTests.MultipleOptionsTest(); + alterTest.prepare(); + + log.info(alterTest.cql); + + session.execute(alterTest.cql); + + // assertTable(alterTest.specification, keyspace, session); + + } + + public class DropTableTest extends DropTableCqlGeneratorTests.DropTableTest { + + /* (non-Javadoc) + * @see org.springframework.cassandra.test.unit.core.cql.generator.TableOperationCqlGeneratorTest#specification() + */ + @Override + public DropTableSpecification specification() { + return DropTableSpecification.dropTable().name(createTableTest.specification.getName()); + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.test.unit.core.cql.generator.TableOperationCqlGeneratorTest#generator() + */ + @Override + public DropTableCqlGenerator generator() { + return new DropTableCqlGenerator(specification); + } + + } + +} \ No newline at end of file diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/AlterTableCqlGeneratorTests.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/AlterTableCqlGeneratorTests.java index da0167667..89a6d5ec4 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/AlterTableCqlGeneratorTests.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/AlterTableCqlGeneratorTests.java @@ -1,15 +1,27 @@ package org.springframework.cassandra.test.unit.core.cql.generator; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; + +import java.util.LinkedHashMap; +import java.util.Map; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.cassandra.core.cql.generator.AlterTableCqlGenerator; import org.springframework.cassandra.core.keyspace.AlterTableSpecification; +import org.springframework.cassandra.core.keyspace.Option; +import org.springframework.cassandra.core.keyspace.TableOption; +import org.springframework.cassandra.core.keyspace.TableOption.CachingOption; +import org.springframework.cassandra.core.keyspace.TableOption.CompactionOption; +import org.springframework.cassandra.core.keyspace.TableOption.CompressionOption; import com.datastax.driver.core.DataType; public class AlterTableCqlGeneratorTests { + private final static Logger log = LoggerFactory.getLogger(AlterTableCqlGeneratorTests.class); + /** * Asserts that the preamble is first & correctly formatted in the given CQL string. */ @@ -45,7 +57,7 @@ public class AlterTableCqlGeneratorTests { public String dropped = "dropped"; public AlterTableSpecification specification() { - return AlterTableSpecification.alterTable().name(name).alter(altered, alteredType).add(added, addedType).drop(dropped); + return AlterTableSpecification.alterTable().name(name).alter(altered, alteredType).add(added, addedType); } public AlterTableCqlGenerator generator() { @@ -61,4 +73,80 @@ public class AlterTableCqlGeneratorTests { String.format("ALTER %s TYPE %s, ADD %s %s, DROP %s", altered, alteredType, added, addedType, dropped), cql); } } + + /** + * Fully test all available create table options + * + * @author David Webb + * + */ + public static class MultipleOptionsTest extends AlterTableTest { + + public String name = "timeseries_table"; + public DataType partitionKeyType0 = DataType.timeuuid(); + public String partitionKey0 = "tid"; + public DataType partitionKeyType1 = DataType.timestamp(); + public String partitionKey1 = "create_timestamp"; + public DataType columnType1 = DataType.text(); + public String column1 = "data_point"; + public Double readRepairChance = 0.6; + public Double dcLocalReadRepairChance = 0.8; + public Double bloomFilterFpChance = 0.002; + public Boolean replcateOnWrite = Boolean.FALSE; + public Long gcGraceSeconds = 1200l; + public String comment = "This is My Table"; + public Map compactionMap = new LinkedHashMap(); + public Map compressionMap = new LinkedHashMap(); + + public AlterTableSpecification specification() { + + // Compaction + compactionMap.put(CompactionOption.CLASS, "SizeTieredCompactionStrategy"); + compactionMap.put(CompactionOption.MIN_THRESHOLD, "4"); + // Compression + compressionMap.put(CompressionOption.SSTABLE_COMPRESSION, "SnappyCompressor"); + compressionMap.put(CompressionOption.CHUNK_LENGTH_KB, 128); + compressionMap.put(CompressionOption.CRC_CHECK_CHANCE, 0.75); + + return (AlterTableSpecification) AlterTableSpecification + .alterTable() + .name(name) + // .with(TableOption.COMPACT_STORAGE) + .with(TableOption.READ_REPAIR_CHANCE, readRepairChance).with(TableOption.COMPACTION, compactionMap) + .with(TableOption.COMPRESSION, compressionMap).with(TableOption.BLOOM_FILTER_FP_CHANCE, bloomFilterFpChance) + .with(TableOption.CACHING, CachingOption.KEYS_ONLY).with(TableOption.REPLICATE_ON_WRITE, replcateOnWrite) + .with(TableOption.COMMENT, comment).with(TableOption.DCLOCAL_READ_REPAIR_CHANCE, dcLocalReadRepairChance) + .with(TableOption.GC_GRACE_SECONDS, gcGraceSeconds); + } + + @Test + public void test() { + + prepare(); + + log.info(cql); + + assertPreamble(name, cql); + // assertColumns(String.format("%s %s, %s %s, %s %s", partitionKey0, partitionKeyType0, partitionKey1, + // partitionKeyType1, column1, columnType1), cql); + // assertPrimaryKey(String.format("(%s, %s)", partitionKey0, partitionKey1), cql); + // assertNullOption(TableOption.COMPACT_STORAGE.getName(), cql); + // assertDoubleOption(TableOption.READ_REPAIR_CHANCE.getName(), readRepairChance, cql); + // assertDoubleOption(TableOption.DCLOCAL_READ_REPAIR_CHANCE.getName(), dcLocalReadRepairChance, cql); + // assertDoubleOption(TableOption.BLOOM_FILTER_FP_CHANCE.getName(), bloomFilterFpChance, cql); + // assertStringOption(TableOption.CACHING.getName(), CachingOption.KEYS_ONLY.getValue(), cql); + // assertStringOption(TableOption.REPLICATE_ON_WRITE.getName(), replcateOnWrite.toString(), cql); + // assertStringOption(TableOption.COMMENT.getName(), comment, cql); + // assertLongOption(TableOption.GC_GRACE_SECONDS.getName(), gcGraceSeconds, cql); + + } + + /* (non-Javadoc) + * @see org.springframework.cassandra.test.unit.core.cql.generator.TableOperationCqlGeneratorTest#generator() + */ + @Override + public AlterTableCqlGenerator generator() { + return new AlterTableCqlGenerator(specification); + } + } } diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/CreateTableCqlGeneratorTests.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/CreateTableCqlGeneratorTests.java index c0a413924..e2befa5cb 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/CreateTableCqlGeneratorTests.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/CreateTableCqlGeneratorTests.java @@ -177,8 +177,6 @@ public class CreateTableCqlGeneratorTests { /** * Fully test all available create table options * - * TODO - Determine how to assert the options with map values - * * @author David Webb * */ diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/DropTableCqlGeneratorTests.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/DropTableCqlGeneratorTests.java index 87d63a1bd..15a41632b 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/DropTableCqlGeneratorTests.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/DropTableCqlGeneratorTests.java @@ -1,6 +1,6 @@ package org.springframework.cassandra.test.unit.core.cql.generator; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; import org.junit.Test; import org.springframework.cassandra.core.cql.generator.DropTableCqlGenerator; @@ -11,8 +11,8 @@ public class DropTableCqlGeneratorTests { /** * Asserts that the preamble is first & correctly formatted in the given CQL string. */ - public static void assertStatement(String tableName, String cql) { - assertTrue(cql.equals("DROP TABLE " + tableName + ";")); + public static void assertStatement(String tableName, boolean ifExists, String cql) { + assertTrue(cql.equals("DROP TABLE " + (ifExists ? "IF EXISTS " : "") + tableName + ";")); } /** @@ -47,7 +47,27 @@ public class DropTableCqlGeneratorTests { public void test() { prepare(); - assertStatement(name, cql); + assertStatement(name, false, cql); } } + + // public static class IfExistsTest extends DropTableTest { + // + // public String name = "mytable"; + // + // public DropTableSpecification specification() { + // return DropTableSpecification.dropTable().ifExists().name(name); + // } + // + // public DropTableCqlGenerator generator() { + // return new DropTableCqlGenerator(specification); + // } + // + // @Test + // public void test() { + // prepare(); + // + // assertStatement(name, true, cql); + // } + // } }