added more tests; verified simple one class persistence is working

This commit is contained in:
trisberg
2010-07-29 10:15:46 -04:00
parent 025ed44f19
commit 5545acd554
5 changed files with 137 additions and 10 deletions

View File

@@ -20,11 +20,13 @@ public abstract class Neo4jHelper {
return new GraphDatabaseUtil(gds).getOrCreateSubReferenceNode(subRefRelType);
}
// public static long count(Class<? extends NodeBacked> entityClass, GraphDatabaseService gds) {
// Node subrefNode = findSubreferenceNode(Person.class, gds);
// // If the subref node is new, there are 0 instances of this entity class
// int count = ((Integer) subrefNode.getProperty(SUBREFERENCE_NODE_COUNTER_KEY, 0));
// return count;
// }
public static long count(Class<? extends NodeBacked> entityClass, GraphDatabaseService gds) {
// TODO: Need to figure out what to do here
// Node subrefNode = findSubreferenceNode(Person.class, gds);
// If the subref node is new, there are 0 instances of this entity class
// int count = ((Integer) subrefNode.getProperty(SUBREFERENCE_NODE_COUNTER_KEY, 0));
// return count;
return 1;
}
}

View File

@@ -5,6 +5,8 @@ import org.springframework.persistence.graph.GraphEntity;
@GraphEntity
public class Person {
private Long id;
private String name;
private int age;

View File

@@ -0,0 +1,81 @@
package org.springframework.persistence.test;
import java.util.ArrayList;
import java.util.List;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.persistence.graph.neo4j.Neo4jHelper;
/**
* EXAMPLE OF CODE THAT SHOULD BE GENERATED BY ROO BESIDES EACH GRAPHENTITY CLASS
*
* Note: Combines X_Roo_Entity with X_Roo_Finder, as
* we need only a single aspect for entities.
* @author rodjohnson
*
*/
privileged aspect Person_Graph_Entity {
// TODO should be a better way of getting this? Could at least pull out gdsholder class
private static GraphDatabaseService graphDatabaseService() {
return new GdsHolder().gds;
}
@Configurable
public static class GdsHolder {
@Autowired
public GraphDatabaseService gds;
}
/**
* Add constructor that takes node.
* @param node
*/
public Person.new(Node node) {
setUnderlyingNode(node);
}
public Long Person.getId() {
return getUnderlyingNode().getId();
}
public static long Person.countPeople() {
return Neo4jHelper.count(Person.class, Person_Graph_Entity.graphDatabaseService());
}
public static List<Person> Person.findAllPeople() {
Node subrefNode = Neo4jHelper.findSubreferenceNode(Person.class, graphDatabaseService());
// TODO Neo4j should add lazy list on top of graph
List<Person> people = new ArrayList<Person>((int) countPeople());
for (Relationship rel : subrefNode.getRelationships(Neo4jHelper.INSTANCE_OF_RELATIONSHIP_TYPE, Direction.INCOMING)) {
Node personNode = rel.getStartNode();
people.add(new Person(personNode));
}
return people;
}
public static Person Person.findPerson(Long id) {
Node n = Person_Graph_Entity.graphDatabaseService().getNodeById(id);
Person found = new Person(n);
return found;
}
// Pluggable query executors/resolvers, discussed with PL
// public static Person.findFooBars(int a, int b) {
// return executeQuery("foobar", a, b);
// // First look for String, then for method
// // QueryInterceptionResolver
// }
// public static List<Person> Person.findPersonEntries(int firstResult,
// int maxResults) {
// throw new UnsupportedOperationException();
// }
}

View File

@@ -2,10 +2,15 @@ package org.springframework.persistence.test.graph;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.persistence.graph.neo4j.NodeBacked;
import org.springframework.persistence.support.EntityInstantiator;
import org.springframework.persistence.test.Person;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
@@ -17,12 +22,18 @@ import org.springframework.transaction.annotation.Transactional;
@ContextConfiguration
public class Neo4jGraphPersistenceTest {
@Autowired
private EntityInstantiator<NodeBacked,Node> nodeInstantiator;
@Autowired
protected GraphDatabaseService graphDatabaseService;
private static Long insertedId = 0L;
@Test
public void testGraphDatabaseServiceCreatedAndAutowired() {
public void testStuffWasAutowired() {
Assert.assertNotNull( graphDatabaseService );
Assert.assertNotNull( nodeInstantiator );
}
@Test
@@ -31,8 +42,39 @@ public class Neo4jGraphPersistenceTest {
public void testUserConstructor() {
int age = 39;
Person p = new Person("Rod", age);
Assert.assertEquals(p.getUnderlyingNode().getProperty("Person.name"), p.getName());
Assert.assertEquals(age, p.getAge());
Assert.assertEquals(p.getName(), p.getUnderlyingNode().getProperty("Person.name"));
Assert.assertEquals(p.getAge(), p.getUnderlyingNode().getProperty("Person.age"));
insertedId = p.getId();
}
@Test
@Transactional
public void testInstantiatedFinder() {
Node n = findPersonTestNode();
Person found = nodeInstantiator.createEntityFromState(n, Person.class);
Assert.assertEquals("Rod", found.getUnderlyingNode().getProperty("Person.name"));
Assert.assertEquals(39, found.getUnderlyingNode().getProperty("Person.age"));
}
@Test
public void printNeo4jData() {
StringBuilder ret = new StringBuilder();
for (Node n : graphDatabaseService.getAllNodes()) {
ret.append("ID: " + n.getId() + " [");
int x = 0;
for (String prop : n.getPropertyKeys()) {
if (x++ > 0) {
ret.append(", ");
}
ret.append(prop + "=" + n.getProperty(prop));
}
ret.append("] ");
}
System.out.println("*** NEO4J DATA: " + ret);
}
private Node findPersonTestNode() {
return graphDatabaseService.getNodeById(insertedId);
}
}

View File

@@ -16,7 +16,7 @@ log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.category.org.springframework=DEBUG
log4j.category.org.springframework=WARN
log4j.category.org.springframework.data=TRACE
log4j.category.org.springframework.datastore=TRACE
log4j.category.org.springframework.persistence=TRACE