package it.unibz.inf.ontop.owlapi;

/*
 * #%L
 * ontop-quest-owlapi
 * %%
 * 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 it.unibz.inf.ontop.injection.OntopSQLOWLAPIConfiguration;
import it.unibz.inf.ontop.owlapi.impl.SimpleOntopOWLEngine;

import it.unibz.inf.ontop.owlapi.impl.SimpleOntopOWLEngine.InvalidOBDASpecificationException;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.sql.Connection;
import java.sql.DriverManager;

import static it.unibz.inf.ontop.utils.OWLAPITestingTools.executeFromFile;


/**
 * This test is adapted from SimpleMappingVirtualABoxTest.
 *
 * A simple test that check if the system is able to handle Mappings for
 * classes/roles and attributes even if there are no URI templates. i.e., the
 * database stores URI's directly.
 *
 * We are going to create an H2 DB, the .sql file is fixed. We will map directly
 * there and then query on top.
 */
public class MetaMappingVirtualABoxMissingColumnTest {

	private Connection conn;

	private static final String owlfile = "src/test/resources/test/metamapping.owl";
	private static final String obdafile = "src/test/resources/test/metamapping_broken.obda";

	private static final String url = "jdbc:h2:mem:questjunitdb2_broken;DATABASE_TO_UPPER=FALSE";
	private static final String username = "sa";
	private static final String password = "";

	@Before
    public void setUp() throws Exception {
		conn = DriverManager.getConnection(url, username, password);
		executeFromFile(conn, "src/test/resources/test/metamapping-create-h2.sql");
	}

	@After
    public void tearDown() throws Exception {
		executeFromFile(conn, "src/test/resources/test/metamapping-drop-h2.sql");
		conn.close();
	}


    @Rule
    public ExpectedException expectedEx = ExpectedException.none();


    @Test
	public void testViEqSig() throws Exception {

        expectedEx.expect(InvalidOBDASpecificationException.class);
        expectedEx.expectMessage("The placeholder(s) code1 in the target do(es) not occur in source query of the mapping assertion");

		OntopSQLOWLAPIConfiguration config = OntopSQLOWLAPIConfiguration.defaultBuilder()
				.nativeOntopMappingFile(obdafile)
				.ontologyFile(owlfile)
				.jdbcUrl(url)
				.jdbcUser(username)
				.jdbcPassword(password)
				.enableTestMode()
				.build();

		//noinspection EmptyTryBlock
		try (OntopOWLEngine reasoner = new SimpleOntopOWLEngine(config)) {
		}
	}


}
