/** * Find node by exact match. * * <p>NOTE: Spaces in the URI have to be encoded as +%20+. */ @Documented @Test public void shouldAddToIndexAndRetrieveItByExactMatch() throws Exception { String indexName = "favorites"; String key = "key"; String value = "the value"; long nodeId = createNode(); value = URIHelper.encode(value); // implicitly create the index JaxRsResponse response = RestRequest.req() .post( functionalTestHelper.indexNodeUri(indexName), createJsonStringFor(nodeId, key, value)); assertEquals(201, response.getStatus()); // search it exact String entity = gen() .noGraph() .expectedStatus(200) .get(functionalTestHelper.indexNodeUri(indexName, key, URIHelper.encode(value))) .entity(); Collection<?> hits = (Collection<?>) JsonHelper.jsonToSingleValue(entity); assertEquals(1, hits.size()); }
@Test public void shouldReturn404WhenPropertySentToANodeWhichDoesNotExist() throws Exception { JaxRsResponse response = RestRequest.req().put(badUri.toString() + "/foo", JsonHelper.createJsonFrom("bar")); assertEquals(404, response.getStatus()); response.close(); }
@Test public void shouldBeValidJSONOnResponse() throws JsonParseException { JaxRsResponse response = RestRequest.req().get(getPropertyUri("foo")); assertThat(response.getType().toString(), containsString(MediaType.APPLICATION_JSON)); assertNotNull(JsonHelper.createJsonFrom(response.getEntity())); response.close(); }
@Test public void shouldGetLocationHeadersWhenCreatingThings() throws Exception { int originalNodeCount = countNodes(); JaxRsResponse response = RestRequest.req() .post( batchUri(), new PrettyJSON() .array() .object() .key("method") .value("POST") .key("to") .value("/node") .key("body") .object() .key("age") .value(1) .endObject() .endObject() .endArray() .toString()); assertEquals(200, response.getStatus()); assertEquals(originalNodeCount + 1, countNodes()); List<Map<String, Object>> results = JsonHelper.jsonToList(response.getEntity()); assertEquals(1, results.size()); Map<String, Object> result = results.get(0); assertTrue(((String) result.get("location")).length() > 0); }
@Test public void shouldGet404ForNonExistingRelationship() { String uri = functionalTestHelper.dataUri() + "relationship/999999/properties/foo"; JaxRsResponse response = RestRequest.req().get(uri); assertEquals(404, response.getStatus()); response.close(); }
@Test public void shouldGet404ForPropertiesOnNonExistentRelationship() { JaxRsResponse response = RestRequest.req().get(functionalTestHelper.dataUri() + "relationship/999999/properties"); assertEquals(404, response.getStatus()); response.close(); }
@Test public void shouldRollbackAllWhenInsertingIllegalData() throws JsonParseException, ClientHandlerException, UniformInterfaceException { String jsonString = "[" + "{ " + "\"method\":\"POST\"," + "\"to\":\"/node\", " + "\"body\":{ \"age\":1 }" + "}," + "{ " + "\"method\":\"POST\"," + "\"to\":\"/node\", " + "\"body\":{ \"age\":{ \"age\":{ \"age\":1 } } }" + "}" + "]"; int originalNodeCount = countNodes(); JaxRsResponse response = RestRequest.req().post(batchUri(), jsonString); assertEquals(500, response.getStatus()); assertEquals(originalNodeCount, countNodes()); }
@Test public void shouldForwardUnderlyingErrors() throws Exception { JaxRsResponse response = RestRequest.req() .post( batchUri(), new PrettyJSON() .array() .object() .key("method") .value("POST") .key("to") .value("/node") .key("body") .object() .key("age") .array() .value(true) .value("hello") .endArray() .endObject() .endObject() .endArray() .toString()); assertEquals(500, response.getStatus()); Map<String, Object> res = JsonHelper.jsonToMap(response.getEntity()); assertTrue(((String) res.get("message")).startsWith("Invalid JSON array in POST body")); }
@Test public void shouldGet404WhenDeletingNonExtistentIndex() { String indexName = "nosuchindex"; String indexUri = functionalTestHelper.nodeIndexUri() + indexName; JaxRsResponse response = RestRequest.req().delete(indexUri); assertEquals(Status.NOT_FOUND.getStatusCode(), response.getStatus()); }
@Test public void shouldGetNodeRepresentationFromIndexUri() throws JsonParseException { long nodeId = helper.createNode(); String key = "key2"; String value = "value"; String indexName = "mindex"; helper.createNodeIndex(indexName); JaxRsResponse response = RestRequest.req() .post( functionalTestHelper.indexNodeUri(indexName), createJsonStringFor(nodeId, key, value)); assertEquals(Status.CREATED.getStatusCode(), response.getStatus()); String indexUri = response.getHeaders().getFirst("Location"); response = RestRequest.req().get(indexUri); assertEquals(200, response.getStatus()); String entity = response.getEntity(); Map<String, Object> map = JsonHelper.jsonToMap(entity); assertNotNull(map.get("self")); }
@Test public void shouldGetNodeIndexRoot() { JaxRsResponse response = RestRequest.req().get(functionalTestHelper.nodeIndexUri(), MediaType.TEXT_HTML_TYPE); assertEquals(Status.OK.getStatusCode(), response.getStatus()); assertValidHtml(response.getEntity()); response.close(); }
@Test public void shouldHaveJsonDataInResponse() throws Exception { JaxRsResponse response = retrieveNodeFromService(nodeUri.toString()); Map<String, Object> map = JsonHelper.jsonToMap(response.getEntity()); assertTrue(map.containsKey("self")); response.close(); }
@Test public void shouldReturn400WhenSendinIncompatibleJsonProperties() throws JsonParseException { Map<String, Object> map = new HashMap<String, Object>(); map.put("jim", new HashMap<String, Object>()); JaxRsResponse response = updateNodePropertiesOnServer(map); assertEquals(400, response.getStatus()); response.close(); }
@Test public void shouldReturn204WhenRelationshipPropertyIsRemoved() throws DatabaseBlockedException { long relationshipId = helper.createRelationship("LOVES"); Map<String, Object> map = new HashMap<String, Object>(); map.put("jim", "tobias"); helper.setRelationshipProperties(relationshipId, map); JaxRsResponse response = removeRelationshipPropertyOnServer(relationshipId, "jim"); assertEquals(204, response.getStatus()); }
@Test public void shouldGet404WhenRequestingIndexUriWhichDoesntExist() { String key = "key3"; String value = "value"; String indexName = "nosuchindex"; String indexUri = functionalTestHelper.nodeIndexUri() + indexName + "/" + key + "/" + value; JaxRsResponse response = RestRequest.req().get(indexUri); assertEquals(Status.NOT_FOUND.getStatusCode(), response.getStatus()); }
@Test public void shouldReturn404WhenPropertiesSentToANodeWhichDoesNotExist() throws JsonParseException { Map<String, Object> map = new HashMap<String, Object>(); map.put("jim", "tobias"); JaxRsResponse response = RestRequest.req().put(badUri, JsonHelper.createJsonFrom(map)); assertEquals(404, response.getStatus()); response.close(); }
@Test public void shouldRespondWith200AndEmptyListOfRelationshipRepresentationsWhenGettingOutgoingRelationshipsForANodeWithoutRelationships() throws JsonParseException { JaxRsResponse response = sendRetrieveRequestToServer(nodeWithoutRelationships, "/out"); assertEquals(200, response.getStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); verifyRelReps(0, response.getEntity(String.class)); response.close(); }
@Test public void shouldGet200WhenRetrievingValidRelationship() throws DatabaseBlockedException { long relationshipId = helper.createRelationship("LIKES"); JaxRsResponse response = RestRequest.req().get(functionalTestHelper.relationshipUri(relationshipId)); assertEquals(200, response.getStatus()); response.close(); }
@Test public void shouldBeJSONContentTypeOnPropertiesResponse() { long relId = helper.createRelationship("LIKES"); helper.setRelationshipProperties(relId, Collections.<String, Object>singletonMap("foo", "bar")); JaxRsResponse response = RestRequest.req() .get(functionalTestHelper.dataUri() + "relationship/" + relId + "/properties"); assertThat(response.getType().toString(), containsString(MediaType.APPLICATION_JSON)); response.close(); }
@Test public void shouldRespondWith400WhenSendingCorruptJson() throws Exception { final String indexName = "botherable-index"; helper.createNodeIndex(indexName); final String corruptJson = "{\"key\" \"myKey\"}"; JaxRsResponse response = RestRequest.req().post(functionalTestHelper.indexNodeUri(indexName), corruptJson); assertEquals(400, response.getStatus()); response.close(); }
@Test public void shouldRedirectToWebadminOnHtmlRequest() throws Exception { Client nonRedirectingClient = Client.create(); nonRedirectingClient.setFollowRedirects(false); JaxRsResponse clientResponse = new RestRequest(null, nonRedirectingClient) .get(server().baseUri().toString(), MediaType.TEXT_HTML_TYPE); assertEquals(303, clientResponse.getStatus()); }
@Test public void shouldGet200AndContentLengthForProperties() { long relId = helper.createRelationship("LIKES"); helper.setRelationshipProperties(relId, Collections.<String, Object>singletonMap("foo", "bar")); JaxRsResponse response = RestRequest.req() .get(functionalTestHelper.dataUri() + "relationship/" + relId + "/properties"); assertEquals(200, response.getStatus()); assertNotNull(response.getHeaders().get("Content-Length")); response.close(); }
@Test public void shouldGetThomasAndersonDirectly() { JaxRsResponse response = RestRequest.req() .get(functionalTestHelper.nodeUri(thomasAnderson), MediaType.TEXT_HTML_TYPE); assertEquals(Status.OK.getStatusCode(), response.getStatus()); String entity = response.getEntity(); assertTrue(entity.contains("Thomas Anderson")); assertValidHtml(entity); response.close(); }
/** * Property values can not be nested. * * <p>Nesting properties is not supported. You could for example store the nested json as a string * instead. */ @Documented @Test public void shouldReturn400WhenSendinIncompatibleJsonProperty() throws Exception { gen.get() .payload("{\"foo\" : {\"bar\" : \"baz\"}}") .expectedStatus(400) .post(functionalTestHelper.nodeUri()); JaxRsResponse response = setNodePropertyOnServer("jim", new HashMap<String, Object>()); assertEquals(400, response.getStatus()); response.close(); }
@Test public void shouldGet200WhenGettingNodesFromIndexWithNoHits() { String indexName = "empty-index"; helper.createNodeIndex(indexName); JaxRsResponse response = RestRequest.req() .get( functionalTestHelper.indexNodeUri( indexName, "non-existent-key", "non-existent-value")); assertEquals(200, response.getStatus()); response.close(); }
/** Update node properties. */ @Documented @Test public void shouldReturn204WhenPropertiesAreUpdated() throws JsonParseException { Map<String, Object> map = new HashMap<String, Object>(); map.put("jim", "tobias"); gen.get() .payload(JsonHelper.createJsonFrom(map)) .expectedStatus(204) .put(propertiesUri.toString()); JaxRsResponse response = updateNodePropertiesOnServer(map); assertEquals(204, response.getStatus()); }
@Test public void shouldGetTrinityWhenSearchingForHer() { JaxRsResponse response = RestRequest.req() .get( functionalTestHelper.indexNodeUri("node", "name", "Trinity"), MediaType.TEXT_HTML_TYPE); assertEquals(Status.OK.getStatusCode(), response.getStatus()); String entity = response.getEntity(); assertTrue(entity.contains("Trinity")); assertValidHtml(entity); response.close(); }
@Test public void shouldGetARelationshipRepresentationInJsonWhenRetrievingValidRelationship() throws Exception { long relationshipId = helper.createRelationship("LIKES"); JaxRsResponse response = RestRequest.req().get(functionalTestHelper.relationshipUri(relationshipId)); String entity = response.getEntity(String.class); assertNotNull(entity); isLegalJson(entity); response.close(); }
@Test public void shouldBeAbleToIndexValuesContainingSpaces() throws Exception { final long nodeId = helper.createNode(); final String key = "key"; final String value = "value with spaces in it"; String indexName = "spacey-values"; helper.createNodeIndex(indexName); final RestRequest request = RestRequest.req(); JaxRsResponse response = request.post( functionalTestHelper.indexNodeUri(indexName), createJsonStringFor(nodeId, key, value)); assertEquals(Status.CREATED.getStatusCode(), response.getStatus()); URI location = response.getLocation(); response.close(); response = request.get(functionalTestHelper.indexNodeUri(indexName, key, URIHelper.encode(value))); assertEquals(Status.OK.getStatusCode(), response.getStatus()); Collection<?> hits = (Collection<?>) JsonHelper.jsonToSingleValue(response.getEntity()); assertEquals(1, hits.size()); response.close(); CLIENT.resource(location).delete(); response = request.get(functionalTestHelper.indexNodeUri(indexName, key, URIHelper.encode(value))); hits = (Collection<?>) JsonHelper.jsonToSingleValue(response.getEntity()); assertEquals(0, hits.size()); }
@Test public void shouldGetThomasAndersonLovesTrinityRelationship() { JaxRsResponse response = RestRequest.req() .get( functionalTestHelper.relationshipUri(thomasAndersonLovesTrinity), MediaType.TEXT_HTML_TYPE); assertEquals(Status.OK.getStatusCode(), response.getStatus()); String entity = response.getEntity(); assertTrue(entity.contains("strength")); assertTrue(entity.contains("100")); assertTrue(entity.contains("LOVES")); assertValidHtml(entity); response.close(); }