Search in sources :

Example 46 with Node

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

the class TestRelationship method createAndClearCacheBeforeCommit.

@Test
public void createAndClearCacheBeforeCommit() {
    Node node = getGraphDb().createNode();
    node.createRelationshipTo(getGraphDb().createNode(), TEST);
    assertEquals(1, Iterables.count(node.getRelationships()));
}
Also used : Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 47 with Node

use of org.neo4j.graphdb.Node 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 48 with Node

use of org.neo4j.graphdb.Node 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 49 with Node

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

the class TestRelationship method shouldLoadAllRelationships.

@Test
public void shouldLoadAllRelationships() throws Exception {
    // GIVEN
    GraphDatabaseService db = getGraphDbAPI();
    Node node;
    try (Transaction tx = db.beginTx()) {
        node = db.createNode();
        for (int i = 0; i < 112; i++) {
            node.createRelationshipTo(db.createNode(), MyRelTypes.TEST);
            db.createNode().createRelationshipTo(node, MyRelTypes.TEST);
        }
        tx.success();
    }
    // WHEN
    long one, two;
    try (Transaction tx = db.beginTx()) {
        one = Iterables.count(node.getRelationships(MyRelTypes.TEST, Direction.OUTGOING));
        two = Iterables.count(node.getRelationships(MyRelTypes.TEST, Direction.OUTGOING));
        tx.success();
    }
    // THEN
    assertEquals(two, one);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 50 with Node

use of org.neo4j.graphdb.Node 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)

Aggregations

Node (org.neo4j.graphdb.Node)1281 Test (org.junit.Test)781 Transaction (org.neo4j.graphdb.Transaction)540 Relationship (org.neo4j.graphdb.Relationship)375 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)146 NotFoundException (org.neo4j.graphdb.NotFoundException)78 File (java.io.File)65 LinkedList (java.util.LinkedList)60 RelationshipType (org.neo4j.graphdb.RelationshipType)58 HashMap (java.util.HashMap)57 Label (org.neo4j.graphdb.Label)57 Path (org.neo4j.graphdb.Path)52 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)46 HashSet (java.util.HashSet)45 Map (java.util.Map)45 WeightedPath (org.neo4j.graphalgo.WeightedPath)37 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)35 ArrayList (java.util.ArrayList)30 List (java.util.List)27 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)26