|
a |
|
b/src/test/java/org/sbaresearch/owl/JenaQueryFacadeTest.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.ontology.OntModel;
|
|
|
21 |
import com.hp.hpl.jena.ontology.OntModelSpec;
|
|
|
22 |
import com.hp.hpl.jena.query.QuerySolution;
|
|
|
23 |
import com.hp.hpl.jena.rdf.model.Model;
|
|
|
24 |
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
|
|
25 |
import org.apache.commons.io.IOUtils;
|
|
|
26 |
import org.apache.jena.riot.RiotException;
|
|
|
27 |
import org.junit.Test;
|
|
|
28 |
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
|
|
|
29 |
|
|
|
30 |
import java.util.ArrayList;
|
|
|
31 |
import java.util.Arrays;
|
|
|
32 |
import java.util.List;
|
|
|
33 |
|
|
|
34 |
import static org.hamcrest.CoreMatchers.is;
|
|
|
35 |
import static org.hamcrest.Matchers.containsInAnyOrder;
|
|
|
36 |
import static org.junit.Assert.assertThat;
|
|
|
37 |
import static org.junit.Assert.assertTrue;
|
|
|
38 |
|
|
|
39 |
public class JenaQueryFacadeTest {
|
|
|
40 |
|
|
|
41 |
private String sparqlGetAllClasses = "" +
|
|
|
42 |
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
|
|
|
43 |
"SELECT ?subject WHERE { ?subject rdfs:subClassOf ?object }";
|
|
|
44 |
private List<String> allClasses = new ArrayList<String>() {{
|
|
|
45 |
add(TestResources.ontologyBaseUrl + "#A");
|
|
|
46 |
add(TestResources.ontologyBaseUrl + "#B");
|
|
|
47 |
add(TestResources.ontologyBaseUrl + "#C");
|
|
|
48 |
}};
|
|
|
49 |
|
|
|
50 |
@Test(expected = IllegalArgumentException.class)
|
|
|
51 |
public void testQuery_emptyQuery_shouldThrowException() throws OWLOntologyStorageException {
|
|
|
52 |
JenaQueryFacade queryFacade = createQueryFacade();
|
|
|
53 |
queryFacade.query("");
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
@Test(expected = IllegalArgumentException.class)
|
|
|
57 |
public void testQuery_queryIsNull_shouldThrowException() throws OWLOntologyStorageException {
|
|
|
58 |
JenaQueryFacade queryFacade = createQueryFacade();
|
|
|
59 |
queryFacade.query(null);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
@Test(expected = InvalidQueryException.class)
|
|
|
63 |
public void testQuery_invalidQuery_shouldThrowException() throws OWLOntologyStorageException {
|
|
|
64 |
JenaQueryFacade queryFacade = createQueryFacade();
|
|
|
65 |
queryFacade.query("blah");
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
@Test
|
|
|
69 |
public void testExtractColumn_shouldReturnSpecifiedColumn() throws OWLOntologyStorageException {
|
|
|
70 |
JenaQueryFacade queryFacade = createQueryFacade();
|
|
|
71 |
List<String> actual = JenaQueryFacade.extractColumn(queryFacade.query(sparqlGetAllClasses), "subject");
|
|
|
72 |
assertThat(actual, containsInAnyOrder(allClasses.toArray()));
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
@Test
|
|
|
76 |
public void testRemoveInColumn_resultsContainNeedle_shouldRemoveNeedle() {
|
|
|
77 |
List<String> actual = JenaQueryFacade.removeInColumn(Arrays.asList("blah#01", "blah#02"), "blah#");
|
|
|
78 |
assertThat(actual, containsInAnyOrder("01", "02"));
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
@Test
|
|
|
82 |
public void testRemoveInColumn_resultsDoesNotContainNeedle_shouldNotChangeAnything() {
|
|
|
83 |
List<String> actual = JenaQueryFacade.removeInColumn(Arrays.asList("blah#01", "blah#02"), "blub#");
|
|
|
84 |
assertThat(actual, containsInAnyOrder("blah#01", "blah#02"));
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
@Test
|
|
|
88 |
public void testRemoveNamespace_containingNamespaces_shouldReturnFragments() {
|
|
|
89 |
List<String> actual = JenaQueryFacade.removeNamespace(Arrays.asList("blah#01", "blah#02"));
|
|
|
90 |
assertThat(actual, containsInAnyOrder("01", "02"));
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
@Test
|
|
|
94 |
public void testRemoveNamespace_containingFragmentsOnly_shouldNotAlterList() {
|
|
|
95 |
List<String> actual = JenaQueryFacade.removeNamespace(Arrays.asList("01", "02"));
|
|
|
96 |
assertThat(actual, containsInAnyOrder("01", "02"));
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
@Test
|
|
|
100 |
public void testQuery_validQuery_shouldReturnResults() throws OWLOntologyStorageException {
|
|
|
101 |
JenaQueryFacade queryFacade = createQueryFacade();
|
|
|
102 |
List<QuerySolution> actual = queryFacade.query(sparqlGetAllClasses);
|
|
|
103 |
for (QuerySolution solution : actual) {
|
|
|
104 |
assertTrue(allClasses.contains(solution.get("subject").toString()));
|
|
|
105 |
}
|
|
|
106 |
assertThat(actual.size(), is(allClasses.size()));
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
@Test(expected = RiotException.class)
|
|
|
110 |
public void testQuery_queryWithUndefinedPrefix_shouldThrowException() throws OWLOntologyStorageException {
|
|
|
111 |
JenaQueryFacade queryFacade = createQueryFacadeWithoutPrefix();
|
|
|
112 |
queryFacade.query(sparqlGetAllClasses);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
private JenaQueryFacade createQueryFacade() {
|
|
|
116 |
return new JenaQueryFacade(createModel(TestResources.testOntology));
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
private JenaQueryFacade createQueryFacadeWithoutPrefix() {
|
|
|
120 |
return new JenaQueryFacade(createModel(TestResources.testOntologyWithoutPrefix));
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
private Model createModel(String ontology) {
|
|
|
124 |
OntModel ontologyModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
|
|
125 |
return ontologyModel.read(IOUtils.toInputStream(ontology), TestResources.ontologyBaseUrl);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
}
|