Permalink
Browse files

Minor test fixes, still quite a bit of red.

  • Loading branch information...
jakewins committed Apr 24, 2012
1 parent 4547b59 commit 0ede25f1315987eab249469fb4f47e033cd2a136
@@ -32,7 +32,7 @@
public class REST {
private final static JsonFactory jsonFactory = new JsonFactory(new ObjectMapper());
static Client client = Client.create();
- static String URI = RestServiceTest.BASE_URI;
+ static String URI;
private WebResource.Builder resource;
private int status;
private URI location;
@@ -92,7 +92,7 @@ private REST handle(ClientResponse response) {
}
private void readEntityIfPossible(ClientResponse response) {
- if (status != Response.Status.NO_CONTENT.getStatusCode()) {
+ if (status != Response.Status.NO_CONTENT.getStatusCode() && response.getLength() > 0) {
this.entity = readEntity(response, Object.class);
}
}
@@ -172,13 +172,17 @@ public REST expectUri(String path, Object value) {
}
private Object getPath(String path) {
- final String[] parts = path.split("[./]");
- Map map = (Map) entity;
- for (int i = 0; i < parts.length - 1; i++) {
- String part = parts[i];
- map = (Map) map.get(part);
+ try {
+ final String[] parts = path.split("[./]");
+ Map map = (Map) entity;
+ for (int i = 0; i < parts.length - 1; i++) {
+ String part = parts[i];
+ map = (Map) map.get(part);
+ }
+ return map.get(parts[parts.length - 1]);
+ } catch(NullPointerException e) {
+ throw new RuntimeException("Expected response to the following path of keys: " + path);
}
- return map.get(parts[parts.length - 1]);
}
public REST assertStatus(Response.Status expectedStatus) {
@@ -1,175 +0,0 @@
-package org.neo4j.smack;
-
-import static org.junit.Assert.assertEquals;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.util.Map;
-
-import javax.ws.rs.core.MediaType;
-
-import org.codehaus.jackson.JsonFactory;
-import org.codehaus.jackson.map.ObjectMapper;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.neo4j.graphdb.Node;
-import org.neo4j.graphdb.Transaction;
-import org.neo4j.kernel.AbstractGraphDatabase;
-import org.neo4j.smack.api.DatabaseService;
-import org.neo4j.test.ImpermanentGraphDatabase;
-
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.api.client.ClientResponse;
-import com.sun.jersey.api.client.WebResource;
-
-/**
- * @author mh
- * @since 14.11.11
- */
-public class SmackServerTest {
- public static final String HOST = "localhost";
- public static final int PORT = 7473;
- public static final String BASE_URI = "http://" + HOST + ":" + PORT + "/";
- public static final int ROOT_NODE_ID = 0;
- static SmackServer server;
- private static AbstractGraphDatabase gds;
- private final static JsonFactory jsonFactory = new JsonFactory(new ObjectMapper());
-
- @BeforeClass
- public static void setUp() throws Exception {
- gds = new ImpermanentGraphDatabase();
- final Transaction tx = gds.beginTx();
- gds.getReferenceNode().setProperty("name", "test");
- tx.success();
- tx.finish();
- server = new SmackServer(HOST, PORT, new Database(gds));
- server.addRoute("", new DatabaseService(""));
- server.start();
- }
-
- @AfterClass
- public static void tearDown() throws Exception {
- server.stop();
- gds.shutdown();
- }
-
- @Test
- public void testGetInfo() throws Exception {
- final String uri = BASE_URI + "info";
- WebResource resource = Client.create().resource(uri);
- ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
- .type(MediaType.APPLICATION_JSON)
- .get(ClientResponse.class);
-
- final int status = response.getStatus();
- System.out.printf("GET to [%s], status code [%d], returned data: %n%s",
- uri, status, response.getEntity(String.class));
- response.close();
- assertEquals("info returned 200", 200, status);
- }
-
- @Test
- public void testGetWrongUriReturnsError() throws Exception {
- final String uri = BASE_URI + "wrong_uri";
- WebResource resource = Client.create().resource(uri);
- ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
- .type(MediaType.APPLICATION_JSON)
- .get(ClientResponse.class);
-
- final int status = response.getStatus();
- System.out.printf("GET to [%s], status code [%d], returned data: %n%s",
- uri, status, response.getEntity(String.class));
- response.close();
- assertEquals("wrong uri returned 404", 404, status);
- }
-
- @Test
- public void testWrongMethodReturnsError() throws Exception {
- final String uri = BASE_URI + "wrong_uri";
- WebResource resource = Client.create().resource(uri);
- ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
- .type(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class);
-
- final int status = response.getStatus();
- System.out.printf("POST to [%s], status code [%d], returned data: %n%s",
- uri, status, response.getEntity(String.class));
- response.close();
- assertEquals("wrong method returned 404", 404, status);
- }
-
- @Test
- public void testCreateNode() throws Exception {
- final String uri = BASE_URI + "node";
- WebResource resource = Client.create().resource(uri);
- String data = "{\"name\":\"john\",\"age\":10}";
- ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
- .type(MediaType.APPLICATION_JSON)
- .entity(data)
- .post(ClientResponse.class);
-
- final int status = response.getStatus();
- final URI location = response.getLocation();
- final String entity = response.getEntity(String.class);
- System.out.printf("POST [%s] to [%s], status code [%d] location: %s, returned data: %n%s",
- data, uri, status, location, entity);
- response.close();
- assertEquals("info returned 201", 201, status);
- assertEquals("info returned location for new node", "/node/1", location.toString());
- for (Node node : gds.getAllNodes()) {
- System.out.println("node = " + node);
- for (String prop : node.getPropertyKeys()) {
- System.out.println(prop + ": " + node.getProperty(prop));
- }
- }
- assertEquals("john", gds.getNodeById(1).getProperty("name"));
- assertEquals(10, gds.getNodeById(1).getProperty("age"));
- }
-
- @Test
- public void testWrongPayloadReturnsError() throws Exception {
- final String uri = BASE_URI + "node";
- WebResource resource = Client.create().resource(uri);
- String data = "123";
- ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
- .type(MediaType.APPLICATION_JSON)
- .entity(data)
- .post(ClientResponse.class);
-
- final int status = response.getStatus();
- System.out.printf("POST [%s] to [%s], status code [%d] location: %s, returned data: %n%s",
- data, uri, status, response.getLocation(), response.getEntity(String.class));
- response.close();
- assertEquals("info returned 500", 500, status);
- }
-
- @Test
- public void testGetNode() throws Exception {
- final String uri = BASE_URI + "node/" + ROOT_NODE_ID;
- WebResource resource = Client.create().resource(uri);
- ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
- .type(MediaType.APPLICATION_JSON)
- .get(ClientResponse.class);
-
- final int status = response.getStatus();
- final URI location = response.getLocation();
- final Map data = readEntity(response, Map.class);
- System.out.printf("GET from [%s], status code [%d] location: %s, returned data: %n%s",
- uri, status, location, data);
- response.close();
- assertEquals("info returned 201", 200, status);
- assertEquals("test", data.get("name"));
- //assertEquals("info returned location for new node", "/node/0", location.toString());
- }
-
- private <T> T parseJson(String entity, Class<T> type) throws IOException {
- return jsonFactory.createJsonParser(entity).readValueAs(type);
- }
-
- private <T> T readEntity(ClientResponse response, Class<T> type) throws IOException {
- final InputStream is = response.getEntityInputStream();
- return jsonFactory.createJsonParser(is).readValueAs(type);
- }
-}
@@ -1,54 +1,28 @@
-package org.neo4j.smack;
+package org.neo4j.smack.test.integration;
import static org.neo4j.helpers.collection.MapUtil.map;
import javax.ws.rs.core.Response;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
+import org.junit.Before;
import org.junit.Test;
-import org.neo4j.graphdb.Transaction;
-import org.neo4j.kernel.AbstractGraphDatabase;
-import org.neo4j.smack.api.DatabaseService;
-import org.neo4j.test.ImpermanentGraphDatabase;
+import org.neo4j.smack.REST;
+import org.neo4j.smack.test.util.AbstractRestFunctionalTestBase;
/**
* @author mh
* @since 14.11.11
*/
-public class RestServiceTest {
- public static final String HOST = "localhost";
- public static final int PORT = 7473;
- public static final String BASE_URI = "http://" + HOST + ":" + PORT + "/";
- public static final int ROOT_NODE_ID = 0;
+public class NodeServiceIT extends AbstractRestFunctionalTestBase {
- public static SmackServer server;
-
- private static AbstractGraphDatabase gds;
-
- @BeforeClass
- public static void setUp() throws Exception {
- gds = new ImpermanentGraphDatabase();
- final Transaction tx = gds.beginTx();
- gds.getReferenceNode().setProperty("name", "test");
- tx.success();
- tx.finish();
- server = new SmackServer(HOST, PORT, new Database(gds));
- final DatabaseService dataApi = new DatabaseService("");
- server.addRoute("", dataApi);
- server.start();
- REST.init(BASE_URI,"",gds);
- }
-
- @AfterClass
- public static void tearDown() throws Exception {
- server.stop();
- gds.shutdown();
+ @Before
+ public void setUp() throws Exception {
+ REST.init(getBaseUri(),"",graphdb());
}
@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
@@ -78,12 +52,12 @@ public void testReplaceNodeProperties() throws Exception {
@Test
public void testGetNode() throws Exception {
- final String rootNodeUri = "node/" + ROOT_NODE_ID;
+ final String rootNodeUri = "node/0";
REST.from(rootNodeUri).get().ok().expectUri("self", rootNodeUri);
}
@Test
public void testGetNonExistingNode() throws Exception {
- REST.from("node/" + 9999).get().notFound();
+ REST.from("/node/" + 9999).get().notFound();
}
}
@@ -1,5 +1,14 @@
package org.neo4j.smack.test.util;
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+
+import javax.ws.rs.core.Response.Status;
+
import org.codehaus.jackson.JsonParseException;
import org.junit.Before;
import org.junit.Rule;
@@ -12,14 +21,6 @@
import org.neo4j.test.TestData;
import org.neo4j.visualization.asciidoc.AsciidocHelper;
-import javax.ws.rs.core.Response.Status;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Map;
-
-import static org.junit.Assert.assertEquals;
-
public class AbstractRestFunctionalTestBase extends SharedSmackServerTestBase implements GraphHolder {
protected static final String NODES = "http://localhost:7473/db/data/node/";
@@ -111,15 +112,22 @@ protected String getDataUri()
{
return "http://localhost:7473/db/data/";
}
+
+ protected String getBaseUri()
+ {
+ return "http://localhost:7473";
+ }
protected String getNodeUri( Node node )
{
return getDataUri() + "node/" + node.getId();
}
+
protected String getRelationshipUri( Relationship node )
{
return getDataUri() + "relationship/" + node.getId();
}
+
protected String getNodeIndexUri( String indexName, String key, String value )
{
return postNodeIndexUri( indexName ) + "/" + key + "/" + value;

0 comments on commit 0ede25f

Please sign in to comment.