DATAGRAPH-451 RelationshipEntities will be validated when manipulated.
This commit is contained in:
committed by
Michael Hunger
parent
82976ae062
commit
53da3611e4
@@ -39,7 +39,7 @@ public class NodeDelegatingFieldAccessorFactory extends DelegatingFieldAccessorF
|
||||
template,
|
||||
new PropertyFieldAccessorFactory(template),
|
||||
new ConvertingNodePropertyFieldAccessorFactory(template)),
|
||||
new ValidatingNodePropertyFieldAccessorListenerFactory(template)
|
||||
new ValidatingPropertyFieldAccessorListenerFactory(template)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -33,8 +33,8 @@ public class RelationshipDelegatingFieldAccessorFactory extends DelegatingFieldA
|
||||
new IndexingPropertyFieldAccessorListenerFactory(
|
||||
template,
|
||||
new PropertyFieldAccessorFactory(template),
|
||||
new ConvertingNodePropertyFieldAccessorFactory(template)
|
||||
));
|
||||
new ConvertingNodePropertyFieldAccessorFactory(template)),
|
||||
new ValidatingPropertyFieldAccessorListenerFactory(template));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -31,11 +31,11 @@ import java.lang.annotation.Annotation;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
class ValidatingNodePropertyFieldAccessorListenerFactory implements FieldAccessorListenerFactory {
|
||||
class ValidatingPropertyFieldAccessorListenerFactory implements FieldAccessorListenerFactory {
|
||||
|
||||
private final Neo4jTemplate template;
|
||||
|
||||
ValidatingNodePropertyFieldAccessorListenerFactory(final Neo4jTemplate template) {
|
||||
ValidatingPropertyFieldAccessorListenerFactory(final Neo4jTemplate template) {
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ class ValidatingNodePropertyFieldAccessorListenerFactory implements FieldAccesso
|
||||
|
||||
@Override
|
||||
public FieldAccessListener forField(Neo4jPersistentProperty property) {
|
||||
return new ValidatingNodePropertyFieldAccessorListener(property, template.getValidator());
|
||||
return new ValidatingPropertyFieldAccessorListener(property, template.getValidator());
|
||||
}
|
||||
|
||||
|
||||
@@ -58,14 +58,14 @@ class ValidatingNodePropertyFieldAccessorListenerFactory implements FieldAccesso
|
||||
* @author Michael Hunger
|
||||
* @since 12.09.2010
|
||||
*/
|
||||
public static class ValidatingNodePropertyFieldAccessorListener<T extends PropertyContainer> implements FieldAccessListener {
|
||||
public static class ValidatingPropertyFieldAccessorListener<T extends PropertyContainer> implements FieldAccessListener {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(ValidatingNodePropertyFieldAccessorListener.class);
|
||||
private final static Logger log = LoggerFactory.getLogger(ValidatingPropertyFieldAccessorListener.class);
|
||||
private String propertyName;
|
||||
private Validator validator;
|
||||
private Neo4jPersistentEntity<?> entityType;
|
||||
|
||||
public ValidatingNodePropertyFieldAccessorListener(final Neo4jPersistentProperty field, Validator validator) {
|
||||
public ValidatingPropertyFieldAccessorListener(final Neo4jPersistentProperty field, Validator validator) {
|
||||
this.propertyName = field.getName();
|
||||
this.entityType = field.getOwner();
|
||||
this.validator = validator;
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.springframework.data.neo4j.fieldaccess;
|
||||
|
||||
import javax.validation.ValidationException;
|
||||
import javax.validation.Validator;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.neo4j.graphdb.GraphDatabaseService;
|
||||
import org.neo4j.test.TestGraphDatabaseFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
|
||||
import org.springframework.data.neo4j.config.Neo4jConfiguration;
|
||||
import org.springframework.data.neo4j.model.Friendship;
|
||||
import org.springframework.data.neo4j.model.Person;
|
||||
import org.springframework.data.neo4j.model.PersonRepository;
|
||||
import org.springframework.data.neo4j.model.FriendshipRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
|
||||
/**
|
||||
* Test validation on node and relationship properties.
|
||||
*
|
||||
* @author Timmy Storms
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class ValidatingPropertyFieldAccessorListenerFactoryTest {
|
||||
|
||||
@Configuration
|
||||
@EnableNeo4jRepositories(basePackageClasses = PersonRepository.class, considerNestedRepositories = true)
|
||||
static class Config extends Neo4jConfiguration {
|
||||
Config() throws ClassNotFoundException {
|
||||
setBasePackage(Person.class.getPackage().getName());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GraphDatabaseService graphDatabaseService() {
|
||||
return new TestGraphDatabaseFactory().newImpermanentDatabase();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Validator validator() {
|
||||
return new LocalValidatorFactoryBean();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private PersonRepository personRepo;
|
||||
|
||||
@Autowired
|
||||
private FriendshipRepository friendshipRepo;
|
||||
|
||||
@Test(expected=ValidationException.class)
|
||||
public void testNodePropertyValidation() {
|
||||
personRepo.save(new Person("Li", 102));
|
||||
}
|
||||
|
||||
@Test(expected=ValidationException.class)
|
||||
public void testRelationshipPropertyValidation() {
|
||||
final Person john = personRepo.save(new Person("John", 50));
|
||||
final Person jack = personRepo.save(new Person("Jack", 45));
|
||||
final Friendship friendship = john.knows(jack);
|
||||
friendship.setYears(100);
|
||||
friendshipRepo.save(friendship);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,6 +21,8 @@ import org.springframework.data.neo4j.annotation.*;
|
||||
import org.springframework.data.neo4j.fieldaccess.DynamicProperties;
|
||||
import org.springframework.data.neo4j.support.index.IndexType;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
@@ -56,6 +58,7 @@ public class Friendship implements Serializable {
|
||||
private String type;
|
||||
|
||||
@Indexed(indexType = IndexType.SIMPLE)
|
||||
@Max(75)
|
||||
private int years;
|
||||
|
||||
private Date firstMeetingDate;
|
||||
|
||||
Reference in New Issue
Block a user