@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 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 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 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 assertResponseHaveCorrectContentFromGet() throws Exception { JaxRsResponse response = RestRequest.req().get(functionalTestHelper.dataUri()); String body = response.getEntity(String.class); Map<String, Object> map = JsonHelper.jsonToMap(body); assertEquals(functionalTestHelper.nodeUri(), map.get("node")); assertNotNull(map.get("reference_node")); assertNotNull(map.get("node_index")); assertNotNull(map.get("relationship_index")); assertNotNull(map.get("extensions_info")); assertNotNull(map.get("batch")); response.close(); // Make sure advertised urls work response = RestRequest.req().get((String) map.get("reference_node")); assertEquals(200, response.getStatus()); response.close(); response = RestRequest.req().get((String) map.get("node_index")); assertEquals(204, response.getStatus()); response.close(); response = RestRequest.req().get((String) map.get("relationship_index")); assertEquals(204, response.getStatus()); response.close(); response = RestRequest.req().get((String) map.get("extensions_info")); assertEquals(200, response.getStatus()); response.close(); response = RestRequest.req().post((String) map.get("batch"), "[]"); assertEquals(200, response.getStatus()); response.close(); }
@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 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 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 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 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(); }
@Test public void shouldGet200AndArrayOfNodeRepsWhenGettingFromIndex() throws PropertyValueException { String key = "myKey"; String value = "myValue"; String name1 = "Thomas Anderson"; String name2 = "Agent Smith"; String indexName = "matrix"; final RestRequest request = RestRequest.req(); JaxRsResponse responseToPost = request.post(functionalTestHelper.nodeUri(), "{\"name\":\"" + name1 + "\"}"); assertEquals(201, responseToPost.getStatus()); String location1 = responseToPost.getHeaders().getFirst(HttpHeaders.LOCATION); responseToPost.close(); responseToPost = request.post(functionalTestHelper.nodeUri(), "{\"name\":\"" + name2 + "\"}"); assertEquals(201, responseToPost.getStatus()); String location2 = responseToPost.getHeaders().getFirst(HttpHeaders.LOCATION); responseToPost.close(); responseToPost = request.post( functionalTestHelper.indexNodeUri(indexName), createJsonStringFor(functionalTestHelper.getNodeIdFromUri(location1), key, value)); assertEquals(201, responseToPost.getStatus()); String indexLocation1 = responseToPost.getHeaders().getFirst(HttpHeaders.LOCATION); responseToPost.close(); responseToPost = request.post( functionalTestHelper.indexNodeUri(indexName), createJsonStringFor(functionalTestHelper.getNodeIdFromUri(location2), key, value)); assertEquals(201, responseToPost.getStatus()); String indexLocation2 = responseToPost.getHeaders().getFirst(HttpHeaders.LOCATION); Map<String, String> uriToName = new HashMap<>(); uriToName.put(indexLocation1, name1); uriToName.put(indexLocation2, name2); responseToPost.close(); JaxRsResponse response = RestRequest.req().get(functionalTestHelper.indexNodeUri(indexName, key, value)); assertEquals(200, response.getStatus()); Collection<?> items = (Collection<?>) JsonHelper.jsonToSingleValue(response.getEntity()); int counter = 0; for (Object item : items) { Map<?, ?> map = (Map<?, ?>) item; Map<?, ?> properties = (Map<?, ?>) map.get("data"); assertNotNull(map.get("self")); String indexedUri = (String) map.get("indexed"); assertEquals(uriToName.get(indexedUri), properties.get("name")); counter++; } assertEquals(2, counter); response.close(); }
@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 shouldGetSomeRelationships() { final RestRequest request = RestRequest.req(); JaxRsResponse response = request.get( functionalTestHelper.relationshipsUri( thomasAnderson, RelationshipDirection.all.name(), "KNOWS"), MediaType.TEXT_HTML_TYPE); assertEquals(Status.OK.getStatusCode(), response.getStatus()); String entity = response.getEntity(); assertTrue(entity.contains("KNOWS")); assertFalse(entity.contains("LOVES")); assertValidHtml(entity); response.close(); response = request.get( functionalTestHelper.relationshipsUri( thomasAnderson, RelationshipDirection.all.name(), "LOVES"), MediaType.TEXT_HTML_TYPE); entity = response.getEntity(); assertFalse(entity.contains("KNOWS")); assertTrue(entity.contains("LOVES")); assertValidHtml(entity); response.close(); response = request.get( functionalTestHelper.relationshipsUri( thomasAnderson, RelationshipDirection.all.name(), "LOVES", "KNOWS"), MediaType.TEXT_HTML_TYPE); entity = response.getEntity(); assertTrue(entity.contains("KNOWS")); assertTrue(entity.contains("LOVES")); assertValidHtml(entity); response.close(); }
@Test public void shouldHaveJsonDataInResponse() throws Exception { JaxRsResponse response = getDiscoveryDocument(); Map<String, Object> map = JsonHelper.jsonToMap(response.getEntity()); String managementKey = "management"; assertTrue(map.containsKey(managementKey)); assertNotNull(map.get(managementKey)); String dataKey = "data"; assertTrue(map.containsKey(dataKey)); assertNotNull(map.get(dataKey)); response.close(); }
@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(); }
/** * Add node to index. * * <p>Associates a node with the given key/value pair in the given index. * * <p>NOTE: Spaces in the URI have to be encoded as +%20+. * * <p>CAUTION: This does *not* overwrite previous entries. If you index the same key/value/item * combination twice, two index entries are created. To do update-type operations, you need to * delete the old entry before adding a new one. */ @Documented @Test public void shouldAddToIndex() throws Exception { final String indexName = "favorites"; final String key = "some-key"; final String value = "some value"; long nodeId = createNode(); // implicitly create the index gen() .noGraph() .expectedStatus(201) .payload( JsonHelper.createJsonFrom( generateNodeIndexCreationPayload(key, value, functionalTestHelper.nodeUri(nodeId)))) .post(functionalTestHelper.indexNodeUri(indexName)); // look if we get one entry back JaxRsResponse response = RestRequest.req() .get(functionalTestHelper.indexNodeUri(indexName, key, URIHelper.encode(value))); String entity = response.getEntity(); Collection<?> hits = (Collection<?>) JsonHelper.jsonToSingleValue(entity); assertEquals(1, hits.size()); }
private String entityString(JaxRsResponse JaxRsResponse) { return JaxRsResponse.getEntity(); }