From 529671b975e019a4bf7392b2a5252ffc97317885 Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Mon, 9 Aug 2010 23:25:30 +0200 Subject: [PATCH] david: extended tests, added relationship info --- .../graph/neo4j/Neo4jNodeBacking.aj | 54 +++++++---- .../persistence/test/Person.java | 27 +++++- .../test/graph/Neo4jGraphPersistenceTest.java | 96 +++++++++++++++++-- 3 files changed, 143 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj b/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj index a828022b9..173b8ee36 100644 --- a/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj +++ b/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj @@ -2,6 +2,7 @@ package org.springframework.persistence.graph.neo4j; import java.lang.reflect.Field; +import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.reflect.FieldSignature; import org.neo4j.graphdb.Direction; @@ -118,7 +119,7 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields Neo4J relationship with value=[" + newVal + "]"); return null; } @@ -178,10 +168,16 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields nodeInstantiator; @@ -42,8 +47,7 @@ public class Neo4jGraphPersistenceTest { @Transactional @Rollback(false) public void testUserConstructor() { - int age = 39; - Person p = new Person("Rod", age); + Person p = new Person("Rod", 39); Assert.assertEquals(p.getName(), p.getUnderlyingNode().getProperty("Person.name")); Assert.assertEquals(p.getAge(), p.getUnderlyingNode().getProperty("Person.age")); insertedId = p.getId(); @@ -51,19 +55,91 @@ public class Neo4jGraphPersistenceTest { @Test @Transactional - public void testCreateRelationshipOnSet() { - Person p = new Person("Michael", 35); - Person spouse=new Person("Tina",36); - p.setSpouse(spouse); - Assert.assertEquals("Tina", p.getSpouse().getUnderlyingNode().getProperty("Person.name")); - Node spouseNode=p.getUnderlyingNode().getSingleRelationship(DynamicRelationshipType.withName("Person.spouse"), org.neo4j.graphdb.Direction.OUTGOING).getEndNode(); - Assert.assertEquals(spouse.getUnderlyingNode(), spouseNode); + public void testSetProperties() { + Person p = new Person("Foo", 2); + p.setName("Michael"); + p.setAge(35); + Assert.assertEquals("Michael", p.getUnderlyingNode().getProperty("Person.name")); + Assert.assertEquals(35, p.getUnderlyingNode().getProperty("Person.age")); } - + @Test + @Transactional + public void testCreateRelationshipWithoutAnnotationOnSet() { + log.debug("testCreateRelationshipWithoutAnnotationOnSet"); + Person p = new Person("Michael", 35); + Person spouse = new Person("Tina",36); + p.setSpouse(spouse); + Node spouseNode=p.getUnderlyingNode().getSingleRelationship(DynamicRelationshipType.withName("Person.spouse"), org.neo4j.graphdb.Direction.OUTGOING).getEndNode(); + Assert.assertEquals(spouse.getUnderlyingNode(), spouseNode); + Assert.assertEquals(spouse, p.getSpouse()); + } + + @Test + @Transactional + public void testCreateRelationshipWithAnnotationOnSet() { + log.debug("testCreateRelationshipWithAnnotationOnSet"); + Person p = new Person("Michael", 35); + Person mother = new Person("Gabi",60); + p.setMother(mother); + Node motherNode = p.getUnderlyingNode().getSingleRelationship(DynamicRelationshipType.withName("mother"), org.neo4j.graphdb.Direction.OUTGOING).getEndNode(); + Assert.assertEquals(mother.getUnderlyingNode(), motherNode); + Assert.assertEquals(mother, p.getMother()); + } + + // TODO test delete relationship + @Test + @Transactional + public void testDeleteRelationship() { + log.debug("testDeleteRelationship"); + Person p = new Person("Michael", 35); + Person spouse = new Person("Tina", 36); + p.setSpouse(spouse); + p.setSpouse(null); + Assert.assertNull(p.getUnderlyingNode().getSingleRelationship(DynamicRelationshipType.withName("Person.spouse"), org.neo4j.graphdb.Direction.OUTGOING)); + Assert.assertNull(p.getSpouse()); + } + + // TODO test delete previous relationship + @Test + @Transactional + public void testDeletePreviousRelationshipOnNewRelationship() { + log.debug("testDeletePreviousRelationshipOnNewRelationship"); + Person p = new Person("Michael", 35); + Person spouse = new Person("Tina", 36); + Person friend = new Person("Helga", 34); + p.setSpouse(spouse); + p.setSpouse(friend); + Assert.assertEquals(friend.getUnderlyingNode(), p.getUnderlyingNode().getSingleRelationship(DynamicRelationshipType.withName("Person.spouse"), org.neo4j.graphdb.Direction.OUTGOING).getEndNode()); + Assert.assertEquals(friend, p.getSpouse()); + } + + // TODO test incoming relationship + @Test + @Transactional + public void testCreateIncomingRelationshipWithAnnotationOnSet() { + Person p = new Person("David", 25); + Person boss = new Person("Emil", 32); + p.setBoss(boss); + Assert.assertEquals(boss.getUnderlyingNode(), p.getUnderlyingNode().getSingleRelationship(DynamicRelationshipType.withName("boss"), org.neo4j.graphdb.Direction.INCOMING).getStartNode()); + Assert.assertEquals(boss, p.getBoss()); + } + + // TODO test bidirectional relationship + @Ignore + @Test + @Transactional + public void testBidirectionalRelationshipWithAnnotationOnSet() { + Person p = new Person("Michael", 35); + Person friend = new Person("David", 25); + p.setFriend(friend); + } + + // TODO test remove property (set to null) @Test @Transactional public void testInstantiatedFinder() { + log.debug("testInstantiatedFinder"); Node n = findPersonTestNode(); Person found = nodeInstantiator.createEntityFromState(n, Person.class); Assert.assertEquals("Rod", found.getUnderlyingNode().getProperty("Person.name"));