Switch to unified view

a b/src/test/java/org/sbaresearch/owl/OwlApiFacadeTest.java
1
/**
2
 * Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
3
 * The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
4
 * TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
5
 * development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
8
 * the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
9
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
11
 * limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
12
 * PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
13
 * unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
14
 * any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
15
 * License or out of the use or inability to use the Work.
16
 * See the License for the specific language governing permissions and limitation under the License.
17
 */
18
package org.sbaresearch.owl;
19
20
import com.hp.hpl.jena.rdf.model.Model;
21
import junitparams.JUnitParamsRunner;
22
import junitparams.Parameters;
23
import net.sf.oval.exception.ConstraintsViolatedException;
24
import org.hamcrest.core.Is;
25
import org.junit.Assert;
26
import org.junit.Ignore;
27
import org.junit.Test;
28
import org.junit.runner.RunWith;
29
import org.semanticweb.owlapi.apibinding.OWLManager;
30
import org.semanticweb.owlapi.model.*;
31
import org.semanticweb.owlapi.vocab.OWL2Datatype;
32
import uk.ac.manchester.cs.owl.owlapi.OWL2DatatypeImpl;
33
34
import java.io.File;
35
import java.io.IOException;
36
import java.lang.reflect.InvocationTargetException;
37
import java.lang.reflect.Method;
38
import java.lang.reflect.Modifier;
39
import java.net.URISyntaxException;
40
import java.util.*;
41
42
import static org.hamcrest.CoreMatchers.equalTo;
43
import static org.hamcrest.CoreMatchers.is;
44
import static org.hamcrest.Matchers.containsInAnyOrder;
45
import static org.junit.Assert.assertFalse;
46
import static org.junit.Assert.assertThat;
47
import static org.junit.Assert.assertTrue;
48
49
@RunWith(JUnitParamsRunner.class)
50
public class OwlApiFacadeTest {
51
52
    @Test
53
    public void testSetIRIMapping_unusedIRI_shouldNotAffectMissingImports() throws URISyntaxException, OWLOntologyCreationException {
54
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
55
        owlApi.setIRIMapping(new HashMap<String, String>() {{
56
            put("unused-import", "some-local-replacement");
57
        }});
58
59
        owlApi.load(TestUtils.getUri("/test_missing_import.owl"));
60
61
        Set<OWLOntology> imports = owlApi.getOntology().getImports();
62
        assertThat(imports.size(), is(0));
63
    }
64
65
    @Test
66
    public void testSetIRIMapping_emptyMapping_shouldNotAffectMissingImports() throws URISyntaxException, OWLOntologyCreationException {
67
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
68
        owlApi.setIRIMapping(new HashMap<String, String>());
69
70
        owlApi.load(TestUtils.getUri("/test_missing_import.owl"));
71
72
        Set<OWLOntology> imports = owlApi.getOntology().getImports();
73
        assertThat(imports.size(), is(0));
74
    }
75
76
    @Test
77
    public void testSetIRIMapping_usedIRI_shouldReplaceImports() throws URISyntaxException, OWLOntologyCreationException {
78
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
79
        owlApi.setIRIMapping(new HashMap<String, String>() {{
80
            put("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-34", TestUtils.getUri("/test_ab1.owl"));
81
        }});
82
83
        owlApi.load(TestUtils.getUri("/test_missing_import.owl"));
84
85
        Set<OWLOntology> imports = owlApi.getOntology().getImports();
86
        assertThat(imports.size(), is(1));
87
        assertThat(imports.iterator().next().getOntologyID().getOntologyIRI().toString(), equalTo("http://localhost/test_ab1.owl"));
88
    }
89
90
    @Test(expected = IllegalArgumentException.class)
91
    public void testLoad_malformedUrl_shouldThrowException() throws OWLOntologyCreationException {
92
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
93
        owlApi.load("/malformed/url");
94
    }
95
96
    @Test(expected = OWLOntologyCreationException.class)
97
    public void testLoad_invalidUrl_shouldThrowException() throws OWLOntologyCreationException {
98
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
99
        owlApi.load("http://invalid.com/blah.owl");
100
    }
101
102
    @Test
103
    public void testLoad_validUrl_shouldNotRaiseException() {
104
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
105
        try {
106
            owlApi.load(getOwlUri());
107
        } catch (OWLOntologyCreationException | URISyntaxException e) {
108
            Assert.fail("Unexpected exception: " + e.getMessage());
109
        }
110
    }
111
112
    @Test
113
    public void testLoad_ontologyWithClass_shouldRecognizeClass() throws URISyntaxException, OWLOntologyCreationException {
114
        OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
115
        OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, OWLManager.getOWLDataFactory());
116
117
        owlApi.load(getOwlUri());
118
        assertTrue(invocationHandler.getOntology().containsClassInSignature(
119
                IRI.create("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#ClassA")));
120
    }
