From 05ef8c82aeaa24e2e7d90cc5b4e1f46dcd825354 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 3 Jul 2023 11:44:16 +0200 Subject: [PATCH] Polishing. Add test tags. Convert interface methods to default methods. Reformat code. See #1388 --- .../core/CassandraAdminOperations.java | 55 ++++++++++++------- .../core/CassandraAdminTemplate.java | 14 ++--- ...assandraAdminTemplateIntegrationTests.java | 33 ++++------- 3 files changed, 51 insertions(+), 51 deletions(-) diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java index c9f3795f5..32830786a 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java @@ -15,6 +15,7 @@ */ package org.springframework.data.cassandra.core; +import java.util.Collections; import java.util.Map; import java.util.Optional; @@ -42,34 +43,48 @@ public interface CassandraAdminOperations extends CassandraOperations { */ SchemaFactory getSchemaFactory(); + /** + * Create a table with the name, that is derived from the given {@code entityClass} and also fields corresponding to + * the same class. If the table already exists and parameter {@code ifNotExists} is {@literal true}, this is a no-op + * and {@literal false} is returned. If the table doesn't exist, parameter {@code ifNotExists} is ignored, the table + * is created and {@literal true} is returned. + * + * @param ifNotExists if {@code true}, create the table only if it doesn't exist. + * @param entityClass the class whose fields determine the columns created. + * @since 4.2 + */ + default void createTable(boolean ifNotExists, Class entityClass) { + createTable(ifNotExists, entityClass, Collections.emptyMap()); + } + + /** + * Create a table with the name, that is derived from the given {@code entityClass} and also fields corresponding to + * the same class. If the table already exists and parameter {@code ifNotExists} is {@literal true}, this is a no-op + * and {@literal false} is returned. If the table doesn't exist, parameter {@code ifNotExists} is ignored, the table + * is created and {@literal true} is returned. + * + * @param ifNotExists if {@code true}, create the table only if it doesn't exist. + * @param entityClass the class whose fields determine the columns created. + * @param optionsByName table options, given by the string option name and the appropriate option value. + * @since 4.2 + */ + default void createTable(boolean ifNotExists, Class entityClass, Map optionsByName) { + createTable(ifNotExists, getTableName(entityClass), entityClass, optionsByName); + } + /** * Create a table with the name given and fields corresponding to the given class. If the table already exists and * parameter {@code ifNotExists} is {@literal true}, this is a no-op and {@literal false} is returned. If the table * doesn't exist, parameter {@code ifNotExists} is ignored, the table is created and {@literal true} is returned. * - * @param ifNotExists If true, will only create the table if it doesn't exist, else the create operation will be - * ignored. - * @param tableName The name of the table. - * @param entityClass The class whose fields determine the columns created. - * @param optionsByName Table options, given by the string option name and the appropriate option value. + * @param ifNotExists if {@code true}, create the table only if it doesn't exist. + * @param tableName the name of the table. + * @param entityClass the class whose fields determine the columns created. + * @param optionsByName table options, given by the string option name and the appropriate option value. */ void createTable(boolean ifNotExists, CqlIdentifier tableName, Class entityClass, Map optionsByName); - /** - * Create a table with the name, that is derived from the given {@code entityClass} and also fields corresponding to the - * same class. If the table already exists and parameter {@code ifNotExists} is {@literal true}, this is a no-op and - * {@literal false} is returned. If the table doesn't exist, parameter {@code ifNotExists} is ignored, the table is created - * and {@literal true} is returned. - * - * @param ifNotExists If true, will only create the table if it doesn't exist, else the create operation will be - * ignored. - * @param entityClass The class whose fields determine the columns created. - * @param optionsByName Table options, given by the string option name and the appropriate option value. - */ - void createTable(boolean ifNotExists, Class entityClass, Map optionsByName); - - /** * Drops a table based on the given {@link Class entity type}. The name of the table is derived from either the simple * name of the {@link Class entity class} or name of the table specified with the {@link Table} mapping annotation. @@ -89,7 +104,7 @@ public interface CassandraAdminOperations extends CassandraOperations { /** * Drops the {@link String named} table. * - * @param ifExists If {@literal true}, will only drop the table if it exists, else the drop operation will be ignored. + * @param ifExists if {@code true}, drop the table only if it exists. * @param tableName {@link String Name} of the table to drop. * @since 2.1 */ 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 182c800cc..6f8f947eb 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 @@ -61,8 +61,8 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand * @since 2.2 */ public CassandraAdminTemplate(CqlSession session) { - super(session); + super(session); this.schemaFactory = new SchemaFactory(getConverter()); } @@ -73,6 +73,7 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand * @param converter must not be {@literal null}. */ public CassandraAdminTemplate(CqlSession session, CassandraConverter converter) { + super(session, converter); this.schemaFactory = new SchemaFactory(getConverter()); } @@ -84,8 +85,8 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand * @param converter must not be {@literal null}. */ public CassandraAdminTemplate(SessionFactory sessionFactory, CassandraConverter converter) { - super(sessionFactory, converter); + super(sessionFactory, converter); this.schemaFactory = new SchemaFactory(getConverter()); } @@ -113,8 +114,7 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand CassandraPersistentEntity entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass); CreateTableSpecification createTableSpecification = this.schemaFactory - .getCreateTableSpecificationFor(entity, tableName) - .ifNotExists(ifNotExists); + .getCreateTableSpecificationFor(entity, tableName).ifNotExists(ifNotExists); if (!CollectionUtils.isEmpty(optionsByName)) { optionsByName.forEach((key, value) -> { @@ -130,12 +130,6 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand getCqlOperations().execute(CreateTableCqlGenerator.toCql(createTableSpecification)); } - @Override - public void createTable(boolean ifNotExists, Class entityClass, Map optionsByName) { - CassandraPersistentEntity entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass); - this.createTable(ifNotExists, entity.getTableName(), entityClass, optionsByName); - } - @Override public void dropTable(Class entityClass) { dropTable(getTableName(entityClass)); diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/CassandraAdminTemplateIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/CassandraAdminTemplateIntegrationTests.java index 0f2251116..ef009cd9f 100755 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/CassandraAdminTemplateIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/CassandraAdminTemplateIntegrationTests.java @@ -21,7 +21,6 @@ import java.time.LocalDate; import java.util.Collection; import java.util.Map; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.data.annotation.Id; @@ -84,25 +83,17 @@ class CassandraAdminTemplateIntegrationTests extends AbstractKeyspaceCreatingInt .isEqualTo(0.3); } - @Test + @Test // GH-1388 void shouldCreateTableWithNameDerivedFromEntityClass() { - cassandraAdminTemplate.createTable( - true, - SomeTable.class, - Map.of( - TableOption.COMMENT.getName(), "This is comment for table", TableOption.BLOOM_FILTER_FP_CHANCE.getName(), "0.3" - ) - ); - TableMetadata someTable = getKeyspaceMetadata().getTables() - .values() - .stream() - .findFirst() - .orElse(null); + cassandraAdminTemplate.createTable(true, SomeTable.class, Map.of(TableOption.COMMENT.getName(), + "This is comment for table", TableOption.BLOOM_FILTER_FP_CHANCE.getName(), "0.3")); - Assertions.assertThat(someTable).isNotNull(); - Assertions.assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.COMMENT.getName()))).isEqualTo("This is comment for table"); - Assertions.assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.BLOOM_FILTER_FP_CHANCE.getName()))).isEqualTo(3); + TableMetadata someTable = getKeyspaceMetadata().getTables().values().stream().findFirst().orElse(null); + + assertThat(someTable).isNotNull(); + assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.COMMENT.getName()))) + .isEqualTo("This is comment for table"); } @Test // DATACASS-173 @@ -110,23 +101,23 @@ class CassandraAdminTemplateIntegrationTests extends AbstractKeyspaceCreatingInt assertThat(getKeyspaceMetadata().getTables()).hasSize(0); - cassandraAdminTemplate.createTable(true, User.class, null); + cassandraAdminTemplate.createTable(true, User.class); assertThat(getKeyspaceMetadata().getTables()).hasSize(1); - cassandraAdminTemplate.createTable(true, User.class, null); + cassandraAdminTemplate.createTable(true, User.class); assertThat(getKeyspaceMetadata().getTables()).hasSize(1); } @Test void testDropTable() { - cassandraAdminTemplate.createTable(true, User.class, null); + cassandraAdminTemplate.createTable(true, User.class); assertThat(getKeyspaceMetadata().getTables()).hasSize(1); cassandraAdminTemplate.dropTable(User.class); assertThat(getKeyspaceMetadata().getTables()).hasSize(0); - cassandraAdminTemplate.createTable(true, User.class, null); + cassandraAdminTemplate.createTable(true, User.class); cassandraAdminTemplate.dropTable(CqlIdentifier.fromCql("users")); assertThat(getKeyspaceMetadata().getTables()).hasSize(0);