@Test public void getSingleRelationshipOnNodeWithOneLoopOnly() { 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(); }
@Test @Graph( { "a TO b", "b TO c", "c TO a" } ) public void canCreateGraphFromMultipleStrings() { Map<String,Node> graph = data.get(); Set<Node> unique = new HashSet<>(); Node n = graph.get( "a" ); while ( unique.add( n ) ) { try ( Transaction ignored = graphdb.beginTx() ) { n = n.getSingleRelationship( RelationshipType.withName( "TO" ), Direction.OUTGOING ).getEndNode(); } } assertEquals( graph.size(), unique.size() ); }
@Test public void testGetDirectedRelationship() { Node node1 = getGraphDb().getNodeById( node1Id ); Relationship rel = node1.getSingleRelationship( MyRelTypes.TEST, Direction.OUTGOING ); assertEquals( int1, rel.getProperty( key1 ) ); }
@After public void deleteTestingGraph() { Node node1 = getGraphDb().getNodeById( node1Id ); Node node2 = getGraphDb().getNodeById( node2Id ); node1.getSingleRelationship( MyRelTypes.TEST, Direction.BOTH ).delete(); node1.delete(); node2.delete(); }
@Test @Graph( "I know you" ) public void get_Relationship_by_ID() throws JsonParseException { Node node = data.get().get( "I" ); Relationship relationship; try ( Transaction transaction = node.getGraphDatabase().beginTx() ) { relationship = node.getSingleRelationship( RelationshipType.withName( "know" ), Direction.OUTGOING ); } String response = gen().expectedStatus( com.sun.jersey.api.client.ClientResponse.Status.OK.getStatusCode() ).get( getRelationshipUri( relationship ) ).entity(); assertTrue( JsonHelper.jsonToMap( response ).containsKey( "start" ) ); }
@Test public void getNewlyCreatedLoopRelationshipFromCache() { Node node = getGraphDb().createNode(); node.createRelationshipTo( getGraphDb().createNode(), TEST ); newTransaction(); Relationship relationship = node.createRelationshipTo( node, TEST ); newTransaction(); assertEquals( relationship, node.getSingleRelationship( TEST, Direction.INCOMING ) ); }
@Test public void getOrCreateRelationshipWithUniqueFactory() { final String key = "name"; final String value = "Mattias"; final Node root = graphDb.createNode(); final Index<Relationship> index = relationshipIndex( LuceneIndexImplementation.EXACT_CONFIG ); final RelationshipType type = withName( "SINGLE" ); UniqueFactory<Relationship> factory = new UniqueFactory.UniqueRelationshipFactory( index ) { @Override protected Relationship create( Map<String, Object> properties ) { assertEquals( value, properties.get( key ) ); assertEquals( 1, properties.size() ); return root.createRelationshipTo( graphDatabase().createNode(), type ); } }; Relationship unique = factory.getOrCreate( key, value ); assertEquals( unique, root.getSingleRelationship( type, Direction.BOTH ) ); assertNotNull( unique ); assertEquals( unique, index.get( key, value ).getSingle() ); assertEquals( unique, factory.getOrCreate( key, value ) ); assertEquals( unique, root.getSingleRelationship( type, Direction.BOTH ) ); assertEquals( unique, index.get( key, value ).getSingle() ); finishTx( false ); }
@Test public void testNodeChangeProperty() { Node node1 = getGraphDb().getNodeById( node1Id ); Node node2 = getGraphDb().getNodeById( node2Id ); Relationship rel = node1.getSingleRelationship( MyRelTypes.TEST, Direction.BOTH ); // test change property node1.setProperty( key1, int2 ); node2.setProperty( key1, int1 ); rel.setProperty( key1, int2 ); int[] newIntArray = new int[] { 3, 2, 1 }; node1.setProperty( arrayKey, newIntArray ); node2.setProperty( arrayKey, newIntArray ); rel.setProperty( arrayKey, newIntArray ); }
@Test public void testDirectedRelationship1() { Node node1 = getGraphDb().getNodeById( node1Id ); Relationship rel = node1.getSingleRelationship( MyRelTypes.TEST, Direction.BOTH ); Node[] nodes = rel.getNodes(); assertEquals( 2, nodes.length ); Node node2 = getGraphDb().getNodeById( node2Id ); assertTrue( nodes[0].equals( node1 ) && nodes[1].equals( node2 ) ); assertEquals( node1, rel.getStartNode() ); assertEquals( node2, rel.getEndNode() ); Relationship[] relArray = getRelationshipArray( node1.getRelationships( MyRelTypes.TEST, Direction.OUTGOING ) ); assertEquals( 1, relArray.length ); assertEquals( rel, relArray[0] ); relArray = getRelationshipArray( node2.getRelationships( MyRelTypes.TEST, Direction.INCOMING ) ); assertEquals( 1, relArray.length ); assertEquals( rel, relArray[0] ); }
@Test public void testNodeRemoveProperty() { Node node1 = getGraphDb().getNodeById( node1Id ); Node node2 = getGraphDb().getNodeById( node2Id ); Relationship rel = node1.getSingleRelationship( MyRelTypes.TEST, Direction.BOTH ); // test remove property assertEquals( 1, node1.removeProperty( key1 ) ); assertEquals( 2, node2.removeProperty( key1 ) ); assertEquals( 1, rel.removeProperty( key1 ) ); assertEquals( string1, node1.removeProperty( key2 ) ); assertEquals( string2, node2.removeProperty( key2 ) ); assertEquals( string1, rel.removeProperty( key2 ) ); assertNotNull( node1.removeProperty( arrayKey ) ); assertNotNull( node2.removeProperty( arrayKey ) ); assertNotNull( rel.removeProperty( arrayKey ) ); }
assertEquals( highMark, relAboveTheLine.getId() ); assertEquals( relBelowTheLine, db.getNodeById( idBelow ).getSingleRelationship( this, Direction.OUTGOING ) ); assertEquals( relAboveTheLine, db.getNodeById( idBelow ).getSingleRelationship( this, Direction.INCOMING ) ); assertEquals( idBelow, relBelowTheLine.getId() ); assertEquals( highMark, relAboveTheLine.getId() );
@Test public void ensureCorrectPathEntitiesInShortPath() { /* * (a)-->(b) */ createGraph( "a TO b" ); Node a = getNodeWithName( "a" ); Node b = getNodeWithName( "b" ); Relationship r = a.getSingleRelationship( to, OUTGOING ); Path path = Iterables.single( getGraphDb().bidirectionalTraversalDescription() .mirroredSides( getGraphDb().traversalDescription().relationships( to, OUTGOING ).uniqueness( NODE_PATH ) ) .collisionEvaluator( Evaluators.atDepth( 1 ) ) .sideSelector( SideSelectorPolicies.LEVEL, 1 ) .traverse( a, b ) ); assertContainsInOrder( path.nodes(), a, b ); assertContainsInOrder( path.reverseNodes(), b, a ); assertContainsInOrder( path.relationships(), r ); assertContainsInOrder( path.reverseRelationships(), r ); assertContainsInOrder( path, a, r, b ); assertEquals( a, path.startNode() ); assertEquals( b, path.endNode() ); assertEquals( r, path.lastRelationship() ); }
assertProperties( properties, node ); assertProperties( properties, rel ); Node highNode = node.getSingleRelationship( OTHER_TYPE, Direction.OUTGOING ).getEndNode(); assertProperties( properties, highNode ); verified++;
@Test public void testAddProperty() { String key3 = "key3"; Node node1 = getGraphDb().getNodeById( node1Id ); Node node2 = getGraphDb().getNodeById( node2Id ); Relationship rel = node1.getSingleRelationship( MyRelTypes.TEST, Direction.BOTH ); // add new property node2.setProperty( key3, int1 ); rel.setProperty( key3, int2 ); assertTrue( node1.hasProperty( key1 ) ); assertTrue( node2.hasProperty( key1 ) ); assertTrue( node1.hasProperty( key2 ) ); assertTrue( node2.hasProperty( key2 ) ); assertTrue( node1.hasProperty( arrayKey ) ); assertTrue( node2.hasProperty( arrayKey ) ); assertTrue( rel.hasProperty( arrayKey ) ); assertTrue( !node1.hasProperty( key3 ) ); assertTrue( node2.hasProperty( key3 ) ); assertEquals( int1, node1.getProperty( key1 ) ); assertEquals( int2, node2.getProperty( key1 ) ); assertEquals( string1, node1.getProperty( key2 ) ); assertEquals( string2, node2.getProperty( key2 ) ); assertEquals( int1, rel.getProperty( key1 ) ); assertEquals( string1, rel.getProperty( key2 ) ); assertEquals( int2, rel.getProperty( key3 ) ); }
Relationship realRelationship = db.getRelationshipById( relationship ); assertEquals( realSelfRelationship, realStartNode.getSingleRelationship( RelTypes.REL_TYPE1, Direction.INCOMING ) ); assertEquals( asSet( realSelfRelationship, realRelationship ), Iterables.asSet( realStartNode.getRelationships( Direction.OUTGOING ) ) );
assertEquals( node1.getProperty( "test" ), 1 ); assertEquals( rel.getProperty( "test" ), 11 ); assertEquals( rel, node1.getSingleRelationship( MyRelTypes.TEST, Direction.OUTGOING ) ); node1.delete();
Relationship loadedRel = node1.getSingleRelationship( withName( "TEST" ), Direction.OUTGOING ); assertEquals( rel, loadedRel ); assertThat(loadedRel, inTx(db, hasProperty( "key1" ).withValue( "value1" )));
@Test public void testExtractNode() throws Exception { Long id = db.execute("CREATE (f:Foo)-[rel:FOOBAR {a:1}]->(b:Bar) RETURN id(rel) as id").<Long>columnAs("id").next(); testCall(db, "CALL apoc.refactor.extractNode({ids},['FooBar'],'FOO','BAR')", map("ids", singletonList(id)), (r) -> { assertEquals(id, r.get("input")); Node node = (Node) r.get("output"); assertEquals(true, node.hasLabel(Label.label("FooBar"))); assertEquals(1L, node.getProperty("a")); assertNotNull(node.getSingleRelationship(RelationshipType.withName("FOO"), Direction.OUTGOING)); assertNotNull(node.getSingleRelationship(RelationshipType.withName("BAR"), Direction.INCOMING)); }); } @Test
@Test public void testMergeNodesWithSelfRelationships() throws Exception { Map<String, Object> result = Iterators.single(db.execute("CREATE \n" + "(alice:Person {name:'Alice'}),\n" + "(bob:Person {name:'Bob'}),\n" + "(bob)-[:likes]->(bob) RETURN id(alice) AS aliceId, id(bob) AS bobId")); // Merge (bob) into (alice). // The updated node should have one self relationship. // NB: the "LIMIT 1" here is important otherwise Cypher tries to check if another MATCH is found, causing a failing read attempt to deleted node testCall(db, "MATCH (alice:Person {name:'Alice'}), (bob:Person {name:'Bob'}) WITH * LIMIT 1 CALL apoc.refactor.mergeNodes([alice, bob]) yield node return node", (r)-> { Node node = (Node) r.get("node"); assertEquals(result.get("aliceId"), node.getId()); assertEquals("Bob", node.getProperty("name")); assertEquals(1, node.getDegree(Direction.INCOMING)); assertEquals(1, node.getDegree(Direction.OUTGOING)); assertTrue(node.getSingleRelationship(RelationshipType.withName("likes"), Direction.OUTGOING).getEndNode().equals(node)); }); }
/** * Deletes this sorted tree. */ public void delete() { acquireLock(); Relationship rel = treeRoot.getUnderlyingNode().getSingleRelationship( RelTypes.TREE_ROOT, Direction.INCOMING ); treeRoot.delete(); rel.delete(); }