121
122
    @Test
123
    public void testAddIndividual_shouldAddOnlyThisIndividualToOntology() throws URISyntaxException, OWLOntologyCreationException {
124
        OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
125
        OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, OWLManager.getOWLDataFactory());
126
        owlApi.load(getOwlUri());
127
        owlApi.addIndividual("indiv1");
128
129
        OWLOntology ontology = invocationHandler.getOntology();
130
        assertTrue(ontology.containsIndividualInSignature(owlApi.createIri("indiv1")));
131
        assertFalse(ontology.containsIndividualInSignature(owlApi.createIri("indiv2")));
132
    }
133
134
    @Test
135
    public void testCreateIri_shouldReturnCorrectFormattedIri() throws URISyntaxException, OWLOntologyCreationException {
136
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
137
        owlApi.load(getOwlUri());
138
        String actual = owlApi.createIri("test").toURI().toString();
139
        Assert.assertEquals("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33" + "#" + "test", actual);
140
    }
141
142
    @Test
143
    public void testCreateIri_notAnIri_shouldAppendBaseUrl() throws URISyntaxException, OWLOntologyCreationException {
144
        OwlApiFacade defaultOwlApiFacade = TestUtils.createDefaultOwlApiFacade();
145
        defaultOwlApiFacade.load(getOwlUri());
146
        IRI actual = defaultOwlApiFacade.createIri("test");
147
        Assert.assertEquals("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#test", actual.toString());
148
    }
149
150
    @Test
151
    @Parameters({"http://blah#test"})
152
    public void testCreateIri_alreadyAnIri_shouldNotAlterIri(String iri) throws URISyntaxException, OWLOntologyCreationException {
153
        OwlApiFacade defaultOwlApiFacade = TestUtils.createDefaultOwlApiFacade();
154
        defaultOwlApiFacade.load(getOwlUri());
155
        IRI actual = defaultOwlApiFacade.createIri(iri);
156
        Assert.assertEquals(iri, actual.toString());
157
    }
158
159
    @Test
160
    @Parameters({"http://blah#test, test"})
161
    public void testCreateIri_alreadyAnIri_shouldReturnIriWithFragment(String iri, String expectedFragment) throws URISyntaxException, OWLOntologyCreationException {
162
        OwlApiFacade defaultOwlApiFacade = TestUtils.createDefaultOwlApiFacade();
163
        defaultOwlApiFacade.load(getOwlUri());
164
        IRI actual = defaultOwlApiFacade.createIri(iri);
165
        Assert.assertEquals(expectedFragment, actual.getFragment());
166
    }
167
168
    @Test
169
    @Parameters({"http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#test, test"})
170
    public void testCreateIri_sameIriAsBaseIriFromModel_shouldReturnIriWithFragment(String iri, String expectedFragment) throws URISyntaxException, OWLOntologyCreationException {
171
        OwlApiFacade defaultOwlApiFacade = TestUtils.createDefaultOwlApiFacade();
172
        defaultOwlApiFacade.load(getOwlUri());
173
        IRI actual = defaultOwlApiFacade.createIri(iri);
174
        Assert.assertEquals(expectedFragment, actual.getFragment());
175
    }
176
177
    @Test
178
    @Ignore("Not implemented because it causes issues with other operations provided by the OwlAPI")
179
    @Parameters({"http://blah#23, http://blah#, 23"})
180
    public void testCreateIri_numericFragment_shouldReturnIriWithFragment(String iri, String expectedNamespace, String expectedFragment) throws URISyntaxException, OWLOntologyCreationException {
181
        OwlApiFacade defaultOwlApiFacade = TestUtils.createDefaultOwlApiFacade();
182
        defaultOwlApiFacade.load(getOwlUri());
183
        IRI actual = defaultOwlApiFacade.createIri(iri);
184
        Assert.assertEquals(expectedFragment, actual.getFragment());
185
        Assert.assertEquals(expectedNamespace, actual.getNamespace());
186
        Assert.assertEquals(iri, actual.toString());
187
    }
188
189
    @Test(expected = ConstraintsViolatedException.class)
190
    public void testCreateIri_passingNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException {
191
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
192
        owlApi.load(getOwlUri());
193
        owlApi.createIri(null).toURI().toString();
194
    }
195
196
    @Test(expected = ConstraintsViolatedException.class)
197
    public void testCreateIri_passingEmptyString_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException {
198
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
199
        owlApi.load(getOwlUri());
200
        owlApi.createIri("").toURI().toString();
201
    }
