Search in sources :

Example 21 with Relationship

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

the class TestRelationship method testRelationshipRemoveProperty.

@Test
public void testRelationshipRemoveProperty() {
    Integer int1 = new Integer(1);
    Integer int2 = new Integer(2);
    String string1 = new String("1");
    String string2 = new String("2");
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship rel1 = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    Relationship rel2 = node2.createRelationshipTo(node1, MyRelTypes.TEST);
    // verify that we can rely on PL to reomve non existing properties
    try {
        if (rel1.removeProperty(key1) != null) {
            fail("Remove of non existing property should return null");
        }
    } catch (NotFoundException e) {
    // OK
    }
    try {
        rel1.removeProperty(null);
        fail("Remove null property should throw exception.");
    } catch (IllegalArgumentException e) {
    // OK
    }
    rel1.setProperty(key1, int1);
    rel2.setProperty(key1, string1);
    rel1.setProperty(key2, string2);
    rel2.setProperty(key2, int2);
    try {
        rel1.removeProperty(null);
        fail("Null argument should result in exception.");
    } catch (IllegalArgumentException e) {
    // OK
    }
    // test remove property
    assertEquals(int1, rel1.removeProperty(key1));
    assertEquals(string1, rel2.removeProperty(key1));
    // test remove of non existing property
    try {
        if (rel2.removeProperty(key1) != null) {
            fail("Remove of non existing property should return null");
        }
    } catch (NotFoundException e) {
        // have to set rollback only here
        getTransaction().failure();
    }
    rel1.delete();
    rel2.delete();
    node1.delete();
    node2.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 22 with Relationship

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

the class TestRelationship method testRelationshipAddProperty.

@Test
public void testRelationshipAddProperty() {
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship rel1 = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    Relationship rel2 = node2.createRelationshipTo(node1, MyRelTypes.TEST);
    try {
        rel1.setProperty(null, null);
        fail("Null argument should result in exception.");
    } catch (IllegalArgumentException e) {
    // OK
    }
    Integer int1 = new Integer(1);
    Integer int2 = new Integer(2);
    String string1 = new String("1");
    String string2 = new String("2");
    // add property
    rel1.setProperty(key1, int1);
    rel2.setProperty(key1, string1);
    rel1.setProperty(key2, string2);
    rel2.setProperty(key2, int2);
    assertTrue(rel1.hasProperty(key1));
    assertTrue(rel2.hasProperty(key1));
    assertTrue(rel1.hasProperty(key2));
    assertTrue(rel2.hasProperty(key2));
    assertTrue(!rel1.hasProperty(key3));
    assertTrue(!rel2.hasProperty(key3));
    assertEquals(int1, rel1.getProperty(key1));
    assertEquals(string1, rel2.getProperty(key1));
    assertEquals(string2, rel1.getProperty(key2));
    assertEquals(int2, rel2.getProperty(key2));
    getTransaction().failure();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 23 with Relationship

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

the class TestRelationship method getAllRelationships.

@Test
public void getAllRelationships() throws Exception {
    Set<Relationship> existingRelationships = Iterables.addToCollection(getGraphDb().getAllRelationships(), new HashSet<>());
    Set<Relationship> createdRelationships = new HashSet<>();
    Node node = getGraphDb().createNode();
    for (int i = 0; i < 100; i++) {
        createdRelationships.add(node.createRelationshipTo(getGraphDb().createNode(), MyRelTypes.TEST));
    }
    newTransaction();
    Set<Relationship> allRelationships = new HashSet<>();
    allRelationships.addAll(existingRelationships);
    allRelationships.addAll(createdRelationships);
    int count = 0;
    for (Relationship rel : getGraphDb().getAllRelationships()) {
        assertTrue("Unexpected rel " + rel + ", expected one of " + allRelationships, allRelationships.contains(rel));
        count++;
    }
    assertEquals(allRelationships.size(), count);
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 24 with Relationship

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

the class TestRelationship method testCreateRelationshipWithCommitts.

@Test
public // throws NotFoundException
void testCreateRelationshipWithCommitts() {
    Node n1 = getGraphDb().createNode();
    newTransaction();
    n1 = getGraphDb().getNodeById(n1.getId());
    Node n2 = getGraphDb().createNode();
    n1.createRelationshipTo(n2, MyRelTypes.TEST);
    newTransaction();
    Relationship[] relArray = getRelationshipArray(n1.getRelationships());
    assertEquals(1, relArray.length);
    relArray = getRelationshipArray(n1.getRelationships());
    relArray[0].delete();
    n1.delete();
    n2.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 25 with Relationship

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

the class TestRelationship method deletionOfAlreadyDeletedRelationshipShouldThrow.

@Test(expected = NotFoundException.class)
public void deletionOfAlreadyDeletedRelationshipShouldThrow() {
    // Given
    GraphDatabaseService db = getGraphDb();
    // transaction is opened by test
    Node node1 = db.createNode();
    Node node2 = db.createNode();
    Relationship relationship = node1.createRelationshipTo(node2, TEST);
    commit();
    try (Transaction tx = db.beginTx()) {
        relationship.delete();
        tx.success();
    }
    // When
    try (Transaction tx = db.beginTx()) {
        relationship.delete();
        tx.success();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) 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