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"));
}
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"));
}
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"));
}
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"));
}
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"));
}
Aggregations