Search in sources :

Example 26 with Documented

use of org.neo4j.kernel.impl.annotations.Documented 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 27 with Documented

use of org.neo4j.kernel.impl.annotations.Documented in project neo4j by neo4j.

the class IndexRelationshipIT method put_relationship_if_absent_only_fail.

@Documented("Add an existing relationship to a unique index (already indexed).")
@Test
public void put_relationship_if_absent_only_fail() throws Exception {
    // Given
    final String index = indexes.newInstance(), key = "name", value = "Peter";
    GraphDatabaseService graphdb = graphdb();
    helper.createRelationshipIndex(index);
    try (Transaction tx = graphdb.beginTx()) {
        Node node1 = graphdb.createNode();
        Node node2 = graphdb.createNode();
        Relationship rel = node1.createRelationshipTo(node2, MyRelationshipTypes.KNOWS);
        graphdb.index().forRelationships(index).add(rel, key, value);
        tx.success();
    }
    Relationship rel;
    try (Transaction tx = graphdb.beginTx()) {
        Node node1 = graphdb.createNode();
        Node node2 = graphdb.createNode();
        rel = node1.createRelationshipTo(node2, MyRelationshipTypes.KNOWS);
        tx.success();
    }
    // When & Then
    gen.get().expectedStatus(409).payloadType(MediaType.APPLICATION_JSON_TYPE).payload("{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"uri\":\"" + functionalTestHelper.relationshipUri(rel.getId()) + "\"}").post(functionalTestHelper.relationshipIndexUri() + index + "?uniqueness=create_or_fail");
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 28 with Documented

use of org.neo4j.kernel.impl.annotations.Documented 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)

Example 29 with Documented

use of org.neo4j.kernel.impl.annotations.Documented in project neo4j by neo4j.

the class IndexRelationshipIT method create_a_unique_relationship_or_return_fail___fail.

@Documented("Create a unique relationship or return fail (fail).\n" + "\n" + "Here, in case\n" + "of an already existing relationship, an error should be returned. In this\n" + "example, an existing relationship is found and an error is returned.")
@Test
public void create_a_unique_relationship_or_return_fail___fail() throws Exception {
    final String index = indexes.newInstance(), key = "name", value = "Peter";
    GraphDatabaseService graphdb = graphdb();
    helper.createRelationshipIndex(index);
    try (Transaction tx = graphdb.beginTx()) {
        Node node1 = graphdb.createNode();
        Node node2 = graphdb.createNode();
        Relationship rel = node1.createRelationshipTo(node2, MyRelationshipTypes.KNOWS);
        graphdb.index().forRelationships(index).add(rel, key, value);
        tx.success();
    }
    gen.get().expectedStatus(409).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");
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 30 with Documented

use of org.neo4j.kernel.impl.annotations.Documented in project neo4j by neo4j.

the class CypherIT method testProfiling.

@Test
@Title("Profile a query")
@Documented("By passing in an extra parameter, you can ask the cypher executor to return a profile of the " + "query as it is executed. This can help in locating bottlenecks.")
@Graph(nodes = { @NODE(name = "I", setNameProperty = true), @NODE(name = "you", setNameProperty = true), @NODE(name = "him", setNameProperty = true, properties = { @PROP(key = "age", value = "25", type = GraphDescription.PropType.INTEGER) }) }, relationships = { @REL(start = "I", end = "him", type = "know", properties = {}), @REL(start = "I", end = "you", type = "know", properties = {}) })
public void testProfiling() throws Exception {
    String script = createScript("MATCH (x)-[r]->(n) WHERE id(x) = %I% RETURN type(r), n.name, n.age");
    // WHEN
    String response = doCypherRestCall(cypherUri() + "?profile=true", script, Status.OK);
    // THEN
    Map<String, Object> des = jsonToMap(response);
    assertThat(des.get("plan"), instanceOf(Map.class));
    @SuppressWarnings("unchecked") Map<String, Object> plan = (Map<String, Object>) des.get("plan");
    assertThat(plan.get("name"), instanceOf(String.class));
    assertThat(plan.get("children"), instanceOf(Collection.class));
    assertThat(plan.get("rows"), instanceOf(Number.class));
    assertThat(plan.get("dbHits"), instanceOf(Number.class));
}
Also used : Collection(java.util.Collection) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Map(java.util.Map) JsonHelper.jsonToMap(org.neo4j.server.rest.domain.JsonHelper.jsonToMap) Graph(org.neo4j.test.GraphDescription.Graph) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test) Title(org.neo4j.test.TestData.Title)

Aggregations

Documented (org.neo4j.kernel.impl.annotations.Documented)71 Test (org.junit.Test)70 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)26 Graph (org.neo4j.test.GraphDescription.Graph)21 ResponseEntity (org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity)20 Title (org.neo4j.test.TestData.Title)19 Node (org.neo4j.graphdb.Node)14 Matchers.containsString (org.hamcrest.Matchers.containsString)11 Transaction (org.neo4j.graphdb.Transaction)8 RelationshipRepresentationTest (org.neo4j.server.rest.repr.RelationshipRepresentationTest)7 JsonNode (org.codehaus.jackson.JsonNode)6 JaxRsResponse (org.neo4j.server.rest.JaxRsResponse)6 RESTRequestGenerator (org.neo4j.server.rest.RESTRequestGenerator)6 ArrayList (java.util.ArrayList)5 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)5 Relationship (org.neo4j.graphdb.Relationship)5 Map (java.util.Map)4 URI (java.net.URI)3 FunctionalTestHelper (org.neo4j.server.helpers.FunctionalTestHelper)3 RestRequest (org.neo4j.server.rest.RestRequest)3