202
203
    @Test
204
    public void testAddObjectProperty_shouldAddOnlyOneObjectPropertyToOntology() throws URISyntaxException, OWLOntologyCreationException {
205
        OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
206
        OWLDataFactory dataFactory = OWLManager.getOWLDataFactory();
207
        OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, dataFactory);
208
        owlApi.load(getOwlUri());
209
        OWLNamedIndividual indiv1 = owlApi.addIndividual("indiv1");
210
        OWLNamedIndividual indiv2 = owlApi.addIndividual("indiv2");
211
        OWLNamedIndividual indiv3 = owlApi.addIndividual("indiv3");
212
        String objectPropertyName = "objectProperty";
213
        owlApi.addObjectProperty(indiv1, objectPropertyName, indiv2);
214
215
        OWLOntology ontology = invocationHandler.getOntology();
216
        assertTrue(ontology.containsAxiom(dataFactory.getOWLObjectPropertyAssertionAxiom(dataFactory.getOWLObjectProperty(owlApi.createIri(objectPropertyName)), indiv1, indiv2)));
217
        assertFalse(ontology.containsAxiom(dataFactory.getOWLObjectPropertyAssertionAxiom(dataFactory.getOWLObjectProperty(owlApi.createIri(objectPropertyName)), indiv3, indiv2)));
218
    }
219
220
    @Test
221
    public void testAddDataProperty_shouldAddOnlyOneDataPropertyToOntology() throws URISyntaxException, OWLOntologyCreationException {
222
        OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
223
        OWLDataFactory dataFactory = OWLManager.getOWLDataFactory();
224
        OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, dataFactory);
225
        owlApi.load(getOwlUri());
226
        OWLNamedIndividual indiv1 = owlApi.addIndividual("indiv1");
227
        OWLNamedIndividual indiv2 = owlApi.addIndividual("indiv2");
228
        String dataPropertyName = "dataProperty";
229
        int dataPropertyValue = 23;
230
        owlApi.addDataProperty(indiv1, dataPropertyName, dataFactory.getOWLLiteral(dataPropertyValue));
231
232
        OWLOntology ontology = invocationHandler.getOntology();
233
        assertTrue(ontology.containsAxiom(dataFactory.getOWLDataPropertyAssertionAxiom(dataFactory.getOWLDataProperty(owlApi.createIri(dataPropertyName)), indiv1, dataPropertyValue)));
234
        assertFalse(ontology.containsAxiom(dataFactory.getOWLDataPropertyAssertionAxiom(dataFactory.getOWLDataProperty(owlApi.createIri(dataPropertyName)), indiv2, dataPropertyValue)));
235
    }
236
237
    @Test
238
    public void testMakeInstanceOf_shouldAddOnlyAffectedIndividualAsMemberToClass() throws URISyntaxException, OWLOntologyCreationException {
239
        OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
240
        OWLDataFactory dataFactory = OWLManager.getOWLDataFactory();
241
        OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, dataFactory);
242
        owlApi.load(getOwlUri());
243
        OWLNamedIndividual indiv1 = owlApi.addIndividual("indiv1");
244
        OWLNamedIndividual indiv2 = owlApi.addIndividual("indiv2");
245
        String className = "classA";
246
        owlApi.makeInstanceOf(indiv1, className);
247
248
        OWLOntology ontology = invocationHandler.getOntology();
249
        assertTrue(ontology.containsAxiom(dataFactory.getOWLClassAssertionAxiom(dataFactory.getOWLClass(owlApi.createIri(className)), indiv1)));
250
        assertFalse(ontology.containsAxiom(dataFactory.getOWLClassAssertionAxiom(dataFactory.getOWLClass(owlApi.createIri(className)), indiv2)));
251
    }
252
253
    @Test
254
    public void testAddInvidvidual_providingBaseClass_shouldAddIndividualAndAddAsMemberToClass() throws URISyntaxException, OWLOntologyCreationException {
255
        OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
256
        OWLDataFactory dataFactory = OWLManager.getOWLDataFactory();
257
        OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, dataFactory);
258
        owlApi.load(getOwlUri());
259
        String indivName = "indiv1";
260
        String className = "classA";
261
        OWLNamedIndividual indiv1 = owlApi.addIndividual(indivName, className);
262
263
        OWLOntology ontology = invocationHandler.getOntology();
264
        assertTrue(ontology.containsIndividualInSignature(owlApi.createIri(indivName)));
265
        assertTrue(ontology.containsAxiom(dataFactory.getOWLClassAssertionAxiom(dataFactory.getOWLClass(owlApi.createIri(className)), indiv1)));
266
    }
267
268
    @Test
