package com.orientechnologies.orient.graph.sql;

import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;

public class TestDeleteEdge {

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

    for (int i = 0; i < 10; i++) {
      OrientVertex v1 = graph.addVertex("class:TestVertex");
      OrientVertex v2 = graph.addVertex("class:TestVertex");
      OrientVertex v3 = graph.addVertex("class:TestVertex");
      OrientVertex v4 = graph.addVertex("class:TestVertex");

      Map<String, Object> p1 = new HashMap<String, Object>();
      p1.put("based_on", "0001");
      OrientEdge e1 = v1.addEdge(null, v2, "TestEdge", null, p1);
      e1.save();

      Map<String, Object> p2 = new HashMap<String, Object>();
      p2.put("based_on", "0002");
      OrientEdge e2 = v3.addEdge(null, v4, "TestEdge", null, p2);
      e2.save();

      graph.commit();

      graph.command(new OCommandSQL("delete edge TestEdge where based_on = '0001'")).execute();

      Iterable<OrientVertex> edges = graph.command(new OCommandSQL("select count(*) from TestEdge where based_on = '0001'"))
          .execute();
      assertTrue(edges.iterator().hasNext());
      assertEquals(edges.iterator().next().getProperty("count"), 0l);
    }

    graph.shutdown();
  }
}

@@ -42,16 +42,80 @@
 h.sql;%0A%0A
+import com.orientechnologies.orient.core.metadata.schema.OType;%0A
 import c
@@ -221,16 +221,77 @@
 ntEdge;%0A
+import com.tinkerpop.blueprints.impls.orient.OrientEdgeType;%0A
 import c
@@ -399,16 +399,65 @@
 Vertex;%0A
+import org.junit.After;%0Aimport org.junit.Before;%0A
 import o
@@ -663,13 +663,83 @@
 %0A%0A  
-@Test
+private OrientGraph    graph;%0A  private OrientEdgeType edgeType;%0A%0A  @Before
 %0A  p
@@ -749,17 +749,22 @@
 ic void 
-t
+before
 () %7B%0A   
@@ -768,26 +768,8 @@
     
-final OrientGraph 
 grap
@@ -856,16 +856,209 @@
 admin%22);
+%0A    graph.createVertexType(%22TestVertex%22);%0A    edgeType = graph.createEdgeType(%22TestEdge%22);%0A  %7D%0A%0A  @After%0A  public void after() %7B%0A    graph.drop();%0A  %7D%0A%0A  @Test%0A  public void testDeleteEdge() %7B
 %0A%0A    fo
@@ -1939,19 +1939,8 @@
 '%22))
-%0A          
 .exe
@@ -2076,30 +2076,858 @@
 %0A%0A  
-  graph.shutdown();%0A  %7D
+%7D%0A%0A%0A  @Test%0A  public void testDeleteEdgeValidation() %7B%0A%0A    OrientVertex v1 = graph.addVertex(%22class:TestVertex%22);%0A    OrientVertex v2 = graph.addVertex(%22class:TestVertex%22);%0A%0A    Map%3CString, Object%3E p1 = new HashMap%3CString, Object%3E();%0A    p1.put(%22based_on%22, %220001%22);%0A    OrientEdge e1 = v1.addEdge(null, v2, %22TestEdge%22, null, p1);%0A    e1.save();%0A%0A    graph.commit();%0A    graph.getRawGraph().commit();%0A    edgeType.createProperty(%22mand%22, OType.STRING).setMandatory(true);%0A    graph.getRawGraph().begin();%0A%0A    graph.command(new OCommandSQL(%22delete edge TestEdge where based_on = '0001'%22)).execute();%0A%0A    Iterable%3COrientVertex%3E edges = graph.command(new OCommandSQL(%22select count(*) from TestEdge where based_on = '0001'%22)).execute();%0A    assertTrue(edges.iterator().hasNext());%0A    assertEquals(edges.iterator().next().getProperty(%22count%22), 0l);%0A%0A  %7D%0A
 %0A%7D%0A

test case add for delete not valid edge, issue #3281