/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 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 CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.check;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import de.hft.stuttgart.citydoctor2.CityDoctorValidation;
import de.hft.stuttgart.citydoctor2.datastructure.Building;
import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel;
import de.hft.stuttgart.citydoctor2.exceptions.CityDoctorWriteException;
import de.hft.stuttgart.citydoctor2.parser.CityGmlParseException;
import de.hft.stuttgart.citydoctor2.parser.CityGmlParser;
import de.hft.stuttgart.citydoctor2.parser.InvalidGmlFileException;

/**
 * 
 * @author Matthias Betz
 *
 */
public class CheckerTest {

	@Rule
	public TemporaryFolder folder = new TemporaryFolder();

	@Test
	public void testSchematron() throws CityGmlParseException, InvalidGmlFileException {
		ValidationConfiguration config = ValidationConfiguration.loadStandardValidationConfig();
		config.getRequirements().get(Requirement.R_SE_BS_ROOF_UNFRAGMENTED.toString()).setEnabled(false);
		config.setSchematronFilePathInGlobalParameters("src/test/resources/schematronTest.xml");
		CityDoctorModel model = CityGmlParser.parseCityGmlFile(
				"src/test/resources/SimpleSolid_SrefBS_SchematronTest.gml", config.getParserConfiguration());
		Checker checker = new Checker(config, model);
		checker.runChecks();
		for (Building b : model.getBuildings()) {
			if (b.getGmlId().getGmlString().equals("_Simple_BD.1")) {
				assertTrue(b.containsAnyError());
			} else {
				assertFalse(b.containsAnyError());
			}
		}
		assertFalse(model.getGlobalErrors().isEmpty());
	}

	@Test
	public void testChecker() throws CityGmlParseException, IOException, InvalidGmlFileException, CityDoctorWriteException {

		File f = folder.newFile();
		File f2 = folder.newFile();
		try {
			String[] args = new String[6];
			args[0] = "-in";
			args[1] = "src/test/resources/QA-CS-CONCOMP.gml";
			args[2] = "-xmlReport";
			args[3] = f.getAbsolutePath();
			args[4] = "-pdfReport";
			args[5] = f2.getAbsolutePath();
			CityDoctorValidation.main(args);
			assertTrue(f.exists());
			assertTrue(f2.exists());
		} finally {
			f.delete();
			f2.delete();
		}
	}

	@Test
	public void testStreaming() throws CityGmlParseException, IOException, InvalidGmlFileException, CityDoctorWriteException {
		File f = folder.newFile();
		File f2 = folder.newFile();
		File f3 = folder.newFile();
		try {
			String[] args = new String[10];
			args[0] = "-in";
			args[1] = "src/test/resources/testarea.gml";
			args[2] = "-config";
			args[3] = "src/test/resources/testConfigWithStreaming.yml";
			args[4] = "-pdfReport";
			args[5] = f.getAbsolutePath();
			args[6] = "-xmlReport";
			args[7] = f2.getAbsolutePath();
			args[8] = "-out";
			args[9] = f3.getAbsolutePath();
			CityDoctorValidation.main(args);
			assertTrue(f.exists());
			assertTrue(f2.exists());
			assertTrue(f3.exists());
		} finally {
			f.delete();
			f2.delete();
			f3.delete();
		}
	}
}
