/*
 * Stratio Meta
 *
 * Copyright (c) 2014, Stratio, All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 */

package com.stratio.meta.core.grammar.statements;

import com.stratio.meta.core.grammar.ParsingTest;
import org.testng.annotations.Test;

public class CreateIndexStatementTest extends ParsingTest{

    // CREATE <type_index>? INDEX (IF NOT EXISTS)? <identifier>? ON <tablename> '(' <identifier> (',' <identifier>)* ')'
    // ( USING <string> )? WITH OPTIONS? (<maps> AND <maps>...) ';'
    //DEFAULT → Usual inverted index, Hash index. (By default).
    //LUCENE → Full text index.
    //CUSTOM → custom index. (new feature for release 2)

    @Test
    public void createIndexDefaultBasic() {
        String inputText = "CREATE DEFAULT INDEX index1 ON table1 (field1, field2);";
        testRegularStatement(inputText, "createIndexDefaultBasic");
    }


    @Test
    public void createIndexDefaultIfNotExist() {
        String inputText = "CREATE DEFAULT INDEX IF NOT EXISTS index1 ON table1 (field1, field2);";
        testRegularStatement(inputText, "createIndexDefaultIfNotExist");
    }

    @Test
    public void createIndexDefaultUsing() {
        String inputText = "CREATE DEFAULT INDEX index1 ON table1 (field1, field2) USING com.company.Index.class;";
        testRegularStatement(inputText, "createIndexDefaultUsing");
    }

    @Test
    public void createIndexLucene() {
        String inputText = "CREATE LUCENE INDEX demo_banks ON banks"
                + " (day, entry_id, latitude, longitude, name, address, tags)"
                + " USING \'org.apache.cassandra.db.index.stratio.RowIndex\'"
                + " WITH OPTIONS = {\'schema\': "
                + "\'{default_analyzer:\"org.apache.lucene.analysis.standard.StandardAnalyzer\","
                + "fields:"
                + "{day:{type:\"date\", pattern:\"yyyy-MM-dd\"},"
                + " entry_id:{type:\"uuid\"}, latitude:{type:\"double\"},"
                + " longitude:{type:\"double\"}, name:{type:\"text\"},"
                + " address:{type:\"string\"}, tags:{type:\"boolean\"}}}\'};";
        String expectedText = inputText.replace("INDEX demo_banks ON", "INDEX stratio_lucene_demo_banks ON");
        testRegularStatement(inputText, expectedText, "createIndexLucene");
    }

    @Test
    public void createIndexDefaultAll() {
        String inputText = "CREATE DEFAULT INDEX IF NOT EXISTS index1 "
                + "ON table1 (field1, field2) USING com.company.Index.class "
                + "WITH OPTIONS = {'key1': 'val1'};";
        testRegularStatement(inputText, "createIndexDefaultAll");
    }

    @Test
    public void createIndexWithOptions2() {
        //for (String t: new String[]{ "CUSTOM", "DEFAULT", "LUCENE" }){
        for (String t: new String[]{ "DEFAULT", "LUCENE" }){
                String inputText = "CREATE " + t + " INDEX IF NOT EXISTS index1 "
                    + "ON table1 (field1, field2) USING com.company.Index.class "
                    + "WITH OPTIONS = {'key1': 'val1', 'key2': 'val2'};";
                testRegularStatement(inputText, "createIndexWithOptions2");
        }
    }

    @Test
    public void createIndexWrongOptionAssignment(){
        String inputText = "CREATE LUCENE INDEX index1 ON table1 (field1, field2) WITH OPTIONS opt1:val1;";
        testRecoverableError(inputText, "createIndexWrongOptionAssignment");
    }

    @Test
    public void createIndexDefaultBasicWithSpaceBeforeSemicolon() {
        String inputText = "CREATE DEFAULT INDEX index1 ON table1 (field1; field2);";
        testRecoverableError(inputText, "createIndexDefaultBasicWithSpaceBeforeSemicolon");
    }

}
@@ -3233,24 +3233,31 @@
  void create
+Default
 IndexWithOpt
@@ -3278,150 +3278,387 @@
     
-//for (String t: new String%5B%5D%7B %22CUSTOM%22, %22DEFAULT%22, %22LUCENE%22 %7D)%7B%0A        for (String t: new String%5B%5D%7B %22DEFAULT%22, %22LUCENE%22 %7D)%7B%0A        
+        String inputText = %22CREATE DEFAULT INDEX IF NOT EXISTS index1 %22%0A                    + %22ON table1 (field1, field2) USING com.company.Index.class %22%0A                    + %22WITH OPTIONS = %7B'key1': 'val1', 'key2': 'val2'%7D;%22;%0A                testRegularStatement(inputText, %22createIndexWithOptions2%22);%0A    %7D%0A%0A    @Test%0A    public void createLuceneIndexWithOptions2() %7B%0A
         
 Stri
@@ -3645,32 +3645,36 @@
 ns2() %7B%0A        
+    
 String inputText
@@ -3688,17 +3688,14 @@
 ATE 
-%22 + t + %22
+LUCENE
  IND
@@ -3872,32 +3872,116 @@
  'val2'%7D;%22;%0A    
+        String expectedTest = inputText.replace(%22index1%22, %22stratio_lucene_index1%22);%0A
             test
@@ -3999,32 +3999,46 @@
 ement(inputText,
+ expectedTest,
  %22createIndexWit
@@ -4058,19 +4058,10 @@
     
-    %7D%0A    
 %7D
+%0A
 %0A%0A  

Fix create index test