Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit dcd9f6823303353f5fc003cf341ce57394eba7b0 @phifty committed Nov 28, 2012
5 .gitignore
@@ -0,0 +1,5 @@
+.idea
+.gradle
+/build
+/lib
+/out
12 README.md
@@ -0,0 +1,12 @@
+# me.phifty.neo4j-graph-v1.0
+
+Vert.x module to read and write a neo4j graph database.
+
+## Configuration
+
+The following JSON structure shows an example configuration.
+
+ {
+ "base_address": "graph", # the base address of the module
+ "path": "/var/graph" # the path where to store the data
+ }
28 build.gradle
@@ -0,0 +1,28 @@
+
+apply plugin: "java"
+
+version = '1.0'
+jar {
+ manifest {
+ attributes 'Implementation-Title': 'Vertx filesystem persistor',
+ 'Implementation-Version': version
+ }
+}
+
+repositories {
+ mavenCentral()
+}
+
+dependencies {
+ compile group: 'org.codehaus.groovy', name: 'groovy', version: '2.0.4'
+ compile group: 'org.mozilla', name: 'rhino', version: '1.7R4'
+
+ compile group: 'org.vert-x', name: 'vertx-core', version: '1.3.0.final'
+ compile group: 'org.vert-x', name: 'vertx-platform', version: '1.3.0.final'
+
+ compile group: 'org.neo4j', name: 'neo4j', version: '1.9.M01'
+
+ testCompile group: 'junit', name: 'junit', version: '4.+'
+ testCompile group: 'org.vert-x', name: 'vertx-testframework', version: '1.3.0.final'
+ testCompile group: 'org.neo4j', name: 'neo4j-kernel', version: '1.9.M01', classifier: 'tests'
+}
4 mod.json
@@ -0,0 +1,4 @@
+{
+ "main": "org.vertx.java.busmods.Neo4jGraphModule",
+ "auto-redeploy": true
+}
22 src/main/java/me/phifty/graph/Graph.java
@@ -0,0 +1,22 @@
+package me.phifty.graph;
+
+import java.util.Map;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public interface Graph {
+
+ public void createNode(Map<String, Object> properties, Handler<Long> handler);
+
+ public void updateNode(long id, Map<String, Object> properties, Handler<Boolean> handler);
+
+ public void fetchNode(long id, Handler<Map<String, Object>> handler);
+
+ public void removeNode(long id, Handler<Boolean> handler);
+
+ public void clear(Handler<Boolean> handler);
+
+ public void shutdown();
+
+}
12 src/main/java/me/phifty/graph/Handler.java
@@ -0,0 +1,12 @@
+package me.phifty.graph;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public interface Handler<T> {
+
+ public void handle(T value);
+
+ public void exception(Exception exception);
+
+}
12 src/main/java/me/phifty/graph/neo4j/GraphDatabaseServiceFactory.java
@@ -0,0 +1,12 @@
+package me.phifty.graph.neo4j;
+
+import org.neo4j.graphdb.GraphDatabaseService;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public interface GraphDatabaseServiceFactory {
+
+ public GraphDatabaseService create(String path);
+
+}
126 src/main/java/me/phifty/graph/neo4j/Neo4jGraph.java
@@ -0,0 +1,126 @@
+package me.phifty.graph.neo4j;
+
+import me.phifty.graph.Graph;
+import me.phifty.graph.Handler;
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.graphdb.Node;
+import org.neo4j.graphdb.NotFoundException;
+import org.neo4j.graphdb.Transaction;
+import org.neo4j.tooling.GlobalGraphOperations;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public class Neo4jGraph implements Graph {
+
+ private GraphDatabaseService graphDatabaseService;
+
+ public Neo4jGraph(String path) {
+ this(path, new Neo4jGraphDatabaseServiceFactory());
+ }
+
+ public Neo4jGraph(String path, GraphDatabaseServiceFactory graphDatabaseServiceFactory) {
+ graphDatabaseService = graphDatabaseServiceFactory.create(path);
+ }
+
+ @Override
+ public void createNode(Map<String, Object> properties, Handler<Long> handler) {
+ Transaction transaction = graphDatabaseService.beginTx();
+ try {
+ Node node = graphDatabaseService.createNode();
+ setProperties(node, properties);
+ transaction.success();
+ handler.handle(node.getId());
+ } catch (Exception exception) {
+ transaction.failure();
+ handler.exception(exception);
+ } finally {
+ transaction.finish();
+ }
+ }
+
+ @Override
+ public void updateNode(long id, Map<String, Object> properties, Handler<Boolean> handler) {
+ Transaction transaction = graphDatabaseService.beginTx();
+ try {
+ Node node = graphDatabaseService.getNodeById(id);
+ setProperties(node, properties);
+ transaction.success();
+ handler.handle(true);
+ } catch (Exception exception) {
+ transaction.failure();
+ handler.exception(exception);
+ } finally {
+ transaction.finish();
+ }
+ }
+
+ @Override
+ public void fetchNode(long id, Handler<Map<String, Object>> handler) {
+ try {
+ Node node = graphDatabaseService.getNodeById(id);
+ handler.handle(getProperties(node));
+ } catch (NotFoundException exception) {
+ handler.handle(null);
+ } catch (Exception exception) {
+ handler.exception(exception);
+ }
+ }
+
+ @Override
+ public void removeNode(long id, Handler<Boolean> handler) {
+ Transaction transaction = graphDatabaseService.beginTx();
+ try {
+ Node node = graphDatabaseService.getNodeById(id);
+ node.delete();
+ transaction.success();
+ handler.handle(true);
+ } catch (Exception exception) {
+ transaction.failure();
+ handler.exception(exception);
+ } finally {
+ transaction.finish();
+ }
+ }
+
+ @Override
+ public void clear(Handler<Boolean> handler) {
+ Transaction transaction = graphDatabaseService.beginTx();
+ try {
+ GlobalGraphOperations globalGraphOperations = GlobalGraphOperations.at(graphDatabaseService);
+ for (Node node : globalGraphOperations.getAllNodes()) {
+ node.delete();
+ }
+ transaction.success();
+ handler.handle(true);
+ } catch (Exception exception) {
+ transaction.failure();
+ handler.exception(exception);
+ } finally {
+ transaction.finish();
+ }
+ }
+
+ @Override
+ public void shutdown() {
+ graphDatabaseService.shutdown();
+ }
+
+ private void setProperties(Node node, Map<String, Object> properties) {
+ for (Map.Entry<String, Object> property : properties.entrySet()) {
+ node.setProperty(property.getKey(), property.getValue());
+ }
+ }
+
+ private Map<String, Object> getProperties(Node node) {
+ Map<String, Object> result = new HashMap<>();
+ for (String key : node.getPropertyKeys()) {
+ result.put(key, node.getProperty(key));
+ }
+ return result;
+ }
+
+}
27 src/main/java/me/phifty/graph/neo4j/Neo4jGraphDatabaseServiceFactory.java
@@ -0,0 +1,27 @@
+package me.phifty.graph.neo4j;
+
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.graphdb.factory.GraphDatabaseFactory;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public class Neo4jGraphDatabaseServiceFactory implements GraphDatabaseServiceFactory {
+
+ @Override
+ public GraphDatabaseService create(String path) {
+ GraphDatabaseService graphDatabaseService = new GraphDatabaseFactory().newEmbeddedDatabase(path);
+ registerShutdownHook(graphDatabaseService);
+ return graphDatabaseService;
+ }
+
+ private void registerShutdownHook(final GraphDatabaseService graphDatabaseService) {
+ Runtime.getRuntime().addShutdownHook(new Thread() {
+ @Override
+ public void run() {
+ graphDatabaseService.shutdown();
+ }
+ });
+ }
+
+}
14 src/main/java/org/vertx/java/busmods/Configuration.java
@@ -0,0 +1,14 @@
+package org.vertx.java.busmods;
+
+import java.util.Map;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public interface Configuration {
+
+ public String getBaseAddress();
+
+ public String getPath();
+
+}
20 src/main/java/org/vertx/java/busmods/DefaultConfiguration.java
@@ -0,0 +1,20 @@
+package org.vertx.java.busmods;
+
+import java.io.File;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public class DefaultConfiguration implements Configuration {
+
+ @Override
+ public String getBaseAddress() {
+ return "neo4j-graph";
+ }
+
+ @Override
+ public String getPath() {
+ return System.getProperty("user.home") + File.pathSeparator + "graph";
+ }
+
+}
193 src/main/java/org/vertx/java/busmods/Neo4jGraphModule.java
@@ -0,0 +1,193 @@
+package org.vertx.java.busmods;
+
+import me.phifty.graph.Graph;
+import me.phifty.graph.neo4j.Neo4jGraph;
+import org.vertx.java.busmods.json.JsonConfiguration;
+import org.vertx.java.core.Handler;
+import org.vertx.java.core.eventbus.Message;
+import org.vertx.java.core.json.JsonObject;
+import org.vertx.java.deploy.Verticle;
+
+import java.util.Map;
+
+public class Neo4jGraphModule extends Verticle {
+
+ private Configuration configuration;
+ private Graph database;
+
+ @Override
+ public void start() throws Exception {
+ initializeConfiguration();
+ initializeDatabase();
+ registerStoreNodeHandler();
+ registerFetchNodeHandler();
+ registerRemoveNodeHandler();
+ registerClearHandler();
+ }
+
+ @Override
+ public void stop() throws Exception {
+ database.shutdown();
+ super.stop();
+ }
+
+ private void initializeConfiguration() {
+ configuration = new JsonConfiguration(container.getConfig());
+ }
+
+ private void initializeDatabase() {
+ database = new Neo4jGraph(configuration.getPath());
+ }
+
+ private void registerStoreNodeHandler() {
+ final Graph database = this.database;
+ vertx.eventBus().registerHandler(configuration.getBaseAddress() + ".nodes.store", new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(final Message<JsonObject> message) {
+ Map<String, Object> body = message.body.toMap();
+ Map<String, Object> properties = body.containsKey("properties") ? (Map<String, Object>)body.get("properties") : null;
+
+ if (body.containsKey("id")) {
+ long id = Long.parseLong(body.get("id").toString());
+
+ try {
+ database.updateNode(id, properties, new me.phifty.graph.Handler<Boolean>() {
+ @Override
+ public void handle(Boolean value) {
+ message.reply(doneMessage(true));
+ }
+
+ @Override
+ public void exception(Exception exception) {
+ message.reply(failMessage(exception));
+ }
+ });
+ } catch (Exception exception) {
+ message.reply(failMessage(exception));
+ }
+ } else {
+ try {
+ database.createNode(properties, new me.phifty.graph.Handler<Long>() {
+ @Override
+ public void handle(Long id) {
+ message.reply(idMessage(id));
+ }
+
+ @Override
+ public void exception(Exception exception) {
+ message.reply(failMessage(exception));
+ }
+ });
+ } catch (Exception exception) {
+ message.reply(failMessage(exception));
+ }
+ }
+ }
+ });
+ }
+
+ private void registerFetchNodeHandler() {
+ final Graph database = this.database;
+ vertx.eventBus().registerHandler(configuration.getBaseAddress() + ".nodes.fetch", new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(final Message<JsonObject> message) {
+ long id = message.body.getLong("id");
+
+ try {
+ database.fetchNode(id, new me.phifty.graph.Handler<Map<String, Object>>() {
+ @Override
+ public void handle(Map<String, Object> node) {
+ message.reply(node == null ? null : nodeMessage(node));
+ }
+
+ @Override
+ public void exception(Exception exception) {
+ message.reply(failMessage(exception));
+ }
+ });
+ } catch (Exception exception) {
+ message.reply(failMessage(exception));
+ }
+ }
+ });
+ }
+
+ private void registerRemoveNodeHandler() {
+ vertx.eventBus().registerHandler(configuration.getBaseAddress() + ".nodes.remove", new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(final Message<JsonObject> message) {
+ long id = message.body.getLong("id");
+
+ try {
+ database.removeNode(id, new me.phifty.graph.Handler<Boolean>() {
+ @Override
+ public void handle(Boolean value) {
+ message.reply(doneMessage(true));
+ }
+
+ @Override
+ public void exception(Exception exception) {
+ message.reply(failMessage(exception));
+ }
+ });
+ } catch (Exception exception) {
+ message.reply(failMessage(exception));
+ }
+ }
+ });
+ }
+
+ private void registerClearHandler() {
+ vertx.eventBus().registerHandler(configuration.getBaseAddress() + ".clear", new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(final Message<JsonObject> message) {
+ try {
+ database.clear(new me.phifty.graph.Handler<Boolean>() {
+ @Override
+ public void handle(Boolean value) {
+ message.reply(doneMessage(true));
+ }
+
+ @Override
+ public void exception(Exception exception) {
+ message.reply(failMessage(exception));
+ }
+ });
+ } catch (Exception exception) {
+ message.reply(failMessage(exception));
+ }
+ }
+ });
+ }
+
+ private JsonObject doneMessage(Boolean done) {
+ JsonObject message = new JsonObject();
+ message.putBoolean("done", done);
+ return message;
+ }
+
+ private JsonObject idMessage(long id) {
+ JsonObject message = new JsonObject();
+ message.putNumber("id", id);
+ return message;
+ }
+
+ private JsonObject nodeMessage(Map<String, Object> properties) {
+ JsonObject message = new JsonObject();
+ for (Map.Entry<String, Object> property : properties.entrySet()) {
+ if (property.getValue() instanceof String) {
+ message.putString(property.getKey(), (String)property.getValue());
+ } else if (property.getValue() instanceof Number) {
+ message.putNumber(property.getKey(), (Number)property.getValue());
+ }
+ }
+ return message;
+ }
+
+ private JsonObject failMessage(Exception exception) {
+ JsonObject message = new JsonObject();
+ message.putString("exception", exception.toString());
+ return message;
+ }
+
+}
30 src/main/java/org/vertx/java/busmods/json/JsonConfiguration.java
@@ -0,0 +1,30 @@
+package org.vertx.java.busmods.json;
+
+import org.vertx.java.busmods.Configuration;
+import org.vertx.java.busmods.DefaultConfiguration;
+import org.vertx.java.core.json.JsonObject;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public class JsonConfiguration implements Configuration {
+
+ private JsonObject object;
+ private Configuration defaultConfiguration;
+
+ public JsonConfiguration(JsonObject object) {
+ this.object = object;
+ this.defaultConfiguration = new DefaultConfiguration();
+ }
+
+ @Override
+ public String getBaseAddress() {
+ return object.getString("base_address", defaultConfiguration.getBaseAddress());
+ }
+
+ @Override
+ public String getPath() {
+ return object.getString("path", defaultConfiguration.getPath());
+ }
+
+}
13 src/main/resources/langs.properties
@@ -0,0 +1,13 @@
+# Mapping between main file extensions and the verticle factory for the class
+
+java=org.vertx.java.deploy.impl.java.JavaVerticleFactory
+class=org.vertx.java.deploy.impl.java.JavaVerticleFactory
+js=org.vertx.java.deploy.impl.rhino.RhinoVerticleFactory
+coffee=org.vertx.java.deploy.impl.rhino.RhinoVerticleFactory
+rb=org.vertx.java.deploy.impl.jruby.JRubyVerticleFactory
+groovy=org.vertx.groovy.deploy.impl.groovy.GroovyVerticleFactory
+py=org.vertx.java.deploy.impl.jython.JythonVerticleFactory
+
+# Default - if none match this will be assumed
+
+default=org.vertx.java.deploy.impl.java.JavaVerticleFactory
30 src/test/java/me/phifty/graph/test/FakeHandler.java
@@ -0,0 +1,30 @@
+package me.phifty.graph.test;
+
+import me.phifty.graph.Handler;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public class FakeHandler<T> implements Handler<T> {
+
+ private T value;
+
+ @Override
+ public void handle(T value) {
+ this.value = value;
+ }
+
+ @Override
+ public void exception(Exception exception) {
+ exception.printStackTrace();
+ }
+
+ public void reset() {
+ value = null;
+ }
+
+ public T getValue() {
+ return value;
+ }
+
+}
17 src/test/java/me/phifty/graph/test/neo4j/FakeGraphDatabaseServiceFactory.java
@@ -0,0 +1,17 @@
+package me.phifty.graph.test.neo4j;
+
+import me.phifty.graph.neo4j.GraphDatabaseServiceFactory;
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.test.TestGraphDatabaseFactory;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public class FakeGraphDatabaseServiceFactory implements GraphDatabaseServiceFactory {
+
+ @Override
+ public GraphDatabaseService create(String path) {
+ return new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().newGraphDatabase();
+ }
+
+}
112 src/test/java/me/phifty/graph/test/neo4j/Neo4jGraphTest.java
@@ -0,0 +1,112 @@
+package me.phifty.graph.test.neo4j;
+
+import me.phifty.graph.Graph;
+import me.phifty.graph.neo4j.Neo4jGraph;
+import me.phifty.graph.test.FakeHandler;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public class Neo4jGraphTest {
+
+ private FakeHandler<Long> idHandler = new FakeHandler<>();
+ private FakeHandler<Boolean> doneHandler = new FakeHandler<>();
+ private FakeHandler<Map<String, Object>> nodeHandler = new FakeHandler<>();
+ private Graph graph;
+
+ @Before
+ public void setUp() throws Exception {
+ graph = new Neo4jGraph("/tmp/test", new FakeGraphDatabaseServiceFactory());
+ }
+
+ @After
+ public void tearDown() {
+ idHandler.reset();
+ doneHandler.reset();
+ nodeHandler.reset();
+ graph.shutdown();
+ }
+
+ @Test
+ public void testCreateNode() {
+ graph.createNode(testNode(), idHandler);
+ Assert.assertTrue(idHandler.getValue() > 0);
+
+ graph.fetchNode(idHandler.getValue(), nodeHandler);
+ assertTestNode(nodeHandler.getValue());
+ }
+
+ @Test
+ public void testUpdateNode() {
+ long id = addTestNode();
+
+ graph.updateNode(id, updatedTestNode(), doneHandler);
+ Assert.assertTrue(doneHandler.getValue());
+
+ graph.fetchNode(id, nodeHandler);
+ assertUpdatedTestNode(nodeHandler.getValue());
+ }
+
+ @Test
+ public void testFetchNode() {
+ long id = addTestNode();
+
+ graph.fetchNode(id, nodeHandler);
+ assertTestNode(nodeHandler.getValue());
+ }
+
+ @Test
+ public void testRemoveNode() {
+ long id = addTestNode();
+
+ graph.removeNode(id, doneHandler);
+ Assert.assertTrue(doneHandler.getValue());
+
+ graph.fetchNode(id, nodeHandler);
+ Assert.assertNull(nodeHandler.getValue());
+ }
+
+ @Test
+ public void testClear() {
+ long id = addTestNode();
+
+ graph.clear(doneHandler);
+ Assert.assertTrue(doneHandler.getValue());
+
+ graph.fetchNode(id, nodeHandler);
+ Assert.assertNull(nodeHandler.getValue());
+ }
+
+ private long addTestNode() {
+ graph.createNode(testNode(), idHandler);
+ return idHandler.getValue();
+ }
+
+ private void assertTestNode(Map<String, Object> properties) {
+ Assert.assertEquals("test node", properties.get("content"));
+ }
+
+ private void assertUpdatedTestNode(Map<String, Object> properties) {
+ Assert.assertEquals("updated test node", properties.get("content"));
+ }
+
+ private Map<String, Object> testNode() {
+ Map<String, Object> result = new HashMap<>();
+ result.put("content", "test node");
+ return result;
+ }
+
+ private Map<String, Object> updatedTestNode() {
+ Map<String, Object> result = new HashMap<>();
+ result.put("content", "updated test node");
+ return result;
+ }
+
+}
33 src/test/java/org/vertx/java/busmods/test/Neo4jGraphModuleTest.java
@@ -0,0 +1,33 @@
+package org.vertx.java.busmods.test;
+
+import org.vertx.java.framework.TestBase;
+
+public class Neo4jGraphModuleTest extends TestBase {
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ startApp(Neo4jGraphTestClient.class.getName());
+ }
+
+ public void testCreateNode() {
+ startTest(getMethodName());
+ }
+
+ public void testUpdateNode() {
+ startTest(getMethodName());
+ }
+
+ public void testFetchNode() {
+ startTest(getMethodName());
+ }
+
+ public void testRemoveNode() {
+ startTest(getMethodName());
+ }
+
+ public void testClear() {
+ startTest(getMethodName());
+ }
+
+}
223 src/test/java/org/vertx/java/busmods/test/Neo4jGraphTestClient.java
@@ -0,0 +1,223 @@
+package org.vertx.java.busmods.test;
+
+import org.vertx.java.busmods.Neo4jGraphModule;
+import org.vertx.java.core.Handler;
+import org.vertx.java.core.eventbus.Message;
+import org.vertx.java.core.json.JsonObject;
+import org.vertx.java.framework.TestClientBase;
+
+/**
+ * @author phifty <b.phifty@gmail.com>
+ */
+public class Neo4jGraphTestClient extends TestClientBase {
+
+ private long testNodeId;
+
+ @Override
+ public void start() {
+ super.start();
+
+ JsonObject configuration = new JsonObject();
+ configuration.putString("base_address", "test.neo4j-graph");
+ configuration.putString("path", "/tmp/graph");
+
+ container.deployVerticle(Neo4jGraphModule.class.getName(), configuration, 1, new Handler<String>() {
+ @Override
+ public void handle(String deploymentId) {
+ tu.appReady();
+ }
+ });
+ }
+
+ public void testCreateNode() {
+ vertx.eventBus().send("test.neo4j-graph.nodes.store", generateCreateNodeMessage("test node"), new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(Message<JsonObject> message) {
+ checkForException(message);
+ fetchTestNode(new Handler<String>() {
+ @Override
+ public void handle(String content) {
+ try {
+ tu.azzert("test node".equals(content), "should store the right node");
+ } finally {
+ clearAll(new Handler<Boolean>() {
+ @Override
+ public void handle(Boolean done) {
+ tu.testComplete();
+ }
+ });
+ }
+ }
+ });
+ }
+ });
+ }
+
+ public void testUpdateNode() {
+ addTestNode(new Handler<Long>() {
+ @Override
+ public void handle(Long id) {
+ vertx.eventBus().send("test.neo4j-graph.nodes.store", generateUpdateNodeMessage(id, "updated test node"), new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(Message<JsonObject> message) {
+ checkForException(message);
+ fetchTestNode(new Handler<String>() {
+ @Override
+ public void handle(String content) {
+ try {
+ tu.azzert("updated test node".equals(content), "should store the right node");
+ } finally {
+ clearAll(new Handler<Boolean>() {
+ @Override
+ public void handle(Boolean done) {
+ tu.testComplete();
+ }
+ });
+ }
+ }
+ });
+ }
+ });
+ }
+ });
+ }
+
+ public void testFetchNode() {
+ addTestNode(new Handler<Long>() {
+ @Override
+ public void handle(Long id) {
+ vertx.eventBus().send("test.neo4j-graph.nodes.fetch", generateFetchNodeMessage(id), new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(Message<JsonObject> message) {
+ try {
+ checkForException(message);
+ tu.azzert("test node".equals(message.body.getString("content")), "should respond the right content");
+ } finally {
+ clearAll(new Handler<Boolean>() {
+ @Override
+ public void handle(Boolean done) {
+ tu.testComplete();
+ }
+ });
+ }
+ }
+ });
+ }
+ });
+ }
+
+ public void testRemoveNode() {
+ addTestNode(new Handler<Long>() {
+ @Override
+ public void handle(Long id) {
+ vertx.eventBus().send("test.neo4j-graph.nodes.remove", generateRemoveNodeMessage(id), new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(Message<JsonObject> message) {
+ fetchTestNode(new Handler<String>() {
+ @Override
+ public void handle(String content) {
+ try {
+ tu.azzert(content == null, "should remove the content");
+ } finally {
+ tu.testComplete();
+ }
+ }
+ });
+ }
+ });
+ }
+ });
+ }
+
+ public void testClear() {
+ addTestNode(new Handler<Long>() {
+ @Override
+ public void handle(Long id) {
+ vertx.eventBus().send("test.neo4j-graph.clear", null, new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(Message<JsonObject> message) {
+ checkForException(message);
+ fetchTestNode(new Handler<String>() {
+ @Override
+ public void handle(String content) {
+ try {
+ tu.azzert(content == null, "should clear all data");
+ } finally {
+ tu.testComplete();
+ }
+ }
+ });
+ }
+ });
+ }
+ });
+ }
+
+ private void checkForException(Message<JsonObject> message) {
+ if (message.body.getString("exception") != null) {
+ tu.exception(new Exception(message.body.getString("exception")), "received exception message");
+ }
+ }
+
+ private void addTestNode(final Handler<Long> handler) {
+ vertx.eventBus().send("test.neo4j-graph.nodes.store", generateCreateNodeMessage("test node"), new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(Message<JsonObject> message) {
+ testNodeId = message.body.getLong("id");
+ handler.handle(testNodeId);
+ }
+ });
+ }
+
+ private void fetchTestNode(final Handler<String> handler) {
+ vertx.eventBus().send("test.neo4j-graph.nodes.fetch", generateFetchNodeMessage(testNodeId), new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(Message<JsonObject> message) {
+ handler.handle(message.body == null ? null : message.body.getString("content"));
+ }
+ });
+ }
+
+ private void clearAll(Handler<Boolean> handler) {
+ sendAndReceiveDoneMessage("test.neo4j-graph.clear", null, handler);
+ }
+
+ private void sendAndReceiveDoneMessage(String address, JsonObject message, final Handler<Boolean> handler) {
+ vertx.eventBus().send(address, message, new Handler<Message<JsonObject>>() {
+ @Override
+ public void handle(Message<JsonObject> message) {
+ handler.handle(message.body.getBoolean("done"));
+ }
+ });
+ }
+
+ private JsonObject generateCreateNodeMessage(String content) {
+ JsonObject message = new JsonObject();
+ JsonObject properties = new JsonObject();
+ properties.putString("content", content);
+ message.putObject("properties", properties);
+ return message;
+ }
+
+ private JsonObject generateUpdateNodeMessage(long id, String content) {
+ JsonObject message = new JsonObject();
+ JsonObject properties = new JsonObject();
+ properties.putString("content", content);
+ message.putNumber("id", id);
+ message.putObject("properties", properties);
+ return message;
+ }
+
+ private JsonObject generateFetchNodeMessage(long id) {
+ JsonObject message = new JsonObject();
+ message.putNumber("id", id);
+ return message;
+ }
+
+ private JsonObject generateRemoveNodeMessage(long id) {
+ JsonObject message = new JsonObject();
+ message.putNumber("id", id);
+ return message;
+ }
+
+}
23 vertx-neo4j-graph.iml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+ <component name="NewModuleRootManager" inherit-compiler-output="false">
+ <output url="file://$MODULE_DIR$/build/classes/main" />
+ <output-test url="file://$MODULE_DIR$/build/classes/test" />
+ <exclude-output />
+ <content url="file://$MODULE_DIR$">
+ <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
+ <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
+ <sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
+ </content>
+ <orderEntry type="inheritedJdk" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ <orderEntry type="library" name="junit:junit:4.9" level="project" />
+ <orderEntry type="library" name="org.codehaus.groovy:groovy:2.0.4" level="project" />
+ <orderEntry type="library" name="org.mozilla:rhino:1.7R4" level="project" />
+ <orderEntry type="library" name="org.vert-x:vertx-core:1.3.0.final" level="project" />
+ <orderEntry type="library" name="org.vert-x:vertx-platform:1.3.0.final" level="project" />
+ <orderEntry type="library" name="org.vert-x:vertx-testframework:1.3.0.final" level="project" />
+ <orderEntry type="library" name="org.neo4j:neo4j:1.9.M01" level="project" />
+ </component>
+</module>
+

0 comments on commit dcd9f68

Please sign in to comment.
Something went wrong with that request. Please try again.