269
    public void testGetOWLLiteral() {
270
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
271
        String literal = "literal";
272
        OWLLiteral actual = owlApi.getOWLLiteral(literal, OWL2DatatypeImpl.getDatatype(OWL2Datatype.XSD_STRING));
273
        Assert.assertEquals(actual, OWLManager.getOWLDataFactory().getOWLLiteral(literal));
274
    }
275
276
    @SuppressWarnings("ResultOfMethodCallIgnored")
277
    @Test
278
    public void testSave_shouldPersistOntologyToFile() throws OWLOntologyStorageException, URISyntaxException, OWLOntologyCreationException, IOException {
279
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
280
        owlApi.load(getOwlUri());
281
        owlApi.addIndividual("indiv1");
282
        File tmpFile = File.createTempFile("owl-api-facade-tests-", null);
283
        tmpFile.deleteOnExit();
284
        String filename = tmpFile.getAbsolutePath();
285
286
        owlApi.save(filename);
287
288
        OWLOntology ontology = OWLManager.createOWLOntologyManager().loadOntology(IRI.create("file:" + filename));
289
        assertTrue(ontology.containsIndividualInSignature(owlApi.createIri("indiv1")));
290
        assertFalse(ontology.containsIndividualInSignature(owlApi.createIri("indiv2")));
291
292
        new File(filename).delete();
293
    }
294
295
    @SuppressWarnings("ResultOfMethodCallIgnored")
296
    @Test
297
    public void testCreate_shouldCreateEmptyOntology() throws OWLOntologyStorageException, URISyntaxException, OWLOntologyCreationException {
298
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
299
        owlApi.create("http;//test");
300
        owlApi.addIndividual("indiv1");
301
302
        OWLOntology ontology = owlApi.getOntology();
303
        assertTrue(ontology.containsIndividualInSignature(owlApi.createIri("indiv1")));
304
        assertFalse(ontology.containsIndividualInSignature(owlApi.createIri("indiv2")));
305
        Assert.assertEquals(1, ontology.getIndividualsInSignature().size());
306
        Assert.assertEquals(0, ontology.getClassesInSignature().size());
307
        Assert.assertEquals(1, ontology.getSignature().size());
308
    }
309
310
    @Test(expected = ConstraintsViolatedException.class)
311
    public void testAddOntology_noOntologyLoaded_shouldThrowException() throws URISyntaxException {
312
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
313
        owlApi.addOntology(TestUtils.getUri("/test2.owl"));
314
    }
315
316
    @Test
317
    public void testAddOntology_ontologyLoaded_shouldAddOntology() throws URISyntaxException, OWLOntologyCreationException, OWLOntologyStorageException {
318
        OwlManagerInvocationHandler invocationHandler = new OwlManagerInvocationHandler(OWLManager.createOWLOntologyManager());
319
        OWLDataFactory dataFactory = OWLManager.getOWLDataFactory();
320
        OwlApiFacade owlApi = TestUtils.getOwlApiFacadeUsingProxy(invocationHandler, dataFactory);
321
        owlApi.load(getOwlUri());
322
323
        owlApi.addOntology(TestUtils.getUri("/test2.owl"));
324
325
        OWLOntology ontology = invocationHandler.getOntology();
326
        assertTrue(ontology.getDirectImportsDocuments().contains(IRI.create(TestUtils.getUri("/test2.owl"))));
327
    }
328
329
    @Test
330
    public void testMethods_noOntology_shouldThrowException() throws InvocationTargetException, IllegalAccessException, InstantiationException {
331
        testExecMethod(getMethodsThatNeedOntology(), TestUtils.createDefaultOwlApiFacade());
332
    }
333
334
    @Test
335
    public void testMethods_noOntologyManager_shouldThrowException() throws InvocationTargetException, IllegalAccessException, InstantiationException {
336
        testExecMethod(getMethodsThatNeedOntologyManager(), new OwlApiFacade(null, OWLManager.getOWLDataFactory()));
337
    }
338
339
    @Test
340
    public void testMethods_noDataFactory_shouldThrowException() throws InvocationTargetException, IllegalAccessException, InstantiationException {
341
        testExecMethod(getMethodsThatNeedDataFactory(), new OwlApiFacade(OWLManager.createOWLOntologyManager(), null));
342
    }
343
344
    @SuppressWarnings("ResultOfMethodCallIgnored")
