/**
* 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 org.sbaresearch.owl;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.semanticweb.owlapi.model.*;
import java.io.ByteArrayInputStream;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;
@RunWith(JUnitParamsRunner.class)
public class AlternativesBuilderTest {
@Test
@Parameters({"IndivD, http://localhost/test_ab3.owl#IndivD"})
public void testFindIndividual_indivInBaseOntology_shouldReturnIRI(String indivName, String expectedIRI) throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
IRI iri = alternativesBuilder.findIndividualByFragmentAndLabel(indivName);
assertThat(iri.toString(), equalTo(expectedIRI));
}
@Test
@Parameters({
"IndivA, http://localhost/test_ab1.owl#IndivA",
"IndivB, http://localhost/test_ab2.owl#IndivB"
})
public void testFindIndividual_indivInImportedOntology_shouldReturnIRI(String indivName, String expectedIRI) throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
IRI iri = alternativesBuilder.findIndividualByFragmentAndLabel(indivName);
assertThat(iri.toString(), equalTo(expectedIRI));
}
@Test
@Parameters({
"IndivE, http://localhost/test_ab4_usingLabel.owl#bf342340921",
"IndivA, http://localhost/test_ab1_usingLabel.owl#ab1232323"
})
public void testFindIndividual_byLabel_shouldReturnIRI(String indivName, String expectedIRI) throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createAlternativesBuilderModelUsingLabel();
IRI actualIRI = alternativesBuilder.findIndividualByFragmentAndLabel(indivName);
assertThat(actualIRI.toString(), equalTo(expectedIRI));
}
@Test
public void testCopyOntology_validSource_shouldContainSameNumberOfAxioms() throws OWLOntologyCreationException, URISyntaxException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
OWLOntology actual = alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1"));
assertThat(actual.getAxiomCount(), is(alternativesBuilder.getModel().getOntology().getAxiomCount()));
}
@Test
public void testCopyOntology_validSource_shouldHaveNewIRI() throws OWLOntologyCreationException, URISyntaxException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
IRI targetIRI = IRI.create("http://localhost/abtest1");
OWLOntology actual = alternativesBuilder.copyOntology(targetIRI);
assertThat(actual.getOntologyID().getOntologyIRI(), equalTo(targetIRI));
}
@Test
public void testCopyOntology_validSourceExistingManager_shouldNotCreateNewManager() throws OWLOntologyCreationException, URISyntaxException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
IRI targetIRI = IRI.create("http://localhost/abtest1");
alternativesBuilder.copyOntology(targetIRI, alternativesBuilder.getModel().getOntology().getOWLOntologyManager());
assertTrue(alternativesBuilder.getModel().getOntology().containsIndividualInSignature(IRI.create("http://localhost/abtest1#IndivD")));
}
@Test
public void testCopyOntology_validSource_shouldCreateNewManager() throws OWLOntologyCreationException, URISyntaxException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
IRI targetIRI = IRI.create("http://localhost/abtest1");
alternativesBuilder.copyOntology(targetIRI);
assertFalse(alternativesBuilder.getModel().getOntology().containsIndividualInSignature(IRI.create("http://localhost/abtest1#IndivD")));
}
@Test
public void testCopyOntology_validSource_shouldRenameIndivs() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
final OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
Set<OWLNamedIndividual> actualIndivs = newOntology.getOntology().getIndividualsInSignature();
final Set<OWLNamedIndividual> expectedIndivs = new HashSet<OWLNamedIndividual>() {{
add(newOntology.getDataFactory().getOWLNamedIndividual(IRI.create("http://localhost/abtest1.owl#IndivD")));
}};
assertThat(expectedIndivs, containsInAnyOrder(actualIndivs.toArray()));
}
@Test
public void testCopyOntology_validSource_shouldRenameClasses() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
final OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
Set<OWLClass> actualClasses = newOntology.getOntology().getClassesInSignature();
final Set<OWLClass> expectedClasses = new HashSet<OWLClass>() {{
add(newOntology.getDataFactory().getOWLClass(IRI.create("http://localhost/abtest1.owl#ClassD")));
}};
assertThat(expectedClasses, containsInAnyOrder(actualClasses.toArray()));
}
@Test
public void testCopyOntology_validSourceWithoutImports_shouldContainNoImports() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
OwlApiFacade model = TestUtils.createDefaultOwlApiFacade();
model.load(TestUtils.getUri("/test_ab1.owl"));
AlternativesBuilder alternativesBuilder = new AlternativesBuilder(model);
OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
Set<IRI> actualImports = newOntology.getOntology().getDirectImportsDocuments();
assertThat(actualImports.size(), is(0));
}
@Test
public void testCopyOntology_validSourceWithImports_shouldContainAllImports() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
Set<IRI> expectedImports = alternativesBuilder.getModel().getOntology().getDirectImportsDocuments();
OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
Set<IRI> actualImports = newOntology.getOntology().getDirectImportsDocuments();
assertThat(actualImports, containsInAnyOrder(expectedImports.toArray()));
}
@Test
public void testRenameIndividualByFragment_validFragment_shouldRenameIndividual() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
alternativesBuilder.renameIndividualByIRI(alternativesBuilder.getModel().createIri("IndivD"), "xIndivD");
assertThat(alternativesBuilder.getModel().getIndividualByUnqualifiedName("xIndivD", false).getIRI().toString(), equalTo("http://localhost/test_ab3.owl#xIndivD"));
}
@Test (expected = OwlElementNotFoundException.class)
public void testRenameIndividualByFragment_inexistingFragment_shouldThrowException() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
alternativesBuilder.renameIndividualByIRI(alternativesBuilder.getModel().createIri("yIndivD"), "xIndivD");
}
@Test
public void testRenameIndividualByFragment_indivHasLabel_shouldRenameLabelAlso() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createAlternativesBuilderModelUsingLabel();
alternativesBuilder.renameIndividualByIRI(alternativesBuilder.getModel().createIri("bf342340921"), "xIndivD");
assertThat(alternativesBuilder.getModel().getIndividualByLabel("xIndivD").getIRI().toString(), equalTo("http://localhost/test_ab4_usingLabel.owl#xIndivD"));
}
@Test
public void testRenameIndividualByLabel_validLabel_shouldRenameLabelButNotTheFragment() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createAlternativesBuilderModelUsingLabel();
alternativesBuilder.renameIndividualByLabel("IndivE", "xIndivE");
assertThat(alternativesBuilder.getModel().getIndividualByLabel("xIndivE").getIRI().toString(), equalTo("http://localhost/test_ab4_usingLabel.owl#bf342340921"));
}
@Test (expected = OwlElementNotFoundException.class)
public void testRenameIndividualByLabel_inexistingLabel_shouldThrowException() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
alternativesBuilder.renameIndividualByLabel("yIndivD", "xIndivD");
}
@Test
public void testToXmlString_validOntology_shouldReturnXmlString() throws OWLOntologyCreationException, URISyntaxException {
OwlApiFacade source = TestUtils.createDefaultOwlApiFacade();
source.load(TestUtils.getUri("/test_ab1.owl"));
String actual = AlternativesBuilder.toXMLString(source.getOntology());
OwlApiFacade ontology = TestUtils.createDefaultOwlApiFacade();
ontology.load(new ByteArrayInputStream(actual.getBytes()));
assertThat(ontology.getOntology().getOntologyID().getOntologyIRI().toString(), equalTo("http://localhost/test_ab1.owl"));
}
private AlternativesBuilder createDefaultAlternativesBuilder() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade model = TestUtils.createDefaultOwlApiFacade();
model.load(TestUtils.getUri("/test_ab3.owl"), new HashMap<String, String>() {{
put("http://localhost/test_ab1.owl", TestUtils.getUri("/test_ab1.owl"));
put("http://localhost/test_ab2.owl", TestUtils.getUri("/test_ab2.owl"));
}});
return new AlternativesBuilder(model);
}
private AlternativesBuilder createAlternativesBuilderModelUsingLabel() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade model = TestUtils.createDefaultOwlApiFacade();
model.load(TestUtils.getUri("/test_ab4_usingLabel.owl"), new HashMap<String, String>() {{
put("http://localhost/test_ab1_usingLabel.owl", TestUtils.getUri("/test_ab1_usingLabel.owl"));
put("http://localhost/test_ab2.owl", TestUtils.getUri("/test_ab2.owl"));
}});
return new AlternativesBuilder(model);
}
}