Search in sources :

Example 16 with Documented

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

the class TransactionDocIT 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().noGraph().expectedStatus(200).payload(quotedJson("{ 'statements': [ { 'statement': 'This is not a valid Cypher Statement.' } ] }")).post(location + "/commit");
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    assertErrors(result, Status.Statement.SyntaxError);
}
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 17 with Documented

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

the class TransactionDocIT 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(txUri(), HTTP.RawPayload.quotedJson("{ 'statements': [ { 'statement': 'CREATE (n) RETURN id(n)' } ] }"));
    String location = firstReq.location();
    // Document
    ResponseEntity response = gen.get().noGraph().expectedStatus(200).delete(location);
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    assertNoErrors(result);
    Integer id = resultCell(firstReq, 0, 0);
    verifyNodeDoesNotExist(id);
}
Also used : ResponseEntity(org.neo4j.doc.server.rest.RESTDocsGenerator.ResponseEntity) HTTP(org.neo4j.doc.server.HTTP) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.Test)

Example 18 with Documented

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

the class TransactionDocIT method begin_and_commit_a_transaction_in_one_request.

@Test
@Documented("Begin and commit a transaction in one request\n" + "\n" + "If there is no need to keep a transaction open across multiple HTTP requests, you can begin a transaction,\n" + "execute statements, and commit with just a single HTTP request.")
public void begin_and_commit_a_transaction_in_one_request() throws JsonParseException {
    // Document
    ResponseEntity response = gen.get().noGraph().expectedStatus(200).payload(quotedJson("{ 'statements': [ { 'statement': 'CREATE (n) RETURN id(n)' } ] }")).post(txCommitUri());
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    assertNoErrors(result);
    Integer id = resultCell(result, 0, 0);
    verifyNodeExists(id);
}
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 19 with Documented

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

the class TransactionDocIT 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().noGraph().expectedStatus(200).payload(quotedJson("{ 'statements': [ ] }")).post(location);
    // Then
    Map<String, Object> result = jsonToMap(response.entity());
    assertNoErrors(result);
    long newExpirationTime = expirationTime(result);
    assertTrue("Expiration time was not increased", newExpirationTime > initialExpirationTime);
}
Also used : ResponseEntity(org.neo4j.doc.server.rest.RESTDocsGenerator.ResponseEntity) HTTP(org.neo4j.doc.server.HTTP) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.Test)

Example 20 with Documented

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

the class AuthenticationDocIT method successful_authentication.

@Test
@Documented("Authenticate to access the server\n" + "\n" + "Authenticate by sending a username and a password to Neo4j using HTTP Basic Auth.\n" + "Requests should include an +Authorization+ header, with a value of +Basic <payload>+,\n" + "where \"payload\" is a base64 encoded string of \"username:password\".")
public void successful_authentication() throws JsonParseException, IOException {
    // Given
    startServerWithConfiguredUser();
    HTTP.Response response = HTTP.withBasicAuth("neo4j", "secret").POST(txCommitURL("system"), query("SHOW USERS"));
    assertThat(response.status(), equalTo(200));
    final JsonNode jsonNode = getResultRow(response);
    assertThat(jsonNode.get(0).asText(), equalTo("neo4j"));
    assertThat(jsonNode.get(1).asBoolean(), equalTo(false));
}
Also used : HTTP(org.neo4j.doc.server.HTTP) JsonNode(com.fasterxml.jackson.databind.JsonNode) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.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