/**
* 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 com.hp.hpl.jena.rdf.model.Model;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import net.sf.oval.exception.ConstraintsViolatedException;
import org.hamcrest.core.Is;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.vocab.OWL2Datatype;
import uk.ac.manchester.cs.owl.owlapi.OWL2DatatypeImpl;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;
import java.util.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(JUnitParamsRunner.class)
public class OwlApiFacadeTest {
@Test
public void testSetIRIMapping_unusedIRI_shouldNotAffectMissingImports() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.setIRIMapping(new HashMap<String, String>() {{
put("unused-import", "some-local-replacement");
}});
owlApi.load(TestUtils.getUri("/test_missing_import.owl"));
Set<OWLOntology> imports = owlApi.getOntology().getImports();
assertThat(imports.size(), is(0));
}
@Test
public void testSetIRIMapping_emptyMapping_shouldNotAffectMissingImports() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.setIRIMapping(new HashMap<String, String>());
owlApi.load(TestUtils.getUri("/test_missing_import.owl"));
Set<OWLOntology> imports = owlApi.getOntology().getImports();
assertThat(imports.size(), is(0));
}
@Test
public void testSetIRIMapping_usedIRI_shouldReplaceImports() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.setIRIMapping(new HashMap<String, String>() {{
put("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-34", TestUtils.getUri("/test_ab1.owl"));
}});
owlApi.load(TestUtils.getUri("/test_missing_import.owl"));
Set<OWLOntology> imports = owlApi.getOntology().getImports();
assertThat(imports.size(), is(1));
assertThat(imports.iterator().next().getOntologyID().getOntologyIRI().toString(), equalTo("http://localhost/test_ab1.owl"));
}
@Test(expected = IllegalArgumentException.class)
public void testLoad_malformedUrl_shouldThrowException() throws OWLOntologyCreationException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load("/malformed/url");
}
@Test(expected = OWLOntologyCreationException.class)
public void testLoad_invalidUrl_shouldThrowException() throws OWLOntologyCreationException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load("http://invalid.com/blah.owl");
}
@Test
public void testLoad_validUrl_shouldNotRaiseException() {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
try {
owlApi.load(getOwlUri());
} catch (OWLOntologyCreationException | URISyntaxException e) {
Assert.fail("Unexpected exception: " + e.getMessage());
}
}
@Test
public void testLoad_ontologyWithClass_shouldRecognizeClass() throws URISyntaxException, OWLOntologyCreationException {
OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, OWLManager.getOWLDataFactory());
owlApi.load(getOwlUri());
assertTrue(invocationHandler.getOntology().containsClassInSignature(
IRI.create("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#ClassA")));
}
@Test
public void testAddIndividual_shouldAddOnlyThisIndividualToOntology() throws URISyntaxException, OWLOntologyCreationException {
OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, OWLManager.getOWLDataFactory());
owlApi.load(getOwlUri());
owlApi.addIndividual("indiv1");
OWLOntology ontology = invocationHandler.getOntology();
assertTrue(ontology.containsIndividualInSignature(owlApi.createIri("indiv1")));
assertFalse(ontology.containsIndividualInSignature(owlApi.createIri("indiv2")));
}
@Test
public void testCreateIri_shouldReturnCorrectFormattedIri() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(getOwlUri());
String actual = owlApi.createIri("test").toURI().toString();
Assert.assertEquals("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33" + "#" + "test", actual);
}
@Test
public void testCreateIri_notAnIri_shouldAppendBaseUrl() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade defaultOwlApiFacade = TestUtils.createDefaultOwlApiFacade();
defaultOwlApiFacade.load(getOwlUri());
IRI actual = defaultOwlApiFacade.createIri("test");
Assert.assertEquals("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#test", actual.toString());
}
@Test
@Parameters({"http://blah#test"})
public void testCreateIri_alreadyAnIri_shouldNotAlterIri(String iri) throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade defaultOwlApiFacade = TestUtils.createDefaultOwlApiFacade();
defaultOwlApiFacade.load(getOwlUri());
IRI actual = defaultOwlApiFacade.createIri(iri);
Assert.assertEquals(iri, actual.toString());
}
@Test
@Parameters({"http://blah#test, test"})
public void testCreateIri_alreadyAnIri_shouldReturnIriWithFragment(String iri, String expectedFragment) throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade defaultOwlApiFacade = TestUtils.createDefaultOwlApiFacade();
defaultOwlApiFacade.load(getOwlUri());
IRI actual = defaultOwlApiFacade.createIri(iri);
Assert.assertEquals(expectedFragment, actual.getFragment());
}
@Test
@Parameters({"http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#test, test"})
public void testCreateIri_sameIriAsBaseIriFromModel_shouldReturnIriWithFragment(String iri, String expectedFragment) throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade defaultOwlApiFacade = TestUtils.createDefaultOwlApiFacade();
defaultOwlApiFacade.load(getOwlUri());
IRI actual = defaultOwlApiFacade.createIri(iri);
Assert.assertEquals(expectedFragment, actual.getFragment());
}
@Test
@Ignore("Not implemented because it causes issues with other operations provided by the OwlAPI")
@Parameters({"http://blah#23, http://blah#, 23"})
public void testCreateIri_numericFragment_shouldReturnIriWithFragment(String iri, String expectedNamespace, String expectedFragment) throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade defaultOwlApiFacade = TestUtils.createDefaultOwlApiFacade();
defaultOwlApiFacade.load(getOwlUri());
IRI actual = defaultOwlApiFacade.createIri(iri);
Assert.assertEquals(expectedFragment, actual.getFragment());
Assert.assertEquals(expectedNamespace, actual.getNamespace());
Assert.assertEquals(iri, actual.toString());
}
@Test(expected = ConstraintsViolatedException.class)
public void testCreateIri_passingNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(getOwlUri());
owlApi.createIri(null).toURI().toString();
}
@Test(expected = ConstraintsViolatedException.class)
public void testCreateIri_passingEmptyString_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(getOwlUri());
owlApi.createIri("").toURI().toString();
}
@Test
public void testAddObjectProperty_shouldAddOnlyOneObjectPropertyToOntology() throws URISyntaxException, OWLOntologyCreationException {
OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
OWLDataFactory dataFactory = OWLManager.getOWLDataFactory();
OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, dataFactory);
owlApi.load(getOwlUri());
OWLNamedIndividual indiv1 = owlApi.addIndividual("indiv1");
OWLNamedIndividual indiv2 = owlApi.addIndividual("indiv2");
OWLNamedIndividual indiv3 = owlApi.addIndividual("indiv3");
String objectPropertyName = "objectProperty";
owlApi.addObjectProperty(indiv1, objectPropertyName, indiv2);
OWLOntology ontology = invocationHandler.getOntology();
assertTrue(ontology.containsAxiom(dataFactory.getOWLObjectPropertyAssertionAxiom(dataFactory.getOWLObjectProperty(owlApi.createIri(objectPropertyName)), indiv1, indiv2)));
assertFalse(ontology.containsAxiom(dataFactory.getOWLObjectPropertyAssertionAxiom(dataFactory.getOWLObjectProperty(owlApi.createIri(objectPropertyName)), indiv3, indiv2)));
}
@Test
public void testAddDataProperty_shouldAddOnlyOneDataPropertyToOntology() throws URISyntaxException, OWLOntologyCreationException {
OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
OWLDataFactory dataFactory = OWLManager.getOWLDataFactory();
OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, dataFactory);
owlApi.load(getOwlUri());
OWLNamedIndividual indiv1 = owlApi.addIndividual("indiv1");
OWLNamedIndividual indiv2 = owlApi.addIndividual("indiv2");
String dataPropertyName = "dataProperty";
int dataPropertyValue = 23;
owlApi.addDataProperty(indiv1, dataPropertyName, dataFactory.getOWLLiteral(dataPropertyValue));
OWLOntology ontology = invocationHandler.getOntology();
assertTrue(ontology.containsAxiom(dataFactory.getOWLDataPropertyAssertionAxiom(dataFactory.getOWLDataProperty(owlApi.createIri(dataPropertyName)), indiv1, dataPropertyValue)));
assertFalse(ontology.containsAxiom(dataFactory.getOWLDataPropertyAssertionAxiom(dataFactory.getOWLDataProperty(owlApi.createIri(dataPropertyName)), indiv2, dataPropertyValue)));
}
@Test
public void testMakeInstanceOf_shouldAddOnlyAffectedIndividualAsMemberToClass() throws URISyntaxException, OWLOntologyCreationException {
OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
OWLDataFactory dataFactory = OWLManager.getOWLDataFactory();
OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, dataFactory);
owlApi.load(getOwlUri());
OWLNamedIndividual indiv1 = owlApi.addIndividual("indiv1");
OWLNamedIndividual indiv2 = owlApi.addIndividual("indiv2");
String className = "classA";
owlApi.makeInstanceOf(indiv1, className);
OWLOntology ontology = invocationHandler.getOntology();
assertTrue(ontology.containsAxiom(dataFactory.getOWLClassAssertionAxiom(dataFactory.getOWLClass(owlApi.createIri(className)), indiv1)));
assertFalse(ontology.containsAxiom(dataFactory.getOWLClassAssertionAxiom(dataFactory.getOWLClass(owlApi.createIri(className)), indiv2)));
}
@Test
public void testAddInvidvidual_providingBaseClass_shouldAddIndividualAndAddAsMemberToClass() throws URISyntaxException, OWLOntologyCreationException {
OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
OWLDataFactory dataFactory = OWLManager.getOWLDataFactory();
OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, dataFactory);
owlApi.load(getOwlUri());
String indivName = "indiv1";
String className = "classA";
OWLNamedIndividual indiv1 = owlApi.addIndividual(indivName, className);
OWLOntology ontology = invocationHandler.getOntology();
assertTrue(ontology.containsIndividualInSignature(owlApi.createIri(indivName)));
assertTrue(ontology.containsAxiom(dataFactory.getOWLClassAssertionAxiom(dataFactory.getOWLClass(owlApi.createIri(className)), indiv1)));
}
@Test
public void testGetOWLLiteral() {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
String literal = "literal";
OWLLiteral actual = owlApi.getOWLLiteral(literal, OWL2DatatypeImpl.getDatatype(OWL2Datatype.XSD_STRING));
Assert.assertEquals(actual, OWLManager.getOWLDataFactory().getOWLLiteral(literal));
}
@SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void testSave_shouldPersistOntologyToFile() throws OWLOntologyStorageException, URISyntaxException, OWLOntologyCreationException, IOException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(getOwlUri());
owlApi.addIndividual("indiv1");
File tmpFile = File.createTempFile("owl-api-facade-tests-", null);
tmpFile.deleteOnExit();
String filename = tmpFile.getAbsolutePath();
owlApi.save(filename);
OWLOntology ontology = OWLManager.createOWLOntologyManager().loadOntology(IRI.create("file:" + filename));
assertTrue(ontology.containsIndividualInSignature(owlApi.createIri("indiv1")));
assertFalse(ontology.containsIndividualInSignature(owlApi.createIri("indiv2")));
new File(filename).delete();
}
@SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void testCreate_shouldCreateEmptyOntology() throws OWLOntologyStorageException, URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.create("http;//test");
owlApi.addIndividual("indiv1");
OWLOntology ontology = owlApi.getOntology();
assertTrue(ontology.containsIndividualInSignature(owlApi.createIri("indiv1")));
assertFalse(ontology.containsIndividualInSignature(owlApi.createIri("indiv2")));
Assert.assertEquals(1, ontology.getIndividualsInSignature().size());
Assert.assertEquals(0, ontology.getClassesInSignature().size());
Assert.assertEquals(1, ontology.getSignature().size());
}
@Test(expected = ConstraintsViolatedException.class)
public void testAddOntology_noOntologyLoaded_shouldThrowException() throws URISyntaxException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.addOntology(TestUtils.getUri("/test2.owl"));
}
@Test
public void testAddOntology_ontologyLoaded_shouldAddOntology() throws URISyntaxException, OWLOntologyCreationException, OWLOntologyStorageException {
OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
OWLDataFactory dataFactory = OWLManager.getOWLDataFactory();
OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, dataFactory);
owlApi.load(getOwlUri());
owlApi.addOntology(TestUtils.getUri("/test2.owl"));
OWLOntology ontology = invocationHandler.getOntology();
assertTrue(ontology.getDirectImportsDocuments().contains(IRI.create(TestUtils.getUri("/test2.owl"))));
}
@Test
public void testMethods_noOntology_shouldThrowException() throws InvocationTargetException, IllegalAccessException, InstantiationException {
testExecMethod(getMethodsThatNeedOntology(), TestUtils.createDefaultOwlApiFacade());
}
@Test
public void testMethods_noOntologyManager_shouldThrowException() throws InvocationTargetException, IllegalAccessException, InstantiationException {
testExecMethod(getMethodsThatNeedOntologyManager(), new OwlApiFacade(null, OWLManager.getOWLDataFactory()));
}
@Test
public void testMethods_noDataFactory_shouldThrowException() throws InvocationTargetException, IllegalAccessException, InstantiationException {
testExecMethod(getMethodsThatNeedDataFactory(), new OwlApiFacade(OWLManager.createOWLOntologyManager(), null));
}
@SuppressWarnings("ResultOfMethodCallIgnored")
private void testExecMethod(List<String> methods, OwlApiFacade owlApi) throws IllegalAccessException {
String defaultStringArg = "str";
Boolean defaultBooleanArg = false;
for (Method method : OwlApiFacade.class.getDeclaredMethods()) {
if (!Modifier.isPublic(method.getModifiers())) continue;
if (!methods.contains(method.getName())) continue;
try {
Object[] args = new Object[method.getParameterTypes().length];
for (int i = 0; i < method.getParameterTypes().length; ++i) {
if (method.getParameterTypes()[i].equals(String.class)) {
args[i] = defaultStringArg;
} else if (method.getParameterTypes()[i].equals(boolean.class)) {
args[i] = defaultBooleanArg;
} else args[i] = null;
}
method.invoke(owlApi, args);
} catch (InvocationTargetException e) {
if (!(e.getTargetException() instanceof ConstraintsViolatedException)) {
Assert.fail(String.format("Unexpected exception of method %s: %s - %s", method.getName(),
e.getTargetException().getClass().getName(), e.getTargetException().getMessage()));
}
}
}
// cleanup
new File(defaultStringArg).delete();
}
// TODO: test getJenaModel with missing imports
private List<String> getMethodsThatNeedOntology() {
return new ArrayList<String>() {{
add("save");
add("addDataProperty");
add("addObjectProperty");
add("makeInstanceOf");
add("createIri");
add("addIndividual");
add("addOntology");
add("containsIndividual");
add("getIndividual");
add("getJenaModel");
add("getClassesOfIndividual");
add("isIndividualOfClass");
add("containsIndividualByLabel");
}};
}
private List<String> getMethodsThatNeedOntologyManager() {
return new ArrayList<String>() {{
add("load");
add("create");
add("addIndividual");
add("makeInstanceOf");
add("addObjectProperty");
add("addDataProperty");
add("save");
add("addOntology");
add("getJenaModel");
add("setIRIMapping");
}};
}
private List<String> getMethodsThatNeedDataFactory() {
return new ArrayList<String>() {{
add("addIndividual");
add("getIndividual");
add("makeInstanceOf");
add("addObjectProperty");
add("addDataProperty");
add("getOWLLiteral");
add("addOntology");
}};
}
@Test
public void testLoad_missingImports_shouldBeResolvedUsingIriMapping() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
Map<String, String> iriMapping = new HashMap<String, String>() {{
put("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-34", TestUtils.getUri("/test2.owl"));
}};
owlApiFacade.load(TestUtils.getUri("/test_missing_import.owl"), iriMapping);
}
@Test
public void testLoad_missingImports_shouldNotThrowException() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(TestUtils.getUri("/test_missing_import.owl"));
Assert.assertNotNull(owlApiFacade.getOntology());
}
@Test
@Parameters(
{"test", "23"}
)
public void testGetIndividual_individualExists_shouldReturnIndividual(String indivName) throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
OWLNamedIndividual testIndiv = owlApiFacade.addIndividual(indivName);
Assert.assertEquals(testIndiv, owlApiFacade.getIndividual(indivName));
}
@Test(expected = OwlElementNotFoundException.class)
public void testGetIndividual_individualDoesNotExist_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
owlApiFacade.getIndividual("blah");
}
@Test(expected = OwlElementNotFoundException.class)
@Parameters(
{"http://blah#, test"}
)
public void testGetIndividual_fullyQualifiedIndividualWrongIri_shouldThrowException(String namespace, String fragment) throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
OWLNamedIndividual testIndiv = owlApiFacade.addIndividual(fragment);
Assert.assertEquals(testIndiv, owlApiFacade.getIndividual(namespace + fragment));
}
@Test
@Parameters({
"http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#, test",
"http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#, 23",
"http://blah#, test",
"http://blah#, 23"
})
public void testGetIndividual_fullyQualifiedIndividual_shouldReturnIndividual(String namespace, String fragment) throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
OWLNamedIndividual testIndiv = owlApiFacade.addIndividual(namespace + fragment);
Assert.assertEquals(testIndiv, owlApiFacade.getIndividual(namespace + fragment));
}
@Test
@Parameters(
{"http://importtest, test"}
)
public void testGetIndividualByUnqualifiedName_individualInImportedNamespace_shouldReturnIndividual(String url, String indivName) throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
owlApiFacade.addOntology(url);
OWLNamedIndividual testIndiv = owlApiFacade.addIndividual(url + "#" + indivName);
Assert.assertEquals(testIndiv, owlApiFacade.getIndividualByUnqualifiedName(indivName, true));
}
@Test(expected = ConstraintsViolatedException.class)
public void testGetIndividualByLabel_labelIsNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(TestUtils.getUri("/test_ab4_usingLabel.owl"));
owlApiFacade.getIndividualByLabel(null);
}
@Test(expected=OwlElementNotFoundException.class)
public void testGetIndividualByLabel_individualDoesNotExists_shouldReturnFalse() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
owlApiFacade.getIndividualByLabel("testblah");
}
@Test
public void testGetIndividualByLabel_individualExists_shouldReturn() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(TestUtils.getUri("/test_ab4_usingLabel.owl"));
Assert.assertEquals("http://localhost/test_ab4_usingLabel.owl#bf342340921", owlApiFacade.getIndividualByLabel("IndivE").getIRI().toString());
}
@Test
public void testGetdividualByLabel_individualInImportedOntology_shouldReturnTrue() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.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"));
}});
Assert.assertEquals("http://localhost/test_ab1_usingLabel.owl#ab1232323", owlApiFacade.getIndividualByLabel("IndivA").getIRI().toString());
}
@Test
@Parameters({
"http://blah#, b", "http://blah#, 23", "http://timbus.teco.edu/ontologies/Scenarios/MusicClassification.owl#, 7cb4eeb2"
})
public void testGetFragment_shouldReturnFragment(String namespace, String fragment) throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
Assert.assertEquals(OwlApiFacade.getFragment(IRI.create(namespace + fragment)), fragment);
Assert.assertEquals(OwlApiFacade.getFragment(IRI.create(namespace, fragment)), fragment);
}
@Test
@Parameters({
"http://blah#, b", "http://blah#, 23", "http://timbus.teco.edu/ontologies/Scenarios/MusicClassification.owl#, 7cb4eeb2"
})
public void testGetNamespace_shouldReturnNamespace(String namespace, String fragment) throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
Assert.assertEquals(OwlApiFacade.getNamespace(IRI.create(namespace + fragment)), namespace);
Assert.assertEquals(OwlApiFacade.getNamespace(IRI.create(namespace, fragment)), namespace);
}
@Test
public void testContainsIndividual_individualExists_shouldReturnTrue() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
String testIndivName = "test";
owlApiFacade.addIndividual(testIndivName);
Assert.assertEquals(true, owlApiFacade.containsIndividual(testIndivName));
}
@Test
public void testContainsIndividual_individualDoesNotExists_shouldReturnFalse() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
String testIndivName = "testblah";
Assert.assertEquals(false, owlApiFacade.containsIndividual(testIndivName));
}
@Test
@Parameters(
{"http://importtest, test"}
)
public void testContainsndividual_individualInImportedNamespace_shouldReturnTrue(String url, String indivName) throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
owlApiFacade.addOntology(url);
owlApiFacade.addIndividual(url + "#" + indivName);
Assert.assertEquals(true, owlApiFacade.containsIndividual(indivName));
}
@Test(expected = ConstraintsViolatedException.class)
public void testContainsIndividualByLabel_labelIsNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(TestUtils.getUri("/test_ab4_usingLabel.owl"));
owlApiFacade.containsIndividualByLabel(null);
}
@Test
public void testContainsIndividualByLabel_individualExists_shouldReturnTrue() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(TestUtils.getUri("/test_ab4_usingLabel.owl"));
Assert.assertEquals(true, owlApiFacade.containsIndividualByLabel("IndivE"));
}
@Test
public void testContainsIndividualByLabel_individualDoesNotExists_shouldReturnFalse() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.load(getOwlUri());
Assert.assertEquals(false, owlApiFacade.containsIndividualByLabel("testblah"));
}
@Test
public void testContainsndividualByLabel_individualInImportedOntology_shouldReturnTrue() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
owlApiFacade.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"));
}});
Assert.assertEquals(true, owlApiFacade.containsIndividualByLabel("IndivA"));
}
@Test
public void testConstructor_shouldInitOwlApiCompletely() throws OWLOntologyStorageException, URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(getOwlUri());
owlApi.addIndividual("indiv1");
OwlApiFacade owlApiUnderTest = new OwlApiFacade(owlApi.getOntology());
assertTrue("Ontology is not most likely not initialized.", owlApiUnderTest.containsIndividual(owlApi.createIri("indiv1").toString()));
assertFalse("Ontology is not most likely initialized.", owlApiUnderTest.containsIndividual(owlApi.createIri("indiv2").toString()));
owlApiUnderTest.addIndividual("indiv2"); // Manager is likely not initialized if this throws an exception.
assertTrue("DataFactory is most likely not initialized.", owlApiUnderTest.containsIndividual(owlApi.createIri("indiv2").toString())); // DataFactory is likely not initialized if this throws an exception.
Assert.assertNotNull("DataFactory is most likely not initialized.", owlApiUnderTest.getIndividual("indiv2"));
}
@Test
public void testLoad_fromStream_shouldLoadOntology() throws OWLOntologyCreationException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(this.getClass().getResourceAsStream("/test.owl"));
assertTrue(owlApi.getOntology().containsClassInSignature(
IRI.create("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#ClassA")));
assertThat(owlApi.getOntology().getClassesInSignature().size(), Is.is(2));
}
@Test
@Ignore
public void testGetJenaModel_validModel_shouldReturnJenaModel() throws OWLOntologyCreationException, OWLOntologyStorageException, IOException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(this.getClass().getResourceAsStream("/test.owl"));
Model jenaModel = owlApi.getJenaModel(false);
Assert.assertNotNull(jenaModel);
Assert.assertEquals(29, jenaModel.size());
}
@Test
public void testGetClassesOfIndividual_classDefinedInSameOntology_shouldReturnClasses() throws OWLOntologyCreationException, OwlElementNotFoundException {
IRI[] expected = new IRI[] { IRI.create("http://localhost/test_ab1.owl#ClassA") };
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(this.getClass().getResourceAsStream("/test_ab1.owl"));
OWLNamedIndividual indiv = owlApi.getIndividualByUnqualifiedName("IndivA", false);
List<IRI> actual = owlApi.getClassesOfIndividual(indiv);
assertThat(actual, containsInAnyOrder(expected));
}
@Test
public void testGetClassesOfIndividual_classesDefinedInDifferentOntology_shouldReturnClasses() throws OWLOntologyCreationException, OwlElementNotFoundException, URISyntaxException {
IRI[] expected = new IRI[] {
IRI.create("http://localhost/test_ab1.owl#ClassA"),
IRI.create("http://localhost/test_ab2.owl#ClassB"),
IRI.create("http://localhost/test_ab3.owl#ClassD")
};
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.setIRIMapping(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"));
put("http://localhost/test_ab3.owl", TestUtils.getUri("/test_ab3.owl"));
}});
owlApi.load(this.getClass().getResourceAsStream("/test_ab4.owl"));
OWLNamedIndividual indiv = owlApi.getIndividualByUnqualifiedName("IndivE", false);
List<IRI> actual = owlApi.getClassesOfIndividual(indiv);
assertThat(actual, containsInAnyOrder(expected));
}
@Test
public void testGetClassesOfIndividual_noClassDefined_shouldReturnEmptyList() throws OWLOntologyCreationException, OwlElementNotFoundException, URISyntaxException {
IRI[] expected = new IRI[] { };
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(TestUtils.getUri("/test_ab4.owl"));
OWLNamedIndividual indiv = owlApi.getIndividualByUnqualifiedName("IndivF", false);
List<IRI> actual = owlApi.getClassesOfIndividual(indiv);
assertThat(actual, containsInAnyOrder(expected));
}
@Test(expected = ConstraintsViolatedException.class)
public void testGetClassesOfIndividual_indivNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(TestUtils.getUri("/test_ab4.owl"));
owlApi.getClassesOfIndividual(null);
}
@Test(expected = ConstraintsViolatedException.class)
public void testIsIndividualOfClass_indivNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(TestUtils.getUri("/test_ab4.owl"));
owlApi.isIndividualOfClass(null, IRI.create("blah"));
}
@Test(expected = ConstraintsViolatedException.class)
public void testIsIndividualOfClass_classIriNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(TestUtils.getUri("/test_ab4.owl"));
OWLNamedIndividual indiv = owlApi.getIndividualByUnqualifiedName("IndivF", false);
owlApi.isIndividualOfClass(indiv, null);
}
@Test
public void testIsIndividualOfClass_validIndivAndClass_shouldReturnAsExpected() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
owlApi.load(TestUtils.getUri("/test_ab4.owl"));
OWLNamedIndividual indiv = owlApi.getIndividualByUnqualifiedName("IndivE", false);
assertTrue(owlApi.isIndividualOfClass(indiv, IRI.create("http://localhost/test_ab1.owl#ClassA")));
assertTrue(owlApi.isIndividualOfClass(indiv, IRI.create("http://localhost/test_ab2.owl#ClassB")));
assertTrue(owlApi.isIndividualOfClass(indiv, IRI.create("http://localhost/test_ab3.owl#ClassD")));
assertFalse(owlApi.isIndividualOfClass(indiv, IRI.create("http://localhost/test_ab4.owl#ClassE")));
}
//<editor-fold desc="helper methods">
private String getOwlUri() throws URISyntaxException {
return TestUtils.getUri("/test.owl");
}
//</editor-fold>
}