Skip to content

Commit

Permalink
[eBlQUOcs] Bug in apoc.create.cloneToVirtual (neo4j/apoc#375) (#3570)
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 authored May 17, 2023
1 parent 581fa59 commit 77c71cd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
22 changes: 13 additions & 9 deletions core/src/main/java/apoc/result/VirtualPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package apoc.result;

import org.apache.commons.collections4.CollectionUtils;
import org.neo4j.graphdb.Entity;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
Expand All @@ -26,6 +27,7 @@

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -145,19 +147,21 @@ public String toString() {
}

private void requireConnected(Relationship relationship) {
Node previousEndNode;
final List<Node> previousNodes = getPreviousNodes();
boolean isRelConnectedToPrevious = CollectionUtils.containsAny( previousNodes, relationship.getNodes() );
if (!isRelConnectedToPrevious) {
throw new IllegalArgumentException("Relationship is not part of current path.");
}
}

private List<Node> getPreviousNodes() {
Relationship previousRelationship = lastRelationship();
if (previousRelationship != null) {
previousEndNode = previousRelationship.getEndNode();
} else {
previousEndNode = endNode();
}
if (!relationship.getStartNode().equals(previousEndNode)
&& !relationship.getEndNode().equals(previousEndNode)) {
throw new IllegalArgumentException("Relationship is not part of current path.");
return Arrays.asList(previousRelationship.getNodes());
}
return List.of(endNode());
}

public static final class Builder {
private final Node start;
private final List<Relationship> relationships = new ArrayList<>();
Expand Down
27 changes: 27 additions & 0 deletions core/src/test/java/apoc/create/CreateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,33 @@ public void testClonePathShouldNotEditOriginalOne() {
});
}

@Test
public void testClonePathWithMixedDirectionRelationships() {
// rel `:b` is to the left, rel `d` is to the right
db.executeTransactionally("CREATE (:a {id: 1})<-[:b {id: 10}]-(:c {id: 2})-[:d {id: 20}]->(:e {id: 3})");

testCall(db, "MATCH p = (:a)<-[:b]-(:c)-[:d]->(:e) " +
"CALL apoc.create.clonePathToVirtual(p) YIELD path RETURN path", r -> {
Path path = (Path) r.get("path");
Iterator<Node> nodes = path.nodes().iterator();
Node node = nodes.next();
assertEquals(Map.of("id", 1L), node.getAllProperties());
node = nodes.next();
assertEquals(Map.of("id", 2L), node.getAllProperties());
node = nodes.next();
assertEquals(Map.of("id", 3L), node.getAllProperties());
assertFalse(nodes.hasNext());

Iterator<Relationship> rels = path.relationships().iterator();
Relationship rel = rels.next();
assertEquals(Map.of("id", 10L), rel.getAllProperties());
rel = rels.next();
assertEquals(Map.of("id", 20L), rel.getAllProperties());
assertFalse(rels.hasNext());

});
}

@Test
public void testClonePathShouldNotDuplicateRelsWithMultipaths() {
//create path with single rels
Expand Down

0 comments on commit 77c71cd

Please sign in to comment.