345
    private void testExecMethod(List<String> methods, OwlApiFacade owlApi) throws IllegalAccessException {
346
        String defaultStringArg = "str";
347
        Boolean defaultBooleanArg = false;
348
349
        for (Method method : OwlApiFacade.class.getDeclaredMethods()) {
350
            if (!Modifier.isPublic(method.getModifiers())) continue;
351
            if (!methods.contains(method.getName())) continue;
352
            try {
353
                Object[] args = new Object[method.getParameterTypes().length];
354
                for (int i = 0; i < method.getParameterTypes().length; ++i) {
355
                    if (method.getParameterTypes()[i].equals(String.class)) {
356
                        args[i] = defaultStringArg;
357
                    } else if (method.getParameterTypes()[i].equals(boolean.class)) {
358
                        args[i] = defaultBooleanArg;
359
                    } else args[i] = null;
360
                }
361
                method.invoke(owlApi, args);
362
            } catch (InvocationTargetException e) {
363
                if (!(e.getTargetException() instanceof ConstraintsViolatedException)) {
364
                    Assert.fail(String.format("Unexpected exception of method %s: %s - %s", method.getName(),
365
                            e.getTargetException().getClass().getName(), e.getTargetException().getMessage()));
366
                }
367
            }
368
        }
369
370
        // cleanup
371
        new File(defaultStringArg).delete();
372
    }
373
374
    // TODO: test getJenaModel with missing imports
375
376
    private List<String> getMethodsThatNeedOntology() {
377
        return new ArrayList<String>() {{
378
            add("save");
379
            add("addDataProperty");
380
            add("addObjectProperty");
381
            add("makeInstanceOf");
382
            add("createIri");
383
            add("addIndividual");
384
            add("addOntology");
385
            add("containsIndividual");
386
            add("getIndividual");
387
            add("getJenaModel");
388
            add("getClassesOfIndividual");
389
            add("isIndividualOfClass");
390
            add("containsIndividualByLabel");
391
        }};
392
    }
393
394
    private List<String> getMethodsThatNeedOntologyManager() {
395
        return new ArrayList<String>() {{
396
            add("load");
397
            add("create");
398
            add("addIndividual");
399
            add("makeInstanceOf");
400
            add("addObjectProperty");
401
            add("addDataProperty");
402
            add("save");
403
            add("addOntology");
404
            add("getJenaModel");
405
            add("setIRIMapping");
406
        }};
407
    }
408
409
    private List<String> getMethodsThatNeedDataFactory() {
410
        return new ArrayList<String>() {{
411
            add("addIndividual");
412
            add("getIndividual");
413
            add("makeInstanceOf");
414
            add("addObjectProperty");
415
            add("addDataProperty");
416
            add("getOWLLiteral");
417
            add("addOntology");
418
        }};
419
    }
420
421
    @Test
422
    public void testLoad_missingImports_shouldBeResolvedUsingIriMapping() throws URISyntaxException, OWLOntologyCreationException {
423
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
424
        Map<String, String> iriMapping = new HashMap<String, String>() {{
425
            put("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-34", TestUtils.getUri("/test2.owl"));
426
        }};
427
        owlApiFacade.load(TestUtils.getUri("/test_missing_import.owl"), iriMapping);
428
    }
429
430
    @Test
431
    public void testLoad_missingImports_shouldNotThrowException() throws URISyntaxException, OWLOntologyCreationException {
432
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
433
        owlApiFacade.load(TestUtils.getUri("/test_missing_import.owl"));
434
        Assert.assertNotNull(owlApiFacade.getOntology());
435
    }
436
437
    @Test
438
    @Parameters(
439
            {"test", "23"}
440
    )
441
    public void testGetIndividual_individualExists_shouldReturnIndividual(String indivName) throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
442
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
443
        owlApiFacade.load(getOwlUri());
444
        OWLNamedIndividual testIndiv = owlApiFacade.addIndividual(indivName);
445
        Assert.assertEquals(testIndiv, owlApiFacade.getIndividual(indivName));
446
    }
447
448
    @Test(expected = OwlElementNotFoundException.class)
449
    public void testGetIndividual_individualDoesNotExist_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
450
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
451
        owlApiFacade.load(getOwlUri());
452
        owlApiFacade.getIndividual("blah");
453
    }
454
455
    @Test(expected = OwlElementNotFoundException.class)
456
    @Parameters(
457
            {"http://blah#, test"}
458
    )
459
    public void testGetIndividual_fullyQualifiedIndividualWrongIri_shouldThrowException(String namespace, String fragment) throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
460
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
461
        owlApiFacade.load(getOwlUri());
462
        OWLNamedIndividual testIndiv = owlApiFacade.addIndividual(fragment);
463
        Assert.assertEquals(testIndiv, owlApiFacade.getIndividual(namespace + fragment));
464
    }
465
466
    @Test
467
    @Parameters({
468
            "http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#, test",
469
            "http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#, 23",
470
            "http://blah#, test",
471
            "http://blah#, 23"
472
    })
