/*
 * 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.optimizer.xml;

import java.util.Collection;

import org.junit.jupiter.api.Test;
import org.teiid.api.exception.query.QueryParserException;
import org.teiid.query.optimizer.xml.ContextReplacerVisitor;
import org.teiid.query.parser.QueryParser;
import org.teiid.query.sql.lang.Criteria;
import org.teiid.query.sql.symbol.GroupSymbol;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 */
public class TestContextReplacerVisitor {

	public GroupSymbol exampleGroupSymbol(int number) {
		return new GroupSymbol("group." + number); //$NON-NLS-1$
	}
	
	public void helpTestReplacer(String critString, String expectedCrit, int numContexts) throws QueryParserException {
        Criteria crit = QueryParser.getQueryParser().parseCriteria(critString);
        
		Collection contexts = ContextReplacerVisitor.replaceContextFunctions(crit);
		assertTrue(expectedCrit.equalsIgnoreCase(crit.toString()), "Actual crit " +  crit + " didn't meet expected crit " + expectedCrit); //$NON-NLS-1$ //$NON-NLS-2$
        assertTrue(contexts.size() == numContexts, "Should've gotten " + numContexts + " but got " + contexts.size()); //$NON-NLS-1$ //$NON-NLS-2$
	}

	@Test
	public void testSimplePredicate() throws Exception {
        helpTestReplacer("context(a, x)='3'", "x = '3'", 1); //$NON-NLS-1$ //$NON-NLS-2$
	}

    /**
     * per 7332 - case-sensitivity bug
     */
    @Test
    public void testSimplePredicate2() throws Exception {
        helpTestReplacer("Context(a, x)='3'", "x = '3'", 1); //$NON-NLS-1$ //$NON-NLS-2$
    }

	@Test
    public void testConjunct() throws Exception {
        helpTestReplacer("context(a, x)='3' OR context(b, y)='j'", "(x = '3') or (y = 'j')", 2); //$NON-NLS-1$ //$NON-NLS-2$
    }

}
