package com.orientechnologies.distribution.integration;

import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import org.junit.Ignore;
import org.junit.Test;

import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

/**
 * Created by frank on 15/03/2017.
 */
public class ODemoDbConsistencyIT extends OIntegrationTestTemplate {

  @Test
  public void testMetadta() throws Exception {

    int hotelNumbers = 1154;
    int restaurantsNumbers = 1951;

    int casteNumbers = 127;
    int archSiteNumbers = 55;
    int monumentNumbers = 137;
    int theatreNumbers = 117;

    // total number of vertices
    //assertEquals(0, db.getMetadata().getSchema().getClass("V").count());

    // total number of edgex
    //assertEquals(0, db.getMetadata().getSchema().getClass("E").count());

    // vertex class existence
    OSchema schema = db.getMetadata().getSchema();
    assertThat(schema.getClass("V")).isNotNull();
    assertThat(schema.getClass("V").getSubclasses()).hasSize(14);

    assertThat(schema.getClass("Locations")).isNotNull();
    assertThat(schema.getClass("Locations").count()).isEqualTo(3541);

    assertThat(schema.getClass("Attractions")).isNotNull();
    assertThat(schema.getClass("Attractions").count()).isEqualTo(436);

    assertThat(schema.getClass("Castles")).isNotNull();
    assertThat(schema.getClass("Castles").count()).isEqualTo(casteNumbers);

    assertThat(schema.getClass("Theatres")).isNotNull();
    assertThat(schema.getClass("Theatres").count()).isEqualTo(theatreNumbers);

    assertThat(schema.getClass("Monuments")).isNotNull();
    assertThat(schema.getClass("Monuments").count()).isEqualTo(monumentNumbers);

    assertThat(schema.getClass("ArchaeologicalSites")).isNotNull();
    assertThat(schema.getClass("ArchaeologicalSites").count()).isEqualTo(archSiteNumbers);

    assertThat(schema.getClass("Services")).isNotNull();
    assertThat(schema.getClass("Services").count()).isEqualTo(3105);

    assertThat(schema.getClass("Hotels")).isNotNull();
    assertThat(schema.getClass("Hotels").count()).isEqualTo(hotelNumbers);

    assertThat(schema.getClass("Restaurants")).isNotNull();
    assertThat(schema.getClass("Restaurants").count()).isEqualTo(restaurantsNumbers);

    assertThat(schema.getClass("Profiles")).isNotNull();
    //assertEquals(0, db.getMetadata().getSchema().getClass("Profiles").count());

    assertThat(schema.getClass("Customers")).isNotNull();
    assertThat(schema.getClass("Customers").count()).isEqualTo(400);

    assertThat(schema.getClass("Orders")).isNotNull();
    //assertEquals(0, db.getMetadata().getSchema().getClass("Orders").count());

    assertThat(schema.getClass("Countries")).isNotNull();
    assertThat(schema.getClass("Countries").count()).isEqualTo(249);

    assertThat(schema.getClass("Reviews")).isNotNull();
    //assertEquals(0, db.getMetadata().getSchema().getClass("Reviews").count());

    // edge class existence
    assertThat(schema.getClass("E")).isNotNull();
    assertThat(schema.getClass("E").getSubclasses()).hasSize(10);

    assertThat(schema.getClass("HasCustomer")).isNotNull();
    assertThat(schema.getClass("HasVisited")).isNotNull();
    assertThat(schema.getClass("IsFromCountry")).isNotNull();
    assertThat(schema.getClass("HasStayed")).isNotNull();
    assertThat(schema.getClass("MadeReview")).isNotNull();
    assertThat(schema.getClass("HasReview")).isNotNull();
    assertThat(schema.getClass("HasEaten")).isNotNull();
    assertThat(schema.getClass("HasProfile")).isNotNull();
    assertThat(schema.getClass("HasFriend")).isNotNull();
    assertThat(schema.getClass("HasUsedService")).isNotNull();

    // Vertex inheritance
    // to do

    // Edge inheritance
    // method 1
    assertEquals("E", schema.getClass("HasEaten").getSuperClassesNames().get(0));
    assertEquals("HasUsedService", schema.getClass("HasEaten").getSuperClassesNames().get(1));
    assertEquals("E", schema.getClass("HasStayed").getSuperClassesNames().get(0));
    assertEquals("HasUsedService", schema.getClass("HasStayed").getSuperClassesNames().get(1));

    //method 2

    List<OResult> results = db.query("SELECT DISTINCT(@class) AS className from `HasUsedService` ORDER BY className ASC").stream()
        .collect(Collectors.toList());
    assertEquals(2, results.size());
    assertEquals("HasEaten", results.get(0).getProperty("className"));
    assertEquals("HasStayed", results.get(1).getProperty("className"));

    //method 3 - fails, to ask Luigi why
    //sqlQuery = "SELECT COUNT(*) as edgeCount, DISTINCT(@class) AS edgeClassName from `HasUsedService` GROUP BY edgeClassName ORDER BY edgeClassName ASC";
    //qResult = db.command(new OCommandSQL(sqlQuery)).execute();
    //assertEquals(2,qResult.size());
    //assertEquals( "HasEaten", qResult.get(0).field("className"));
    //assertEquals( "HasStayed", qResult.get(1).field("className"));

  }

