// Path: connector-elasticsearch/src/main/java/com/stratio/connector/elasticsearch/core/engine/query/functions/ESFunction.java
// public abstract class ESFunction {
// 
//     public static final String CONTAINS = "contains";
//     public static final String MATCH_PHRASE = "match_phrase";
//     public static final String MULTI_MATCH = "multi_match";
//     public static final String MATCH_PREFIX = "match_prefix";
//     public static final String MULTI_MATCH_FUZZY = "multi_match_fuzzy";
//     public static final String FUZZY = "fuzzy";
// 
// 
//     /**
//      * Function Name
//      */
//     private String name;
// 
//     /**
//      * The function parameters.
//      */
//     private List<Selector> parameters;
// 
//     /**
//      * Default constructor.
//      *
//      * @param name Function Name
//      * @param paramareters function parameters.
//      */
//     protected ESFunction(String name, List<Selector> paramareters) {
//         this.parameters = paramareters;
//         this.name = name;
//     }
// 
//     public List<Selector> getParameters() {
//         return parameters;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     /**
//      * Build a {@link org.elasticsearch.index.query.QueryBuilder} that satisfy this function.
//      *
//      * @return
//      */
//     public abstract QueryBuilder buildQuery() throws ExecutionException;
// 
// 
//     /**
//      * Builds a ESFunction using the FunctionRelation specification.cd
//      * @param function
//      * @return A ESFunction child.
//      * @throws UnsupportedException is the FunctionRelation isn't supported.
//      */
//     public static ESFunction build(FunctionRelation function) throws UnsupportedException {
//         List parameters = new ArrayList();
//         parameters.addAll(function.getFunctionSelectors());
// 
//         switch(function.getFunctionName()){
//             case CONTAINS:
//                 return new Match(parameters);
//             case MATCH_PHRASE:
//                 return new MatchPhrase(parameters);
//             case MULTI_MATCH:
//                 return new MultiMatch(parameters);
//             case MATCH_PREFIX:
//                 return new MatchPrefix(parameters);
//             case MULTI_MATCH_FUZZY:
//                 return new MultiMatchFuzzy(parameters);
//             case FUZZY:
//                 return new Fuzzy(parameters);
//             default:
//                 throw new UnsupportedException("The function [" + function.getFunctionName() + "] is not supported");
//         }
//     }
// 
//     /**
//      * Checks function signature
//      *
//      * @param functionName                   function name
//      * @param checkEmpty                     flag that indicates if empty values are allowed
//      * @param expectedParameters      expected number of parameters
//      * @param expectedSignature         expected function signature
//      * @throws ExecutionException
//      */
//     public void validateFunctionSignature (String functionName, boolean checkEmpty, int expectedParameters, String expectedSignature)
//             throws ExecutionException {
// 
//         if (null == parameters || parameters.size() != expectedParameters){
//             throw new ExecutionException("Wrong number of parameters for \"" + functionName + "\" function. " +
//                     "Expected signature: " + expectedSignature + ".");
//         }
//         if (checkEmpty){
//             for (Selector parameter : parameters) {
//                 if (null == parameter || parameter.getStringValue().trim().isEmpty()){
//                     throw new ExecutionException("Every parameter in \"" + functionName + "\" function must have a non empty value.");
//                 }
//             }
//         }
//     }
// }


