/*
 * Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
 * license agreements.  See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.  Crate licenses
 * this file to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial agreement.
 */

package io.crate.lucene;

import com.google.common.collect.Sets;
import io.crate.analyze.WhereClause;
import io.crate.metadata.FunctionIdent;
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.Functions;
import io.crate.operation.operator.EqOperator;
import io.crate.operation.operator.InOperator;
import io.crate.operation.operator.OperatorModule;
import io.crate.planner.symbol.*;
import io.crate.types.DataType;
import io.crate.types.DataTypes;
import io.crate.types.SetType;
import org.apache.lucene.search.*;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.index.cache.IndexCache;
import org.elasticsearch.search.internal.SearchContext;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Answers;

import java.util.Arrays;

import static io.crate.testing.TestingHelpers.createFunction;
import static io.crate.testing.TestingHelpers.createReference;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

public class LuceneQueryBuilderTest {

    private LuceneQueryBuilder builder;

    @Before
    public void setUp() throws Exception {
        Functions functions = new ModulesBuilder()
                .add(new OperatorModule()).createInjector().getInstance(Functions.class);
        builder = new LuceneQueryBuilder(functions,
                mock(SearchContext.class, Answers.RETURNS_MOCKS.get()),
                mock(IndexCache.class, Answers.RETURNS_MOCKS.get()));
    }

    @Test
    public void testWhereRefEqRef() throws Exception {
        Reference foo = createReference("foo", DataTypes.STRING);
        Query query = convert(eq(foo, foo));
        assertThat(query, instanceOf(FilteredQuery.class));
    }


    @Test
    public void testWhereRefInSetLiteralIsConvertedToBooleanQuery() throws Exception {
        DataType dataType = new SetType(DataTypes.STRING);
        Reference foo = createReference("foo", DataTypes.STRING);
        WhereClause whereClause = new WhereClause(
                createFunction(InOperator.NAME, DataTypes.BOOLEAN,
                        foo,
                        Literal.newLiteral(dataType, Sets.newHashSet(new BytesRef("foo"), new BytesRef("bar")))
                ));
        Query query = convert(whereClause);
        assertThat(query, instanceOf(BooleanQuery.class));
        for (BooleanClause booleanClause : (BooleanQuery) query) {
            assertThat(booleanClause.getQuery(), instanceOf(TermQuery.class));
        }
    }


    private Query convert(WhereClause eq) {
        return builder.convert(eq).query;
    }

    private WhereClause eq(Symbol left, Symbol right) {
        return new WhereClause(new Function(new FunctionInfo(
                new FunctionIdent(EqOperator.NAME, Arrays.asList(left.valueType(), right.valueType())), DataTypes.BOOLEAN),
                Arrays.<Symbol>asList(left, right)
        ));
    }
}
@@ -1268,26 +1268,17 @@
 perator.
-EqOperator
+*
 ;%0Aimport
@@ -1291,87 +1291,112 @@
 ate.
-operation.operator.InOperator;%0Aimport io.crate.operation.operator.OperatorModul
+planner.symbol.Function;%0Aimport io.crate.planner.symbol.Literal;%0Aimport io.crate.planner.symbol.Referenc
 e;%0Ai
@@ -1425,17 +1425,22 @@
 .symbol.
-*
+Symbol
 ;%0Aimport
@@ -1659,16 +1659,80 @@
 uilder;%0A
+import org.elasticsearch.common.lucene.search.MatchNoDocsQuery;%0A
 import o
@@ -2104,16 +2104,56 @@
 anceOf;%0A
+import static org.hamcrest.Matchers.is;%0A
 import s
@@ -2701,24 +2701,217 @@
 )));%0A    %7D%0A%0A
+    @Test%0A    public void testNoMatchWhereClause() throws Exception %7B%0A        Query query = convert(WhereClause.NO_MATCH);%0A        assertThat(query, instanceOf(MatchNoDocsQuery.class));%0A    %7D%0A%0A
     @Test%0A  
@@ -3137,24 +3137,827 @@
 s));%0A    %7D%0A%0A
+    @Test%0A    public void testLteQuery() throws Exception %7B%0A        Query query = convert(new WhereClause(createFunction(LteOperator.NAME,%0A                DataTypes.BOOLEAN,%0A                createReference(%22x%22, DataTypes.INTEGER),%0A                Literal.newLiteral(10))));%0A        assertThat(query, instanceOf(NumericRangeQuery.class));%0A        assertThat(query.toString(), is(%22x:%7B* TO 10%5D%22));%0A    %7D%0A%0A    @Test%0A    public void testGteQuery() throws Exception %7B%0A        Query query = convert(new WhereClause(createFunction(GteOperator.NAME,%0A                DataTypes.BOOLEAN,%0A                createReference(%22x%22, DataTypes.INTEGER),%0A                Literal.newLiteral(10))));%0A        assertThat(query, instanceOf(NumericRangeQuery.class));%0A        assertThat(query.toString(), is(%22x:%5B10 TO *%7D%22));%0A    %7D%0A
 %0A    @Test%0A 

increase coverage