/*
 *  ProActive Parallel Suite(TM): The Java(TM) library for
 *     Parallel, Distributed, Multi-Core Computing for
 *     Enterprise Grids & Clouds
 *
 *  Copyright (C) 1997-2016 INRIA/University of
 *                  Nice-Sophia Antipolis/ActiveEon
 *  Contact: proactive@ow2.org or contact@activeeon.com
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Affero General Public License
 *  as published by the Free Software Foundation; version 3 of
 *  the License.
 *
 *  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
 *  Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 *
 *  If needed, contact us to obtain a release under GPL Version 2 or 3
 *  or a different license than the AGPL.
 *
 *  Initial developer(s):               The ProActive Team
 *                          http://proactive.inria.fr/team_members.htm
 */
package org.ow2.proactive.workflow_catalog.rest.query;

import org.ow2.proactive.workflow_catalog.rest.query.parser.WorkflowCatalogQueryLanguageParser;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.junit.Test;
import org.mockito.Mockito;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 * Unit and functional tests associated to {@link WorkflowCatalogQueryLanguageListener}.
 *
 * @author ActiveEon Team
 * @see WorkflowCatalogQueryCompiler
 */
public class WorkflowCatalogQueryLanguageListenerTest {

    @Test
    public void testInitialization() {
        WorkflowCatalogQueryLanguageListener listener =
                new WorkflowCatalogQueryLanguageListener();

        assertThat(listener.atomicClausesToFuncMap).isNotEmpty();
        assertThat(listener.keyValueClausesToFuncMap).isNotEmpty();

        assertThat(listener.stackOfClauses).isEmpty();
        assertThat(listener.stackOfContexts).isEmpty();
        assertThat(listener.stackOfSubQueries).isEmpty();

        assertThat(listener.booleanExpression).isNull();
    }

    /**
     * Walk on a Tree representation of a valid WCQL query
     */
    @Test
    public void testWalkOnValidQuery() throws SyntaxException {
        WorkflowCatalogQueryCompiler queryCompiler =
                new WorkflowCatalogQueryCompiler();

        String query = "generic_information(\"Infrastructure\", \"Amazon EC2\") " +
                "AND generic_information(\"Type\", \"Public\") "
                + "AND variable(\"CPU\", \"*\") OR generic_information(\"Cloud\", \"Amazon EC2\") "
                + "AND variable(\"CPU\", \"*\") OR name=\"Amazon\"";

        WorkflowCatalogQueryLanguageParser.StartContext context =
                queryCompiler.compile(query);

        ParseTreeWalker walker = new ParseTreeWalker();

        WorkflowCatalogQueryLanguageListener listener =
                new WorkflowCatalogQueryLanguageListener();

        listener = Mockito.spy(listener);

        walker.walk(listener, context);

        verify(listener).enterStart(Mockito.any());
        verify(listener, times(3)).enterAndExpression(Mockito.any());
        verify(listener, times(2)).enterOrExpression(Mockito.any());
        verify(listener, times(1)).enterAtomicClause(Mockito.any());
        verify(listener, times(5)).enterKeyValueClause(Mockito.any());

        verify(listener).exitStart(Mockito.any());
        verify(listener, times(3)).exitAndExpression(Mockito.any());
        verify(listener, times(2)).exitOrExpression(Mockito.any());
        verify(listener, times(1)).exitAtomicClause(Mockito.any());
        verify(listener, times(5)).exitKeyValueClause(Mockito.any());

        assertThat(listener.stackOfClauses).isEmpty();
        assertThat(listener.stackOfContexts).isEmpty();
        assertThat(listener.stackOfSubQueries).isEmpty();

        assertThat(listener.booleanExpression).isNotNull();
    }

}
@@ -1458,24 +1458,49 @@
 TreeWalker;%0A
+import org.junit.Before;%0A
 import org.j
@@ -1537,16 +1537,79 @@
 Mockito;
+%0Aimport org.springframework.beans.factory.annotation.Autowired;
 %0A%0Aimport
@@ -1658,16 +1658,61 @@
 rtThat;%0A
+import static org.junit.Assert.assertEquals;%0A
 import s
@@ -2015,169 +2015,220 @@
     
-@Test%0A    public void testInitialization() %7B%0A        WorkflowCatalogQueryLanguageListener listener =%0A                new WorkflowCatalogQueryLanguageListener();%0A
+private WorkflowCatalogQueryLanguageListener listener;%0A%0A    @Before%0A    public void setUp() %7B%0A        listener = new WorkflowCatalogQueryLanguageListener();%0A    %7D%0A%0A    @Test%0A    public void testInitialization() %7B
 %0A   
@@ -3341,125 +3341,8 @@
 );%0A%0A
-        WorkflowCatalogQueryLanguageListener listener =%0A                new WorkflowCatalogQueryLanguageListener();%0A%0A
     
@@ -4317,9 +4317,474 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testSanitizeLiteral() %7B%0A        assertEquals(%22CPU%22, listener.sanitizeLiteral(%22%5C%22CPU%5C%22%22));%0A        assertEquals(%22%25PU%22, listener.sanitizeLiteral(%22%5C%22*PU%5C%22%22));%0A        assertEquals(%22CP%25%22, listener.sanitizeLiteral(%22%5C%22CP*%5C%22%22));%0A        assertEquals(%22%25P%25%22, listener.sanitizeLiteral(%22%5C%22*P*%5C%22%22));%0A        assertEquals(%22CPU*%22, listener.sanitizeLiteral(%22%5C%22CPU%5C%5C%5C%5C*%5C%22%22));%0A        assertEquals(%22CPU%5C%5C%5C%5C%25%22, listener.sanitizeLiteral(%22%5C%22CPU%25%5C%22%22));%0A    %7D%0A%0A
 %7D

Add unit tests for the sanitizeLiteral method