public static VirtualValue path( VirtualValue... pathElements ) { assert pathElements.length % 2 == 1; NodeValue[] nodes = new NodeValue[pathElements.length / 2 + 1]; RelationshipValue[] rels = new RelationshipValue[pathElements.length / 2]; nodes[0] = (NodeValue) pathElements[0]; for ( int i = 1; i < pathElements.length; i += 2 ) { rels[i / 2] = (RelationshipValue) pathElements[i]; nodes[i / 2 + 1] = (NodeValue) pathElements[i + 1]; } return VirtualValues.path( nodes, rels ); }
@Test void pathShouldHandleNulls() { assertThrows( AssertionError.class, () -> VirtualValues.path( null, null ) ); }
@Test void pathShouldHandleNullNodes() { assertThrows( AssertionError.class, () -> VirtualValues.path( null, relationships( 1L ) ) ); } }
@Test void pathShouldHandleNullEdge() { assertThrows( AssertionError.class, () -> VirtualValues.path( nodes( 1L ), null ) ); }
@Test void pathShouldContainOneMoreNodeThenEdges() { assertThrows( Exception.class, () -> VirtualValues.path( nodes( 1L, 2L ), relationships() ) ); }
@Test void pathShouldNotOnlyContainRelationship() { assertThrows( AssertionError.class, () -> VirtualValues.path( nodes(), relationships( 1L ) ) ); }
@Test public void shouldHandlePaths() { // Given NodeValue n1 = nodeValue( 42L, stringArray( "L" ), EMPTY_MAP ); NodeValue n2 = nodeValue( 43L, stringArray( "L" ), EMPTY_MAP ); PathValue p = path( new NodeValue[]{n1, n2}, new RelationshipValue[]{relationshipValue( 1L, n1, n2, stringValue( "T" ), EMPTY_MAP )} ); // When p.writeTo( converter ); // Then Object value = converter.value(); assertThat( value, instanceOf( Path.class ) ); Path path = (Path) value; assertThat( path.length(), equalTo( 1 ) ); assertThat( path.startNode().getId(), equalTo( 42L ) ); assertThat( path.endNode().getId(), equalTo( 43L ) ); assertThat( path.relationships().iterator().next().getId(), equalTo( 1L ) ); }
@Test public void shouldHandleSingleNodePath() { // Given Node node; try ( Transaction tx = db.beginTx() ) { node = db.createNode(); tx.success(); } // When Path mapped = mapper.mapPath( path( asNodeValues( node ), asRelationshipsValues() ) ); // Then try ( Transaction ignore = db.beginTx() ) { assertThat( mapped.length(), equalTo( 0 ) ); assertThat( mapped.startNode(), equalTo( node ) ); assertThat( mapped.endNode(), equalTo( node ) ); assertThat( Iterables.asList( mapped.relationships() ), hasSize( 0 ) ); assertThat( Iterables.asList( mapped.reverseRelationships() ), hasSize( 0 ) ); assertThat( Iterables.asList( mapped.nodes() ), equalTo( singletonList( node ) ) ); assertThat( Iterables.asList( mapped.reverseNodes() ), equalTo( singletonList( node ) ) ); assertThat( mapped.lastRelationship(), nullValue() ); assertThat( Iterators.asList( mapped.iterator() ), equalTo( singletonList( node ) ) ); } }
@Test public void shouldHandleSingleRelationshipPath() { // Given Node start, end; Relationship relationship; try ( Transaction tx = db.beginTx() ) { start = db.createNode(); end = db.createNode(); relationship = start.createRelationshipTo( end, RelationshipType.withName( "R" ) ); tx.success(); } // When Path mapped = mapper.mapPath( path( asNodeValues( start, end ), asRelationshipsValues( relationship ) ) ); // Then try ( Transaction ignore = db.beginTx() ) { assertThat( mapped.length(), equalTo( 1 ) ); assertThat( mapped.startNode(), equalTo( start ) ); assertThat( mapped.endNode(), equalTo( end ) ); assertThat( Iterables.asList( mapped.relationships() ), equalTo( singletonList( relationship ) ) ); assertThat( Iterables.asList( mapped.reverseRelationships() ), equalTo( singletonList( relationship ) ) ); assertThat( Iterables.asList( mapped.nodes() ), equalTo( Arrays.asList( start, end ) ) ); assertThat( Iterables.asList( mapped.reverseNodes() ), equalTo( Arrays.asList( end, start ) ) ); assertThat( mapped.lastRelationship(), equalTo( relationship ) ); assertThat( Iterators.asList( mapped.iterator() ), equalTo( Arrays.asList( start, relationship, end ) ) ); } }
@Test void shouldHandlePaths() { // Given NodeValue startNode = VirtualValues.nodeValue( 1L, Values.stringArray( "L" ), EMPTY_MAP ); NodeValue endNode = VirtualValues.nodeValue( 2L, Values.stringArray( "L" ), EMPTY_MAP ); RelationshipValue rel = VirtualValues.relationshipValue( 42L, startNode, endNode, stringValue( "R" ), EMPTY_MAP ); PathValue path = VirtualValues.path( new NodeValue[]{startNode, endNode}, new RelationshipValue[]{rel} ); PrettyPrinter printer = new PrettyPrinter(); // When path.writeTo( printer ); // Then assertThat( printer.value(), equalTo( "(id=1 :L)-[id=42 :R]->(id=2 :L)" ) ); }
Path mapped = mapper.mapPath( path( asNodeValues( a, b, c, d, e ), asRelationshipsValues( r1, r2, r3, r4 ) ) );
node1, relationship1, path( new NodeValue[] {node1, node2, node3}, new RelationshipValue[]{relationship1, relationship2} ), map( new String[] {"alpha", "beta"},
), shouldWrite( VirtualValues.path( new NodeValue[]{nodeValue( 20L, stringArray( "L" ), emptyMap() ), nodeValue( 40L, stringArray( "L" ), emptyMap() )},