/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library 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 2.1 of the License, or (at your option) any later version.
 * 
 * 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *  
 */

package org.teiid.query.sql.visitor;

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.teiid.api.exception.query.QueryParserException;
import org.teiid.query.parser.QueryParser;
import org.teiid.query.sql.lang.Command;
import org.teiid.query.sql.symbol.Expression;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestAggregateSymbolCollectorVisitor {

    public void helpTestCollectAggregates(String sql, String[] aggs, String[] elems) throws QueryParserException {
        // Parse command
        Command command = QueryParser.getQueryParser().parseCommand(sql);
        
        // Find aggregates
        List<Expression> foundAggs = new ArrayList<Expression>();
        List<Expression> foundElements = new ArrayList<Expression>();
        AggregateSymbolCollectorVisitor.getAggregates(command, foundAggs, foundElements, null, null, null);
        
        // Compare
        assertEquals(aggs.length, foundAggs.size(), "Incorrect number of aggregates: " + foundAggs); //$NON-NLS-1$
        for(int i=0; i<aggs.length; i++) {
            assertEquals(aggs[i], foundAggs.get(i).toString(), "Incorrect agg match at " + i); //$NON-NLS-1$
        }            

        assertEquals(elems.length, foundElements.size(), "Incorrect number of elements: " + foundElements); //$NON-NLS-1$
        for(int i=0; i<elems.length; i++) {
            assertEquals(elems[i], foundElements.get(i).toString(), "Incorrect agg match at " + i); //$NON-NLS-1$
        }            
                    
    }

    @Test
    public void testCollectAggs1() throws QueryParserException {
        helpTestCollectAggregates("SELECT COUNT(*) FROM pm1.g1", new String[] { "COUNT(*)"}, new String[] { }); //$NON-NLS-1$ //$NON-NLS-2$
    }

    @Test
    public void testCollectAggs2() throws QueryParserException {
        helpTestCollectAggregates("SELECT * FROM pm1.g1 HAVING MAX(e2) > 0",  //$NON-NLS-1$
            new String[] { "MAX(e2)"},  //$NON-NLS-1$
            new String[] { });
    }

    @Test
    public void testCollectAggs3() throws QueryParserException {
        helpTestCollectAggregates(
            "SELECT COUNT(e1), MAX(DISTINCT e1) FROM pm1.g1 GROUP BY e1 HAVING MAX(e2) > 0 AND NOT MIN(e2) < 100",  //$NON-NLS-1$
            new String[] { "COUNT(e1)", "MAX(DISTINCT e1)", "MAX(e2)", "MIN(e2)"},  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            new String[] { "e1"}); //$NON-NLS-1$ 
    }

    @Test
    public void testCollectAggs4() throws QueryParserException {
        helpTestCollectAggregates(
            "SELECT e1 FROM pm1.g1 GROUP BY e1 HAVING MAX(e2) > 0 AND NOT MIN(e2) < 100 AND e3 < 200",  //$NON-NLS-1$
            new String[] { "MAX(e2)", "MIN(e2)"},  //$NON-NLS-1$ //$NON-NLS-2$
            new String[] { "e1", "e1", "e3"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }


}
