Search in sources :

Example 36 with Documented

use of org.neo4j.annotations.documented.Documented in project neo4j by neo4j.

the class TransactionTestIT method handling_errors.

@Test
@Documented("Handling errors\n" + "\n" + "The result of any request against the transaction endpoint is streamed back to the client.\n" + "Therefore the server does not know whether the request will be successful or not when it sends the HTTP status\n" + "code.\n" + "\n" + "Because of this, all requests against the transactional endpoint will return 200 or 201 status code, regardless\n" + "of whether statements were successfully executed. At the end of the response payload, the server includes a list\n" + "of errors that occurred while executing statements. If this list is empty, the request completed successfully.\n" + "\n" + "If any errors occur while executing statements, the server will roll back the transaction.\n" + "\n" + "In this example, we send the server an invalid statement to demonstrate error handling.\n" + " \n" + "For more information on the status codes, see <<status-codes>>.")
public void handling_errors() throws JsonParseException {
    // Given
    String location = POST(txUri()).location();
    // Document
    ResponseEntity response = gen.get().expectedStatus(200).payload(quotedJson("{ 'statements': [ { 'statement': 'This is not a valid Cypher Statement.' } ] }")).post(txCommitUri());
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    assertErrors(result, Status.Statement.SyntaxError);
}
Also used : ResponseEntity(org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.jupiter.api.Test)

Example 37 with Documented

use of org.neo4j.annotations.documented.Documented in project neo4j by neo4j.

the class TransactionTestIT 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().expectedStatus(200).payload(quotedJson("{ 'statements': [ { 'statement': 'CREATE (n) RETURN n','resultDataContents':['REST'] } ] }")).post(location);
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    List 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(dbUri()));
    assertNoErrors(result);
}
Also used : ResponseEntity(org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.jupiter.api.Test)

Example 38 with Documented

use of org.neo4j.annotations.documented.Documented in project neo4j by neo4j.

the class TransactionTestIT method reset_transaction_timeout_of_an_open_transaction.

@Test
@Documented("Reset transaction timeout of an open transaction\n" + "\n" + "Every orphaned transaction is automatically expired after a period of inactivity.  This may be prevented\n" + "by resetting the transaction timeout.\n" + "\n" + "The timeout may be reset by sending a keep-alive request to the server that executes an empty list of statements.\n" + "This request will reset the transaction timeout and return the new time at which the transaction will\n" + "expire as an RFC1123 formatted timestamp value in the ``transaction'' section of the response.")
public void reset_transaction_timeout_of_an_open_transaction() throws JsonParseException, InterruptedException {
    // Given
    HTTP.Response initialResponse = POST(txUri());
    String location = initialResponse.location();
    long initialExpirationTime = expirationTime(jsonToMap(initialResponse.rawContent()));
    // This generous wait time is necessary to compensate for limited resolution of RFC 1123 timestamps
    // and the fact that the system clock is allowed to run "backwards" between threads
    // (cf. http://stackoverflow.com/questions/2978598)
    // 
    Thread.sleep(3000);
    // Document
    ResponseEntity response = gen.get().expectedStatus(200).payload(quotedJson("{ 'statements': [ ] }")).post(location);
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    assertNoErrors(result);
    long newExpirationTime = expirationTime(result);
    assertTrue(newExpirationTime > initialExpirationTime, "Expiration time was not increased");
}
Also used : ResponseEntity(org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity) HTTP(org.neo4j.test.server.HTTP) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.jupiter.api.Test)

Example 39 with Documented

use of org.neo4j.annotations.documented.Documented in project neo4j by neo4j.

the class TransactionTestIT method execute_multiple_statements.

@Test
@Documented("Execute multiple statements\n" + "\n" + "You can send multiple Cypher statements in the same request.\n" + "The response will contain the result of each statement.")
public void execute_multiple_statements() throws JsonParseException {
    // Document
    ResponseEntity response = gen.get().expectedStatus(200).payload(quotedJson("{ 'statements': [ { 'statement': 'CREATE (n) RETURN id(n)' }, " + "{ 'statement': 'CREATE (n $props) RETURN n', " + "'parameters': { 'props': { 'name': 'My Node' } } } ] }")).post(txCommitUri());
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    assertNoErrors(result);
    Integer id = resultCell(result, 0, 0);
    verifyNodeExists(id);
    assertThat(response.entity()).contains("My Node");
}
Also used : ResponseEntity(org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.jupiter.api.Test)

Example 40 with Documented

use of org.neo4j.annotations.documented.Documented in project neo4j by neo4j.

the class TransactionTestIT method return_results_in_graph_format.

@Test
@Documented("Return results in graph format\n" + "\n" + "If you want to understand the graph structure of nodes and relationships returned by your query,\n" + "you can specify the \"graph\" results data format. For example, this is useful when you want to visualise the\n" + "graph structure. The format collates all the nodes and relationships from all columns of the result,\n" + "and also flattens collections of nodes and relationships, including paths.")
public void return_results_in_graph_format() throws JsonParseException {
    // Document
    ResponseEntity response = gen.get().expectedStatus(200).payload(quotedJson("{'statements':[{'statement':" + "'CREATE ( bike:Bike { weight: 10 } ) " + "CREATE ( frontWheel:Wheel { spokes: 3 } ) " + "CREATE ( backWheel:Wheel { spokes: 32 } ) " + "CREATE p1 = (bike)-[:HAS { position: 1 } ]->(frontWheel) " + "CREATE p2 = (bike)-[:HAS { position: 2 } ]->(backWheel) " + "RETURN bike, p1, p2', " + "'resultDataContents': ['row','graph']}] }")).post(txCommitUri());
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    assertNoErrors(result);
    Map<String, List<Object>> row = graphRow(result, 0);
    assertEquals(3, row.get("nodes").size());
    assertEquals(2, row.get("relationships").size());
}
Also used : ResponseEntity(org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity) ArrayList(java.util.ArrayList) List(java.util.List) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.jupiter.api.Test)

Aggregations

Documented (org.neo4j.annotations.documented.Documented)43 Test (org.junit.Test)23 Test (org.junit.jupiter.api.Test)15 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)13 ResponseEntity (org.neo4j.doc.server.rest.RESTDocsGenerator.ResponseEntity)13 ResponseEntity (org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity)12 Graph (org.neo4j.doc.test.GraphDescription.Graph)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ArrayList (java.util.ArrayList)6 List (java.util.List)5 Transaction (org.neo4j.graphdb.Transaction)4 Map (java.util.Map)3 HTTP (org.neo4j.doc.server.HTTP)3 JavaTestDocsGenerator (org.neo4j.doc.tools.JavaTestDocsGenerator)3 Node (org.neo4j.graphdb.Node)3 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)3 JsonHelper.jsonToMap (org.neo4j.server.rest.domain.JsonHelper.jsonToMap)3 HTTP (org.neo4j.test.server.HTTP)3 Statement (org.junit.runners.model.Statement)2 RESTDocsGenerator (org.neo4j.doc.server.rest.RESTDocsGenerator)2