I found the solution to my problem :
TraversalDescription must be given info about what to do from the starting node, not to the target node. For instance, if I got A - [:REL] -> B
and start from A
, I must indicate an OUTGOING direction for the relation.
This seems not user friendly for me, as it's unlike filters, which I though it was.
Furthermore, I needed to add .evaluator(Evaluators.excludeStartPosition())
to exclude the initial node from the result.
I don't know if the explication is clear enough, but here's a working code :
/**
* Find components of Caramel : tests that level 1 relationship works
*/
@Test
public void testCaramelContainsSugar() {
try (Transaction ignored = graphDb.beginTx()) {
Node n = null;
TraversalDescription traversalDescription = graphDb.traversalDescription()
.breadthFirst()
.relationships(FoodRelationshipType.CONTAINS, Direction.OUTGOING)
.evaluator(Evaluators.excludeStartPosition());
Traverser traverser = traversalDescription.traverse(
getSingleNode(LabelType.COOKED_MEAL, PropertyType.NAME.getName(), "Caramel")
);
List<Node> endNodes = new ArrayList<>();
for (Path path : traverser) {
endNodes.add(path.endNode());
}
Assert.assertTrue(endNodes.contains(getSingleNode(LabelType.RAW_MATERIAL, PropertyType.NAME.getName(), "Sugar")));
}
}