Skip to content

Package: QueryRunner

QueryRunner

nameinstructionbranchcomplexitylinemethod
QueryRunner(Logger, DataAccessor)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
executeUpdateRunsDeleteOnRepository()
M: 0 C: 37
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
executeUpdateRunsInsertOnRepository()
M: 0 C: 38
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
executeUpdateRunsUpdateOnRepository()
M: 0 C: 41
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
lambda$1(Query)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$4(Map, OWLClassA)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$5(Map, OWLClassA)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$6(Map, OWLClassA)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$7(Map, OWLClassD)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$8(Map, OWLClassT)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
querySupportsProcessingResultsUsingStream()
M: 0 C: 50
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
queryWithConstructorMappingReturnsCorrectInstances()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
queryWithEntityMappingLoadsReferencedEntitiesAsWell()
M: 0 C: 77
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 18
100%
M: 0 C: 1
100%
queryWithEntityMappingLoadsReferencedEntityAndInheritedAttributes()
M: 0 C: 90
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
queryWithEntityMappingReturnsCorrectManagedInstances()
M: 0 C: 36
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
queryWithMappingReturnsResultWithVariablesMappedAccordingly()
M: 0 C: 57
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
selectTypesWithDisableInferenceQueryHintReturnsOnlyAssertedTypes()
M: 0 C: 66
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
selectWithOptionalReturnsNullInUnfilledColumns()
M: 4 C: 50
93%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 14
93%
M: 0 C: 1
100%
selectionByObjectPropertySupportsEntityAsQueryParameter()
M: 0 C: 43
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
settingStringParameterEscapesTheParameterValue()
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
testCreateNamedNativeQueryWithParameterSetting()
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
testGetSingleResult()
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testGetSingleResultMultiples()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
testGetSingleResultNoResult()
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
testSelectByDataProperty()
M: 0 C: 54
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
testSelectByObjectProperty()
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
testSelectByType()
M: 0 C: 60
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 16
100%
M: 0 C: 1
100%
testSelectQueryWithPositionalParameters()
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testSelectTypes()
M: 0 C: 59
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
testSetMaxResults()
M: 1 C: 40
98%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 11
100%
M: 0 C: 1
100%
testSetMaxResultsNegative()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
testSetMaxResultsZero()
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
verifyOWLClassAInstances(List, Map)
M: 0 C: 31
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
verifyOwlClassAInstance(OWLClassA, OWLClassA)
M: 3 C: 20
87%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 6
75%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.test.query.runner;
14:
15: import cz.cvut.kbss.jopa.exceptions.NoResultException;
16: import cz.cvut.kbss.jopa.exceptions.NoUniqueResultException;
17: import cz.cvut.kbss.jopa.model.EntityManager;
18: import cz.cvut.kbss.jopa.model.annotations.OWLClass;
19: import cz.cvut.kbss.jopa.model.query.Query;
20: import cz.cvut.kbss.jopa.query.QueryHints;
21: import cz.cvut.kbss.jopa.test.*;
22: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
23: import cz.cvut.kbss.jopa.test.environment.Generators;
24: import cz.cvut.kbss.jopa.test.environment.Quad;
25: import cz.cvut.kbss.jopa.test.query.QueryTestEnvironment;
26: import cz.cvut.kbss.jopa.vocabulary.RDFS;
27: import cz.cvut.kbss.ontodriver.model.LangString;
28: import org.junit.jupiter.api.Test;
29: import org.slf4j.Logger;
30:
31: import java.net.URI;
32: import java.util.*;
33: import java.util.stream.Collectors;
34:
35: import static org.hamcrest.MatcherAssert.assertThat;
36: import static org.hamcrest.Matchers.greaterThanOrEqualTo;
37: import static org.junit.jupiter.api.Assertions.*;
38:
39: public abstract class QueryRunner extends BaseQueryRunner {
40:
41: private static final String SELECT_E_BY_TYPE =
42: "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
43:
44: protected QueryRunner(Logger logger, DataAccessor dataAccessor) {
45: super(logger, dataAccessor);
46: }
47:
48: @Test
49: void testSelectByType() {
50: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#TypeA> . }";
51: final Query q = getEntityManager().createNativeQuery(query);
52: final List<OWLClassA> as = QueryTestEnvironment.getDataByContext(null, OWLClassA.class);
53: assertNotNull(q);
54:
55: final List res = q.getResultList();
56:
57: assertNotNull(res);
58: assertFalse(res.isEmpty());
59: assertEquals(as.size(), res.size());
60: boolean found;
61:• for (OWLClassA a : as) {
62: found = false;
63:• for (Object row : res) {
64:• if (a.getUri().equals(row)) {
65: found = true;
66: break;
67: }
68: }
69: assertTrue(found);
70: }
71: }
72:
73: @Test
74: void testSelectByDataProperty() {
75: final String query =
76: "SELECT ?y WHERE { ?x ?stringAtt ?y . }";
77: final Query q = getEntityManager().createNativeQuery(query)
78: .setParameter("stringAtt", URI.create(Vocabulary.P_B_STRING_ATTRIBUTE));
79: final Set<String> exp = QueryTestEnvironment.getDataByContext(null, OWLClassB.class).stream()
80: .map(OWLClassB::getStringAttribute).collect(Collectors.toSet());
81:
82: final List res = q.getResultList();
83:
84: assertNotNull(res);
85: assertFalse(res.isEmpty());
86: assertEquals(exp.size(), res.size());
87:• for (Object lst2 : res) {
88: assertTrue(lst2 instanceof LangString);
89: // False means we got the expected value
90: assertFalse(exp.add(((LangString) lst2).getValue()));
91: }
92: }
93:
94: @Test
95: void testSelectByObjectProperty() {
96: final OWLClassD d = QueryTestEnvironment.getData(OWLClassD.class).get(0);
97: final String query = "SELECT ?x WHERE { ?x a ?type ; ?hasA ?y . }";
98: final Query q = getEntityManager().createNativeQuery(query);
99: q.setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_D))
100: .setParameter("hasA", URI.create(Vocabulary.P_HAS_OWL_CLASS_A)).setParameter("y", d.getOwlClassA().getUri());
101:
102: final List res = q.getResultList();
103:
104: assertEquals(1, res.size());
105: final Object subRes = res.get(0);
106: assertEquals(d.getUri(), subRes);
107: }
108:
109: @Test
110: void testSelectTypes() {
111: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
112: final Set<String> types = a.getTypes();
113: types.add(a.getClass().getAnnotation(OWLClass.class).iri());
114: final String query = "SELECT ?x WHERE { ?instance a ?x . }";
115: final Query q = getEntityManager().createNativeQuery(query);
116: q.setParameter("instance", a.getUri());
117:
118: final List res = q.getResultList();
119: // The result can contain more types (inference)
120: assertThat(res.size(), greaterThanOrEqualTo(types.size()));
121:• for (String type : types) {
122: assertTrue(res.contains(URI.create(type)));
123: }
124: }
125:
126: @Test
127: void testSetMaxResults() {
128: final Query q = getEntityManager().createNativeQuery(SELECT_E_BY_TYPE);
129: final int max = 5;
130:• assertTrue(max < QueryTestEnvironment.getData(OWLClassE.class).size());
131: assertEquals(Integer.MAX_VALUE, q.getMaxResults());
132: q.setMaxResults(max);
133: assertEquals(max, q.getMaxResults());
134:
135: final List res = q.getResultList();
136: assertNotNull(res);
137: assertFalse(res.isEmpty());
138: assertEquals(max, res.size());
139: }
140:
141: @Test
142: void testSetMaxResultsNegative() {
143: final Query q = getEntityManager().createNativeQuery(SELECT_E_BY_TYPE);
144: assertThrows(IllegalArgumentException.class, () -> q.setMaxResults(-1));
145: }
146:
147: @Test
148: void testSetMaxResultsZero() {
149: final Query q = getEntityManager().createNativeQuery(SELECT_E_BY_TYPE);
150: q.setMaxResults(0);
151:
152: final List res = q.getResultList();
153: assertNotNull(res);
154: assertTrue(res.isEmpty());
155: }
156:
157: @Test
158: void testGetSingleResult() {
159: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
160: final String query =
161: "SELECT ?x WHERE { ?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#A-stringAttribute> ?y .}";
162: final Query q = getEntityManager().createNativeQuery(query);
163: q.setParameter("y", a.getStringAttribute(), "en");
164:
165: final Object res = q.getSingleResult();
166: assertNotNull(res);
167: assertEquals(a.getUri(), res);
168: }
169:
170: @Test
171: void testGetSingleResultMultiples() {
172: final Query q = getEntityManager().createNativeQuery(SELECT_E_BY_TYPE);
173: assertThrows(NoUniqueResultException.class, q::getSingleResult);
174: }
175:
176: @Test
177: void testGetSingleResultNoResult() {
178: final String query =
179: "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassX> . }";
180: final Query q = getEntityManager().createNativeQuery(query);
181: assertThrows(NoResultException.class, q::getSingleResult);
182: }
183:
184: @Test
185: void testSelectQueryWithPositionalParameters() {
186: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
187: final String query =
188: "SELECT ?x WHERE { ?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#A-stringAttribute> $ .}";
189: final Query q = getEntityManager().createNativeQuery(query);
190: q.setParameter(1, a.getStringAttribute(), "en");
191:
192: final Object res = q.getSingleResult();
193: assertNotNull(res);
194: assertEquals(a.getUri(), res);
195: }
196:
197: @Test
198: public void selectWithOptionalReturnsNullInUnfilledColumns() {
199: final String query =
200: "SELECT ?x ?s WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> ." +
201: " OPTIONAL {?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#E-stringAttribute> ?s . } }";
202: final OWLClassE e = new OWLClassE();
203: final EntityManager em = getEntityManager();
204: em.getTransaction().begin();
205: em.persist(e);
206: em.getTransaction().commit();
207: final Query q = em.createNativeQuery(query);
208:
209: final List result = q.getResultList();
210: assertFalse(result.isEmpty());
211:• for (Object row : result) {
212: final Object[] rowArr = (Object[]) row;
213:• if (rowArr[0].equals(e.getUri().toString())) {
214: assertNull(rowArr[1]);
215: }
216: }
217: }
218:
219: @Test
220: void testCreateNamedNativeQueryWithParameterSetting() {
221: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
222: final List res = getEntityManager().createNamedQuery("OWLClassA.findByString")
223: .setParameter("str", a.getStringAttribute(), "en").getResultList();
224: assertFalse(res.isEmpty());
225: assertTrue(res.contains(a.getUri()));
226: }
227:
228: @Test
229: void queryWithMappingReturnsResultWithVariablesMappedAccordingly() {
230: final List res = getEntityManager().createNativeQuery("SELECT * WHERE {" +
231: "?x a <" + Vocabulary.C_OWL_CLASS_A + "> ;" +
232: "<" + Vocabulary.P_A_STRING_ATTRIBUTE + "> ?y ." +
233: "}", OWLClassA.VARIABLE_MAPPING).getResultList();
234: final Map<String, Object> expected = new HashMap<>();
235: QueryTestEnvironment.getData(OWLClassA.class)
236: .forEach(a -> expected.put(a.getUri().toString(),
237: new LangString(a.getStringAttribute(), "en")));
238: assertEquals(expected.size(), res.size());
239:• for (Object row : res) {
240: assertTrue(row instanceof Object[]);
241: final Object[] elems = (Object[]) row;
242: assertEquals(2, elems.length);
243: assertTrue(expected.containsKey(elems[0]));
244: assertEquals(expected.get(elems[0]), elems[1]);
245: }
246: }
247:
248: @Test
249: void queryWithConstructorMappingReturnsCorrectInstances() {
250: final List res = getEntityManager().createNativeQuery("SELECT * WHERE {" +
251: "?x a <" + Vocabulary.C_OWL_CLASS_A + "> ;" +
252: "<" + Vocabulary.P_A_STRING_ATTRIBUTE + "> ?y ." +
253: "}", OWLClassA.CONSTRUCTOR_MAPPING)
254: .getResultList();
255: final Map<URI, OWLClassA> expected = new HashMap<>();
256: QueryTestEnvironment.getData(OWLClassA.class).forEach(a -> expected.put(a.getUri(), a));
257:
258: verifyOWLClassAInstances(res, expected);
259: }
260:
261: private void verifyOWLClassAInstances(List res, Map<URI, OWLClassA> expected) {
262:• for (Object item : res) {
263: assertTrue(item instanceof OWLClassA);
264: final OWLClassA a = (OWLClassA) item;
265: assertTrue(expected.containsKey(a.getUri()));
266: assertEquals(expected.get(a.getUri()).getStringAttribute(), a.getStringAttribute());
267: }
268: }
269:
270: @Test
271: void queryWithEntityMappingReturnsCorrectManagedInstances() {
272: final List res = getEntityManager().createNativeQuery("SELECT * WHERE {" +
273: "?x a <" + Vocabulary.C_OWL_CLASS_A + "> ;" +
274: "<" + Vocabulary.P_A_STRING_ATTRIBUTE + "> ?stringAttribute ." +
275: "}", OWLClassA.ENTITY_MAPPING).getResultList();
276: final Map<URI, OWLClassA> expected = new HashMap<>();
277: QueryTestEnvironment.getData(OWLClassA.class).forEach(a -> expected.put(a.getUri(), a));
278:
279: verifyOWLClassAInstances(res, expected);
280:• for (Object item : res) {
281: assertTrue(getEntityManager().contains(item));
282: }
283: }
284:
285: @Test
286: void queryWithEntityMappingLoadsReferencedEntitiesAsWell() {
287: final List res = getEntityManager().createNativeQuery("SELECT ?x ?y WHERE {" +
288: "?x a ?dType ;" +
289: "?hasA ?y . }", OWLClassD.MAPPING_NAME)
290: .setParameter("dType", URI.create(Vocabulary.C_OWL_CLASS_D))
291: .setParameter("hasA", URI.create(Vocabulary.P_HAS_OWL_CLASS_A))
292: .getResultList();
293: final Map<URI, OWLClassD> expected = new HashMap<>();
294: QueryTestEnvironment.getData(OWLClassD.class).forEach(d -> expected.put(d.getUri(), d));
295:
296: assertEquals(expected.size(), res.size());
297:• for (Object row : res) {
298: assertTrue(row instanceof OWLClassD);
299: final OWLClassD inst = (OWLClassD) row;
300: assertTrue(expected.containsKey(inst.getUri()));
301: assertNotNull(inst.getOwlClassA());
302: final OWLClassA expectedA = expected.get(inst.getUri()).getOwlClassA();
303: verifyOwlClassAInstance(expectedA, inst.getOwlClassA());
304: assertTrue(getEntityManager().contains(inst));
305: assertTrue(getEntityManager().contains(inst.getOwlClassA()));
306: }
307: }
308:
309: private void verifyOwlClassAInstance(OWLClassA expected, OWLClassA actual) {
310:• if (expected == null) {
311: assertNull(actual);
312: return;
313: }
314: assertNotNull(actual);
315: assertEquals(expected.getUri(), actual.getUri());
316: assertEquals(expected.getStringAttribute(), actual.getStringAttribute());
317: assertEquals(expected.getTypes(), actual.getTypes());
318: }
319:
320: @Test
321: public void queryWithEntityMappingLoadsReferencedEntityAndInheritedAttributes() {
322: final List res = getEntityManager().createNativeQuery("SELECT * WHERE {" +
323: "?x a ?type ;" +
324: "?hasA ?y ;" +
325: "?rdfsLabel ?label ;" +
326: "?hasDescription ?description ;" +
327: "?hasInt ?intAttribute ." +
328: "}", OWLClassT.MAPPING_NAME)
329: .setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_T))
330: .setParameter("hasA", URI.create(Vocabulary.P_HAS_OWL_CLASS_A))
331: .setParameter("rdfsLabel", URI.create(RDFS.LABEL))
332: .setParameter("hasDescription", URI.create(Vocabulary.DC_DESCRIPTION))
333: .setParameter("hasInt", URI.create(Vocabulary.P_T_INTEGER_ATTRIBUTE))
334: .getResultList();
335: final Map<URI, OWLClassT> expected = new HashMap<>();
336: QueryTestEnvironment.getData(OWLClassT.class).forEach(t -> expected.put(t.getUri(), t));
337:
338: assertEquals(expected.size(), res.size());
339:• for (Object row : res) {
340: assertTrue(row instanceof OWLClassT);
341: final OWLClassT tActual = (OWLClassT) row;
342: assertTrue(expected.containsKey(tActual.getUri()));
343: final OWLClassT tExpected = expected.get(tActual.getUri());
344: assertEquals(tExpected.getName(), tActual.getName());
345: assertEquals(tExpected.getDescription(), tActual.getDescription());
346: assertEquals(tExpected.getIntAttribute(), tActual.getIntAttribute());
347: verifyOwlClassAInstance(tExpected.getOwlClassA(), tActual.getOwlClassA());
348: }
349: }
350:
351: @Test
352: public void executeUpdateRunsUpdateOnRepository() {
353: final EntityManager em = getEntityManager();
354: final OWLClassA instance = QueryTestEnvironment.getData(OWLClassA.class).get(0);
355: final String newValue = "UpdatedValue";
356: final String update = "DELETE { ?inst ?property ?origValue . }" +
357: "INSERT { ?inst ?property ?newValue . } WHERE {" +
358: "?inst ?property ?origValue . }";
359: em.createNativeQuery(update).setParameter("inst", instance.getUri()).setParameter("property", URI.create(
360: Vocabulary.P_A_STRING_ATTRIBUTE)).setParameter("newValue", newValue, "en").executeUpdate();
361:
362: final OWLClassA result = em.find(OWLClassA.class, instance.getUri());
363: assertEquals(newValue, result.getStringAttribute());
364: }
365:
366: @Test
367: public void executeUpdateRunsDeleteOnRepository() {
368: final EntityManager em = getEntityManager();
369: final OWLClassA instance = QueryTestEnvironment.getData(OWLClassA.class).get(0);
370: assertNotNull(instance.getStringAttribute());
371: final String update = "DELETE { ?inst ?property ?origValue . } WHERE {" +
372: "?inst ?property ?origValue . }";
373: em.createNativeQuery(update).setParameter("inst", instance.getUri())
374: .setParameter("property", URI.create(Vocabulary.P_A_STRING_ATTRIBUTE)).executeUpdate();
375:
376: final OWLClassA result = em.find(OWLClassA.class, instance.getUri());
377: assertNull(result.getStringAttribute());
378: }
379:
380: @Test
381: public void executeUpdateRunsInsertOnRepository() {
382: final EntityManager em = getEntityManager();
383: final URI newType = Generators.generateUri();
384: final OWLClassA instance = QueryTestEnvironment.getData(OWLClassA.class).get(0);
385: final String update = "INSERT DATA { ?inst a ?newType . }";
386: em.createNativeQuery(update).setParameter("inst", instance.getUri())
387: .setParameter("newType", newType).executeUpdate();
388:
389: final OWLClassA result = em.find(OWLClassA.class, instance.getUri());
390: assertTrue(result.getTypes().contains(newType.toString()));
391: }
392:
393: @Test
394: void settingStringParameterEscapesTheParameterValue() {
395: final EntityManager em = getEntityManager();
396: final String query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n" +
397: "SELECT ?x WHERE { ?x rdfs:comment ?comment . }";
398: final String paramValue = "string\nWith\nNewlines";
399: final List result = em.createNativeQuery(query).setParameter("comment", paramValue, "en").getResultList();
400: assertTrue(result.isEmpty()); // The point here is that no exception is thrown and a result is returned
401: }
402:
403: @Test
404: void querySupportsProcessingResultsUsingStream() {
405: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
406: final Set<String> types = a.getTypes();
407: types.add(a.getClass().getAnnotation(OWLClass.class).iri());
408: final String query = "SELECT ?x WHERE { ?instance a ?x . }";
409: final Query q = getEntityManager().createNativeQuery(query).setParameter("instance", a.getUri());
410:
411: final Set<String> result = (Set<String>) q.getResultStream().map(Object::toString).collect(Collectors.toSet());
412: assertTrue(result.containsAll(types));
413: assertThat(result.size(), greaterThanOrEqualTo(types.size()));
414: }
415:
416: @Test
417: void selectionByObjectPropertySupportsEntityAsQueryParameter() {
418: final OWLClassD d = QueryTestEnvironment.getData(OWLClassD.class).get(0);
419: final String query = "SELECT ?x WHERE { ?x a ?type ; ?hasA ?y . }";
420: final Query q = getEntityManager().createNativeQuery(query);
421: q.setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_D))
422: .setParameter("hasA", URI.create(Vocabulary.P_HAS_OWL_CLASS_A)).setParameter("y", d.getOwlClassA());
423:
424: final List res = q.getResultList();
425:
426: assertEquals(1, res.size());
427: final Object subRes = res.get(0);
428: assertEquals(d.getUri(), subRes);
429: }
430:
431: /**
432: * Enhancement #101
433: */
434: @Test
435: public void selectTypesWithDisableInferenceQueryHintReturnsOnlyAssertedTypes() throws Exception {
436: final String superType = Vocabulary.CLASS_IRI_BASE + "A-superclass";
437: persistTestData(Collections.singleton(
438: new Quad(URI.create(Vocabulary.C_OWL_CLASS_A), URI.create(RDFS.SUB_CLASS_OF), URI.create(superType))),
439: getEntityManager());
440: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
441: final Set<String> types = new HashSet<>(a.getTypes());
442: types.add(a.getClass().getAnnotation(OWLClass.class).iri());
443: final String query = "SELECT DISTINCT ?type WHERE { ?instance a ?type . }";
444: final Query q = getEntityManager().createNativeQuery(query);
445: q.setParameter("instance", a.getUri())
446: .setHint(QueryHints.DISABLE_INFERENCE, true);
447:
448: final List res = q.getResultList();
449: // The result can contain more types (inference)
450: assertEquals(types, res.stream().map(Object::toString).collect(Collectors.toSet()));
451: }
452: }