Search in sources :

Example 6 with PrettyJSON

use of org.neo4j.doc.server.rest.PrettyJSON in project neo4j-documentation by neo4j.

the class StreamingBatchOperationDocIT method execute_multiple_operations_in_batch_streaming.

/**
 * By specifying an extended header attribute in the HTTP request,
 * the server will stream the results back as soon as they are processed on the server side
 * instead of constructing a full response when all entities are processed.
 */
@SuppressWarnings("unchecked")
@Test
@Graph("Joe knows John")
public void execute_multiple_operations_in_batch_streaming() throws Exception {
    long idJoe = data.get().get("Joe").getId();
    String jsonString = new PrettyJSON().array().object().key("method").value("PUT").key("to").value("/node/" + idJoe + "/properties").key("body").object().key("age").value(1).endObject().key("id").value(0).endObject().object().key("method").value("GET").key("to").value("/node/" + idJoe).key("id").value(1).endObject().object().key("method").value("POST").key("to").value("/node").key("body").object().key("age").value(1).endObject().key("id").value(2).endObject().object().key("method").value("POST").key("to").value("/node").key("body").object().key("age").value(1).endObject().key("id").value(3).endObject().endArray().toString();
    String entity = gen.get().expectedType(APPLICATION_JSON_TYPE).withHeader(StreamingFormat.STREAM_HEADER, "true").payload(jsonString).expectedStatus(200).post(batchUri()).entity();
    List<Map<String, Object>> results = JsonHelper.jsonToList(entity);
    assertEquals(4, results.size());
    Map<String, Object> putResult = results.get(0);
    Map<String, Object> getResult = results.get(1);
    Map<String, Object> firstPostResult = results.get(2);
    Map<String, Object> secondPostResult = results.get(3);
    // Ids should be ok
    assertEquals(0, putResult.get("id"));
    assertEquals(2, firstPostResult.get("id"));
    assertEquals(3, secondPostResult.get("id"));
    // Should contain "from"
    assertEquals("/node/" + idJoe + "/properties", putResult.get("from"));
    assertEquals("/node/" + idJoe, getResult.get("from"));
    assertEquals("/node", firstPostResult.get("from"));
    assertEquals("/node", secondPostResult.get("from"));
    // Post should contain location
    assertTrue(((String) firstPostResult.get("location")).length() > 0);
    assertTrue(((String) secondPostResult.get("location")).length() > 0);
    // Should have created by the first PUT request
    Map<String, Object> body = (Map<String, Object>) getResult.get("body");
    assertEquals(1, ((Map<String, Object>) body.get("data")).get("age"));
}
Also used : PrettyJSON(org.neo4j.doc.server.rest.PrettyJSON) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) Graph(org.neo4j.test.GraphDescription.Graph) Test(org.junit.Test)

Example 7 with PrettyJSON

use of org.neo4j.doc.server.rest.PrettyJSON in project neo4j-documentation by neo4j.

the class StreamingBatchOperationDocIT method shouldRollbackAllWhenInsertingIllegalData.

@Test
public void shouldRollbackAllWhenInsertingIllegalData() throws JsonParseException, ClientHandlerException, UniformInterfaceException, JSONException {
    String jsonString = new PrettyJSON().array().object().key("method").value("POST").key("to").value("/node").key("body").object().key("age").value(1).endObject().endObject().object().key("method").value("POST").key("to").value("/node").key("body").object().key("age").object().key("age").value(1).endObject().endObject().endObject().endArray().toString();
    long originalNodeCount = countNodes();
    JaxRsResponse response = RestRequest.req().accept(APPLICATION_JSON_TYPE).header(StreamingFormat.STREAM_HEADER, "true").post(batchUri(), jsonString);
    assertEquals(200, response.getStatus());
    assertEquals(400, singleResult(response, 1).get("status"));
    assertEquals(originalNodeCount, countNodes());
}
Also used : PrettyJSON(org.neo4j.doc.server.rest.PrettyJSON) Matchers.containsString(org.hamcrest.Matchers.containsString) JaxRsResponse(org.neo4j.doc.server.rest.JaxRsResponse) Test(org.junit.Test)

Example 8 with PrettyJSON

use of org.neo4j.doc.server.rest.PrettyJSON in project neo4j-documentation by neo4j.

the class StreamingBatchOperationDocIT method refer_to_items_created_earlier_in_the_same_batch_job_streaming.

/**
 * The batch operation API allows you to refer to the URI returned from a
 * created resource in subsequent job descriptions, within the same batch
 * call.
 *
 * Use the +{[JOB ID]}+ special syntax to inject URIs from created resources
 * into JSON strings in subsequent job descriptions.
 */
