Ejemplo n.º 1
0
  @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());
  }
Ejemplo n.º 2
0
  @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"));
  }
Ejemplo n.º 3
0
  @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();
  }
Ejemplo n.º 4
0
  @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 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 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 shouldGet404ForPropertiesOnNonExistentRelationship() {
   JaxRsResponse response =
       RestRequest.req().get(functionalTestHelper.dataUri() + "relationship/999999/properties");
   assertEquals(404, response.getStatus());
   response.close();
 }
 @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 shouldReturn404WhenPropertySentToANodeWhichDoesNotExist() throws Exception {
   JaxRsResponse response =
       RestRequest.req().put(badUri.toString() + "/foo", JsonHelper.createJsonFrom("bar"));
   assertEquals(404, response.getStatus());
   response.close();
 }
Ejemplo n.º 11
0
 @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());
 }
Ejemplo n.º 12
0
  /**
   * 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());
  }
Ejemplo n.º 13
0
 @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 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();
 }
Ejemplo n.º 15
0
 @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 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
     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();
 }
Ejemplo n.º 19
0
 @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 shouldGet200WhenRetrievingValidRelationship() throws DatabaseBlockedException {
    long relationshipId = helper.createRelationship("LIKES");

    JaxRsResponse response =
        RestRequest.req().get(functionalTestHelper.relationshipUri(relationshipId));

    assertEquals(200, response.getStatus());
    response.close();
  }
Ejemplo n.º 21
0
 @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 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 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());
  }
Ejemplo n.º 25
0
 @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());
 }
Ejemplo n.º 27
0
 @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();
 }
Ejemplo n.º 28
0
 @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();
 }
Ejemplo n.º 29
0
  /**
   * POST ${org.neo4j.server.rest.web}/index/node/{indexName}/{key}/{value}
   * "http://uri.for.node.to.index"
   */
  @Test
  public void shouldRespondWith201CreatedWhenIndexingJsonNodeUri() {
    final long nodeId = helper.createNode();
    final String key = "key";
    final String value = "value";
    final String indexName = "testy";
    helper.createNodeIndex(indexName);

    JaxRsResponse response =
        RestRequest.req()
            .post(
                functionalTestHelper.indexNodeUri(indexName),
                createJsonStringFor(nodeId, key, value));
    assertEquals(201, response.getStatus());
    assertNotNull(response.getHeaders().getFirst("Location"));
    assertEquals(Arrays.asList((Long) nodeId), helper.getIndexedNodes(indexName, key, value));
  }
Ejemplo n.º 30
0
  @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();
  }