Search in sources :

Example 26 with ResponseEntity

use of org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity in project neo4j by neo4j.

the class TransactionTest method rollback_an_open_transaction.

@Test
@Documented("Rollback an open transaction\n" + "\n" + "Given that you have an open transaction, you can send a rollback request. The server will rollback the\n" + "transaction. Any further statements trying to run in this transaction will fail immediately.")
public void rollback_an_open_transaction() throws JsonParseException {
    // Given
    HTTP.Response firstReq = POST(getDataUri() + "transaction", HTTP.RawPayload.quotedJson("{ 'statements': [ { 'statement': 'CREATE (n) RETURN id(n)' } ] }"));
    String location = firstReq.location();
    // Document
    ResponseEntity response = gen.get().expectedStatus(200).delete(location);
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    assertNoErrors(result);
    Integer id = resultCell(firstReq, 0, 0);
    assertThat(GET(getNodeUri(id)).status(), is(404));
}
Also used : ResponseEntity(org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity) HTTP(org.neo4j.test.server.HTTP) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 27 with ResponseEntity

use of org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity in project neo4j by neo4j.

the class TransactionTest method errors_in_open_transaction.

@Test
@Documented("Handling errors in an open transaction\n" + "\n" + "Whenever there is an error in a request the server will rollback the transaction.\n" + "By inspecting the response for the presence/absence of the `transaction` key you can tell if the " + "transaction is still open")
public void errors_in_open_transaction() throws JsonParseException {
    // Given
    String location = POST(getDataUri() + "transaction").location();
    // Document
    ResponseEntity response = gen.get().expectedStatus(200).payload(quotedJson("{ 'statements': [ { 'statement': 'This is not a valid Cypher Statement.' } ] }")).post(location);
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    assertThat(result, not(hasKey("transaction")));
}
Also used : ResponseEntity(org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 28 with ResponseEntity

use of org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity 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 29 with ResponseEntity

use of org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity 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 30 with ResponseEntity

use of org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity 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)

Aggregations

ResponseEntity (org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity)36 Test (org.junit.Test)22 Documented (org.neo4j.kernel.impl.annotations.Documented)20 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)12 Test (org.junit.jupiter.api.Test)12 Documented (org.neo4j.annotations.documented.Documented)12 ArrayList (java.util.ArrayList)6 List (java.util.List)5 HTTP (org.neo4j.test.server.HTTP)4 Node (org.neo4j.graphdb.Node)3 Transaction (org.neo4j.graphdb.Transaction)3 Map (java.util.Map)2 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)2 JsonHelper.jsonToMap (org.neo4j.server.rest.domain.JsonHelper.jsonToMap)2 Matchers.containsString (org.hamcrest.Matchers.containsString)1