* #%L
 * Alfresco Search Services
 * %%
 * Copyright (C) 2005 - 2020 Alfresco Software Limited
 * %%
 * This file is part of the Alfresco software. 
 * If the software was purchased under a paid Alfresco license, the terms of 
 * the paid license agreement will prevail.  Otherwise, the software is 
 * provided under the following open source license terms:
 * Alfresco 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 of the License, or
 * (at your option) any later version.
 * Alfresco is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
 * #L%

package org.alfresco.solr.query;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.LinkedList;

import org.alfresco.repo.search.impl.parsers.FTSQueryException;
import org.alfresco.repo.search.impl.parsers.FTSQueryParser;
import org.alfresco.service.cmr.search.SearchParameters;
import org.apache.lucene.analysis.tokenattributes.PackedTokenAttributeImpl;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.util.Version;
import org.apache.solr.request.SolrQueryRequest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public class Solr4QueryParserTest

    private Solr4QueryParser parser;
    private final static String TEST_FIELD = "creator";
    private final static String TEST_QUERY = "[email protected]";
    private SearchParameters searchParameters = new SearchParameters();
    public void setUp() throws Exception
        SolrQueryRequest req = Mockito.mock(SolrQueryRequest.class);
        parser = new Solr4QueryParser(req, Version.LATEST, "TEXT", null, FTSQueryParser.RerankPhase.SINGLE_PASS);

    private PackedTokenAttributeImpl getTokenAttribute(String text, int startOffset, int endOffset)
    	PackedTokenAttributeImpl token = new PackedTokenAttributeImpl();
    	token.setOffset(startOffset, endOffset);
    	return token;
    public void testGetSearchParameters()
    	assertEquals(searchParameters, parser.getSearchParameters());
    public void testSQLTranslationFailure() throws ParseException
    	    parser.getLikeQuery("example", "_%woof_\\" , null);
    	catch(FTSQueryException e)
    public void testFlatQueryShouldBeGeneratedFromSequentiallyShiftedTokens() throws Exception
        // prepare test data
        LinkedList<PackedTokenAttributeImpl> tokenSequenceWithRepeatedGroup = new LinkedList<PackedTokenAttributeImpl>();
        tokenSequenceWithRepeatedGroup.add(getTokenAttribute(TEST_QUERY.substring(0, 4), 0, 4));
        tokenSequenceWithRepeatedGroup.add(getTokenAttribute(TEST_QUERY.substring(5, 6), 5, 6));
        tokenSequenceWithRepeatedGroup.add(getTokenAttribute(TEST_QUERY.substring(6, 10), 6, 10));
        tokenSequenceWithRepeatedGroup.add(getTokenAttribute(TEST_QUERY.substring(10, 11), 10, 11));
        assertTrue("All tokens in test data must be sequentially shifted",
        LinkedList<LinkedList<PackedTokenAttributeImpl>> fixedTokenSequences = new LinkedList<LinkedList<PackedTokenAttributeImpl>>();
        // call method to test
        SpanQuery q = parser.generateSpanOrQuery(TEST_FIELD, fixedTokenSequences);
        // check results
        assertTrue(q instanceof SpanNearQuery);
        SpanNearQuery spanNearQuery = (SpanNearQuery) q;
        assertEquals("Slop between term must be 0", 0, spanNearQuery.getSlop());
        assertTrue("Terms must be in order", spanNearQuery.isInOrder());
        SpanQuery[] termClauses = spanNearQuery.getClauses();
        assertEquals("Flat query must be generated (Query: " + q + ")", tokenSequenceWithRepeatedGroup.size(), termClauses.length);
        for (int i = 0; i < termClauses.length; i++)
            assertTrue(termClauses[i] instanceof SpanTermQuery);
            assertEquals("All tokens must become spanQuery terms",
                    tokenSequenceWithRepeatedGroup.get(i).toString(), ((SpanTermQuery) termClauses[i]).getTerm().text());