Skip to content

Commit 936a3f9

Browse files
committed
Polishing.
Add test tags. Convert interface methods to default methods. Reformat code. See #1388
1 parent b354eba commit 936a3f9

File tree

3 files changed

+55
-53
lines changed

3 files changed

+55
-53
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.cassandra.core;
1717

18+
import java.util.Collections;
1819
import java.util.Map;
1920
import java.util.Optional;
2021

@@ -43,32 +44,46 @@ public interface CassandraAdminOperations extends CassandraOperations {
4344
SchemaFactory getSchemaFactory();
4445

4546
/**
46-
* Create a table with the name given and fields corresponding to the given class. If the table already exists and
47-
* parameter {@code ifNotExists} is {@literal true}, this is a no-op and {@literal false} is returned. If the table
48-
* doesn't exist, parameter {@code ifNotExists} is ignored, the table is created and {@literal true} is returned.
47+
* Create a table with the name, that is derived from the given {@code entityClass} and also fields corresponding to
48+
* the same class. If the table already exists and parameter {@code ifNotExists} is {@literal true}, this is a no-op
49+
* and {@literal false} is returned. If the table doesn't exist, parameter {@code ifNotExists} is ignored, the table
50+
* is created and {@literal true} is returned.
4951
*
50-
* @param ifNotExists If true, will only create the table if it doesn't exist, else the create operation will be
51-
* ignored.
52-
* @param tableName The name of the table.
53-
* @param entityClass The class whose fields determine the columns created.
54-
* @param optionsByName Table options, given by the string option name and the appropriate option value.
52+
* @param ifNotExists if {@code true}, create the table only if it doesn't exist.
53+
* @param entityClass the class whose fields determine the columns created.
54+
* @since 4.2
5555
*/
56-
void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass,
57-
Map<String, Object> optionsByName);
56+
default void createTable(boolean ifNotExists, Class<?> entityClass) {
57+
createTable(ifNotExists, entityClass, Collections.emptyMap());
58+
}
5859

5960
/**
60-
* Create a table with the name, that is derived from the given {@code entityClass} and also fields corresponding to the
61-
* same class. If the table already exists and parameter {@code ifNotExists} is {@literal true}, this is a no-op and
62-
* {@literal false} is returned. If the table doesn't exist, parameter {@code ifNotExists} is ignored, the table is created
63-
* and {@literal true} is returned.
61+
* Create a table with the name, that is derived from the given {@code entityClass} and also fields corresponding to
62+
* the same class. If the table already exists and parameter {@code ifNotExists} is {@literal true}, this is a no-op
63+
* and {@literal false} is returned. If the table doesn't exist, parameter {@code ifNotExists} is ignored, the table
64+
* is created and {@literal true} is returned.
6465
*
65-
* @param ifNotExists If true, will only create the table if it doesn't exist, else the create operation will be
66-
* ignored.
67-
* @param entityClass The class whose fields determine the columns created.
68-
* @param optionsByName Table options, given by the string option name and the appropriate option value.
66+
* @param ifNotExists if {@code true}, create the table only if it doesn't exist.
67+
* @param entityClass the class whose fields determine the columns created.
68+
* @param optionsByName table options, given by the string option name and the appropriate option value.
69+
* @since 4.2
6970
*/
70-
void createTable(boolean ifNotExists, Class<?> entityClass, Map<String, Object> optionsByName);
71+
default void createTable(boolean ifNotExists, Class<?> entityClass, Map<String, Object> optionsByName) {
72+
createTable(ifNotExists, getTableName(entityClass), entityClass, optionsByName);
73+
}
7174

75+
/**
76+
* Create a table with the name given and fields corresponding to the given class. If the table already exists and
77+
* parameter {@code ifNotExists} is {@literal true}, this is a no-op and {@literal false} is returned. If the table
78+
* doesn't exist, parameter {@code ifNotExists} is ignored, the table is created and {@literal true} is returned.
79+
*
80+
* @param ifNotExists if {@code true}, create the table only if it doesn't exist.
81+
* @param tableName the name of the table.
82+
* @param entityClass the class whose fields determine the columns created.
83+
* @param optionsByName table options, given by the string option name and the appropriate option value.
84+
*/
85+
void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass,
86+
Map<String, Object> optionsByName);
7287

7388
/**
7489
* Drops a table based on the given {@link Class entity type}. The name of the table is derived from either the simple
@@ -89,7 +104,7 @@ void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityCl
89104
/**
90105
* Drops the {@link String named} table.
91106
*
92-
* @param ifExists If {@literal true}, will only drop the table if it exists, else the drop operation will be ignored.
107+
* @param ifExists if {@code true}, drop the table only if it exists.
93108
* @param tableName {@link String Name} of the table to drop.
94109
* @since 2.1
95110
*/

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand
6161
* @since 2.2
6262
*/
6363
public CassandraAdminTemplate(CqlSession session) {
64-
super(session);
6564

65+
super(session);
6666
this.schemaFactory = new SchemaFactory(getConverter());
6767
}
6868

