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-orientdb.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("(:");

    List<String> resultMessages = new ArrayList<String>();

    for (ODocument document : result) {
      resultMessages.add(document.<String> field("message"));
    }

    //issue #1787, works fine locally, not on CI
//    Assert.assertEqualsNoOrder(messages.toArray(), resultMessages.toArray(),
//    "arrays are different: "+toString(messages)+" - "+toString(resultMessages) );
  }

  private String toString(List<String> resultMessages) {
    StringBuilder result = new StringBuilder();
    result.append("[");
    boolean first = true;
    for (String msg : resultMessages) {
      if (!first) {
        result.append(", ");
      }
      result.append("\"");
      result.append(msg);
      result.append("\"");
      first = false;
    }
    result.append("]");
    return result.toString();
  }

  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();

  }

  public void testIsClassName(){
    OrientGraph graph = new OrientGraph(database, false);
    graph.shutdown();
    database.open("admin", "admin");
    graph.createVertexType("Like").createProperty("anything", OType.STRING);
    graph.createVertexType("Is").createProperty("anything", OType.STRING);
  }

}

@@ -2073,22 +2073,22 @@
 %0A    %7D%0A%0A
+//
     
-//
 issue #1
@@ -2122,18 +2122,16 @@
 t on CI%0A
-//
     Asse
@@ -2199,18 +2199,16 @@
 rray(),%0A
-//
     %22arr

Enable test case for issue 1787