/*
 * 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$
 *
 */
// https://issues.jboss.org/browse/TEIID-2047 (only the test class was taken from the issue)
// https://issues.jboss.org/browse/TEIID-5046
// https://issues.jboss.org/browse/TEIID-5106
// https://issues.redhat.com/browse/TEIID-3901
package org.teiid.metadata;

import static org.junit.jupiter.api.Assertions.*;

import java.util.HashMap;

import org.junit.jupiter.api.Test;
import org.teiid.CommandContext;
import org.teiid.UserDefinedAggregate;
import org.teiid.adminapi.impl.ModelMetaData;

@SuppressWarnings("nls")
public class TestMetadataFactory {

	@Test public void testCreateFunction() throws NoSuchMethodException, SecurityException {
		FunctionMethod fm = MetadataFactory.createFunctionFromMethod("x", TestMetadataFactory.class.getMethod("someFunction"));
		assertEquals(Boolean.class, fm.getReturnParameter().getJavaType());

		fm = MetadataFactory.createFunctionFromMethod("x", TestMetadataFactory.class.getMethod("someArrayFunction"));
		assertEquals(String[].class, fm.getReturnParameter().getJavaType());
	}

	// TODO uncomment the int test after implementation TEIID-2047 and huge TEIID-1280 issues
	/*@Test
	public void testMergeFrom() {
		Schema s = new Schema();
		s.setName("schema-name");
		s.setUUID("uuid");
		s.setPhysical(false);
		s.setVisible(true);
		
		MetadataFactory mf = new MetadataFactory("vdb-name", 1, "model-name", null, null, null);
		mf.mergeFrom(s);
		
		assertEquals("schema-name", mf.getName());
		assertEquals("uuid", mf.getUUID());
		assertEquals(false, mf.isPhysical());
		assertEquals(true, mf.isVisible());
	}*/

	@Test public void testCorrectName() {
		ModelMetaData mmd = new ModelMetaData();
		mmd.setName("foo");
		HashMap<String, Datatype> types = new HashMap<String, Datatype>();
		Datatype value = new Datatype();
		value.setName("string");
		types.put("string", value);
		MetadataFactory factory = new MetadataFactory("x", 1, types, mmd);
		Table x = factory.addTable("x");
		Column c = factory.addColumn("a.b", "string", x);
		assertEquals("a_b", c.getName());
	}

	@Test public void testDuplicateColumns() {
		ModelMetaData mmd = new ModelMetaData();
		mmd.setName("foo");
		mmd.addProperty("importer.renameDuplicateColumns", "true");
		HashMap<String, Datatype> types = new HashMap<String, Datatype>();
		Datatype value = new Datatype();
		value.setName("string");
		types.put("string", value);
		MetadataFactory factory = new MetadataFactory("x", 1, types, mmd);
		Table x = factory.addTable("x");
		Column c = factory.addColumn("a_b", "string", x);
		assertEquals("a_b", c.getName());
		c = factory.addColumn("a_B", "string", x);
		assertEquals("a_B_1", c.getName());
	}
	
	
	 @Test public void testDuplicateTables() {
		ModelMetaData mmd = new ModelMetaData();
        mmd.setName("foo");
        mmd.addProperty("importer.renameDuplicateTables", "true");
        HashMap<String, Datatype> types = new HashMap<String, Datatype>();
        Datatype value = new Datatype();
        value.setName("string");
        types.put("string", value);
        MetadataFactory factory = new MetadataFactory("x", 1, types, mmd);
        Table x = factory.addTable("x");
        assertEquals("x", x.getName());
        Table x1 = factory.addTable("X");
        assertEquals("X_1", x1.getName());
	    Table x2 = factory.addTable("X");
        assertEquals("X_2", x2.getName());
	}

	@Test public void testCreateAggregateFunction() throws NoSuchMethodException, SecurityException {
		FunctionMethod fm = MetadataFactory.createFunctionFromMethod("x", MyUDAF.class.getMethod("addInput", String.class));
		//assertEquals(Boolean.class, fm.getOutputParameter().getJavaType());
		assertEquals("boolean", fm.getReturnParameter().getType());
		assertNotNull(fm.getAggregateAttributes());
	}

	public static boolean someFunction() {
		return true;
	}

	public static String[] someArrayFunction() {
		return null;
	}

	public static class MyUDAF implements UserDefinedAggregate<Boolean> {
		@Override
		public Boolean getResult(CommandContext commandContext) {
			return null;
		}

		@Override
		public void reset() {

		}

		public void addInput(String val) {

		}
	}

}
