Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Add test tags. Convert interface methods to default methods. Reformat code.

See #1388
  • Loading branch information
mp911de committed Jul 3, 2023
1 parent b354eba commit 05ef8c8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.cassandra.core;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -43,32 +44,46 @@ public interface CassandraAdminOperations extends CassandraOperations {
SchemaFactory getSchemaFactory();

/**
* 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.
* 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 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 entityClass the class whose fields determine the columns created.
* @since 4.2
*/
void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass,
Map<String, Object> optionsByName);
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.
* 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.
* @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
*/
void createTable(boolean ifNotExists, Class<?> entityClass, Map<String, Object> optionsByName);
default void createTable(boolean ifNotExists, Class<?> entityClass, Map<String, Object> 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 {@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<String, Object> optionsByName);

/**
* Drops a table based on the given {@link Class entity type}. The name of the table is derived from either the simple
Expand All @@ -89,7 +104,7 @@ void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityCl
/**
* 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -73,6 +73,7 @@ public CassandraAdminTemplate(CqlSession session) {
* @param converter must not be {@literal null}.
*/
public CassandraAdminTemplate(CqlSession session, CassandraConverter converter) {

super(session, converter);
this.schemaFactory = new SchemaFactory(getConverter());
}
Expand All @@ -84,8 +85,8 @@ public CassandraAdminTemplate(CqlSession session, CassandraConverter converter)
* @param converter must not be {@literal null}.
*/
public CassandraAdminTemplate(SessionFactory sessionFactory, CassandraConverter converter) {
super(sessionFactory, converter);

super(sessionFactory, converter);
this.schemaFactory = new SchemaFactory(getConverter());
}

Expand Down Expand Up @@ -113,8 +114,7 @@ public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> e
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) -> {
Expand All @@ -130,12 +130,6 @@ public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> e
getCqlOperations().execute(CreateTableCqlGenerator.toCql(createTableSpecification));
}

@Override
public void createTable(boolean ifNotExists, Class<?> entityClass, Map<String, Object> optionsByName) {
CassandraPersistentEntity<?> entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass);
this.createTable(ifNotExists, entity.getTableName(), entityClass, optionsByName);
}

@Override
public void dropTable(Class<?> entityClass) {
dropTable(getTableName(entityClass));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;
Expand Down Expand Up @@ -84,49 +83,41 @@ void shouldApplyTableOptions() {
.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);

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);

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);

assertThat(someTable).isNotNull();
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.COMMENT.getName())))
.isEqualTo("This is comment for table");
}

@Test // DATACASS-173
void testCreateTables() {

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);
Expand Down

0 comments on commit 05ef8c8

Please sign in to comment.