From 74ce2432b243f42c5ec7fc5a9c39fe06373ecc75 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 9 Oct 2014 19:30:18 +0200 Subject: [PATCH] DATACASS-173: Use ifNotExists when creating tables Honor ifNotExists argument when using the AdminTemplate, add test for createTable and minor documentation polishing Reference: DATACASS-173 --- .../core/CassandraAdminTemplate.java | 9 +- .../template/CassandraAdminTemplateTest.java | 97 +++++++++++++++++++ 2 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraAdminTemplateTest.java diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java index 3b2dd6827..317417e21 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java @@ -43,14 +43,15 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand /** * Constructor used for a basic template configuration * - * @param keyspace must not be {@literal null}. + * @param session must not be {@literal null}. + * @param converter must not be {@literal null}. */ public CassandraAdminTemplate(Session session, CassandraConverter converter) { super(session, converter); } @Override - public void createTable(boolean ifNotExists, final CqlIdentifier tableName, Class entityClass, + public void createTable(final boolean ifNotExists, final CqlIdentifier tableName, Class entityClass, Map optionsByName) { final CassandraPersistentEntity entity = getCassandraMappingContext().getPersistentEntity(entityClass); @@ -59,8 +60,8 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand @Override public Object doInSession(Session s) throws DataAccessException { - String cql = new CreateTableCqlGenerator(getCassandraMappingContext().getCreateTableSpecificationFor(entity)) - .toCql(); + String cql = new CreateTableCqlGenerator(getCassandraMappingContext(). + getCreateTableSpecificationFor(entity).ifNotExists(ifNotExists)).toCql(); log.debug(cql); diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraAdminTemplateTest.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraAdminTemplateTest.java new file mode 100644 index 000000000..4f188f2be --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraAdminTemplateTest.java @@ -0,0 +1,97 @@ +/* + * Copyright 2013-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.template; + +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.*; + +import java.util.Collection; + +import org.junit.Before; +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.cassandra.core.keyspace.DropTableSpecification; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.cassandra.convert.CassandraConverter; +import org.springframework.data.cassandra.core.CassandraAdminTemplate; +import org.springframework.data.cassandra.test.integration.simpletons.Book; +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.KeyspaceMetadata; +import com.datastax.driver.core.Metadata; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.TableMetadata; + +/** + * Test for CassandraAdminTemplate + * + * @author Mark Paluch + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class CassandraAdminTemplateTest extends AbstractSpringDataEmbeddedCassandraIntegrationTest { + + @Configuration + public static class Config extends IntegrationTestConfig { + + @Override + public String[] getEntityBasePackages() { + return new String[] { Book.class.getPackage().getName() }; + } + } + + @Autowired private CassandraAdminTemplate cassandraAdminTemplate; + @Autowired private CassandraConverter converter; + + @Before + public void before() { + KeyspaceMetadata keyspace = getKeyspaceMetadata(); + Collection tables = keyspace.getTables(); + for (TableMetadata table : tables) { + cassandraAdminTemplate.execute(DropTableSpecification.dropTable(table.getName())); + } + } + + private KeyspaceMetadata getKeyspaceMetadata() { + Metadata metadata = getSession().getCluster().getMetadata(); + return metadata.getKeyspace(getSession().getLoggedKeyspace()); + } + + private Session getSession() { + return template.getSession(); + } + + /** + * + * @see DATACASS-173 + */ + @Test + public void testCreateTables() throws Exception { + + assertThat(getKeyspaceMetadata().getTables().size(), is(0)); + + cassandraAdminTemplate.createTable(true, CqlIdentifier.cqlId("book"), Book.class, null); + assertThat(getKeyspaceMetadata().getTables().size(), is(1)); + + cassandraAdminTemplate.createTable(true, CqlIdentifier.cqlId("book"), Book.class, null); + assertThat(getKeyspaceMetadata().getTables().size(), is(1)); + } +}