473
    public void testGetIndividual_fullyQualifiedIndividual_shouldReturnIndividual(String namespace, String fragment) throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
474
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
475
        owlApiFacade.load(getOwlUri());
476
        OWLNamedIndividual testIndiv = owlApiFacade.addIndividual(namespace + fragment);
477
        Assert.assertEquals(testIndiv, owlApiFacade.getIndividual(namespace + fragment));
478
    }
479
480
    @Test
481
    @Parameters(
482
            {"http://importtest, test"}
483
    )
484
    public void testGetIndividualByUnqualifiedName_individualInImportedNamespace_shouldReturnIndividual(String url, String indivName) throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
485
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
486
        owlApiFacade.load(getOwlUri());
487
        owlApiFacade.addOntology(url);
488
        OWLNamedIndividual testIndiv = owlApiFacade.addIndividual(url + "#" + indivName);
489
        Assert.assertEquals(testIndiv, owlApiFacade.getIndividualByUnqualifiedName(indivName, true));
490
    }
491
492
    @Test(expected = ConstraintsViolatedException.class)
493
    public void testGetIndividualByLabel_labelIsNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
494
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
495
        owlApiFacade.load(TestUtils.getUri("/test_ab4_usingLabel.owl"));
496
        owlApiFacade.getIndividualByLabel(null);
497
    }
498
499
    @Test(expected=OwlElementNotFoundException.class)
500
    public void testGetIndividualByLabel_individualDoesNotExists_shouldReturnFalse() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
501
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
502
        owlApiFacade.load(getOwlUri());
503
        owlApiFacade.getIndividualByLabel("testblah");
504
    }
505
506
    @Test
507
    public void testGetIndividualByLabel_individualExists_shouldReturn() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
508
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
509
        owlApiFacade.load(TestUtils.getUri("/test_ab4_usingLabel.owl"));
510
        Assert.assertEquals("http://localhost/test_ab4_usingLabel.owl#bf342340921", owlApiFacade.getIndividualByLabel("IndivE").getIRI().toString());
511
    }
512
513
    @Test
514
    public void testGetdividualByLabel_individualInImportedOntology_shouldReturnTrue() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
515
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
516
        owlApiFacade.load(TestUtils.getUri("/test_ab4_usingLabel.owl"), new HashMap<String, String>() {{
517
            put("http://localhost/test_ab1_usingLabel.owl", TestUtils.getUri("/test_ab1_usingLabel.owl"));
518
            put("http://localhost/test_ab2.owl", TestUtils.getUri("/test_ab2.owl"));
519
        }});
520
        Assert.assertEquals("http://localhost/test_ab1_usingLabel.owl#ab1232323", owlApiFacade.getIndividualByLabel("IndivA").getIRI().toString());
521
    }
522
523
    @Test
524
    @Parameters({
525
            "http://blah#, b", "http://blah#, 23", "http://timbus.teco.edu/ontologies/Scenarios/MusicClassification.owl#, 7cb4eeb2"
526
    })
527
    public void testGetFragment_shouldReturnFragment(String namespace, String fragment) throws URISyntaxException, OWLOntologyCreationException {
528
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
529
        owlApiFacade.load(getOwlUri());
530
        Assert.assertEquals(OwlApiFacade.getFragment(IRI.create(namespace + fragment)), fragment);
531
        Assert.assertEquals(OwlApiFacade.getFragment(IRI.create(namespace, fragment)), fragment);
532
    }
533
534
    @Test
535
    @Parameters({
536
            "http://blah#, b", "http://blah#, 23", "http://timbus.teco.edu/ontologies/Scenarios/MusicClassification.owl#, 7cb4eeb2"
537
    })
538
    public void testGetNamespace_shouldReturnNamespace(String namespace, String fragment) throws URISyntaxException, OWLOntologyCreationException {
539
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
540
        owlApiFacade.load(getOwlUri());
541
        Assert.assertEquals(OwlApiFacade.getNamespace(IRI.create(namespace + fragment)), namespace);
542
        Assert.assertEquals(OwlApiFacade.getNamespace(IRI.create(namespace, fragment)), namespace);
543
    }
544
545
    @Test
546
    public void testContainsIndividual_individualExists_shouldReturnTrue() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
547
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
548
        owlApiFacade.load(getOwlUri());
549
        String testIndivName = "test";
550
        owlApiFacade.addIndividual(testIndivName);
551
        Assert.assertEquals(true, owlApiFacade.containsIndividual(testIndivName));
552
    }
553
554
    @Test
555
    public void testContainsIndividual_individualDoesNotExists_shouldReturnFalse() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
556
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
557
        owlApiFacade.load(getOwlUri());
