/**
* Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
* The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
* TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
* development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
*
* 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, including without
* limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
* PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
* unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
* any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
* License or out of the use or inability to use the Work.
* See the License for the specific language governing permissions and limitation under the License.
*/
package net.timbusproject.dpes.alternative.AlternativesBuilder;
import net.timbusproject.dpes.alternative.IntegrationTest;
import net.timbusproject.dpes.alternative.PreservationAlternative;
import net.timbusproject.dpes.alternative.Risk;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.sba_research.timbus.kb.PackageKnowledgeBase;
import org.sbaresearch.owl.JenaQueryFacade;
import org.sbaresearch.owl.OwlApiFacade;
import org.sbaresearch.owl.OwlElementNotFoundException;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
@Category(IntegrationTest.class)
public class PackageAlternativesBuilderTest {
public static final String KB_OWL_PATH = "http://timbus.teco.edu/ontologies/preservationIdentifier/virtualPackgeProviders-Ubuntu_12_10.owl";
public static final String BASE_URL = "http://localhost/test.owl";
public static final String MODEL_OWL_PATH = "http://timbus.teco.edu/ontologies/examples/MusicClassification/main.owl";
@Test
public void getAlternativePackages_packageWithAlternatives_shouldReturnPackages() throws OWLOntologyCreationException, OWLOntologyStorageException, IOException, OwlElementNotFoundException {
PackageKnowledgeBase knowledgeBase = createKnowledgeBase();
OwlApiFacade originalModelFacade = new OwlApiFacade(OWLManager.createOWLOntologyManager(), OWLManager.getOWLDataFactory());
originalModelFacade.load(MODEL_OWL_PATH);
String affectedResource = "oracle-java6-installer";
PreservationAlternativesBuilder builder = new PackageAlternativesBuilder(knowledgeBase, BASE_URL, originalModelFacade, affectedResource, 9999, false);
Risk risk = new Risk("riskid", "riskname", "riskdescription", affectedResource);
IRI individualAtRisk = originalModelFacade.findIndividualByFragmentAndLabel(risk.getAffectedResource());
List<PreservationAlternative> actual = builder.createAlternatives(risk, individualAtRisk);
assertThat(actual.size(), is(9));
}
@Test
public void getAlternativePackages_packageWithAlternativesUsingReasoner_shouldAddDependingPackages() throws OWLOntologyCreationException, OWLOntologyStorageException, IOException, OwlElementNotFoundException, URISyntaxException {
int limit = 1;
PackageKnowledgeBase knowledgeBase = createKnowledgeBase();
OwlApiFacade originalModelFacade = new OwlApiFacade(OWLManager.createOWLOntologyManager(), OWLManager.getOWLDataFactory());
originalModelFacade.load("file://" + getClass().getResource("/test/package_alternative_test.owl").getPath());
String affectedResource = "nano";
PreservationAlternativesBuilder builder = new PackageAlternativesBuilder(knowledgeBase, BASE_URL, originalModelFacade, affectedResource, limit, true);
Risk risk = new Risk("riskid", "riskname", "riskdescription", affectedResource);
IRI individualAtRisk = originalModelFacade.findIndividualByFragmentAndLabel(risk.getAffectedResource());
List<PreservationAlternative> actual = builder.createAlternatives(risk, individualAtRisk);
assertThat(actual.size(), is(limit));
OwlApiFacade modifiedModel = OwlApiFacade.create();
modifiedModel.load(IOUtils.toInputStream(actual.get(0).getModifiedContextModel()));
assertTrue(modifiedModel.containsIndividualByLabel("xjed:i386-common"));
}
@Test
public void getAlternativePackages_packageWithAlternativesUsingReasoner_shouldRemoveOrphanedPackages() throws OWLOntologyCreationException, OWLOntologyStorageException, IOException, OwlElementNotFoundException, URISyntaxException {
int limit = 1;
PackageKnowledgeBase knowledgeBase = createKnowledgeBase();
OwlApiFacade originalModelFacade = new OwlApiFacade(OWLManager.createOWLOntologyManager(), OWLManager.getOWLDataFactory());
originalModelFacade.load("file://" + getClass().getResource("/test/package_alternative_test2.owl").getPath());
String affectedResource = "nano";
PreservationAlternativesBuilder builder = new PackageAlternativesBuilder(knowledgeBase, BASE_URL, originalModelFacade, affectedResource, limit, true);
Risk risk = new Risk("riskid", "riskname", "riskdescription", affectedResource);
IRI individualAtRisk = originalModelFacade.findIndividualByFragmentAndLabel(risk.getAffectedResource());
List<PreservationAlternative> actual = builder.createAlternatives(risk, individualAtRisk);
assertThat(actual.size(), is(limit));
OwlApiFacade modifiedModel = OwlApiFacade.create();
modifiedModel.load(IOUtils.toInputStream(actual.get(0).getModifiedContextModel()));
assertFalse(modifiedModel.containsIndividualByLabel("nano-common"));
}
private PackageKnowledgeBase createKnowledgeBase() {
return new PackageKnowledgeBase(new JenaQueryFacade(JenaQueryFacade.getModelFromPath(KB_OWL_PATH, true)));
}
}