DATAGRAPH-1420 - Check for TargetNode in RelationshipProperties.

This commit is contained in:
Gerrit Meier
2020-11-03 08:12:06 +01:00
parent 7dae4bd8ea
commit 547a1df4bd
2 changed files with 49 additions and 2 deletions

View File

@@ -154,8 +154,12 @@ final class DefaultNeo4jPersistentProperty extends AnnotationBasedPersistentProp
@NonNull
private Class<?> getRelationshipPropertiesTargetType(Class<?> relationshipPropertiesType) {
return this.mappingContext.getPersistentEntity(relationshipPropertiesType)
.getPersistentProperty(TargetNode.class).getType();
Neo4jPersistentProperty persistentProperty = this.mappingContext.getPersistentEntity(relationshipPropertiesType)
.getPersistentProperty(TargetNode.class);
if (persistentProperty == null) {
throw new MappingException("Missing @TargetNode declaration in " + relationshipPropertiesType);
}
return persistentProperty.getType();
}
@Override

View File

@@ -16,6 +16,7 @@
package org.springframework.data.neo4j.core.mapping;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import java.util.Arrays;
@@ -28,12 +29,15 @@ import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.neo4j.core.schema.DynamicLabels;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;
import org.springframework.data.neo4j.core.schema.Relationship;
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
import org.springframework.data.neo4j.core.schema.TargetNode;
/**
* @author Gerrit Meier
@@ -89,6 +93,23 @@ class DefaultNeo4jPersistentEntityTest {
schema.setInitialEntitySet(new HashSet<>(Arrays.asList(entityToTest)));
assertThatIllegalStateException().isThrownBy(() -> schema.initialize()).withMessageMatching(expectedMessage);
}
@Test // DATAGRAPH-1420
void doesNotFailOnCorrectRelationshipProperties() {
Neo4jPersistentEntity<?> persistentEntity = new Neo4jMappingContext()
.getPersistentEntity(EntityWithCorrectRelationshipProperties.class);
assertThat(persistentEntity).isNotNull();
}
@Test // DATAGRAPH-1420
void doesFailOnRelationshipPropertiesWithMissingTargetNode() {
assertThatExceptionOfType(MappingException.class)
.isThrownBy(() -> new Neo4jMappingContext()
.getPersistentEntity(EntityWithInCorrectRelationshipProperties.class))
.withMessageContaining("Missing @TargetNode declaration in");
}
}
@Nested
@@ -391,4 +412,26 @@ class DefaultNeo4jPersistentEntityTest {
private Map<String, List<Neo4jMappingContextTest.BikeNode>> bikes2;
}
static class EntityWithCorrectRelationshipProperties {
@Id private String id;
@Relationship HasTargetNodeRelationshipProperties rel;
}
static class EntityWithInCorrectRelationshipProperties {
@Id private String id;
@Relationship HasNoTargetNodeRelationshipProperties rel;
}
@RelationshipProperties
static class HasTargetNodeRelationshipProperties {
@TargetNode
EntityWithExplicitPrimaryLabel entity;
}
@RelationshipProperties
static class HasNoTargetNodeRelationshipProperties {
}
}