Search in sources :

Example 11 with ResponseEntity

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

the class GetOnRootDocIT method streaming.

@Documented("All responses from the REST API can be transmitted as JSON streams, resulting in\n" + "better performance and lower memory overhead on the server side. To use\n" + "streaming, supply the header `X-Stream: true` with each request.")
@Test
public void streaming() throws Exception {
    data.get();
    ResponseEntity responseEntity = gen().docHeadingLevel(2).noGraph().withHeader(StreamingFormat.STREAM_HEADER, "true").expectedType(APPLICATION_JSON_TYPE).expectedStatus(200).get(getDataUri());
    JaxRsResponse response = responseEntity.response();
    // this gets the full media type, including things like
    // ; stream=true at the end
    String foundMediaType = response.getType().toString();
    String expectedMediaType = StreamingFormat.MEDIA_TYPE.toString();
    assertEquals(expectedMediaType, foundMediaType);
    String body = responseEntity.entity();
    Map<String, Object> map = JsonHelper.jsonToMap(body);
    assertEquals(getDataUri() + "node", map.get("node"));
}
Also used : ResponseEntity(org.neo4j.doc.server.rest.RESTDocsGenerator.ResponseEntity) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 12 with ResponseEntity

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

the class IndexNodeDocIT method get_or_create_unique_node_if_already_existing.

@Documented("Get or create unique node (existing).\n" + "\n" + "Here,\n" + "a node is not created but the existing unique node returned, since another node\n" + "is indexed with the same data already. The node data returned is then that of the\n" + "already existing node.")
@Test
public void get_or_create_unique_node_if_already_existing() throws Exception {
    final String index = indexes.newInstance(), key = "name", value = "Peter";
    GraphDatabaseService graphdb = graphdb();
    try (Transaction tx = graphdb().beginTx()) {
        Node peter = graphdb.createNode();
        peter.setProperty(key, value);
        peter.setProperty("sequence", 1);
        graphdb.index().forNodes(index).add(peter, key, value);
        tx.success();
    }
    helper.createNodeIndex(index);
    ResponseEntity response = gen().noGraph().expectedStatus(200).payloadType(MediaType.APPLICATION_JSON_TYPE).payload("{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"properties\": {\"" + key + "\": \"" + value + "\", \"sequence\": 2}}").post(functionalTestHelper.nodeIndexUri() + index + "?uniqueness=get_or_create");
    Map<String, Object> result = JsonHelper.jsonToMap(response.entity());
    Map<String, Object> data = assertCast(Map.class, result.get("data"));
    assertEquals(value, data.get(key));
    assertEquals(1, data.get("sequence"));
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) ResponseEntity(org.neo4j.doc.server.rest.RESTDocsGenerator.ResponseEntity) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 13 with ResponseEntity

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

the class IndexNodeDocIT method get_or_create_a_node_in_an_unique_index.

@Documented("Get or create unique node (create).\n" + "\n" + "The node is created if it doesn't exist in the unique index already.")
@Test
public void get_or_create_a_node_in_an_unique_index() throws Exception {
    final String index = indexes.newInstance(), key = "name", value = "Tobias";
    helper.createNodeIndex(index);
    ResponseEntity response = gen().noGraph().expectedStatus(201).payloadType(MediaType.APPLICATION_JSON_TYPE).payload("{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"properties\": {\"" + key + "\": \"" + value + "\", \"sequence\": 1}}").post(functionalTestHelper.nodeIndexUri() + index + "?uniqueness=get_or_create");
    MultivaluedMap<String, String> headers = response.response().getHeaders();
    Map<String, Object> result = JsonHelper.jsonToMap(response.entity());
    assertEquals(result.get("indexed"), headers.getFirst("Location"));
    Map<String, Object> data = assertCast(Map.class, result.get("data"));
    assertEquals(value, data.get(key));
    assertEquals(1, data.get("sequence"));
}
Also used : ResponseEntity(org.neo4j.doc.server.rest.RESTDocsGenerator.ResponseEntity) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 14 with ResponseEntity

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

the class TransactionDocIT method execute_statements_in_an_open_transaction.

@Test
@Documented("Execute statements in an open transaction\n" + "\n" + "Given that you have an open transaction, you can make a number of requests, each of which executes additional\n" + "statements, and keeps the transaction open by resetting the transaction timeout.")
public void execute_statements_in_an_open_transaction() throws JsonParseException {
    // Given
    String location = POST(txUri()).location();
    // Document
    ResponseEntity response = gen.get().noGraph().expectedStatus(200).payload(quotedJson("{ 'statements': [ { 'statement': 'CREATE (n) RETURN n' } ] }")).post(location);
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    assertThat(result, hasKey("transaction"));
    assertNoErrors(result);
}
Also used : ResponseEntity(org.neo4j.doc.server.rest.RESTDocsGenerator.ResponseEntity) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.Test)

Example 15 with ResponseEntity

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

the class TransactionDocIT method execute_statements_in_an_open_transaction_using_REST.

@Test
@Documented("Execute statements in an open transaction in REST format for the return.\n" + "\n" + "Given that you have an open transaction, you can make a number of requests, each of which executes additional\n" + "statements, and keeps the transaction open by resetting the transaction timeout. Specifying the `REST` format will\n" + "give back full Neo4j Rest API representations of the Neo4j Nodes, Relationships and Paths, if returned.")
public void execute_statements_in_an_open_transaction_using_REST() throws JsonParseException {
    // Given
    String location = POST(txUri()).location();
    // Document
    ResponseEntity response = gen.get().noGraph().expectedStatus(200).payload(quotedJson("{ 'statements': [ { 'statement': 'CREATE (n) RETURN n','resultDataContents':['REST'] } ] }")).post(location);
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    ArrayList rest = (ArrayList) ((Map) ((ArrayList) ((Map) ((ArrayList) result.get("results")).get(0)).get("data")).get(0)).get("rest");
    String selfUri = ((String) ((Map) rest.get(0)).get("self"));
    assertTrue(selfUri.startsWith(getDatabaseUri()));
    assertNoErrors(result);
}
Also used : ResponseEntity(org.neo4j.doc.server.rest.RESTDocsGenerator.ResponseEntity) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Map(java.util.Map) JsonHelper.jsonToMap(org.neo4j.server.rest.domain.JsonHelper.jsonToMap) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.Test)

Aggregations

ResponseEntity (org.neo4j.doc.server.rest.RESTDocsGenerator.ResponseEntity)23 Test (org.junit.Test)22 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)13 Documented (org.neo4j.annotations.documented.Documented)13 Documented (org.neo4j.kernel.impl.annotations.Documented)8 ArrayList (java.util.ArrayList)3 Node (org.neo4j.graphdb.Node)3 Transaction (org.neo4j.graphdb.Transaction)3 List (java.util.List)2 Map (java.util.Map)2 HTTP (org.neo4j.doc.server.HTTP)2 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)2 JsonHelper.jsonToMap (org.neo4j.server.rest.domain.JsonHelper.jsonToMap)2 Matchers.containsString (org.hamcrest.Matchers.containsString)1