Search in sources :

Example 6 with ResponseEntity

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

the class IndexNodeIT method create_a_unique_node_or_fail_create.

@Documented("Create a unique node or return fail (create).\n" + "\n" + "Here, in case\n" + "of an already existing node, an error should be returned. In this\n" + "example, no existing indexed node is found and a new node is created.")
@Test
public void create_a_unique_node_or_fail_create() throws Exception {
    final String index = indexes.newInstance(), key = "name", value = "Tobias";
    helper.createNodeIndex(index);
    ResponseEntity response = gen.get().expectedStatus(201).payloadType(MediaType.APPLICATION_JSON_TYPE).payload("{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"properties\": {\"" + key + "\": \"" + value + "\", \"sequence\": 1}}").post(functionalTestHelper.nodeIndexUri() + index + "?uniqueness=create_or_fail" + "");
    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.server.rest.RESTRequestGenerator.ResponseEntity) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 7 with ResponseEntity

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

the class IndexNodeIT 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().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.server.rest.RESTRequestGenerator.ResponseEntity) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 8 with ResponseEntity

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

the class IndexNodeIT method create_a_unique_node_or_return_fail___fail.

@Documented("Create a unique node or return fail (fail).\n" + "\n" + "Here, in case\n" + "of an already existing node, an error should be returned. In this\n" + "example, an existing node indexed with the same data\n" + "is found and an error is returned.")
@Test
public void create_a_unique_node_or_return_fail___fail() throws Exception {
    final String index = indexes.newInstance(), key = "name", value = "Peter";
    GraphDatabaseService graphdb = graphdb();
    helper.createNodeIndex(index);
    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();
    }
    RestRequest.req();
    ResponseEntity response = gen.get().expectedStatus(409).payloadType(MediaType.APPLICATION_JSON_TYPE).payload("{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"properties\": {\"" + key + "\": \"" + value + "\", \"sequence\": 2}}").post(functionalTestHelper.nodeIndexUri() + index + "?uniqueness=create_or_fail");
    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.server.rest.RESTRequestGenerator.ResponseEntity) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 9 with ResponseEntity

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

the class IndexNodeIT 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().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.server.rest.RESTRequestGenerator.ResponseEntity) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 10 with ResponseEntity

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

the class IndexRelationshipIT method create_a_unique_relationship_or_return_fail___create.

@Documented("Create a unique relationship or return fail (create).\n" + "\n" + "Here, in case\n" + "of an already existing relationship, an error should be returned. In this\n" + "example, no existing relationship is found and a new relationship is created.")
@Test
public void create_a_unique_relationship_or_return_fail___create() throws Exception {
    final String index = indexes.newInstance(), key = "name", value = "Tobias";
    helper.createRelationshipIndex(index);
    ResponseEntity response = gen.get().expectedStatus(201).payloadType(MediaType.APPLICATION_JSON_TYPE).payload("{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"start\": \"" + functionalTestHelper.nodeUri(helper.createNode()) + "\", \"end\": \"" + functionalTestHelper.nodeUri(helper.createNode()) + "\", \"type\":\"" + MyRelationshipTypes.KNOWS + "\"}").post(functionalTestHelper.relationshipIndexUri() + index + "?uniqueness=create_or_fail");
    MultivaluedMap<String, String> headers = response.response().getHeaders();
    Map<String, Object> result = JsonHelper.jsonToMap(response.entity());
    assertEquals(result.get("indexed"), headers.getFirst("Location"));
}
Also used : ResponseEntity(org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.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