@Test
public void refer_to_items_created_earlier_in_the_same_batch_job_streaming() throws Exception {
    String jsonString = new PrettyJSON().array().object().key("method").value("POST").key("to").value("/node").key("id").value(0).key("body").object().key("name").value("bob").endObject().endObject().object().key("method").value("POST").key("to").value("/node").key("id").value(1).key("body").object().key("age").value(12).endObject().endObject().object().key("method").value("POST").key("to").value("{0}/relationships").key("id").value(3).key("body").object().key("to").value("{1}").key("data").object().key("since").value("2010").endObject().key("type").value("KNOWS").endObject().endObject().object().key("method").value("POST").key("to").value("/index/relationship/my_rels").key("id").value(4).key("body").object().key("key").value("since").key("value").value("2010").key("uri").value("{3}").endObject().endObject().endArray().toString();
    String entity = gen.get().expectedType(APPLICATION_JSON_TYPE).withHeader(StreamingFormat.STREAM_HEADER, "true").expectedStatus(200).payload(jsonString).post(batchUri()).entity();
    List<Map<String, Object>> results = JsonHelper.jsonToList(entity);
    assertEquals(4, results.size());
// String rels = gen.get()
// .expectedStatus( 200 ).get( getRelationshipIndexUri( "my_rels", "since", "2010")).entity();
// assertEquals(1, JsonHelper.jsonToList(  rels ).size());
}
Also used : PrettyJSON(org.neo4j.doc.server.rest.PrettyJSON) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) Test(org.junit.Test)

Example 9 with PrettyJSON

use of org.neo4j.doc.server.rest.PrettyJSON in project neo4j-documentation by neo4j.

the class StreamingBatchOperationDocIT method shouldGetLocationHeadersWhenCreatingThings.

@Test
public void shouldGetLocationHeadersWhenCreatingThings() throws Exception {
    long originalNodeCount = countNodes();
    final String jsonString = new PrettyJSON().array().object().key("method").value("POST").key("to").value("/node").key("body").object().key("age").value(1).endObject().endObject().endArray().toString();
    JaxRsResponse response = RestRequest.req().accept(APPLICATION_JSON_TYPE).header(StreamingFormat.STREAM_HEADER, "true").post(batchUri(), jsonString);
    assertEquals(200, response.getStatus());
    final String entity = response.getEntity();
    List<Map<String, Object>> results = JsonHelper.jsonToList(entity);
    assertEquals(originalNodeCount + 1, countNodes());
    assertEquals(1, results.size());
    Map<String, Object> result = results.get(0);
    assertTrue(((String) result.get("location")).length() > 0);
}
Also used : PrettyJSON(org.neo4j.doc.server.rest.PrettyJSON) Matchers.containsString(org.hamcrest.Matchers.containsString) JaxRsResponse(org.neo4j.doc.server.rest.JaxRsResponse) Map(java.util.Map) Test(org.junit.Test)

Example 10 with PrettyJSON

use of org.neo4j.doc.server.rest.PrettyJSON in project neo4j-documentation by neo4j.

the class StreamingBatchOperationDocIT method shouldRollbackAllWhenGivenIncorrectRequest.

@Test
public void shouldRollbackAllWhenGivenIncorrectRequest() throws JsonParseException, ClientHandlerException, UniformInterfaceException, JSONException {
    String jsonString = new PrettyJSON().array().object().key("method").value("POST").key("to").value("/node").key("body").object().key("age").value("1").endObject().endObject().object().key("method").value("POST").key("to").value("/node").key("body").array().value("a_list").value("this_makes_no_sense").endArray().endObject().endArray().toString();
    long originalNodeCount = countNodes();
    JaxRsResponse response = RestRequest.req().accept(APPLICATION_JSON_TYPE).header(StreamingFormat.STREAM_HEADER, "true").post(batchUri(), jsonString);
    assertEquals(200, response.getStatus());
    // Message of the ClassCastException differs in Oracle JDK [typeX cannot be cast to typeY]
    // and IBM JDK [typeX incompatible with typeY]. That is why we check parts of the message and exception class.
    Map<String, String> body = (Map) singleResult(response, 1).get("body");
    assertEquals(BadInputException.class.getSimpleName(), body.get("exception"));
    assertThat(body.get("message"), containsString("java.util.ArrayList"));
    assertThat(body.get("message"), containsString("java.util.Map"));
    assertEquals(400, singleResult(response, 1).get("status"));
    assertEquals(originalNodeCount, countNodes());
}
Also used : BadInputException(org.neo4j.server.rest.repr.BadInputException) PrettyJSON(org.neo4j.doc.server.rest.PrettyJSON) Matchers.containsString(org.hamcrest.Matchers.containsString) JaxRsResponse(org.neo4j.doc.server.rest.JaxRsResponse) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 PrettyJSON (org.neo4j.doc.server.rest.PrettyJSON)12 Matchers.containsString (org.hamcrest.Matchers.containsString)11 Map (java.util.Map)8 JaxRsResponse (org.neo4j.doc.server.rest.JaxRsResponse)8 Graph (org.neo4j.test.GraphDescription.Graph)2 RestRequest (org.neo4j.doc.server.rest.RestRequest)1 Node (org.neo4j.graphdb.Node)1 BadInputException (org.neo4j.server.rest.repr.BadInputException)1