DATACASS-85 - WIP - Added Tests

This commit is contained in:
David T Webb
2014-02-10 11:21:47 -05:00
parent 873ff2659a
commit 3368a68410
3 changed files with 112 additions and 19 deletions

View File

@@ -51,6 +51,7 @@ public class BasicCassandraPersistentEntity<T> extends BasicPersistentEntity<T,
protected CassandraMappingContext mappingContext;
protected final SpelExpressionParser spelParser;
protected final StandardEvaluationContext spelContext;
protected CassandraPersistentEntityMetadataVerifier verifier = new DefaultCassandraPersistentEntityMetadataVerifier();
public BasicCassandraPersistentEntity(TypeInformation<T> typeInformation) {
this(typeInformation, null);
@@ -155,7 +156,6 @@ public class BasicCassandraPersistentEntity<T> extends BasicPersistentEntity<T,
@Override
public void verify() throws MappingException {
super.verify();
CassandraPersistentEntityMetadataVerifier verifier = new DefaultCassandraPersistentEntityMetadataVerifier();
verifier.verify(this);
}
}

View File

@@ -38,11 +38,14 @@ public class DefaultCassandraPersistentEntityMetadataVerifier implements Cassand
return;
}
// TODO
// TODO - Determine total list.
final List<CassandraPersistentProperty> idProperties = new ArrayList<CassandraPersistentProperty>();
final List<CassandraPersistentProperty> compositePrimaryKeys = new ArrayList<CassandraPersistentProperty>();
final List<CassandraPersistentProperty> primaryKeyColumns = new ArrayList<CassandraPersistentProperty>();
/*
* Parse the properties
*/
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
@Override
@@ -59,38 +62,33 @@ public class DefaultCassandraPersistentEntityMetadataVerifier implements Cassand
}
});
// TODO Outline Rules needed
// TODO PK Combinations
// TODO Index, potential verify against TableMetaData...DO NOT CREATE INDEX.
// TODO - Uncomment once Matt merges this support
/*
* Verify that the Primary Key annotations are correct
*/
if (entity.isCompositePrimaryKey()) {
if (primaryKeyColumns.size() == 0) {
throw new IllegalStateException(String.format(
"composite primary key type [%s] has no fields annotated with @%s", entity.getType().getName(),
PrimaryKeyColumn.class.getSimpleName()));
throw new MappingException(String.format("composite primary key type [%s] has no fields annotated with @%s",
entity.getType().getName(), PrimaryKeyColumn.class.getSimpleName()));
}
// there can also be no @PrimaryKey or @Id fields that aren't composite primary keys themselves
for (CassandraPersistentProperty p : idProperties) {
if (!p.getType().isAnnotationPresent(PrimaryKeyClass.class)) {
throw new IllegalStateException(String.format(
throw new MappingException(String.format(
"composite primary key type [%s] property [%s] can only be a composite primary key type itself", entity
.getType().getName(), p.getName()));
}
}
return;
} else {
/*
* Not a Composite Primary Key so there must be at
*/
// TODO Index, potential verify against TableMetaData...DO NOT CREATE INDEX.
}
// else it's not a composite primary key class
}*/
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2011-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.mapping;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cassandra.core.Ordering;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.mapping.DefaultCassandraMappingContext;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.PrimaryKeyClass;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;
/**
* @author dwebb
*
*/
public class BasicCassandraPersistentEntityVerifierTest {
CassandraMappingContext mappingContext;
@Before
public void init() {
mappingContext = new DefaultCassandraMappingContext();
}
@Test
public void testNonPrimaryKeyClass() {
mappingContext.getPersistentEntity(Person.class);
}
@Test
public void testPrimaryKeyClass() {
mappingContext.getPersistentEntity(AnimalPK.class);
mappingContext.getPersistentEntity(Animal.class);
}
@Table
static class Person {
@Id
private String id;
private String firstName;
private String lastName;
}
@Table
static class Animal {
@PrimaryKey
private AnimalPK key;
private String name;
}
@PrimaryKeyClass
static class AnimalPK {
@PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private String species;
@PrimaryKeyColumn(ordinal = 1, type = PrimaryKeyType.PARTITIONED)
private String breed;
@PrimaryKeyColumn(ordinal = 2, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
private String color;
}
}