Search in sources :

Example 36 with Relationship

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

the class TestRelationship method testDirectedRelationship.

@Test
public void testDirectedRelationship() {
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship rel2 = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    Relationship rel3 = node2.createRelationshipTo(node1, MyRelTypes.TEST);
    Node[] nodes = rel2.getNodes();
    assertEquals(2, nodes.length);
    assertTrue(nodes[0].equals(node1) && nodes[1].equals(node2));
    nodes = rel3.getNodes();
    assertEquals(2, nodes.length);
    assertTrue(nodes[0].equals(node2) && nodes[1].equals(node1));
    assertEquals(node1, rel2.getStartNode());
    assertEquals(node2, rel2.getEndNode());
    assertEquals(node2, rel3.getStartNode());
    assertEquals(node1, rel3.getEndNode());
    Relationship[] relArray = getRelationshipArray(node1.getRelationships(MyRelTypes.TEST, Direction.OUTGOING));
    assertEquals(1, relArray.length);
    assertEquals(rel2, relArray[0]);
    relArray = getRelationshipArray(node1.getRelationships(MyRelTypes.TEST, Direction.INCOMING));
    assertEquals(1, relArray.length);
    assertEquals(rel3, relArray[0]);
    relArray = getRelationshipArray(node2.getRelationships(MyRelTypes.TEST, Direction.OUTGOING));
    assertEquals(1, relArray.length);
    assertEquals(rel3, relArray[0]);
    relArray = getRelationshipArray(node2.getRelationships(MyRelTypes.TEST, Direction.INCOMING));
    assertEquals(1, relArray.length);
    assertEquals(rel2, relArray[0]);
    rel2.delete();
    rel3.delete();
    node1.delete();
    node2.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 37 with Relationship

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

the class TestRelationship method testRelGetProperties.

@Test
public void testRelGetProperties() {
    Integer int1 = new Integer(1);
    Integer int2 = new Integer(2);
    String string = new String("3");
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship rel1 = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    try {
        rel1.getProperty(key1);
        fail("get non existing property din't throw exception");
    } catch (NotFoundException e) {
    // OK
    }
    try {
        rel1.getProperty(null);
        fail("get of null key din't throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertTrue(!rel1.hasProperty(key1));
    assertTrue(!rel1.hasProperty(null));
    rel1.setProperty(key1, int1);
    rel1.setProperty(key2, int2);
    rel1.setProperty(key3, string);
    assertTrue(rel1.hasProperty(key1));
    assertTrue(rel1.hasProperty(key2));
    assertTrue(rel1.hasProperty(key3));
    Map<String, Object> properties = rel1.getAllProperties();
    assertTrue(properties.get(key1).equals(int1));
    assertTrue(properties.get(key2).equals(int2));
    assertTrue(properties.get(key3).equals(string));
    properties = rel1.getProperties(key1, key2);
    assertTrue(properties.get(key1).equals(int1));
    assertTrue(properties.get(key2).equals(int2));
    assertFalse(properties.containsKey(key3));
    properties = node1.getProperties();
    assertTrue(properties.isEmpty());
    try {
        String[] names = null;
        node1.getProperties(names);
        fail();
    } catch (NullPointerException e) {
    // Ok
    }
    try {
        String[] names = new String[] { null };
        node1.getProperties(names);
        fail();
    } catch (NullPointerException e) {
    // Ok
    }
    try {
        rel1.removeProperty(key3);
    } catch (NotFoundException e) {
        fail("Remove of property failed.");
    }
    assertTrue(!rel1.hasProperty(key3));
    assertTrue(!rel1.hasProperty(null));
    rel1.delete();
    node2.delete();
    node1.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 38 with Relationship

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

the class TestRelationship method testAddPropertyThenDelete.

@Test
public void testAddPropertyThenDelete() {
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship rel = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    rel.setProperty("test", "test");
    newTransaction();
    rel.setProperty("test2", "test2");
    rel.delete();
    node1.delete();
    node2.delete();
    newTransaction();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 39 with Relationship

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

the class TestRelationship method testRelationshipIsType.

@Test
public void testRelationshipIsType() {
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship rel = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    assertTrue(rel.isType(MyRelTypes.TEST));
    assertTrue(rel.isType(new RelationshipType() {

        @Override
        public String name() {
            return MyRelTypes.TEST.name();
        }
    }));
    assertFalse(rel.isType(MyRelTypes.TEST_TRAVERSAL));
    rel.delete();
    node1.delete();
    node2.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.Test)

Example 40 with Relationship

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

the class TestRelationshipCount method deleteOneRelationship.

private void deleteOneRelationship(Node node, RelType type, Direction direction, int which) {
    Relationship last = null;
    int counter = 0;
    for (Relationship rel : node.getRelationships(type, direction)) {
        if (isLoop(rel) == (direction == Direction.BOTH)) {
            last = rel;
            if (counter++ == which) {
                rel.delete();
                return;
            }
        }
    }
    if (which == Integer.MAX_VALUE && last != null) {
        last.delete();
        return;
    }
    fail("Couldn't find " + (direction == Direction.BOTH ? "loop" : "non-loop") + " relationship " + type.name() + " " + direction + " to delete");
}
Also used : Relationship(org.neo4j.graphdb.Relationship)

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