package org.umlg.sqlg.test;

import com.tinkerpop.gremlin.LoadGraphWith;
import com.tinkerpop.gremlin.process.Traversal;
import com.tinkerpop.gremlin.structure.Edge;
import com.tinkerpop.gremlin.structure.Graph;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.structure.io.GraphReader;
import com.tinkerpop.gremlin.structure.io.graphml.GraphMLReader;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import static com.tinkerpop.gremlin.LoadGraphWith.GraphData.CLASSIC;
import static org.junit.Assert.*;

/**
 * Date: 2014/07/13
 * Time: 6:32 PM
 */
public class TinkerpopTest extends BaseTest {

    @Test
    @LoadGraphWith(CLASSIC)
    public void g_V_asXxX_out_groupByXname_sizeX_asXaX_jumpXx_2X_capXaX() throws IOException {
        readGraphMLIntoGraph(this.sqlG);

        Traversal<Vertex, Map<String, Integer>> traversal = get_g_V_asXxX_out_groupByXname_sizeX_asXaX_jumpXx_2X_capXaX();
        printTraversalForm(traversal);
        final Map<String, Integer> map = traversal.next();
        assertFalse(traversal.hasNext());
        assertEquals(4, map.size());
        assertTrue(map.containsKey("vadas"));
        assertEquals(Integer.valueOf(1), map.get("vadas"));
        assertTrue(map.containsKey("josh"));
        assertEquals(Integer.valueOf(1), map.get("josh"));
        assertTrue(map.containsKey("lop"));
        assertEquals(Integer.valueOf(4), map.get("lop"));
        assertTrue(map.containsKey("ripple"));
        assertEquals(Integer.valueOf(2), map.get("ripple"));

    }

    public Traversal<Vertex, Map<String, Integer>> get_g_V_asXxX_out_groupByXname_sizeX_asXaX_jumpXx_2X_capXaX() {
        return this.sqlG.V().as("x").out().groupBy(v -> v.value("name"), v -> v, vv -> vv.size()).as("a").jump("x", 2).cap("a");
    }

    public Traversal<Vertex, Map<String, Integer>> get_g_V_asXxX_out_groupByXname_sizeX_asXaX_jumpXx_loops_lt_2X_capXaX() {
        return this.sqlG.V().as("x").out().groupBy(v -> v.value("name"), v -> v, vv -> vv.size()).as("a").jump("x", t -> t.getLoops() < 2).cap("a");
    }

    public Traversal<Vertex, Edge> get_g_v1_outEXknowsX_hasXweight_1X_asXhereX_inV_hasXname_joshX_backXhereX(final Object v1Id) {
        return this.sqlG.v(v1Id).outE("knows").has("weight", 1.0f).as("here").inV().has("name", "josh").back("here");
    }

    protected void printTraversalForm(final Traversal traversal) {
        System.out.println("Testing: " + traversal);
        traversal.strategies().apply();
        System.out.println("         " + traversal);
    }

    private static void readGraphMLIntoGraph(final Graph g) throws IOException {
        final GraphReader reader = GraphMLReader.build().create();
        try (final InputStream stream = new FileInputStream(new File("sqlg-test/src/main/resources/tinkerpop-classic.xml"))) {
            reader.readGraph(stream, g);
        }
//        try (final InputStream stream = TinkerpopTest.class.getResourceAsStream("tinkerpop-classic.xml")) {
//            reader.readGraph(stream, g);
//        }
    }

}

@@ -26,52 +26,8 @@
 t;%0A%0A
-import com.tinkerpop.gremlin.LoadGraphWith;%0A
 impo
@@ -113,105 +113,13 @@
 ure.
-Edge;%0Aimport com.tinkerpop.gremlin.structure.Graph;%0Aimport com.tinkerpop.gremlin.structure.Vertex
+Graph
 ;%0Aim
@@ -239,31 +239,8 @@
 der;
-%0Aimport org.junit.Test;
 %0A%0Aim
@@ -350,1914 +350,100 @@
 am;%0A
-import java.util.Map;%0A%0Aimport static com.tinkerpop.gremlin.LoadGraphWith.GraphData.CLASSIC;%0Aimport static org.junit.Assert.*;%0A%0A/**%0A * Date: 2014/07/13%0A * Time: 6:32 PM%0A */%0Apublic class TinkerpopTest extends BaseTest %7B%0A%0A    @Test%0A    @LoadGraphWith(CLASSIC)%0A    public void g_V_asXxX_out_groupByXname_sizeX_asXaX_jumpXx_2X_capXaX() throws IOException %7B%0A        readGraphMLIntoGraph(this.sqlG);%0A%0A        Traversal%3CVertex, Map%3CString, Integer%3E%3E traversal = get_g_V_asXxX_out_groupByXname_sizeX_asXaX_jumpXx_2X_capXaX();%0A        printTraversalForm(traversal);%0A        final Map%3CString, Integer%3E map = traversal.next();%0A        assertFalse(traversal.hasNext());%0A        assertEquals(4, map.size());%0A        assertTrue(map.containsKey(%22vadas%22));%0A        assertEquals(Integer.valueOf(1), map.get(%22vadas%22));%0A        assertTrue(map.containsKey(%22josh%22));%0A        assertEquals(Integer.valueOf(1), map.get(%22josh%22));%0A        assertTrue(map.containsKey(%22lop%22));%0A        assertEquals(Integer.valueOf(4), map.get(%22lop%22));%0A        assertTrue(map.containsKey(%22ripple%22));%0A        assertEquals(Integer.valueOf(2), map.get(%22ripple%22));%0A%0A    %7D%0A%0A    public Traversal%3CVertex, Map%3CString, Integer%3E%3E get_g_V_asXxX_out_groupByXname_sizeX_asXaX_jumpXx_2X_capXaX() %7B%0A        return this.sqlG.V().as(%22x%22).out().groupBy(v -%3E v.value(%22name%22), v -%3E v, vv -%3E vv.size()).as(%22a%22).jump(%22x%22, 2).cap(%22a%22);%0A    %7D%0A%0A    public Traversal%3CVertex, Map%3CString, Integer%3E%3E get_g_V_asXxX_out_groupByXname_sizeX_asXaX_jumpXx_loops_lt_2X_capXaX() %7B%0A        return this.sqlG.V().as(%22x%22).out().groupBy(v -%3E v.value(%22name%22), v -%3E v, vv -%3E vv.size()).as(%22a%22).jump(%22x%22, t -%3E t.getLoops() %3C 2).cap(%22a%22);%0A    %7D%0A%0A    public Traversal%3CVertex, Edge%3E get_g_v1_outEXknowsX_hasXweight_1X_asXhereX_inV_hasXname_joshX_backXhereX(final Object v1Id) %7B%0A        return this.sqlG.v(v1Id).outE(%22knows%22).has(%22weight%22, 1.0f).as(%22here%22).inV().has(%22name%22, %22josh%22).back(%22here%22);%0A    %7D
+%0A/**%0A * Date: 2014/07/13%0A * Time: 6:32 PM%0A */%0Apublic class TinkerpopTest extends BaseTest %7B%0A
 %0A%0A  

remove old non compiling test