JavaEE, Technical

Neo4J – JUnit Test

This is my first dive into Neo4J using its unit testing features to create a basic database to run unit tests against.  First, I create a simple node with first and last name, then I create a node and associate fifty nodes with it. Here is the code.

package neo4j;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.graphdb.*;
import org.neo4j.graphdb.traversal.Traverser;
import org.neo4j.test.TestGraphDatabaseFactory;

import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Unit test for simple App.
*/
public class AppTest {
protected GraphDatabaseService graphDatabaseService;

@Test
public void testCreateNode() {
Transaction tx = graphDatabaseService.beginTx();
Node david = null;

try {
david = graphDatabaseService.createNode();
david.setProperty(“firstname”,”John”);
david.setProperty(“lastname”, “Jave”);

tx.success();
} finally {
tx.finish();
}

assertTrue(“Node id was not greater than zero as expected”, david.getId() > 0l);

Node foundNode = graphDatabaseService.getNodeById(david.getId());
assertEquals(“Node retrieved did not match as expected”, foundNode.getId(), david.getId());
assertEquals(“First name did not match”, foundNode.getProperty(“firstname”), david.getProperty(“firstname”));
out(“Node created. ” + foundNode.getId() + “, ” + foundNode.getProperty(“firstname”));
}

@Test
public void testNodesWithRelation() {
Transaction tx = graphDatabaseService.beginTx();
Node david = null;
Node docs = null;

try {
// david node
david = davidNode();

// reference node
Node referenceNode = graphDatabaseService.getReferenceNode();
referenceNode.createRelationshipTo(david, RelTypes.DAVID_NODE);

// docs node
docs = graphDatabaseService.createNode();
docs.setProperty(“storagelocation”,”/home/ddkit/Downloads”);

// Document Nodes
List<Node> nodesList = createDocs();

// add 50 node relationships
addMultiNodeRelationships(nodesList, david);

// connect to docs node
david.createRelationshipTo(docs, RelTypes.HAS);

org.neo4j.graphdb.Traverser documentsTraverser = getDocConnections(david);
int numDocuments = 0;

for (Node connectedDocNode : documentsTraverser) {
out(“DEPTH: ” + documentsTraverser.currentPosition().depth());
out(“Storage Location: ” + connectedDocNode.getProperty(“storagelocation”));
out(connectedDocNode.getPropertyKeys().toString());
}

tx.success();

} finally {
tx.finish();
}

}

private org.neo4j.graphdb.Traverser getDocConnections(final Node node) {

return node.traverse(org.neo4j.graphdb.Traverser.Order.BREADTH_FIRST,
StopEvaluator.END_OF_GRAPH,
ReturnableEvaluator.ALL_BUT_START_NODE,
RelTypes.HAS,
Direction.OUTGOING);
}

private void addMultiNodeRelationships(List<Node> nodesList, Node david) {
int i = 0;
for (Node n : nodesList) {
out(“Creating relationship from node with firstname of ” + n.getProperty(“firstname”) + ” to node : ” + david);
n.createRelationshipTo(david, RelTypes.HAS);
i++;
out(“Count: ” + i);
}
}

private Node davidNode() {
Node david = graphDatabaseService.createNode();
david.setProperty(“firstname”,”John”);
david.setProperty(“lastname”, “Jave”);

return david;
}

private List<Node> createDocs() {
Node node = null;
List<Node> n = new LinkedList<Node>();
for (int i=0; i < 50; i++) {
node = graphDatabaseService.createNode();
node.setProperty(“firstname”, “David”+(i+1));
n.add(node);
}

out(“Number of nodes created: ” + n.size());
out(“List of nodes:           ” + n);
out(“Node 1: ” + n.get(0).getProperty(“firstname”));
return n;
}

private void out(String s) {
Logger.getAnonymousLogger().info(s);
}

@Before
public void setUp() {
graphDatabaseService = new TestGraphDatabaseFactory()
.newImpermanentDatabaseBuilder().newGraphDatabase();
}

@After
public void tearDown() {
graphDatabaseService.shutdown();
graphDatabaseService = null;
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s