  @Test
  @Ignore
  public void testMatchQueries() throws Exception {

    // MATCH QUERIES TESTS
    // all customers have a country
    OResultSet resultSet = db
        .query("MATCH {class: Customers, as: customer}-IsFromCountry->{class: Countries, as: country} RETURN customer");
    assertThat(resultSet).hasSize(400);

    // all customers have a profile
    resultSet = db.query("MATCH {class: Customers, as: customer}-HasProfile->{class: Profiles, as: profile} RETURN customer");
    assertThat(resultSet).hasSize(400);

    // all customers have at least 1 order
    resultSet = db.query("MATCH {class: Orders, as: order}-HasCustomer->{class: Customers, as:customer} RETURN order");
    assertThat(resultSet.stream().count()).isGreaterThan(60);

  }
}

@@ -602,16 +602,17 @@
 estMetad
+a
 ta() thr
@@ -623,24 +623,54 @@
 Exception %7B%0A
+%09%0A%09int customerNumber = 400;%0A%09
 %0A    int hot
@@ -846,167 +846,27 @@
 17;%0A
-%0A    // total number of vertices%0A    //assertEquals(0, db.getMetadata().getSchema().getClass(%22V%22).count());%0A%0A    // total number of edgex%0A    //assertEquals(0,
+%09%0A%09OSchema schema =
  db.
@@ -894,31 +894,8 @@
 ma()
-.getClass(%22E%22).count())
 ;%0A%0A 
@@ -926,59 +926,12 @@
 ence
-%0A
     
-OSchema schema = db.getMetadata().getSchema();
 %0A   
@@ -1042,16 +1042,73 @@
 ize(14);
+%0A%09//assertThat(schema.getClass(%22V%22).count()).isEqualTo();
 %0A%0A    as
@@ -2598,19 +2598,30 @@
 EqualTo(
-400
+customerNumber
 );%0A%0A    
@@ -3161,16 +3161,73 @@
 ize(10);
+%0A%09//assertThat(schema.getClass(%22E%22).count()).isEqualTo();
 %0A%0A    as
@@ -5136,35 +5136,8 @@
  %7B%0A%0A
-    // MATCH QUERIES TESTS%0A
     
@@ -5345,35 +5345,46 @@
 ultSet).hasSize(
-400
+customerNumber
 );%0A%0A    // all c
@@ -5568,19 +5568,30 @@
 hasSize(
-400
+customerNumber
 );%0A%0A    
@@ -5810,10 +5810,22 @@
 han(
-60
+customerNumber
 );%0A%0A

use customerNumber in ODemoDbConsistencyIT