558
        String testIndivName = "testblah";
559
        Assert.assertEquals(false, owlApiFacade.containsIndividual(testIndivName));
560
    }
561
562
    @Test
563
    @Parameters(
564
            {"http://importtest, test"}
565
    )
566
    public void testContainsndividual_individualInImportedNamespace_shouldReturnTrue(String url, String indivName) throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
567
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
568
        owlApiFacade.load(getOwlUri());
569
        owlApiFacade.addOntology(url);
570
        owlApiFacade.addIndividual(url + "#" + indivName);
571
        Assert.assertEquals(true, owlApiFacade.containsIndividual(indivName));
572
    }
573
574
    @Test(expected = ConstraintsViolatedException.class)
575
    public void testContainsIndividualByLabel_labelIsNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException {
576
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
577
        owlApiFacade.load(TestUtils.getUri("/test_ab4_usingLabel.owl"));
578
        owlApiFacade.containsIndividualByLabel(null);
579
    }
580
581
    @Test
582
    public void testContainsIndividualByLabel_individualExists_shouldReturnTrue() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
583
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
584
        owlApiFacade.load(TestUtils.getUri("/test_ab4_usingLabel.owl"));
585
        Assert.assertEquals(true, owlApiFacade.containsIndividualByLabel("IndivE"));
586
    }
587
588
    @Test
589
    public void testContainsIndividualByLabel_individualDoesNotExists_shouldReturnFalse() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
590
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
591
        owlApiFacade.load(getOwlUri());
592
        Assert.assertEquals(false, owlApiFacade.containsIndividualByLabel("testblah"));
593
    }
594
595
    @Test
596
    public void testContainsndividualByLabel_individualInImportedOntology_shouldReturnTrue() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
597
        OwlApiFacade owlApiFacade = TestUtils.createDefaultOwlApiFacade();
598
        owlApiFacade.load(TestUtils.getUri("/test_ab4_usingLabel.owl"), new HashMap<String, String>() {{
599
            put("http://localhost/test_ab1_usingLabel.owl", TestUtils.getUri("/test_ab1_usingLabel.owl"));
600
            put("http://localhost/test_ab2.owl", TestUtils.getUri("/test_ab2.owl"));
601
        }});
602
        Assert.assertEquals(true, owlApiFacade.containsIndividualByLabel("IndivA"));
603
    }
604
605
    @Test
606
    public void testConstructor_shouldInitOwlApiCompletely() throws OWLOntologyStorageException, URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
607
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
608
        owlApi.load(getOwlUri());
609
        owlApi.addIndividual("indiv1");
610
        OwlApiFacade owlApiUnderTest = new OwlApiFacade(owlApi.getOntology());
611
        assertTrue("Ontology is not most likely not initialized.", owlApiUnderTest.containsIndividual(owlApi.createIri("indiv1").toString()));
612
        assertFalse("Ontology is not most likely initialized.", owlApiUnderTest.containsIndividual(owlApi.createIri("indiv2").toString()));
613
        owlApiUnderTest.addIndividual("indiv2"); // Manager is likely not initialized if this throws an exception.
614
        assertTrue("DataFactory is most likely not initialized.", owlApiUnderTest.containsIndividual(owlApi.createIri("indiv2").toString())); // DataFactory is likely not initialized if this throws an exception.
615
        Assert.assertNotNull("DataFactory is most likely not initialized.", owlApiUnderTest.getIndividual("indiv2"));
616
    }
617
618
    @Test
619
    public void testLoad_fromStream_shouldLoadOntology() throws OWLOntologyCreationException {
620
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
621
        owlApi.load(this.getClass().getResourceAsStream("/test.owl"));
622
        assertTrue(owlApi.getOntology().containsClassInSignature(
623
                IRI.create("http://www.semanticweb.org/test/ontologies/2013/6/untitled-ontology-33#ClassA")));
624
        assertThat(owlApi.getOntology().getClassesInSignature().size(), Is.is(2));
625
    }
626
627
    @Test
628
    @Ignore
629
    public void testGetJenaModel_validModel_shouldReturnJenaModel() throws OWLOntologyCreationException, OWLOntologyStorageException, IOException {
630
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
631
        owlApi.load(this.getClass().getResourceAsStream("/test.owl"));
632
        Model jenaModel = owlApi.getJenaModel(false);
633
        Assert.assertNotNull(jenaModel);
634
        Assert.assertEquals(29, jenaModel.size());
635
    }
636
637
    @Test
