package com.orientechnologies.orient.test.database.auto;

import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import org.testng.Assert;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Andrey Lomakin (a.lomakin-at-orientechnologies.com)
 * @since 3/24/14
 */
@Test
public class SQLCreateVertexTest extends DocumentDBBaseTest {

  @Parameters(value = "url")
  public SQLCreateVertexTest(@Optional String url) {
    super(url);
  }

  public void testCreateVertexByContent() {
		OrientGraph graph = new OrientGraph(database, false);
		graph.shutdown();
		database.open("admin", "admin");

    OSchema schema = database.getMetadata().getSchema();
    if (!schema.existsClass("CreateVertexByContent")) {
      OClass vClass = schema.createClass("CreateVertexByContent", schema.getClass("V"));
      vClass.createProperty("message", OType.STRING);
    }

    database.command(new OCommandSQL("create vertex CreateVertexByContent content { \"message\": \"(:\"}")).execute();
    database.command(new OCommandSQL("create vertex CreateVertexByContent content { \"message\": \"\\\"‎ה, כן?...‎\\\"\"}")).execute();

    List<ODocument> result = database.query(new OSQLSynchQuery<ODocument>("select from CreateVertexByContent"));
    Assert.assertEquals(result.size(), 2);

		List<String> messages = new ArrayList<String>();
		messages.add("\"‎ה, כן?...‎\"");
		messages.add("(:");

		for (ODocument document : result) {
			Assert.assertTrue(messages.remove(document.<String>field("message")));
		}

		Assert.assertEquals(messages.size(), 0);
  }


  public void testCreateVertexBooleanProp() {
    OrientGraph graph = new OrientGraph(database, false);
    graph.shutdown();
    database.open("admin", "admin");


    database.command(new OCommandSQL("create vertex set script = true")).execute();
    database.command(new OCommandSQL("create vertex")).execute();
    database.command(new OCommandSQL("create vertex V")).execute();

    //TODO complete this!
//    database.command(new OCommandSQL("create vertex set")).execute();
//    database.command(new OCommandSQL("create vertex set set set = 1")).execute();



  }


}

@@ -995,18 +995,20 @@
 ent() %7B%0A
-%09%09
+    
 OrientGr
@@ -1053,18 +1053,20 @@
 false);%0A
-%09%09
+    
 graph.sh
@@ -1075,18 +1075,20 @@
 down();%0A
-%09%09
+    
 database
@@ -1615,24 +1615,33 @@
 .%E2%80%8E%5C%5C%5C%22%5C%22%7D%22))
+%0A        
 .execute();%0A
@@ -1798,18 +1798,20 @@
 ), 2);%0A%0A
-%09%09
+    
 List%3CStr
@@ -1851,18 +1851,20 @@
 ing%3E();%0A
-%09%09
+    
 messages
@@ -1888,18 +1888,20 @@
 .%E2%80%8E%5C%22%22);%0A
-%09%09
+    
 messages
@@ -1913,18 +1913,81 @@
 %22(:%22);%0A%0A
-%09%09
+%0A    List%3CString%3E resultMessages = new ArrayList%3CString%3E();%0A%0A    
 for (ODo
@@ -2018,38 +2018,29 @@
 ) %7B%0A
-%09%09%09Assert.assertTrue(m
+      resultM
 essages.
 remo
@@ -2039,14 +2039,11 @@
 ges.
-remove
+add
 (doc
@@ -2056,16 +2056,17 @@
 %3CString%3E
+ 
 field(%22m
@@ -2078,18 +2078,21 @@
 e%22))
-)
 ;%0A
-%09%09%7D%0A%0A%09%09
+    %7D%0A%0A    
 Asse
@@ -2106,16 +2106,23 @@
 rtEquals
+NoOrder
 (message
@@ -2127,25 +2127,51 @@
 ges.
-size(), 0
+toArray(), resultMessages.toArray()
 );%0A  %7D%0A%0A
 %0A  p
@@ -2166,17 +2166,16 @@
 );%0A  %7D%0A%0A
-%0A
   public
@@ -2326,25 +2326,24 @@
  %22admin%22);%0A%0A
-%0A
     database
@@ -2555,16 +2555,17 @@
 %0A%0A    //
+ 
 TODO com
@@ -2576,21 +2576,22 @@
 e this!%0A
-//
+ 
    
+//
  databas
@@ -2653,13 +2653,14 @@
 ();%0A
-//
+ 
    
+//
  dat
@@ -2739,14 +2739,11 @@
 );%0A%0A
-%0A%0A
   %7D%0A%0A
-%0A
 %7D%0A

test case on create vertex, more explicit error messages