package com.orientechnologies.orient.graph.sql;

import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TestOutInChain {

  @BeforeClass
  public static void before() {
    // generate schema
    OrientGraphNoTx graph = new OrientGraphNoTx("memory:" + TestOutInChain.class.getSimpleName(), "admin", "admin");
    graph.command(new OCommandSQL("create class User extends V")).execute();
    graph.command(new OCommandSQL("create class Car extends V")).execute();
    graph.command(new OCommandSQL("create class Owns extends E")).execute();
    graph.shutdown();
  }

  @Test
  public void t() {
    OrientGraph graph = new OrientGraph("memory:" + TestOutInChain.class.getSimpleName(), "admin", "admin");

    Vertex vUser = graph.addVertex("class:User");
    Vertex vCar = graph.addVertex("class:Car");
    graph.addEdge("class:Owns", vUser, vCar, null);

    graph.commit();

    Iterable<Vertex> res = graph.command(new OCommandSQL("select expand( out('Owns') ) from User")).execute();
    assertTrue(res.iterator().hasNext());
    assertEquals("Car", res.iterator().next().getProperty("@class").toString());

    Iterable<Vertex> resEdge = graph.command(new OCommandSQL("select expand( inE('Owns') ) from Car")).execute();
    assertTrue(resEdge.iterator().hasNext());

    // when out('Owns') is executed we have Car vertex (see above)
    // after that inE('Owns') should return Owns edge (see above)
    // but test fails
    resEdge = graph.command(new OCommandSQL("select expand( out('Owns').inE('Owns') ) from User")).execute();
    assertTrue(resEdge.iterator().hasNext());// assertion error here
		graph.shutdown();
  }
}

@@ -115,35 +115,159 @@
 com.
-tinkerpop.blueprints.Vertex
+orientechnologies.orient.core.sql.query.OSQLSynchQuery;%0Aimport com.tinkerpop.blueprints.Vertex;%0Aimport com.tinkerpop.blueprints.impls.orient.OrientEdge
 ;%0Aim
@@ -979,29 +979,366 @@
 ();%0A
-    graph.shutdown();
+%0A    initTestMultipleLabels(graph);%0A    graph.shutdown();%0A  %7D%0A%0A  private static void initTestMultipleLabels(OrientGraphNoTx graph) %7B%0A    graph.command(new OCommandSQL(%22create class V1 extends V%22)).execute();%0A    graph.command(new OCommandSQL(%22create class E1 extends E%22)).execute();%0A    graph.command(new OCommandSQL(%22create class E2 extends E%22)).execute();%0A
 %0A  %7D
@@ -2403,10 +2403,1262 @@
 ();%0A  %7D%0A
+%0A  @Test%0A  public void testMultipleLabels() %7B%0A    //issue #5359%0A%0A    OrientGraph graph = new OrientGraph(%22memory:%22 + TestOutInChain.class.getSimpleName(), %22admin%22, %22admin%22);%0A%0A    graph.command(new OCommandSQL(%22create vertex V1 set name = '1'%22)).execute();%0A    graph.command(new OCommandSQL(%22create vertex V1 set name = '2'%22)).execute();%0A    graph.command(new OCommandSQL(%22create vertex V1 set name = '3'%22)).execute();%0A%0A    graph.command(new OCommandSQL(%22create edge E1 from (select from V1 where name = '1') to (select from V1 where name = '2')%22)).execute();%0A    graph.command(new OCommandSQL(%22create edge E2 from (select from V1 where name = '1') to (select from V1 where name = '3')%22)).execute();%0A%0A    Iterable result = graph.command(new OSQLSynchQuery%3COrientEdge%3E(%22select expand(outE('E1').out.outE('E1', 'E2')) from V1 where name = '1'%22))%0A        .execute();%0A    int count =0;%0A    for(Object e:result)%7B%0A      count++;%0A    %7D%0A%0A    assertEquals(2, count);%0A//    result = graph.command(new OSQLSynchQuery%3COrientEdge%3E(%22select outE('E1').out.outE('E1', 'E2') from V1 where name = '1'%22))%0A//        .execute();%0A//    iterator = result.iterator();%0A//%0A//    assertTrue(iterator.hasNext());%0A//    iterator.next();%0A//    assertTrue(iterator.hasNext());%0A  %7D%0A%0A  
 %7D%0A

Add test case for issue #5359