import com.stratio.connector.commons.engine.query.ProjectParsed;
import com.stratio.connector.elasticsearch.core.engine.query.functions.ESFunction;
import com.stratio.crossdata.common.data.ColumnName;
import com.stratio.crossdata.common.data.TableName;
import com.stratio.crossdata.common.exceptions.ExecutionException;
import com.stratio.crossdata.common.logicalplan.*;
import com.stratio.crossdata.common.metadata.Operations;
import com.stratio.crossdata.common.statements.structures.*;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.Client;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

            Disjunction ds = new Disjunction(Collections.singleton(Operations.FILTER_DISJUNCTION), terms);

            when(parsedQuery.getDisjunctionOfFunctionsList()).thenReturn(Collections.singleton(ds));

            // Mocks return fields property
            buildReturnFields(parsedQuery);

            SearchRequestBuilder requestBuilder = (SearchRequestBuilder) connectorQueryBuilder.buildQuery(client, parsedQuery);
            assertEquals(EXPECTED_QUERY, cleanJson(requestBuilder.toString()));
        }
        catch (Exception exception) {
            fail(exception.getMessage());
        }
    }

    public void buildReturnFields(ProjectParsed parsedQuery) {
        List<String> returnFields = new ArrayList<String>();
        returnFields.add(COLUMN_1);
        returnFields.add(COLUMN_2);
        addReturnFields(returnFields, parsedQuery);
    }

    public FunctionFilter buildFunctionContainsFilter(String columnName, String value) {
        List<Selector> parameters = new ArrayList<>();
        TableName tableName = new TableName("catalog", "table");
        parameters.add(new ColumnSelector(new ColumnName(tableName, columnName)));
        parameters.add(new StringSelector(value));
        parameters.add(new StringSelector("100%"));

// Path: connector-elasticsearch/src/main/java/com/stratio/connector/elasticsearch/core/engine/query/functions/ESFunction.java
// public abstract class ESFunction {
// 
//     public static final String CONTAINS = "contains";
//     public static final String MATCH_PHRASE = "match_phrase";
//     public static final String MULTI_MATCH = "multi_match";
//     public static final String MATCH_PREFIX = "match_prefix";
//     public static final String MULTI_MATCH_FUZZY = "multi_match_fuzzy";
//     public static final String FUZZY = "fuzzy";
// 
// 
//     /**
//      * Function Name
//      */
//     private String name;
// 
//     /**
//      * The function parameters.
//      */
//     private List<Selector> parameters;
// 
//     /**
//      * Default constructor.
//      *
//      * @param name Function Name
//      * @param paramareters function parameters.
//      */
//     protected ESFunction(String name, List<Selector> paramareters) {
//         this.parameters = paramareters;
//         this.name = name;
//     }
// 
//     public List<Selector> getParameters() {
//         return parameters;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     /**
//      * Build a {@link org.elasticsearch.index.query.QueryBuilder} that satisfy this function.
//      *
//      * @return
//      */
//     public abstract QueryBuilder buildQuery() throws ExecutionException;
// 
// 
//     /**
//      * Builds a ESFunction using the FunctionRelation specification.cd
//      * @param function
//      * @return A ESFunction child.
//      * @throws UnsupportedException is the FunctionRelation isn't supported.
//      */
//     public static ESFunction build(FunctionRelation function) throws UnsupportedException {
//         List parameters = new ArrayList();
//         parameters.addAll(function.getFunctionSelectors());
// 
//         switch(function.getFunctionName()){
//             case CONTAINS:
//                 return new Match(parameters);
//             case MATCH_PHRASE:
//                 return new MatchPhrase(parameters);
//             case MULTI_MATCH:
//                 return new MultiMatch(parameters);
//             case MATCH_PREFIX:
//                 return new MatchPrefix(parameters);
//             case MULTI_MATCH_FUZZY:
//                 return new MultiMatchFuzzy(parameters);
//             case FUZZY:
//                 return new Fuzzy(parameters);
//             default:
//                 throw new UnsupportedException("The function [" + function.getFunctionName() + "] is not supported");
//         }
//     }
// 
//     /**
//      * Checks function signature
//      *
//      * @param functionName                   function name
//      * @param checkEmpty                     flag that indicates if empty values are allowed
//      * @param expectedParameters      expected number of parameters
//      * @param expectedSignature         expected function signature
//      * @throws ExecutionException
//      */
//     public void validateFunctionSignature (String functionName, boolean checkEmpty, int expectedParameters, String expectedSignature)
//             throws ExecutionException {
// 
//         if (null == parameters || parameters.size() != expectedParameters){
//             throw new ExecutionException("Wrong number of parameters for \"" + functionName + "\" function. " +
//                     "Expected signature: " + expectedSignature + ".");
//         }
//         if (checkEmpty){
//             for (Selector parameter : parameters) {
//                 if (null == parameter || parameter.getStringValue().trim().isEmpty()){
//                     throw new ExecutionException("Every parameter in \"" + functionName + "\" function must have a non empty value.");
//                 }
//             }
//         }
//     }
// }


// Path: connector-elasticsearch/src/test/java/com/stratio/connector/elasticsearch/core/engine/query/ConnectorQueryBuilderTest.java
import com.stratio.connector.commons.engine.query.ProjectParsed;
import com.stratio.connector.elasticsearch.core.engine.query.functions.ESFunction;
import com.stratio.crossdata.common.data.ColumnName;
import com.stratio.crossdata.common.data.TableName;
import com.stratio.crossdata.common.exceptions.ExecutionException;
import com.stratio.crossdata.common.logicalplan.*;
import com.stratio.crossdata.common.metadata.Operations;
import com.stratio.crossdata.common.statements.structures.*;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.Client;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


            Disjunction ds = new Disjunction(Collections.singleton(Operations.FILTER_DISJUNCTION), terms);

            when(parsedQuery.getDisjunctionOfFunctionsList()).thenReturn(Collections.singleton(ds));

            // Mocks return fields property
            buildReturnFields(parsedQuery);

            SearchRequestBuilder requestBuilder = (SearchRequestBuilder) connectorQueryBuilder.buildQuery(client, parsedQuery);
            assertEquals(EXPECTED_QUERY, cleanJson(requestBuilder.toString()));
        }
        catch (Exception exception) {
            fail(exception.getMessage());
        }
    }

    public void buildReturnFields(ProjectParsed parsedQuery) {
        List<String> returnFields = new ArrayList<String>();
        returnFields.add(COLUMN_1);
        returnFields.add(COLUMN_2);
        addReturnFields(returnFields, parsedQuery);
    }

    public FunctionFilter buildFunctionContainsFilter(String columnName, String value) {
        List<Selector> parameters = new ArrayList<>();
        TableName tableName = new TableName("catalog", "table");
        parameters.add(new ColumnSelector(new ColumnName(tableName, columnName)));
        parameters.add(new StringSelector(value));
        parameters.add(new StringSelector("100%"));

        FunctionRelation function = new FunctionRelation(ESFunction.CONTAINS, parameters,tableName);