package com.thinkaurelius.titan.blueprints.process.traversal.strategy.optimization;

import com.thinkaurelius.titan.graphdb.tinkerpop.optimize.TitanGraphStep;
import org.apache.tinkerpop.gremlin.process.AbstractGremlinProcessTest;
import org.apache.tinkerpop.gremlin.process.IgnoreEngine;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.TraversalEngine;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class TitanGraphStepStrategyTest extends AbstractGremlinProcessTest {

    @Test
    @IgnoreEngine(TraversalEngine.Type.COMPUTER)
    public void shouldFoldInHasContainers() {
        GraphTraversal.Admin traversal = g.V().has("name", "marko").asAdmin();
        assertEquals(2, traversal.getSteps().size());
        assertEquals(HasStep.class, traversal.getEndStep().getClass());
        traversal.applyStrategies();
        assertEquals(1, traversal.getSteps().size());
        assertEquals(TitanGraphStep.class, traversal.getStartStep().getClass());
        assertEquals(TitanGraphStep.class, traversal.getEndStep().getClass());
        assertEquals(1, ((TitanGraphStep) traversal.getStartStep()).getHasContainers().size());
        assertEquals("name", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getKey());
        assertEquals("marko", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getValue());
        ////
        traversal = g.V().has("name", "marko").has("age", P.gt(20)).asAdmin();
        traversal.applyStrategies();
        assertEquals(1, traversal.getSteps().size());
        assertEquals(TitanGraphStep.class, traversal.getStartStep().getClass());
        assertEquals(2, ((TitanGraphStep) traversal.getStartStep()).getHasContainers().size());
        ////
        traversal = g.V().has("name", "marko").out().has("name", "daniel").asAdmin();
        traversal.applyStrategies();
        assertEquals(3, traversal.getSteps().size());
        assertEquals(TitanGraphStep.class, traversal.getStartStep().getClass());
        assertEquals(1, ((TitanGraphStep) traversal.getStartStep()).getHasContainers().size());
        assertEquals("name", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getKey());
        assertEquals("marko", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getValue());
        assertEquals(HasStep.class, traversal.getEndStep().getClass());
        ////
        traversal = g.V().has("name", "marko").out().V().has("name", "daniel").asAdmin();
        traversal.applyStrategies();
        assertEquals(3, traversal.getSteps().size());
        assertEquals(TitanGraphStep.class, traversal.getStartStep().getClass());
        assertEquals(1, ((TitanGraphStep) traversal.getStartStep()).getHasContainers().size());
        assertEquals("name", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getKey());
        assertEquals("marko", ((TitanGraphStep<?, ?>) traversal.getStartStep()).getHasContainers().get(0).getValue());
        assertEquals(TitanGraphStep.class, traversal.getSteps().get(2).getClass());
        assertEquals(1, ((TitanGraphStep) traversal.getSteps().get(2)).getHasContainers().size());
        assertEquals("name", ((TitanGraphStep<?, ?>) traversal.getSteps().get(2)).getHasContainers().get(0).getKey());
        assertEquals("daniel", ((TitanGraphStep<?,?>) traversal.getSteps().get(2)).getHasContainers().get(0).getValue());
        assertEquals(TitanGraphStep.class, traversal.getEndStep().getClass());
    }

}

@@ -78,16 +78,129 @@
 ation;%0A%0A
+import com.thinkaurelius.titan.blueprints.InMemoryGraphProvider;%0Aimport com.thinkaurelius.titan.core.TitanGraph;%0A
 import c
@@ -261,24 +261,25 @@
 nGraphStep;%0A
+%0A
 import org.a
@@ -453,16 +453,138 @@
 rsal.P;%0A
+import org.apache.tinkerpop.gremlin.GraphProviderClass;%0Aimport org.apache.tinkerpop.gremlin.process.ProcessStandardSuite;%0A
 import o
@@ -801,16 +801,17 @@
 asStep;%0A
+%0A
 import o
@@ -824,16 +824,49 @@
 it.Test;
+%0Aimport org.junit.runner.RunWith;
 %0A%0Aimport
@@ -971,12 +971,135 @@
 om)%0A
-
  */%0A
+@RunWith(ProcessStandardSuite.class)%0A@GraphProviderClass(provider = InMemoryGraphProvider.class, graph = TitanGraph.class)%0A
 publ

fix nullpointer in TitanGraphStepStrategyTest testcase