Search in sources :

Example 16 with ResponseEntity

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

the class TransactionDocIT method begin_and_commit_a_legacy_transaction_in_one_request.

@Test
@Documented("Begin and commit a legacy 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_legacy_transaction_in_one_request() throws JsonParseException {
    // Document
    ResponseEntity response = gen.get().noGraph().expectedStatus(200).payload(quotedJson("{ 'statements': [ { 'statement': 'CREATE (n) RETURN id(n)' } ] }")).post(getDatabaseUri() + "data/transaction/commit");
    // 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 17 with ResponseEntity

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

the class TransactionDocIT method include_query_statistics.

@Test
@Documented("Include query statistics\n" + "\n" + "By setting `includeStats` to `true` for a statement, query statistics will be returned for it.")
public void include_query_statistics() throws JsonParseException {
    // Document
    ResponseEntity response = gen.get().noGraph().expectedStatus(200).payload(quotedJson("{ 'statements': [ { 'statement': 'CREATE (n) RETURN id(n)', 'includeStats': true } ] }")).post(txCommitUri());
    // Then
    Map<String, Object> entity = jsonToMap(response.entity());
    assertNoErrors(entity);
    Map<String, Object> firstResult = ((List<Map<String, Object>>) entity.get("results")).get(0);
    assertThat(firstResult, hasKey("stats"));
    Map<String, Object> stats = (Map<String, Object>) firstResult.get("stats");
    assertThat((Integer) stats.get("nodes_created"), equalTo(1));
}
Also used : ResponseEntity(org.neo4j.doc.server.rest.RESTDocsGenerator.ResponseEntity) ArrayList(java.util.ArrayList) List(java.util.List) 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)

Example 18 with ResponseEntity

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

the class TransactionDocIT 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(txUri()).location();
    // Document
    ResponseEntity response = gen.get().noGraph().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.doc.server.rest.RESTDocsGenerator.ResponseEntity) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Documented(org.neo4j.annotations.documented.Documented) Test(org.junit.Test)

Example 19 with ResponseEntity

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

the class TransactionDocIT 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().noGraph().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(), containsString("My Node"));
}
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 20 with ResponseEntity

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

the class TransactionDocIT method commit_an_open_transaction.

@Test
@Documented("Commit an open transaction\n" + "\n" + "Given you have an open transaction, you can send a commit request. Optionally, you submit additional statements\n" + "along with the request that will be executed before committing the transaction.")
public void commit_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 id(n)' } ] }")).post(location + "/commit");
    // 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)

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