638
    public void testGetClassesOfIndividual_classDefinedInSameOntology_shouldReturnClasses() throws OWLOntologyCreationException, OwlElementNotFoundException {
639
        IRI[] expected = new IRI[] { IRI.create("http://localhost/test_ab1.owl#ClassA") };
640
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
641
        owlApi.load(this.getClass().getResourceAsStream("/test_ab1.owl"));
642
        OWLNamedIndividual indiv = owlApi.getIndividualByUnqualifiedName("IndivA", false);
643
        List<IRI> actual = owlApi.getClassesOfIndividual(indiv);
644
        assertThat(actual, containsInAnyOrder(expected));
645
    }
646
647
    @Test
648
    public void testGetClassesOfIndividual_classesDefinedInDifferentOntology_shouldReturnClasses() throws OWLOntologyCreationException, OwlElementNotFoundException, URISyntaxException {
649
        IRI[] expected = new IRI[] {
650
                IRI.create("http://localhost/test_ab1.owl#ClassA"),
651
                IRI.create("http://localhost/test_ab2.owl#ClassB"),
652
                IRI.create("http://localhost/test_ab3.owl#ClassD")
653
        };
654
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
655
        owlApi.setIRIMapping(new HashMap<String, String>() {{
656
            put("http://localhost/test_ab1.owl", TestUtils.getUri("/test_ab1.owl"));
657
            put("http://localhost/test_ab2.owl", TestUtils.getUri("/test_ab2.owl"));
658
            put("http://localhost/test_ab3.owl", TestUtils.getUri("/test_ab3.owl"));
659
        }});
660
        owlApi.load(this.getClass().getResourceAsStream("/test_ab4.owl"));
661
        OWLNamedIndividual indiv = owlApi.getIndividualByUnqualifiedName("IndivE", false);
662
663
        List<IRI> actual = owlApi.getClassesOfIndividual(indiv);
664
665
        assertThat(actual, containsInAnyOrder(expected));
666
    }
667
668
    @Test
669
    public void testGetClassesOfIndividual_noClassDefined_shouldReturnEmptyList() throws OWLOntologyCreationException, OwlElementNotFoundException, URISyntaxException {
670
        IRI[] expected = new IRI[] { };
671
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
672
        owlApi.load(TestUtils.getUri("/test_ab4.owl"));
673
        OWLNamedIndividual indiv = owlApi.getIndividualByUnqualifiedName("IndivF", false);
674
        List<IRI> actual = owlApi.getClassesOfIndividual(indiv);
675
        assertThat(actual, containsInAnyOrder(expected));
676
    }
677
678
    @Test(expected = ConstraintsViolatedException.class)
679
    public void testGetClassesOfIndividual_indivNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
680
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
681
        owlApi.load(TestUtils.getUri("/test_ab4.owl"));
682
        owlApi.getClassesOfIndividual(null);
683
    }
684
685
    @Test(expected = ConstraintsViolatedException.class)
686
    public void testIsIndividualOfClass_indivNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException {
687
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
688
        owlApi.load(TestUtils.getUri("/test_ab4.owl"));
689
        owlApi.isIndividualOfClass(null, IRI.create("blah"));
690
    }
691
692
    @Test(expected = ConstraintsViolatedException.class)
693
    public void testIsIndividualOfClass_classIriNull_shouldThrowException() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
694
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
695
        owlApi.load(TestUtils.getUri("/test_ab4.owl"));
696
        OWLNamedIndividual indiv = owlApi.getIndividualByUnqualifiedName("IndivF", false);
697
        owlApi.isIndividualOfClass(indiv, null);
698
    }
699
700
    @Test
701
    public void testIsIndividualOfClass_validIndivAndClass_shouldReturnAsExpected() throws URISyntaxException, OWLOntologyCreationException, OwlElementNotFoundException {
702
        OwlApiFacade owlApi = TestUtils.createDefaultOwlApiFacade();
703
        owlApi.load(TestUtils.getUri("/test_ab4.owl"));
704
        OWLNamedIndividual indiv = owlApi.getIndividualByUnqualifiedName("IndivE", false);
705
        assertTrue(owlApi.isIndividualOfClass(indiv, IRI.create("http://localhost/test_ab1.owl#ClassA")));
706
        assertTrue(owlApi.isIndividualOfClass(indiv, IRI.create("http://localhost/test_ab2.owl#ClassB")));
707
        assertTrue(owlApi.isIndividualOfClass(indiv, IRI.create("http://localhost/test_ab3.owl#ClassD")));
708
        assertFalse(owlApi.isIndividualOfClass(indiv, IRI.create("http://localhost/test_ab4.owl#ClassE")));
709
    }
710
711
    //<editor-fold desc="helper methods">
712
713
    private String getOwlUri() throws URISyntaxException {
714
        return TestUtils.getUri("/test.owl");
715
    }
716
717
    //</editor-fold>
718
}
719