Skip to content

Method: testGetSingleResult()

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.test.query.runner;
16:
17: import cz.cvut.kbss.jopa.exceptions.NoResultException;
18: import cz.cvut.kbss.jopa.exceptions.NoUniqueResultException;
19: import cz.cvut.kbss.jopa.model.annotations.OWLClass;
20: import cz.cvut.kbss.jopa.model.query.TypedQuery;
21: import cz.cvut.kbss.jopa.test.OWLClassA;
22: import cz.cvut.kbss.jopa.test.OWLClassB;
23: import cz.cvut.kbss.jopa.test.OWLClassD;
24: import cz.cvut.kbss.jopa.test.OWLClassE;
25: import cz.cvut.kbss.jopa.test.query.QueryTestEnvironment;
26: import org.junit.Ignore;
27: import org.junit.Test;
28: import org.slf4j.Logger;
29:
30: import java.net.URI;
31: import java.util.ArrayList;
32: import java.util.List;
33: import java.util.stream.Collectors;
34:
35: import static org.junit.Assert.*;
36:
37: public abstract class TypedQueryRunner extends BaseQueryRunner {
38:
39: protected TypedQueryRunner(Logger logger) {
40: super(logger);
41: }
42:
43: @Test
44: public void testFindAll() {
45: logger.debug("Test: select all entities of a certain type.");
46: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassD> .}";
47: final List<OWLClassD> ds = new ArrayList<>();
48: final TypedQuery<OWLClassD> q = getEntityManager().createNativeQuery(query, OWLClassD.class);
49: ds.addAll(QueryTestEnvironment.getDataByContext(null, OWLClassD.class));
50: final List<OWLClassD> res = q.getResultList();
51: assertNotNull(res);
52: assertFalse(res.isEmpty());
53: assertEquals(ds.size(), res.size());
54: boolean found;
55: for (OWLClassD d : ds) {
56: found = false;
57: for (OWLClassD dd : res) {
58: if (d.getUri().equals(dd.getUri())) {
59: found = true;
60: assertNotNull(dd.getOwlClassA());
61: assertEquals(d.getOwlClassA().getUri(), dd.getOwlClassA().getUri());
62: break;
63: }
64: }
65: assertTrue(found);
66: }
67: }
68:
69: @Test
70: public void testSelectByTypeAndDataPropertyValue() {
71: logger.debug("Test: select entity by its type and data property value.");
72: final OWLClassB b = QueryTestEnvironment.getData(OWLClassB.class).get(5);
73: final String query =
74: "SELECT ?x WHERE { " +
75: "?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassB> ; " +
76: "<http://krizik.felk.cvut.cz/ontologies/jopa/attributes#B-stringAttribute> ?bString . }";
77: final TypedQuery<OWLClassB> q = getEntityManager().createNativeQuery(query, OWLClassB.class);
78: q.setParameter("bString", b.getStringAttribute(), "en");
79: final OWLClassB res = q.getSingleResult();
80: assertNotNull(res);
81: assertEquals(b.getUri(), res.getUri());
82: assertEquals(b.getStringAttribute(), res.getStringAttribute());
83: }
84:
85: @Test
86: public void testSelectByObjectProperty() {
87: logger.debug("Test: select entity by object property value.");
88: final String query = "SELECT ?x WHERE { ?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#hasA> ?y . }";
89: final TypedQuery<OWLClassD> q = getEntityManager().createNativeQuery(query, OWLClassD.class);
90: final List<OWLClassD> ds = new ArrayList<>();
91: ds.addAll(QueryTestEnvironment.getDataByContext(null, OWLClassD.class));
92: final int cnt = ds.size() / 2;
93: assertTrue(cnt > 1);
94: final List<OWLClassD> res = q.setMaxResults(cnt).getResultList();
95: assertEquals(cnt, res.size());
96: }
97:
98: @Test
99: public void testSetMaxResults() {
100: logger.debug("Test: set maximum number of results.");
101: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
102: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(query, OWLClassE.class);
103: final int max = 5;
104: assertTrue(max < QueryTestEnvironment.getData(OWLClassE.class).size());
105: assertEquals(Integer.MAX_VALUE, q.getMaxResults());
106: q.setMaxResults(max);
107: assertEquals(max, q.getMaxResults());
108: final List<OWLClassE> res = q.getResultList();
109: assertNotNull(res);
110: assertFalse(res.isEmpty());
111: assertEquals(max, res.size());
112: }
113:
114: @Test(expected = IllegalArgumentException.class)
115: public void testSetMaxResultsNegative() {
116: logger.debug("Test: set maximum number of results. Negative argument.");
117: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
118: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(query, OWLClassE.class);
119: q.setMaxResults(-1);
120: }
121:
122: @Test
123: public void testSetMaxResultsZero() {
124: logger.debug("Test: set maximum number of results. Zero argument.");
125: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
126: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(query, OWLClassE.class);
127: q.setMaxResults(0);
128: final List<OWLClassE> res = q.getResultList();
129: assertNotNull(res);
130: assertTrue(res.isEmpty());
131: }
132:
133: @Test
134: public void testGetSingleResult() {
135: logger.debug("Test: get single result.");
136: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
137: final String query =
138: "SELECT ?x WHERE { ?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#A-stringAttribute> ?aString .}";
139: final TypedQuery<OWLClassA> q = getEntityManager().createNativeQuery(query, OWLClassA.class);
140: q.setParameter("aString", a.getStringAttribute(), "en");
141: final OWLClassA res = q.getSingleResult();
142: assertNotNull(res);
143: assertEquals(a.getUri(), res.getUri());
144: }
145:
146: @Test(expected = NoUniqueResultException.class)
147: public void testGetSingleResultMultiples() {
148: logger.debug("Test: get single result. No unique result.");
149: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
150: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(query, OWLClassE.class);
151: q.getSingleResult();
152: }
153:
154: @Test(expected = NoResultException.class)
155: public void testGetSingleResultNoResult() {
156: logger.debug("Test: get single result. No result.");
157: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassX> . }";
158: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(query, OWLClassE.class);
159: q.getSingleResult();
160: }
161:
162: @Test(expected = NullPointerException.class)
163: public void testCreateQueryNullQuery() {
164: logger.debug("Test: create query. Null query passed.");
165: getEntityManager().createNativeQuery(null, OWLClassA.class);
166: }
167:
168: @Test(expected = NullPointerException.class)
169: public void testCreateQueryNullClass() {
170: logger.debug("Test: create query. Null result class passed.");
171: final String query = "SELECT ?x WHERE { ?x ?y ?z .}";
172: getEntityManager().createNativeQuery(query, (Class<OWLClassA>) null);
173: }
174:
175: @Test
176: public void askQueryReturnsTrue() {
177: logger.debug("Test: execute a ASK query which returns true.");
178: final String query = "ASK { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassA> . }";
179: final TypedQuery<Boolean> q = getEntityManager().createNativeQuery(query, Boolean.class);
180: final Boolean res = q.getSingleResult();
181: assertNotNull(res);
182: assertTrue(res);
183: }
184:
185: @Test
186: public void askQueryReturnsFalse() {
187: logger.debug("Test: execute a ASK query which returns false.");
188: final String query = "ASK { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassX> . }";
189: final TypedQuery<Boolean> q = getEntityManager().createNativeQuery(query, Boolean.class);
190: final List<Boolean> res = q.getResultList();
191: assertNotNull(res);
192: assertEquals(1, res.size());
193: assertFalse(res.get(0));
194: }
195:
196: @Ignore
197: @Test
198: public void askQueryAgainstTransactionalOntologyContainsUncommittedChangesAsWell() throws Exception {
199: logger.debug("Test: execute an ASK query which returns changes yet to be committed in transaction.");
200: final OWLClassE e = new OWLClassE();
201: getEntityManager().getTransaction().begin();
202: try {
203: getEntityManager().persist(e);
204: final TypedQuery<Boolean> query = getEntityManager().createNativeQuery(
205: "ASK { ?individual a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }",
206: Boolean.class).setParameter("individual", e.getUri());
207: final Boolean res = query.getSingleResult();
208: assertTrue(res);
209: } finally {
210: getEntityManager().getTransaction().rollback();
211: }
212: }
213:
214: @Test
215: public void askQueryWithPositionParameter() {
216: logger.debug("Test: execute an ASK query which returns true, query contains positional parameter.");
217: final String query = "ASK { ?x a $1 . }";
218: final URI paramValue = URI.create(OWLClassA.class.getAnnotation(OWLClass.class).iri());
219: final TypedQuery<Boolean> q = getEntityManager().createNativeQuery(query, Boolean.class)
220: .setParameter(1, paramValue);
221: final Boolean res = q.getSingleResult();
222: assertNotNull(res);
223: assertTrue(res);
224: }
225:
226: @Test
227: public void testCreateTypedNamedNativeQuery() {
228: final List<OWLClassA> expected = QueryTestEnvironment.getData(OWLClassA.class);
229: final List<URI> uris = expected.stream().map(OWLClassA::getUri).collect(Collectors.toList());
230: final List<OWLClassA> res = getEntityManager().createNamedQuery("OWLClassA.findAll", OWLClassA.class)
231: .getResultList();
232: assertEquals(expected.size(), res.size());
233: res.forEach(a -> assertTrue(uris.contains(a.getUri())));
234: }
235: }