public void doTest(final Traversal<?, ?> traversal,
                       final Optional<List<Traversal<?, ?>>> collectedTraversalParents,
                       final Optional<List<Pair<Boolean, Set<String>>>> expectedTraversalParentCacheEntries) {

        List<Pair<TraversalParent, List<Step<?, ?>>>> traversalParentsStepsCollection =
                FilterRankingStrategy.collectStepsOfAssignableClassRecursivelyFromDepthGroupedByParent(traversal.asAdmin());
        Map<TraversalParent, Pair<Boolean, Set<String>>> traversalParentCache = FilterRankingStrategy.getTraversalParentCache(traversalParentsStepsCollection);

        // Old method of collecting traversal parents and their children steps, which can be significantly less efficient
        // when hash codes of very complex steps must be computing. Replacing LinkedHashMap with IdentityHashMap will show some failures
        final Map<TraversalParent, List<Step<?, ?>>> traversalParentsStepsMap =
                TraversalHelper.getStepsOfAssignableClassRecursivelyFromDepth(traversal.asAdmin(), TraversalParent.class, LambdaHolder.class).stream().
                        collect(Collectors.groupingBy(step -> ((Step<?, ?>) step).getTraversal().getParent(), LinkedHashMap::new, Collectors.toList()));
        // Note that the new method does not result in exactly the same list as traversal parents on the same level/depth are ordered
        // differently, i.e., we cannot compare traversalParentsStepsCollection and oldTraversalParentsStepsCollection.
        // The new collection method leads however also to a correct initialization of the cache.
        List<Pair<TraversalParent, List<Step<?, ?>>>> oldTraversalParentsStepsCollection = new ArrayList<>();
        traversalParentsStepsMap.forEach((k, v) -> {
            oldTraversalParentsStepsCollection.add(new Pair<>(k, v));
        });
        // computing the cache on the old way of collecting the traversal parents
        Map<TraversalParent, Pair<Boolean, Set<String>>> oldCollectionTraversalParentCache = FilterRankingStrategy.getTraversalParentCache(oldTraversalParentsStepsCollection);

        expectedTraversalParentCacheEntries.ifPresent(pairs -> Assert.assertEquals(pairs.size(), traversalParentCache.size()));

        if (collectedTraversalParents.isPresent() && expectedTraversalParentCacheEntries.isPresent()) {
            for (int i = 0; i < collectedTraversalParents.get().size(); ++i) {
                final Pair<TraversalParent, List<Step<?, ?>>> traversalParentStepListPair = traversalParentsStepsCollection.get(i);
                final TraversalParent traversalParent = traversalParentStepListPair.getValue0();
                final Traversal<?, ?> expectedTraversalParent = collectedTraversalParents.get().get(i);
                Assert.assertEquals(((DefaultGraphTraversal<?, ?>) expectedTraversalParent).getSteps().get(0), traversalParent);

                final Pair<Boolean, Set<String>> expectedCacheEntry = expectedTraversalParentCacheEntries.get().get(i);
                final Pair<Boolean, Set<String>> cacheEntry = traversalParentCache.get(traversalParent);
                Assert.assertEquals(expectedCacheEntry, cacheEntry);
            }

            Assert.assertEquals(collectedTraversalParents.get().size() + 1, traversalParentsStepsCollection.size());
            Assert.assertEquals(traversalParentsStepsCollection.get(collectedTraversalParents.get().size()).getValue0(), EmptyStep.instance());
            final Pair<Boolean, Set<String>> expectedCacheEntry = expectedTraversalParentCacheEntries.get().get(collectedTraversalParents.get().size());
            final Pair<Boolean, Set<String>> cacheEntry = traversalParentCache.get(EmptyStep.instance());
            Assert.assertEquals(expectedCacheEntry, cacheEntry);
        }

        Assert.assertEquals(oldCollectionTraversalParentCache, traversalParentCache);
    }
Testing whether the cache for the FilterRankingStrategy is correctly initialized,
which includes gathering/collecting the traversal parents and their children steps
in the correct deep first manner.
gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/FilterRankingStrategyCacheInitializationTest.java