Skip to content

Package: TypedQueryRunner

TypedQueryRunner

nameinstructionbranchcomplexitylinemethod
TypedQueryRunner(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%
askQueryAgainstTransactionalOntologyContainsUncommittedChangesAsWell()
M: 44 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
askQueryReturnsFalse()
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
askQueryReturnsTrue()
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
askQueryWithPositionParameter()
M: 0 C: 28
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
cleanupTestData(String)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
lambda$querySupportsCollectionParameters$10(OWLClassA, 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$querySupportsCollectionParameters$9(OWLClassA)
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$querySupportsProcessingResultsUsingStream$7(Set, URI)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$querySupportsSelectionByDate$11(long, int)
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
lambda$querySupportsSelectionByDate$12(LocalDateTime, OWLClassM)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$querySupportsSelectionByDate$13(OWLClassM, OWLClassM)
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$querySupportsSelectionByDate$14(List, OWLClassM)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$selectionByObjectPropertySupportsEntityAsQueryParameter$8(OWLClassA, OWLClassD)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$testCreateQueryNullClass$3()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$testCreateQueryNullQuery$2()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$testCreateTypedNamedNativeQuery$4(List, 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$testSelectByObjectProperty$0(OWLClassA, OWLClassD)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$testSetMaxResultsNegative$1(TypedQuery)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$usingDescriptorAllowsToCustomizeQueryResults$5(OWLClassA)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$usingDescriptorAllowsToCustomizeQueryResults$6(OWLClassA)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
querySupportsCollectionParameters()
M: 0 C: 52
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
querySupportsProcessingResultsUsingStream()
M: 0 C: 36
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
querySupportsSelectionByDate()
M: 0 C: 64
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
querySupportsSelectionByEntityIdentifier()
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
selectionByObjectPropertySupportsEntityAsQueryParameter()
M: 0 C: 82
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 18
100%
M: 0 C: 1
100%
setFirstResultCanBeUsedToOffsetFirstQueryResult()
M: 0 C: 50
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testCreateQueryNullClass()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
testCreateQueryNullQuery()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
testCreateTypedNamedNativeQuery()
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
testFindAll()
M: 0 C: 80
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 20
100%
M: 0 C: 1
100%
testGetSingleResult()
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%
testGetSingleResultMultiples()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
testGetSingleResultNoResult()
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
testSelectByObjectProperty()
M: 0 C: 51
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
testSelectByTypeAndDataPropertyValue()
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
testSetMaxResults()
M: 1 C: 45
98%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 12
100%
M: 0 C: 1
100%
testSetMaxResultsNegative()
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
testSetMaxResultsZero()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
usingDescriptorAllowsToCustomizeQueryResults()
M: 0 C: 32
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
usingUntypedQueryAllowsToSpecifyLimitInQuery()
M: 0 C: 28
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.test.query.runner;
19:
20: import cz.cvut.kbss.jopa.exceptions.NoResultException;
21: import cz.cvut.kbss.jopa.exceptions.NoUniqueResultException;
22: import cz.cvut.kbss.jopa.model.annotations.OWLClass;
23: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
24: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
25: import cz.cvut.kbss.jopa.model.query.TypedQuery;
26: import cz.cvut.kbss.jopa.query.QueryHints;
27: import cz.cvut.kbss.jopa.test.*;
28: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
29: import cz.cvut.kbss.jopa.test.environment.Generators;
30: import cz.cvut.kbss.jopa.test.query.QueryTestEnvironment;
31: import cz.cvut.kbss.ontodriver.Statement;
32: import org.junit.jupiter.api.Disabled;
33: import org.junit.jupiter.api.Test;
34: import org.slf4j.Logger;
35:
36: import java.net.URI;
37: import java.time.LocalDateTime;
38: import java.time.ZoneOffset;
39: import java.time.temporal.ChronoUnit;
40: import java.util.*;
41: import java.util.stream.Collectors;
42: import java.util.stream.IntStream;
43:
44: import static org.junit.jupiter.api.Assertions.*;
45:
46: public abstract class TypedQueryRunner extends BaseQueryRunner {
47:
48: private static final String SELECT_BY_TYPE = "SELECT ?x WHERE { ?x a ?type .}";
49:
50: protected TypedQueryRunner(Logger logger, DataAccessor dataAccessor) {
51: super(logger, dataAccessor);
52: }
53:
54: @Test
55: void testFindAll() {
56: final TypedQuery<OWLClassD> q =
57: getEntityManager().createNativeQuery(SELECT_BY_TYPE, OWLClassD.class).setParameter("type", URI.create(
58: Vocabulary.C_OWL_CLASS_D));
59: final List<OWLClassD> ds = new ArrayList<>(QueryTestEnvironment.getDataByContext(null, OWLClassD.class));
60: final List<OWLClassD> res = q.getResultList();
61: assertNotNull(res);
62: assertFalse(res.isEmpty());
63: assertEquals(ds.size(), res.size());
64: boolean found;
65:• for (OWLClassD d : ds) {
66: found = false;
67:• for (OWLClassD dd : res) {
68:• if (d.getUri().equals(dd.getUri())) {
69: found = true;
70: assertNotNull(dd.getOwlClassA());
71: assertEquals(d.getOwlClassA().getUri(), dd.getOwlClassA().getUri());
72: assertNotNull(dd.getOwlClassA().getStringAttribute());
73: break;
74: }
75: }
76: assertTrue(found);
77: }
78: }
79:
80: @Test
81: void testSelectByTypeAndDataPropertyValue() {
82: final OWLClassB b = QueryTestEnvironment.getData(OWLClassB.class).get(5);
83: final String query =
84: "SELECT ?x WHERE { " +
85: "?x a ?type ; " +
86: "?stringAtt ?bString . }";
87: final TypedQuery<OWLClassB> q = getEntityManager().createNativeQuery(query, OWLClassB.class);
88: q.setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_B))
89: .setParameter("stringAtt", URI.create(Vocabulary.P_B_STRING_ATTRIBUTE))
90: .setParameter("bString", b.getStringAttribute(), "en");
91: final OWLClassB res = q.getSingleResult();
92: assertNotNull(res);
93: assertEquals(b.getUri(), res.getUri());
94: assertEquals(b.getStringAttribute(), res.getStringAttribute());
95: }
96:
97: @Test
98: void testSelectByObjectProperty() {
99: final String query = "SELECT ?x WHERE { ?x a ?type ; ?hasA ?y . }";
100: final List<OWLClassD> ds = new ArrayList<>(QueryTestEnvironment.getData(OWLClassD.class));
101: final OWLClassA a = ds.get(Generators.randomPositiveInt(2, ds.size())).getOwlClassA();
102: final TypedQuery<OWLClassD> q = getEntityManager().createNativeQuery(query, OWLClassD.class)
103: .setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_D))
104: .setParameter("hasA",
105: URI.create(Vocabulary.P_HAS_OWL_CLASS_A))
106: .setParameter("y", a.getUri());
107:
108: final List<OWLClassD> expected = ds.stream().filter(d -> d.getOwlClassA().getUri().equals(a.getUri())).toList();
109: final List<OWLClassD> res = q.getResultList();
110: assertEquals(expected.size(), res.size());
111: }
112:
113: @Test
114: void testSetMaxResults() {
115: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(SELECT_BY_TYPE, OWLClassE.class)
116: .setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_E));
117: final int max = 5;
118:• assertTrue(max < QueryTestEnvironment.getData(OWLClassE.class).size());
119: assertEquals(Integer.MAX_VALUE, q.getMaxResults());
120: q.setMaxResults(max);
121: assertEquals(max, q.getMaxResults());
122: final List<OWLClassE> res = q.getResultList();
123: assertNotNull(res);
124: assertFalse(res.isEmpty());
125: assertEquals(max, res.size());
126: }
127:
128: @Test
129: void testSetMaxResultsNegative() {
130: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(SELECT_BY_TYPE, OWLClassE.class)
131: .setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_E));
132: assertThrows(IllegalArgumentException.class, () -> q.setMaxResults(-1));
133: }
134:
135: @Test
136: void testSetMaxResultsZero() {
137: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(SELECT_BY_TYPE, OWLClassE.class)
138: .setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_E));
139: q.setMaxResults(0);
140: final List<OWLClassE> res = q.getResultList();
141: assertNotNull(res);
142: assertTrue(res.isEmpty());
143: }
144:
145: @Test
146: void testGetSingleResult() {
147: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
148: final String query =
149: "SELECT ?x WHERE { ?x ?stringAtt ?aString .}";
150: final TypedQuery<OWLClassA> q = getEntityManager().createNativeQuery(query, OWLClassA.class);
151: q.setParameter("stringAtt", URI.create(Vocabulary.P_A_STRING_ATTRIBUTE))
152: .setParameter("aString", a.getStringAttribute(), "en");
153: final OWLClassA res = q.getSingleResult();
154: assertNotNull(res);
155: assertEquals(a.getUri(), res.getUri());
156: }
157:
158: @Test
159: void testGetSingleResultMultiples() {
160: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(SELECT_BY_TYPE, OWLClassE.class)
161: .setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_E));
162: assertThrows(NoUniqueResultException.class, q::getSingleResult);
163: }
164:
165: @Test
166: void testGetSingleResultNoResult() {
167: final String query =
168: "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassX> . }";
169: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(query, OWLClassE.class);
170: assertThrows(NoResultException.class, q::getSingleResult);
171: }
172:
173: @Test
174: void testCreateQueryNullQuery() {
175: assertThrows(NullPointerException.class, () -> getEntityManager().createNativeQuery(null, OWLClassA.class));
176: }
177:
178: @Test
179: void testCreateQueryNullClass() {
180: final String query = "SELECT ?x WHERE { ?x ?y ?z .}";
181: assertThrows(NullPointerException.class,
182: () -> getEntityManager().createNativeQuery(query, (Class<OWLClassA>) null));
183: }
184:
185: @Test
186: void askQueryReturnsTrue() {
187: final String query = "ASK { ?x a ?type . }";
188: final TypedQuery<Boolean> q = getEntityManager().createNativeQuery(query, Boolean.class)
189: .setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_A));
190: final Boolean res = q.getSingleResult();
191: assertNotNull(res);
192: assertTrue(res);
193: }
194:
195: @Test
196: void askQueryReturnsFalse() {
197: final String query = "ASK { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassX> . }";
198: final TypedQuery<Boolean> q = getEntityManager().createNativeQuery(query, Boolean.class);
199: final List<Boolean> res = q.getResultList();
200: assertNotNull(res);
201: assertEquals(1, res.size());
202: assertFalse(res.get(0));
203: }
204:
205: @Disabled
206: @Test
207: public void askQueryAgainstTransactionalOntologyContainsUncommittedChangesAsWell() {
208: final OWLClassE e = new OWLClassE();
209: getEntityManager().getTransaction().begin();
210: try {
211: getEntityManager().persist(e);
212: final TypedQuery<Boolean> query = getEntityManager().createNativeQuery(
213: "ASK { ?individual a ?type . }",
214: Boolean.class).setParameter("individual", e.getUri()).setParameter("type",
215: URI.create(Vocabulary.C_OWL_CLASS_E));
216: query.setHint(QueryHints.TARGET_ONTOLOGY, Statement.StatementOntology.TRANSACTIONAL.toString());
217: final Boolean res = query.getSingleResult();
218: assertTrue(res);
219: } finally {
220: getEntityManager().getTransaction().rollback();
221: }
222: }
223:
224: @Test
225: void askQueryWithPositionParameter() {
226: final String query = "ASK { ?x a $1 . }";
227: final URI paramValue = URI.create(OWLClassA.class.getAnnotation(OWLClass.class).iri());
228: final TypedQuery<Boolean> q = getEntityManager().createNativeQuery(query, Boolean.class)
229: .setParameter(1, paramValue);
230: final Boolean res = q.getSingleResult();
231: assertNotNull(res);
232: assertTrue(res);
233: }
234:
235: @Test
236: void testCreateTypedNamedNativeQuery() {
237: final List<OWLClassA> expected = QueryTestEnvironment.getData(OWLClassA.class);
238: final List<URI> uris = expected.stream().map(OWLClassA::getUri).toList();
239: final List<OWLClassA> res = getEntityManager().createNamedQuery("OWLClassA.findAll", OWLClassA.class)
240: .getResultList();
241: assertEquals(expected.size(), res.size());
242: res.forEach(a -> assertTrue(uris.contains(a.getUri())));
243: }
244:
245: @Test
246: void usingDescriptorAllowsToCustomizeQueryResults() {
247: final List<OWLClassA> expected = QueryTestEnvironment.getData(OWLClassA.class);
248: expected.forEach(a -> assertNotNull(a.getStringAttribute()));
249: final Descriptor descriptor = new EntityDescriptor();
250: descriptor.setLanguage("cs");
251: final List<OWLClassA> result = getEntityManager().createNamedQuery("OWLClassA.findAll", OWLClassA.class)
252: .setDescriptor(descriptor).getResultList();
253: assertEquals(expected.size(), result.size());
254: result.forEach(a -> assertNull(a.getStringAttribute())); // Because the data has @en language tag
255: }
256:
257: @Test
258: public void usingUntypedQueryAllowsToSpecifyLimitInQuery() {
259: final List<OWLClassA> expected = QueryTestEnvironment.getData(OWLClassA.class);
260: final int size = expected.size() / 2;
261: final List<OWLClassA> result = getEntityManager().createNativeQuery("SELECT ?x WHERE {" +
262: "?x a ?classA . } LIMIT ?limit", OWLClassA.class)
263: .setParameter("classA", URI.create(Vocabulary.C_OWL_CLASS_A))
264: .setUntypedParameter("limit", size).getResultList();
265: assertEquals(size, result.size());
266: }
267:
268: @Test
269: public void setFirstResultCanBeUsedToOffsetFirstQueryResult() {
270: final List<OWLClassA> expected = QueryTestEnvironment.getData(OWLClassA.class);
271: expected.sort(Comparator.comparing(OWLClassA::getUri));
272: final int offset = expected.size() / 2;
273: final List<OWLClassA> result = getEntityManager().createNamedQuery("OWLClassA.findAll", OWLClassA.class)
274: .setFirstResult(offset).getResultList();
275: assertEquals(expected.size() - offset, result.size());
276:• for (int i = 0; i < result.size(); i++) {
277: assertEquals(expected.get(i + offset).getUri(), result.get(i).getUri());
278: }
279: }
280:
281: @Test
282: void querySupportsProcessingResultsUsingStream() {
283: final TypedQuery<OWLClassD> q =
284: getEntityManager().createNativeQuery(SELECT_BY_TYPE, OWLClassD.class).setParameter("type", URI.create(
285: Vocabulary.C_OWL_CLASS_D));
286: final List<OWLClassD> dList = QueryTestEnvironment.getData(OWLClassD.class);
287: final Set<URI> expected = dList.stream().map(OWLClassD::getUri).collect(Collectors.toSet());
288:
289: q.getResultStream().map(OWLClassD::getUri).forEach(rUri -> assertTrue(expected.contains(rUri)));
290: assertEquals(dList.size(), (int) q.getResultStream().count());
291: }
292:
293: @Test
294: void selectionByObjectPropertySupportsEntityAsQueryParameter() {
295: final String query = "SELECT ?x WHERE { ?x a ?type ; ?hasA ?y . }";
296: final List<OWLClassD> ds = new ArrayList<>(QueryTestEnvironment.getData(OWLClassD.class));
297: final OWLClassA a = ds.get(Generators.randomPositiveInt(2, ds.size())).getOwlClassA();
298: final TypedQuery<OWLClassD> q = getEntityManager().createNativeQuery(query, OWLClassD.class)
299: .setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_D))
300: .setParameter("hasA",
301: URI.create(Vocabulary.P_HAS_OWL_CLASS_A))
302: .setParameter("y", a);
303:
304: final List<OWLClassD> expected = ds.stream().filter(d -> d.getOwlClassA().getUri().equals(a.getUri()))
305: .sorted(Comparator.comparing(OWLClassD::getUri))
306: .toList();
307: final List<OWLClassD> res = q.getResultList();
308: res.sort(Comparator.comparing(OWLClassD::getUri));
309: assertEquals(expected.size(), res.size());
310:• for (int i = 0; i < expected.size(); i++) {
311: assertEquals(expected.get(i).getUri(), res.get(i).getUri());
312: assertNotNull(res.get(i).getOwlClassA());
313: }
314: }
315:
316: @Test
317: protected void querySupportsCollectionParameters() {
318: final String query = "SELECT ?x WHERE { ?x a ?type . FILTER (?x IN (?values)) }";
319: final List<OWLClassA> as = QueryTestEnvironment.getData(OWLClassA.class).stream()
320: .filter(a -> Generators.randomBoolean())
321: .toList();
322: final TypedQuery<OWLClassA> q = getEntityManager().createNativeQuery(query, OWLClassA.class)
323: .setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_A))
324: .setParameter("values", as.stream().map(OWLClassA::getUri)
325: .toList());
326: final List<OWLClassA> result = q.getResultList();
327: assertEquals(as.size(), result.size());
328:• for (OWLClassA exp : as) {
329: assertTrue(result.stream().anyMatch(a -> a.getUri().equals(exp.getUri())));
330: }
331: }
332:
333: @Test
334: protected void querySupportsSelectionByDate() {
335: final long now = System.currentTimeMillis() / 1000L;
336: final List<OWLClassM> mInstances = IntStream.range(0, 10).mapToObj(i -> {
337: final OWLClassM m = new OWLClassM();
338: m.setKey(Generators.generateUri().toString());
339: // Now minus i * hour
340: m.setDateAttribute(new Date(now - i * 60 * 60L));
341: return m;
342: }).toList();
343: getEntityManager().getTransaction().begin();
344: mInstances.forEach(getEntityManager()::persist);
345: getEntityManager().getTransaction().commit();
346: final LocalDateTime param = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS).minusHours(3);
347: final List<OWLClassM> matching = mInstances.stream()
348: .filter(m -> m.getDateAttribute().toInstant().atOffset(ZoneOffset.UTC).isBefore(param.atOffset(ZoneOffset.UTC)))
349: .toList();
350: try {
351: final List<OWLClassM> result = getEntityManager().createQuery("SELECT m FROM OWLClassM m WHERE m.dateAttribute < :date", OWLClassM.class)
352: .setParameter("date", param).getResultList();
353: assertEquals(matching.size(), result.size());
354: matching.forEach(m -> assertTrue(result.stream().anyMatch(rm -> rm.getKey().equals(m.getKey()))));
355: } finally {
356: cleanupTestData(Vocabulary.C_OWL_CLASS_M);
357: }
358: }
359:
360: private void cleanupTestData(String type) {
361: getEntityManager().getTransaction().begin();
362: getEntityManager().createNativeQuery("DELETE WHERE { ?x a ?type . ?x ?y ?z . }")
363: .setParameter("type", URI.create(type)).executeUpdate();
364: getEntityManager().getTransaction().commit();
365: }
366:
367: @Test
368: protected void querySupportsSelectionByEntityIdentifier() {
369: final OWLClassA entity = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassA.class));
370: final OWLClassA result = getEntityManager().createNativeQuery("SELECT ?x WHERE { ?x a ?type . }", OWLClassA.class)
371: .setParameter("type", URI.create(Vocabulary.C_OWL_CLASS_A))
372: .setParameter("x", entity.getUri())
373: .getSingleResult();
374: assertEquals(entity.getUri(), result.getUri());
375: }
376: }