package io.searchbox.core;

import io.searchbox.action.Action;
import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
import io.searchbox.params.Parameters;
import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * @author Dogukan Sonmez
 */
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numDataNodes = 1)
public class ValidateIntegrationTest extends AbstractIntegrationTest {

    @Before
    public void setup() {
        createIndex("twitter");
    }

    @Test
    public void validateQueryWithIndex() throws IOException {
        Validate validate = new Validate.Builder("{\n" +
                "    \"query\" : {\n" +
                "  \"filtered\" : {\n" +
                "    \"query\" : {\n" +
                "      \"query_string\" : {\n" +
                "        \"query\" : \"*:*\"\n" +
                "      }\n" +
                "    },\n" +
                "    \"filter\" : {\n" +
                "      \"term\" : { \"user\" : \"kimchy\" }\n" +
                "    }\n" +
                "  }\n" +
                "    }\n" +
                "}")
                .index("twitter")
                .setParameter(Parameters.EXPLAIN, true)
                .build();
        executeTestCase(validate);
    }

    @Test
    public void validateQueryWithIndexAndType() throws IOException {
        executeTestCase(new Validate.Builder("{\n" +
                "    \"query\" : {\n" +
                "  \"filtered\" : {\n" +
                "    \"query\" : {\n" +
                "      \"query_string\" : {\n" +
                "        \"query\" : \"*:*\"\n" +
                "      }\n" +
                "    },\n" +
                "    \"filter\" : {\n" +
                "      \"term\" : { \"user\" : \"kimchy\" }\n" +
                "    }\n" +
                "  }\n" +
                "    }\n" +
                "}").index("twitter").type("tweet").build());
    }

    private void executeTestCase(Action action) throws RuntimeException, IOException {
        JestResult result = client.execute(action);
        assertTrue(result.getErrorMessage(), result.isSucceeded());
        assertTrue((Boolean) result.getValue("valid"));
    }
}

@@ -717,34 +717,32 @@
                %22
-  
   %5C%22query%5C%22 : %7B%5C
@@ -761,34 +761,32 @@
          %22  
-%5C%22filtered
+  %5C%22bool
 %5C%22 : %7B%5Cn%22 +%0A
@@ -798,39 +798,40 @@
            %22    
-%5C%22query
+  %5C%22must
 %5C%22 : %7B%5Cn%22 +%0A    
@@ -835,32 +835,34 @@
                %22
+  
       %5C%22query_st
@@ -894,32 +894,34 @@
        %22        
+  
 %5C%22query%5C%22 : %5C%22*:
@@ -938,32 +938,34 @@
                %22
+  
       %7D%5Cn%22 +%0A   
@@ -974,32 +974,34 @@
            %22    
+  
 %7D,%5Cn%22 +%0A        
@@ -1001,32 +1001,34 @@
                %22
+  
     %5C%22filter%5C%22 :
@@ -1050,32 +1050,34 @@
          %22      
+  
 %5C%22term%5C%22 : %7B %5C%22u
@@ -1094,32 +1094,62 @@
 kimchy%5C%22 %7D%5Cn%22 +%0A
+                %22      %7D%5Cn%22 +%0A
                 
@@ -1190,36 +1190,8 @@
 %22 +%0A
-                %22    %7D%5Cn%22 +%0A
     
@@ -1506,34 +1506,32 @@
                %22
-  
   %5C%22query%5C%22 : %7B%5C
@@ -1558,18 +1558,16 @@
  %22  
-%5C%22filtered
+  %5C%22bool
 %5C%22 :
@@ -1591,31 +1591,32 @@
        %22    
-%5C%22query
+  %5C%22must
 %5C%22 : %7B%5Cn%22 +%0A
@@ -1624,32 +1624,34 @@
                %22
+  
       %5C%22query_st
@@ -1683,32 +1683,34 @@
        %22        
+ 
+ 
 %5C%22query%5C%22 : %5C%22*:
@@ -1733,32 +1733,34 @@
          %22      
+  
 %7D%5Cn%22 +%0A         
@@ -1763,24 +1763,26 @@
            %22
+  
     %7D,%5Cn%22 +%0A
@@ -1798,24 +1798,26 @@
        %22    
+  
 %5C%22filter%5C%22 :
@@ -1833,32 +1833,34 @@
                %22
+  
       %5C%22term%5C%22 :
@@ -1916,33 +1916,9 @@
     
+ 
-%7D%5Cn%22 +%0A                %22 
  %7D%5Cn
@@ -1941,32 +1941,58 @@
     %22    %7D%5Cn%22 +%0A
+                %22  %7D%5Cn%22 +%0A
                 

Fix ValidateIntegrationTest