Skip to content
Browse files

Still refactoring, getting more tests to pass.

  • Loading branch information...
1 parent 809176d commit b47b394d05af0511f31b5ab3d8159f26f3c92575 @jakewins jakewins committed
View
4 src/main/java/org/neo4j/smack/api/NodeService.java
@@ -50,7 +50,7 @@ public void getRoot(Invocation invocation, Output result) {
@DeserializeWith(ValueOrNullDeserializationStrategy.class)
@SerializeWith(RepresentationSerializationStrategy.class)
public void createNode(Invocation invocation, Output result) throws PropertyValueException, URISyntaxException {
- Map<String, Object> payload = invocation.getDeserializedContent();
+ Map<String, Object> payload = invocation.getContent();
if (payload==null) payload = Collections.emptyMap();
final NodeRepresentation node = actionsFor(invocation).createNode(payload);
final String location = createOutputFormat(invocation).format(node.selfUri());
@@ -93,7 +93,7 @@ public void getAllNodeProperties(Invocation invocation, Output result) throws Pr
@Path(PATH_NODE_PROPERTY)
@DeserializeWith(ValueDeserializationStrategy.class)
public void setNodeProperty(Invocation invocation, Output result) throws PropertyValueException, URISyntaxException, NodeNotFoundException, OperationFailureException {
- actionsFor(invocation).setNodeProperty(getNodeId(invocation), getKey(invocation), invocation.getDeserializedContent());
+ actionsFor(invocation).setNodeProperty(getNodeId(invocation), getKey(invocation), invocation.getContent());
result.okNoContent();
}
View
4 src/main/java/org/neo4j/smack/api/RelationshipService.java
@@ -57,7 +57,7 @@ public void createRelationship(Invocation invocation, Output result)
NoSuchPropertyException, StartNodeNotFoundException,
EndNodeNotFoundException
{
- final Map<String, Object> data = invocation.getDeserializedContent();
+ final Map<String, Object> data = invocation.getContent();
final long endNodeId = extractId(data.get("to"));
final String type = (String) data.get("type");
final Map<String, Object> properties = (Map<String, Object>) data
@@ -161,7 +161,7 @@ public void setRelationshipProperty(Invocation invocation, Output result)
{
actionsFor(invocation).setRelationshipProperty(
getRelationshipId(invocation), getKey(invocation),
- invocation.getDeserializedContent());
+ invocation.getContent());
result.ok();
}
View
2 src/main/java/org/neo4j/smack/api/TransactionService.java
@@ -62,7 +62,7 @@ public void setTransactionState(Invocation req, Output res) throws Exception {
TransactionRegistry database = req.getTxRegistry();
Long txId = req.getTxId();
- actions.setTransactionState(database, txId, (TransactionState)req.getDeserializedContent());
+ actions.setTransactionState(database, txId, (TransactionState)req.getContent());
res.ok();
}
View
11 src/main/java/org/neo4j/smack/event/DatabaseWork.java
@@ -48,8 +48,12 @@ public void perform() throws Exception
{
try
{
- endpoint.invoke(invocation, output);
- } catch (Exception e)
+ if(!hasFailed()) {
+ endpoint.invoke(invocation, output);
+ } else {
+ throw failure;
+ }
+ } catch (Throwable e)
{
if (output.started())
{
@@ -60,6 +64,7 @@ public void perform() throws Exception
} else
{
exceptionOutputWriter.write(output, e);
+ throw new RuntimeException("Work failed, was able to send error reply to client.", e);
}
}
}
@@ -76,7 +81,7 @@ public Throwable getFailureCause() {
@Override
public boolean hasFailed() {
- return failure == null;
+ return failure != null;
}
public WorkTransactionMode getTransactionMode()
View
2 src/main/java/org/neo4j/smack/event/DefaultInvocationImpl.java
@@ -40,7 +40,7 @@ public PathVariables getPathVariables() {
@Override
@SuppressWarnings("unchecked")
- public <T> T getDeserializedContent() {
+ public <T> T getContent() {
return (T)content;
}
View
2 src/main/java/org/neo4j/smack/event/Invocation.java
@@ -27,7 +27,7 @@
public PathVariables getPathVariables();
- public <T> T getDeserializedContent();
+ public <T> T getContent();
public <T> T getDeserializedContent(Class<T> type);
View
7 src/main/java/org/neo4j/smack/event/NettyChannelBackedOutput.java
@@ -20,6 +20,7 @@
package org.neo4j.smack.event;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.CREATED;
+import static org.jboss.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.OK;
import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;
@@ -96,6 +97,12 @@ public void okAt(String location, Object value)
{
send(OK, value, location);
}
+
+ @Override
+ public void notFound()
+ {
+ send(NOT_FOUND, null, null);
+ }
protected boolean started()
{
View
2 src/main/java/org/neo4j/smack/event/Output.java
@@ -18,5 +18,7 @@
public void okNoContent();
public void okAt(String location, Object value);
+
+ public void notFound();
}
View
4 src/main/java/org/neo4j/smack/handler/DatabaseWorkDivider.java
@@ -23,8 +23,8 @@
import org.neo4j.smack.Database;
import org.neo4j.smack.DatabaseWorkerThread;
import org.neo4j.smack.TransactionRegistry;
-import org.neo4j.smack.event.WorkTransactionMode;
import org.neo4j.smack.event.RequestEvent;
+import org.neo4j.smack.event.WorkTransactionMode;
import com.lmax.disruptor.ExceptionHandler;
import com.lmax.disruptor.WorkHandler;
@@ -69,7 +69,7 @@ public void onEvent(RequestEvent event) {
// If this seemed like an open transaction, since client
// did not provide a tx id, it is a single transaction.
- if(txMode != WorkTransactionMode.OPEN_TRANSACTION) {
+ if(txMode == WorkTransactionMode.OPEN_TRANSACTION) {
txMode = WorkTransactionMode.SINGLE_TRANSACTION;
}
}
View
47 src/main/java/org/neo4j/smack/handler/DatabaseWorkPerformer.java
@@ -16,12 +16,6 @@
private long currentTxId = -1l;
- private enum ResultState {
- SUCCESS,
- FAILED_BEFORE_WRITE,
- FAILED_AFTER_WRITE;
- }
-
public DatabaseWorkPerformer(Database database, TransactionRegistry txs) {
this.database = database;
this.txs = txs;
@@ -34,30 +28,51 @@ public void onEvent(DatabaseWork work) throws Exception
switch(work.getTransactionMode())
{
case OPEN_TRANSACTION:
- if(work.getInvocation().getTxId() != currentTxId)
- {
- txs.suspendCurrentTransaction();
- txs.associateWithCurrentThread(work.getInvocation().getTxId());
- currentTxId = work.getInvocation().getTxId();
+ try {
+ if(work.getInvocation().getTxId() != currentTxId)
+ {
+ txs.suspendCurrentTransaction();
+ txs.associateWithCurrentThread(work.getInvocation().getTxId());
+ currentTxId = work.getInvocation().getTxId();
+ }
+ } catch(Throwable e) {
+ work.setFailed(e);
}
work.perform();
break;
case SINGLE_TRANSACTION:
- txs.suspendCurrentTransaction();
- Transaction tx = database.graph.beginTx();
+ Transaction tx = null;
+ try
+ {
+ txs.suspendCurrentTransaction();
+ tx = database.graph.beginTx();
+ } catch(Throwable e)
+ {
+ work.setFailed(e);
+ }
try
{
work.perform();
- tx.success();
+ if(tx != null)
+ {
+ tx.success();
+ }
} finally
{
- tx.finish();
+ if(tx != null)
+ {
+ tx.finish();
+ }
}
break;
case NO_TRANSACTION:
- txs.suspendCurrentTransaction();
+ try {
+ txs.suspendCurrentTransaction();
+ } catch(Throwable e) {
+ work.setFailed(e);
+ }
work.perform();
break;
}
View
2 src/main/java/org/neo4j/smack/handler/ExceptionOutputWriter.java
@@ -55,7 +55,7 @@ public ExceptionOutputWriter() {
exceptionSerializationStrategy = new ExceptionSerializationStrategy();
}
- public void write(NettyChannelBackedOutput output, Exception e)
+ public void write(NettyChannelBackedOutput output, Throwable e)
{
e.printStackTrace();
output.send(getErrorStatus(e), e, null, exceptionSerializationStrategy);
View
4 src/main/java/org/neo4j/smack/routing/AnnotationBasedRoutingDefinition.java
@@ -32,7 +32,7 @@
import org.neo4j.smack.annotation.SerializeWith;
import org.neo4j.smack.annotation.Transactional;
import org.neo4j.smack.event.Invocation;
-import org.neo4j.smack.event.NettyChannelBackedOutput;
+import org.neo4j.smack.event.Output;
import org.neo4j.smack.serialization.DeserializationStrategy;
import org.neo4j.smack.serialization.SerializationStrategy;
@@ -62,7 +62,7 @@ public MethodInvokingEndpoint(InvocationVerb verb, Method method,
this.deserializationStrategy = deserializationStrategy;
}
- public void invoke(Invocation request, NettyChannelBackedOutput result) throws Exception
+ public void invoke(Invocation request, Output result) throws Exception
{
method.invoke(underlyingObject, request, result);
}
View
4 src/main/java/org/neo4j/smack/routing/Endpoint.java
@@ -20,14 +20,14 @@
package org.neo4j.smack.routing;
import org.neo4j.smack.event.Invocation;
-import org.neo4j.smack.event.NettyChannelBackedOutput;
+import org.neo4j.smack.event.Output;
import org.neo4j.smack.serialization.DeserializationStrategy;
import org.neo4j.smack.serialization.SerializationStrategy;
public interface Endpoint {
// TODO java7 use method-handle
- public void invoke(Invocation invocation, NettyChannelBackedOutput result) throws Exception;
+ public void invoke(Invocation invocation, Output result) throws Exception;
public InvocationVerb getVerb();
public DeserializationStrategy<?> getDeserializationStrategy();
public SerializationStrategy<?> getSerializationStrategy();
View
41 src/main/java/org/neo4j/smack/routing/NotFoundEndpoint.java
@@ -0,0 +1,41 @@
+package org.neo4j.smack.routing;
+
+import org.neo4j.smack.event.Invocation;
+import org.neo4j.smack.event.Output;
+import org.neo4j.smack.serialization.DeserializationStrategy;
+import org.neo4j.smack.serialization.SerializationStrategy;
+
+public class NotFoundEndpoint implements Endpoint {
+
+ @Override
+ public void invoke(Invocation invocation, Output result)
+ throws Exception
+ {
+ result.notFound();
+ }
+
+ @Override
+ public InvocationVerb getVerb()
+ {
+ return InvocationVerb.GET;
+ }
+
+ @Override
+ public DeserializationStrategy<?> getDeserializationStrategy()
+ {
+ return DeserializationStrategy.NO_OP;
+ }
+
+ @Override
+ public SerializationStrategy<?> getSerializationStrategy()
+ {
+ return SerializationStrategy.NO_OP;
+ }
+
+ @Override
+ public boolean isTransactional()
+ {
+ return false;
+ }
+
+}
View
1 src/main/java/org/neo4j/smack/routing/RouteEntry.java
@@ -33,7 +33,6 @@
protected Endpoint headEndpoint;
public void setEndpoint(InvocationVerb verb, Endpoint endpoint) {
- System.out.println(this);
switch(verb) {
case GET:
getEndpoint = endpoint;
View
11 src/main/java/org/neo4j/smack/routing/Router.java
@@ -33,6 +33,7 @@
private RouteEntry [] routes;
private static final Logger logger=Logger.getLogger(Router.class);
+ private static Endpoint notFoundEndpoint = new NotFoundEndpoint();
public Endpoint route(RequestEvent event)
{
@@ -48,10 +49,10 @@ public Endpoint route(RequestEvent event)
event.getPathVariables().add(matchResult, route.pattern); // todo is this the best way ?
return endpoint;
}
- throw new ResourceNotFoundException("Path '" + path + "' does not support '"+event.getVerb()+"'." );
+ return notFoundEndpoint;
}
}
- throw new ResourceNotFoundException("Path '" + path + "' not found." );
+ return notFoundEndpoint;
}
public void compileRoutes() {
@@ -69,13 +70,17 @@ public void compileRoutes() {
route.setEndpoint(definition.getEndpoint().getVerb(), definition.getEndpoint());
// todo what happens if multiple paths have differnt verbs?
}
+
+ for(RouteEntry route : routeMap.values())
+ System.out.println(route);
+
routes = routeMap.values().toArray(new RouteEntry[routeMap.size()]);
}
private RouteEntry createRoute(RouteDefinitionEntry definition) {
RouteEntry route = new RouteEntry();
final PathTemplate template = new PathTemplate(definition.getPath());
- route.pattern = new PathPattern(template,"/");
+ route.pattern = new PathPattern(template, definition.getPath().equals("") ? "/" : "");
return route;
}
}
View
33 src/test/java/org/neo4j/smack/REST.java
@@ -1,17 +1,9 @@
package org.neo4j.smack;
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.api.client.ClientResponse;
-import com.sun.jersey.api.client.WebResource;
-import org.codehaus.jackson.JsonFactory;
-import org.codehaus.jackson.map.ObjectMapper;
-import org.neo4j.graphdb.GraphDatabaseService;
-import org.neo4j.graphdb.Node;
-import org.neo4j.graphdb.PropertyContainer;
-import org.neo4j.graphdb.Relationship;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -19,9 +11,19 @@
import java.net.URI;
import java.util.Map;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.codehaus.jackson.JsonFactory;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.graphdb.Node;
+import org.neo4j.graphdb.PropertyContainer;
+import org.neo4j.graphdb.Relationship;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
/**
* @author mh
@@ -148,8 +150,7 @@ public REST ok() {
return assertStatus(Response.Status.OK);
}
- public REST location(String location) {
- final String expected = mergeUri(URI, location);
+ public REST location(String expected) {
final String value = this.location.toString();
assertTrue("Location " + expected + " matches " + value, value.matches(expected));
return this;
View
7 src/test/java/org/neo4j/smack/RestServiceTest.java
@@ -48,16 +48,17 @@ public static void tearDown() throws Exception {
@Test
public void testGetDbInfo() throws Exception {
- REST.from("").get().ok().expectUri("reference_node", "node/0");
+ REST.from("/").get().ok().expectUri("reference_node", "node/0");
}
@Test
public void testCreateNodeWithProperties() throws Exception {
- REST.to("node").post(map("name","John","age",10)).created().location("node/\\d+").expect("data/name", "John").expect("data.age", 10).compareNodeProperties("age", "name");
+ REST.to("node").post(map("name","John","age",10)).created().location("/node/\\d+").expect("data/name", "John").expect("data.age", 10).compareNodeProperties("age", "name");
}
+
@Test
public void testCreateNode() throws Exception {
- REST.to("node").post().created().location("node/\\d+").expect("data", map()).compareNodeProperties("!name");
+ REST.to("node").post().created().location("/node/\\d+").expect("data", map()).compareNodeProperties("!name");
}
@Test
View
42 src/test/java/org/neo4j/smack/routing/TestRouter.java
@@ -1,11 +1,13 @@
package org.neo4j.smack.routing;
+import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.neo4j.smack.event.Invocation;
+import org.neo4j.smack.event.Output;
import org.neo4j.smack.event.RequestEvent;
-import org.neo4j.smack.event.NettyChannelBackedOutput;
import org.neo4j.smack.serialization.DeserializationStrategy;
import org.neo4j.smack.serialization.SerializationStrategy;
@@ -16,7 +18,7 @@ public void shouldRouteVerbsCorrectly() {
Endpoint e = new Endpoint() {
@Override
- public void invoke(Invocation ctx, NettyChannelBackedOutput result) throws Exception { }
+ public void invoke(Invocation ctx, Output result) throws Exception { }
@Override
public InvocationVerb getVerb() {
@@ -48,17 +50,12 @@ public boolean isTransactional() {
Endpoint found = r.route(req);
assertNotNull(found);
- ResourceNotFoundException ex = null;
- try {
- req = new RequestEvent();
- req.setVerb(InvocationVerb.POST);
- req.setPath("/db/data");
-
- r.route(req);
- } catch(ResourceNotFoundException rx) {
- ex = rx;
- }
- assertNotNull(ex);
+ req = new RequestEvent();
+ req.setVerb(InvocationVerb.POST);
+ req.setPath("/db/data");
+
+ Endpoint endpoint = r.route(req);
+ assertThat(endpoint, instanceOf(NotFoundEndpoint.class));
}
@Test
@@ -67,7 +64,7 @@ public void shouldRouteSimplePathsCorrectly() {
@Override
public void invoke(Invocation ctx,
- NettyChannelBackedOutput response) throws Exception { }
+ Output response) throws Exception { }
@Override
public InvocationVerb getVerb() {
@@ -98,18 +95,13 @@ public boolean isTransactional() {
Endpoint found = r.route(req);
assertNotNull(found);
+
+ req = new RequestEvent();
+ req.setVerb(InvocationVerb.GET);
+ req.setPath("/db/da");
- ResourceNotFoundException ex = null;
- try {
- req = new RequestEvent();
- req.setVerb(InvocationVerb.GET);
- req.setPath("/db/da");
-
- r.route(req);
- } catch(ResourceNotFoundException rx) {
- ex = rx;
- }
- assertNotNull(ex);
+ Endpoint endpoint = r.route(req);
+ assertThat(endpoint, instanceOf(NotFoundEndpoint.class));
}
}

0 comments on commit b47b394

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