@@ -73,6 +73,7 @@ public CassandraAdminTemplate(CqlSession session) {
7373
* @param converter must not be {@literal null}.
7474
*/
7575
public CassandraAdminTemplate(CqlSession session, CassandraConverter converter) {
76+
7677
super(session, converter);
7778
this.schemaFactory = new SchemaFactory(getConverter());
7879
}
@@ -84,8 +85,8 @@ public CassandraAdminTemplate(CqlSession session, CassandraConverter converter)
8485
* @param converter must not be {@literal null}.
8586
*/
8687
public CassandraAdminTemplate(SessionFactory sessionFactory, CassandraConverter converter) {
87-
super(sessionFactory, converter);
8888

89+
super(sessionFactory, converter);
8990
this.schemaFactory = new SchemaFactory(getConverter());
9091
}
9192

@@ -113,8 +114,7 @@ public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> e
113114
CassandraPersistentEntity<?> entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass);
114115

115116
CreateTableSpecification createTableSpecification = this.schemaFactory
116-
.getCreateTableSpecificationFor(entity, tableName)
117-
.ifNotExists(ifNotExists);
117+
.getCreateTableSpecificationFor(entity, tableName).ifNotExists(ifNotExists);
118118

119119
if (!CollectionUtils.isEmpty(optionsByName)) {
120120
optionsByName.forEach((key, value) -> {
@@ -130,12 +130,6 @@ public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> e
130130
getCqlOperations().execute(CreateTableCqlGenerator.toCql(createTableSpecification));
131131
}
132132

133-
@Override
134-
public void createTable(boolean ifNotExists, Class<?> entityClass, Map<String, Object> optionsByName) {
135-
CassandraPersistentEntity<?> entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass);
136-
this.createTable(ifNotExists, entity.getTableName(), entityClass, optionsByName);
137-
}
138-
139133
@Override
140134
public void dropTable(Class<?> entityClass) {
141135
dropTable(getTableName(entityClass));

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/CassandraAdminTemplateIntegrationTests.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Collection;
2222
import java.util.Map;
2323

24-
import org.assertj.core.api.Assertions;
2524
import org.junit.jupiter.api.BeforeEach;
2625
import org.junit.jupiter.api.Test;
2726
import org.springframework.data.annotation.Id;
@@ -84,49 +83,43 @@ void shouldApplyTableOptions() {
8483
.isEqualTo(0.3);
8584
}
8685

87-
@Test
86+
@Test // GH-1388
8887
void shouldCreateTableWithNameDerivedFromEntityClass() {
89-
cassandraAdminTemplate.createTable(
90-
true,
91-
SomeTable.class,
92-
Map.of(
93-
TableOption.COMMENT.getName(), "This is comment for table", TableOption.BLOOM_FILTER_FP_CHANCE.getName(), "0.3"
94-
)
95-
);
96-
97-
TableMetadata someTable = getKeyspaceMetadata().getTables()
98-
.values()
99-
.stream()
100-
.findFirst()
101-
.orElse(null);
102-
103-
Assertions.assertThat(someTable).isNotNull();
104-
Assertions.assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.COMMENT.getName()))).isEqualTo("This is comment for table");
105-
Assertions.assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.BLOOM_FILTER_FP_CHANCE.getName()))).isEqualTo(3);
88+
89+
cassandraAdminTemplate.createTable(true, SomeTable.class, Map.of(TableOption.COMMENT.getName(),
90+
"This is comment for table", TableOption.BLOOM_FILTER_FP_CHANCE.getName(), "0.3"));
91+
92+
TableMetadata someTable = getKeyspaceMetadata().getTables().values().stream().findFirst().orElse(null);
93+
94+
assertThat(someTable).isNotNull();
95+
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.COMMENT.getName())))
96+
.isEqualTo("This is comment for table");
97+
assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.BLOOM_FILTER_FP_CHANCE.getName())))
98+
.isEqualTo(3);
10699
}
107100

108101
@Test // DATACASS-173
109102
void testCreateTables() {
110103

111104
assertThat(getKeyspaceMetadata().getTables()).hasSize(0);
112105

113-
cassandraAdminTemplate.createTable(true, User.class, null);
106+
cassandraAdminTemplate.createTable(true, User.class);
114107
assertThat(getKeyspaceMetadata().getTables()).hasSize(1);
115108

116-
cassandraAdminTemplate.createTable(true, User.class, null);
109+
cassandraAdminTemplate.createTable(true, User.class);
117110
assertThat(getKeyspaceMetadata().getTables()).hasSize(1);
118111
}
119112

120113
@Test
121114
void testDropTable() {
122115

123-
cassandraAdminTemplate.createTable(true, User.class, null);
116+
cassandraAdminTemplate.createTable(true, User.class);
124117
assertThat(getKeyspaceMetadata().getTables()).hasSize(1);
125118

126119
cassandraAdminTemplate.dropTable(User.class);
127120
assertThat(getKeyspaceMetadata().getTables()).hasSize(0);
128121

129-
cassandraAdminTemplate.createTable(true, User.class, null);
122+
cassandraAdminTemplate.createTable(true, User.class);
130123
cassandraAdminTemplate.dropTable(CqlIdentifier.fromCql("users"));
131124

132125
assertThat(getKeyspaceMetadata().getTables()).hasSize(0);

0 commit comments

Comments
 (0)