package it.unibz.krdb.sql;

/*
 * #%L
 * ontop-test
 * %%
 * Copyright (C) 2009 - 2014 Free University of Bozen-Bolzano
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import junit.framework.TestCase;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractConstraintTest extends TestCase {
	
	private DBMetadata metadata;
	
	private static final String TB_BOOK = "Book";
	private static final String TB_WRITER = "Writer";
	private static final String TB_EDITION = "Edition";
	private static final String TB_BOOKWRITER = "BookWriter";
	
	private static Logger log = LoggerFactory.getLogger(AbstractConstraintTest.class);
	
	public AbstractConstraintTest(String method) {
		super(method);
	}
	
	@Override
	public void setUp() {
		try {
			log.info(getConnectionString() + "\n");
			Connection conn = DriverManager.getConnection(getConnectionString(), getConnectionUsername(), getConnectionPassword());
			metadata = DBMetadataExtractor.getMetaData(conn, null);
		} 
		catch (SQLException e) { 
			e.printStackTrace();
		}
	}
	
	public void testPrimaryKey() {
		log.info("==== PRIMARY KEY ====");
		
		Collection<TableDefinition> tables = metadata.getTables();
		for (TableDefinition t : tables) {
			UniqueConstraint pkc =  t.getPrimaryKey();
			if (pkc != null) {
				Collection<Attribute> pk = pkc.getAttributes();
				if (checkName(t, TB_BOOK)) {
					assertEquals(1, pk.size());
				//} else if (checkName(t, TB_BOOKWRITER)) {
				//	assertEquals(0, pk.size());
				} else if (checkName(t, TB_EDITION)) {
					assertEquals(1, pk.size());
				} else if (checkName(t, TB_WRITER)) {
					assertEquals(1, pk.size());
				}
				writeLog(t.getName(), pk);
			}
			else
				assertTrue(checkName(t, TB_BOOKWRITER));
		}
		log.info("\n");
	}
	
	public void testForeignKey() {
		log.info("==== FOREIGN KEY ====");
		
		Collection<TableDefinition> tables = metadata.getTables();
		for (TableDefinition t : tables) {
			List<ForeignKeyConstraint> fk =  t.getForeignKeys();
			if (checkName(t, TB_BOOK)) {
				assertEquals(0, fk.size());
			} else if (checkName(t, TB_BOOKWRITER)) {
				assertEquals(2, fk.size());
			} else if (checkName(t, TB_EDITION)) {
				assertEquals(1, fk.size());
			} else if (checkName(t, TB_WRITER)) {
				assertEquals(0, fk.size());
			}
			writeLog(t.getName(), fk);
		}
		log.info("\n");
	}
	
	private boolean checkName(TableDefinition table, String value) {
		final String tableName = table.getName();
		return tableName.equalsIgnoreCase(value);
	}
	
	private void writeLog(String tableName, Object keys) {
		log.info(String.format("%s(%s)", tableName, keys.toString()));
	}
	
	protected abstract String getDriverName();
	protected abstract String getConnectionString();
	protected abstract String getConnectionUsername();
	protected abstract String getConnectionPassword();
}

@@ -1942,83 +1942,8 @@
 ();%0A
-%09%09%09if (pkc != null) %7B%0A%09%09%09%09Collection%3CAttribute%3E pk = pkc.getAttributes();%0A%09
 %09%09%09i
@@ -1966,33 +1966,32 @@
 TB_BOOK)) %7B%0A%09%09%09%09
-%09
 assertEquals(1, 
@@ -1984,32 +1984,49 @@
 sertEquals(1, pk
+c.getAttributes()
 .size());%0A%09%09%09%09//
@@ -2022,19 +2022,16 @@
 ());%0A%09%09%09
-%09//
 %7D else i
@@ -2071,41 +2071,34 @@
 %0A%09%09%09
-%09//
 %09assert
-Equals(0, pk.size()
+True(pkc == null
 );%0A
-%09
 %09%09%09%7D
@@ -2131,33 +2131,32 @@
 EDITION)) %7B%0A%09%09%09%09
-%09
 assertEquals(1, 
@@ -2149,32 +2149,49 @@
 sertEquals(1, pk
+c.getAttributes()
 .size());%0A%09%09%09%09%7D 
@@ -2180,25 +2180,24 @@
 ().size());%0A
-%09
 %09%09%09%7D else if
@@ -2229,17 +2229,16 @@
 ) %7B%0A%09%09%09%09
-%09
 assertEq
@@ -2251,109 +2251,104 @@
 , pk
-.size());%0A%09%09%09%09%7D%0A%09%09%09%09writeLog(t.getName(), pk);%0A%09%09%09%7D%0A%09%09%09else%0A%09%09%09%09assertTrue(checkName(t, TB_BOOKWRITER
+c.getAttributes().size());%0A%09%09%09%7D%0A%09%09%09if (pkc != null)%0A%09%09%09%09writeLog(t.getName(), pkc.getAttributes(
 ));%0A

test fix