package com.orientechnologies.orient.graph.blueprints;

import com.orientechnologies.orient.core.record.impl.ODocument;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientEdgeType;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
import org.junit.Test;

import java.util.List;

import static junit.framework.TestCase.assertEquals;

public class OrderedEdgesGraphTest {
  private static String      DB_URL = "memory:" + OrderedEdgesGraphTest.class.getSimpleName();
  private static OrientGraph graph;
  private final OrientVertex mainPerson;

  public OrderedEdgesGraphTest() {
    graph = new OrientGraph(DB_URL);
    graph.setUseLightweightEdges(true);
    graph.setAutoStartTx(false);
    graph.commit();

    if (graph.getEdgeType("Knows") == null) {
      OrientEdgeType knows = graph.createEdgeType("Knows");
      OrientVertexType person = graph.createVertexType("Person");

      person.createEdgeProperty(Direction.OUT, "Knows").setOrdered(true);
    }

    graph.setAutoStartTx(true);

    mainPerson = graph.addVertex("class:Person", new Object[] { "index", 0 });

    for (int i = 1; i < 101; ++i) {
      final Vertex newVertex = graph.addVertex("class:Person", new Object[] { "index", i });
      mainPerson.addEdge("Knows", newVertex);
    }
  }

  @Test
  public void testEdgeOrder() {

    OrientVertex loadedPerson = graph.getVertex(mainPerson.getId());
    graph.setUseLightweightEdges(true);
    int i = 1;
    for (Edge e : loadedPerson.getEdges(Direction.OUT)) {
      assertEquals(e.getVertex(Direction.IN).getProperty("index"), i++);
    }

    graph.shutdown();

  }

  @Test
  public void testReplacePosition() {

    OrientVertex loadedPerson = graph.getVertex(mainPerson.getId());
    graph.setUseLightweightEdges(true);
    int i = 1;
    List<ODocument> edges = loadedPerson.getRecord().field("out_Knows");

    ODocument edge10 = edges.remove(9);
    edges.add(edge10);

    graph.shutdown();

    graph = new OrientGraph(DB_URL);
    graph.setUseLightweightEdges(true);
    loadedPerson = graph.getVertex(mainPerson.getId());
    edges = loadedPerson.getRecord().field("out_Knows");
    assertEquals(graph.getVertex(edges.get(9)).getProperty("index"), 11);
    assertEquals(graph.getVertex(edges.get(99)).getProperty("index"), 10);
    graph.shutdown();
  }
}

@@ -1542,25 +1542,36 @@
 geOrder() %7B%0A
-%0A
+    try %7B%0A  
     OrientVe
@@ -1619,32 +1619,34 @@
 erson.getId());%0A
+  
     graph.setUse
@@ -1665,32 +1665,34 @@
 dges(true);%0A    
+  
 int i = 1;%0A    f
@@ -1682,24 +1682,26 @@
  int i = 1;%0A
+  
     for (Edg
@@ -1742,24 +1742,26 @@
 ion.OUT)) %7B%0A
+  
       assert
@@ -1821,31 +1821,50 @@
 , i++);%0A    
-%7D%0A%0A
+  %7D%0A    %7D finally %7B%0A 
     
+ 
 graph.shutdo
@@ -1864,24 +1864,30 @@
 .shutdown();
+%0A    %7D
 %0A%0A  %7D%0A%0A  @Te
@@ -1927,17 +1927,16 @@
 ion() %7B%0A
-%0A
     Orie
@@ -1936,32 +1936,89 @@
    OrientVertex 
+loadedPerson;%0A    List%3CODocument%3E edges;%0A    try %7B%0A      
 loadedPerson = g
@@ -2053,24 +2053,26 @@
 tId());%0A    
+ 
+ 
 graph.setUse
@@ -2095,24 +2095,26 @@
 (true);%0A    
+  
 int i = 1;%0A 
@@ -2112,39 +2112,25 @@
  i = 1;%0A    
-List%3CODocument%3E
+ 
  edges = loa
@@ -2168,24 +2168,26 @@
 t_Knows%22);%0A%0A
+  
     ODocumen
@@ -2214,24 +2214,26 @@
 ove(9);%0A    
+  
 edges.add(ed
@@ -2239,17 +2239,34 @@
 dge10);%0A
-%0A
+    %7D finally %7B%0A  
     grap
@@ -2278,16 +2278,22 @@
 tdown();
+%0A    %7D
 %0A%0A    gr
@@ -2315,32 +2315,44 @@
 tGraph(DB_URL);%0A
+    try %7B%0A  
     graph.setUse
@@ -2371,24 +2371,26 @@
 dges(true);%0A
+  
     loadedPe
@@ -2433,24 +2433,26 @@
 tId());%0A    
+  
 edges = load
@@ -2488,24 +2488,26 @@
 ut_Knows%22);%0A
+  
     assertEq
@@ -2568,16 +2568,18 @@
 ), 11);%0A
+  
     asse
@@ -2641,24 +2641,42 @@
 dex%22), 10);%0A
+    %7D finally %7B%0A  
     graph.sh
@@ -2685,14 +2685,20 @@
 down();%0A
+    %7D%0A
   %7D%0A%7D%0A

add try finally on OrderedEdgesGraphTest for avoid dirty thread local in case of failure