Search in sources :

Example 56 with Node

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

the class TestLoopRelationships method canAddLoopRelationship.

@Test
public void canAddLoopRelationship() {
    Node node = getGraphDb().createNode();
    node.createRelationshipTo(node, TEST);
    newTransaction();
    for (Direction dir : Direction.values()) {
        int count = 0;
        for (Relationship rel : node.getRelationships(dir)) {
            count++;
            assertEquals("start node", node, rel.getStartNode());
            assertEquals("end node", node, rel.getEndNode());
            assertEquals("other node", node, rel.getOtherNode(node));
        }
        assertEquals(dir.name() + " relationship count", 1, count);
    }
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Example 57 with Node

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

the class TestLoopRelationships method getSingleRelationshipOnNodeWithOneLoopOnly.

@Test
public void getSingleRelationshipOnNodeWithOneLoopOnly() throws Exception {
    Node node = getGraphDb().createNode();
    Relationship singleRelationship = node.createRelationshipTo(node, TEST);
    assertEquals(singleRelationship, node.getSingleRelationship(TEST, Direction.OUTGOING));
    assertEquals(singleRelationship, node.getSingleRelationship(TEST, Direction.INCOMING));
    assertEquals(singleRelationship, node.getSingleRelationship(TEST, Direction.BOTH));
    commit();
    newTransaction();
    assertEquals(singleRelationship, node.getSingleRelationship(TEST, Direction.OUTGOING));
    assertEquals(singleRelationship, node.getSingleRelationship(TEST, Direction.INCOMING));
    assertEquals(singleRelationship, node.getSingleRelationship(TEST, Direction.BOTH));
    finish();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 58 with Node

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

the class TestLoopRelationships method cannotDeleteNodeWithLoopStillAttached.

@Test
public void cannotDeleteNodeWithLoopStillAttached() throws Exception {
    // Given
    GraphDatabaseService db = getGraphDb();
    Node node;
    try (Transaction tx = db.beginTx()) {
        node = db.createNode();
        node.createRelationshipTo(node, DynamicRelationshipType.withName("MAYOR_OF"));
        tx.success();
    }
    // And given a transaction deleting just the node
    Transaction tx = newTransaction();
    node.delete();
    tx.success();
    // Expect
    exception.expect(ConstraintViolationException.class);
    exception.expectMessage("Cannot delete node<" + node.getId() + ">, because it still has relationships. " + "To delete this node, you must first delete its relationships.");
    // When I commit
    tx.close();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 59 with Node

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

the class TestLoopRelationships method testAddAndRemoveLoopRelationshipAndOtherRelationships.

private void testAddAndRemoveLoopRelationshipAndOtherRelationships(int size) {
    for (boolean[] delete : permutations(size)) {
        for (int i = 0; i < size; i++) {
            Node root = getGraphDb().createNode();
            Relationship[] relationships = createRelationships(size, i, root);
            for (int j = 0; j < size; j++) {
                if (delete[j]) {
                    relationships[j].delete();
                    relationships[j] = null;
                }
                newTransaction();
            }
            verifyRelationships(String.format("loop on %s of %s, delete %s", i, size, Arrays.toString(delete)), root, i, relationships);
        }
    }
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 60 with Node

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

the class TestNeo4j method testRandomPropertyName.

@Test
public void testRandomPropertyName() {
    Node node1 = getGraphDb().createNode();
    String key = "random_" + new Random(System.currentTimeMillis()).nextLong();
    node1.setProperty(key, "value");
    assertEquals("value", node1.getProperty(key));
    node1.delete();
}
Also used : Random(java.util.Random) Node(org.neo4j.graphdb.Node) 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