Search in sources :

Example 61 with Relationship

use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.

the class ExecutionResultSerializerTest method shouldProduceResultStreamWithLegacyRestFormat.

@Test
public void shouldProduceResultStreamWithLegacyRestFormat() throws Exception {
    // given
    Node[] node = { node(0, properties(property("name", "node0"))), node(1, properties(property("name", "node1"))), node(2, properties(property("name", "node2"))) };
    Relationship[] rel = { relationship(0, node[0], "KNOWS", node[1], property("name", "rel0")), relationship(1, node[2], "LOVES", node[1], property("name", "rel1")) };
    Path path = GraphMock.path(node[0], link(rel[0], node[1]), link(rel[1], node[2]));
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ExecutionResultSerializer serializer = getSerializerWith(output, "http://base.uri/");
    // when
    serializer.statementResult(mockExecutionResult(map("node", node[0], "rel", rel[0], "path", path, "map", map("n1", node[1], "r1", rel[1]))), false, ResultDataContent.rest);
    serializer.finish();
    // then
    String result = output.toString(UTF_8.name());
    JsonNode json = jsonNode(result);
    Map<String, Integer> columns = new HashMap<>();
    int col = 0;
    JsonNode results = json.get("results").get(0);
    for (JsonNode column : results.get("columns")) {
        columns.put(column.getTextValue(), col++);
    }
    JsonNode row = results.get("data").get(0).get("rest");
    JsonNode jsonNode = row.get(columns.get("node"));
    JsonNode jsonRel = row.get(columns.get("rel"));
    JsonNode jsonPath = row.get(columns.get("path"));
    JsonNode jsonMap = row.get(columns.get("map"));
    assertEquals("http://base.uri/node/0", jsonNode.get("self").getTextValue());
    assertEquals("http://base.uri/relationship/0", jsonRel.get("self").getTextValue());
    assertEquals(2, jsonPath.get("length").getNumberValue());
    assertEquals("http://base.uri/node/0", jsonPath.get("start").getTextValue());
    assertEquals("http://base.uri/node/2", jsonPath.get("end").getTextValue());
    assertEquals("http://base.uri/node/1", jsonMap.get("n1").get("self").getTextValue());
    assertEquals("http://base.uri/relationship/1", jsonMap.get("r1").get("self").getTextValue());
}
Also used : Path(org.neo4j.graphdb.Path) HashMap(java.util.HashMap) JsonNode(org.codehaus.jackson.JsonNode) JsonHelper.jsonNode(org.neo4j.server.rest.domain.JsonHelper.jsonNode) Node(org.neo4j.graphdb.Node) JsonNode(org.codehaus.jackson.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test) Neo4jJsonCodecTest(org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)

Example 62 with Relationship

use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.

the class ExecutionResultSerializerTest method mockPath.

private static Path mockPath(Map<String, Object> startNodeProperties, Map<String, Object> relationshipProperties, Map<String, Object> endNodeProperties) {
    Node startNode = node(1, properties(startNodeProperties));
    Node endNode = node(2, properties(endNodeProperties));
    Relationship relationship = relationship(1, properties(relationshipProperties), startNode, "RELATED", endNode);
    return path(startNode, Link.link(relationship, endNode));
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) JsonHelper.jsonNode(org.neo4j.server.rest.domain.JsonHelper.jsonNode) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 63 with Relationship

use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.

the class DeletionTest method shouldDeleteRecords.

/**
     * The problem would manifest even if the transaction was performed on the Master, it would then occur when the
     * Slave pulls updates and tries to apply the transaction. The reason for the test to run transactions against the
     * Slave is because it makes guarantees for when the master has to apply the transaction.
     */
@Test
public void shouldDeleteRecords() throws Throwable {
    // given
    ManagedCluster cluster = clusterRule.startCluster();
    HighlyAvailableGraphDatabase master = cluster.getMaster();
    HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
    Relationship rel;
    try (Transaction tx = slave.beginTx()) {
        rel = slave.createNode().createRelationshipTo(slave.createNode(), withName("FOO"));
        tx.success();
    }
    try (Transaction transaction = master.beginTx()) {
        assertNotNull(master.getRelationshipById(rel.getId()));
    }
    // when
    try (Transaction tx = slave.beginTx()) {
        rel.delete();
        tx.success();
    }
    // then - there should have been no exceptions
    slave.shutdown();
    master.shutdown();
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ManagedCluster(org.neo4j.kernel.impl.ha.ClusterManager.ManagedCluster) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 64 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class AbstractTestBase method removeAllNodes.

protected static void removeAllNodes(boolean removeReference) {
    Transaction tx = graphdb.beginTx();
    try {
        Node reference = removeReference ? null : graphdb.getReferenceNode();
        for (Node node : graphdb.getAllNodes()) {
            for (Relationship rel : node.getRelationships()) {
                rel.delete();
            }
            if (!node.equals(reference)) {
                node.delete();
            }
        }
        tx.success();
    } finally {
        tx.finish();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 65 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class TestRelationship method testRelationshipCreateAndDelete.

@Test
public void testRelationshipCreateAndDelete() {
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship relationship = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    Relationship[] relArray1 = getRelationshipArray(node1.getRelationships());
    Relationship[] relArray2 = getRelationshipArray(node2.getRelationships());
    assertEquals(1, relArray1.length);
    assertEquals(relationship, relArray1[0]);
    assertEquals(1, relArray2.length);
    assertEquals(relationship, relArray2[0]);
    relArray1 = getRelationshipArray(node1.getRelationships(MyRelTypes.TEST));
    assertEquals(1, relArray1.length);
    assertEquals(relationship, relArray1[0]);
    relArray2 = getRelationshipArray(node2.getRelationships(MyRelTypes.TEST));
    assertEquals(1, relArray2.length);
    assertEquals(relationship, relArray2[0]);
    relArray1 = getRelationshipArray(node1.getRelationships(MyRelTypes.TEST, Direction.OUTGOING));
    assertEquals(1, relArray1.length);
    relArray2 = getRelationshipArray(node2.getRelationships(MyRelTypes.TEST, Direction.INCOMING));
    assertEquals(1, relArray2.length);
    relArray1 = getRelationshipArray(node1.getRelationships(MyRelTypes.TEST, Direction.INCOMING));
    assertEquals(0, relArray1.length);
    relArray2 = getRelationshipArray(node2.getRelationships(MyRelTypes.TEST, Direction.OUTGOING));
    assertEquals(0, relArray2.length);
    relationship.delete();
    node2.delete();
    node1.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Aggregations

Relationship (org.neo4j.graphdb.Relationship)490 Node (org.neo4j.graphdb.Node)370 Test (org.junit.Test)250 Transaction (org.neo4j.graphdb.Transaction)138 LinkedList (java.util.LinkedList)48 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)48 RelationshipType (org.neo4j.graphdb.RelationshipType)38 NotFoundException (org.neo4j.graphdb.NotFoundException)35 Path (org.neo4j.graphdb.Path)29 HashMap (java.util.HashMap)27 Direction (org.neo4j.graphdb.Direction)26 HashSet (java.util.HashSet)24 ArrayList (java.util.ArrayList)18 RelationshipIndex (org.neo4j.graphdb.index.RelationshipIndex)18 Map (java.util.Map)14 WeightedPath (org.neo4j.graphalgo.WeightedPath)14 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)14 List (java.util.List)13 PropertyContainer (org.neo4j.graphdb.PropertyContainer)12 Graph (org.neo4j.test.GraphDescription.Graph)11