Skip to content

Method: askQueryWithPositionParameter()

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 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.annotations.OWLClass;
18: import cz.cvut.kbss.jopa.model.query.TypedQuery;
19: import cz.cvut.kbss.jopa.test.OWLClassA;
20: import cz.cvut.kbss.jopa.test.OWLClassB;
21: import cz.cvut.kbss.jopa.test.OWLClassD;
22: import cz.cvut.kbss.jopa.test.OWLClassE;
23: import cz.cvut.kbss.jopa.test.query.QueryTestEnvironment;
24: import org.junit.Ignore;
25: import org.junit.Test;
26: import org.slf4j.Logger;
27:
28: import java.net.URI;
29: import java.util.ArrayList;
30: import java.util.List;
31:
32: import static org.junit.Assert.*;
33:
34: public abstract class TypedQueryRunner extends BaseQueryRunner {
35:
36: protected TypedQueryRunner(Logger logger) {
37: super(logger);
38: }
39:
40: @Test
41: public void testFindAll() {
42: logger.debug("Test: select all entities of a certain type.");
43: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassD> .}";
44: final List<OWLClassD> ds = new ArrayList<>();
45: final TypedQuery<OWLClassD> q = getEntityManager().createNativeQuery(query, OWLClassD.class);
46: ds.addAll(QueryTestEnvironment.getDataByContext(null, OWLClassD.class));
47: final List<OWLClassD> res = q.getResultList();
48: assertNotNull(res);
49: assertFalse(res.isEmpty());
50: assertEquals(ds.size(), res.size());
51: boolean found;
52: for (OWLClassD d : ds) {
53: found = false;
54: for (OWLClassD dd : res) {
55: if (d.getUri().equals(dd.getUri())) {
56: found = true;
57: assertNotNull(dd.getOwlClassA());
58: assertEquals(d.getOwlClassA().getUri(), dd.getOwlClassA().getUri());
59: break;
60: }
61: }
62: assertTrue(found);
63: }
64: }
65:
66: @Test
67: public void testSelectByTypeAndDataPropertyValue() {
68: logger.debug("Test: select entity by its type and data property value.");
69: final OWLClassB b = QueryTestEnvironment.getData(OWLClassB.class).get(5);
70: final String query =
71: "SELECT ?x WHERE { " +
72: "?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassB> ; " +
73: "<http://krizik.felk.cvut.cz/ontologies/jopa/attributes#B-stringAttribute> ?bString . }";
74: final TypedQuery<OWLClassB> q = getEntityManager().createNativeQuery(query, OWLClassB.class);
75: q.setParameter("bString", b.getStringAttribute(), "en");
76: final OWLClassB res = q.getSingleResult();
77: assertNotNull(res);
78: assertEquals(b.getUri(), res.getUri());
79: assertEquals(b.getStringAttribute(), res.getStringAttribute());
80: }
81:
82: @Test
83: public void testSelectByObjectProperty() {
84: logger.debug("Test: select entity by object property value.");
85: final String query = "SELECT ?x WHERE { ?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#hasA> ?y . }";
86: final TypedQuery<OWLClassD> q = getEntityManager().createNativeQuery(query, OWLClassD.class);
87: final List<OWLClassD> ds = new ArrayList<>();
88: ds.addAll(QueryTestEnvironment.getDataByContext(null, OWLClassD.class));
89: final int cnt = ds.size() / 2;
90: assertTrue(cnt > 1);
91: final List<OWLClassD> res = q.setMaxResults(cnt).getResultList();
92: assertEquals(cnt, res.size());
93: }
94:
95: @Test
96: public void testSetMaxResults() {
97: logger.debug("Test: set maximum number of results.");
98: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
99: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(query, OWLClassE.class);
100: final int max = 5;
101: assertTrue(max < QueryTestEnvironment.getData(OWLClassE.class).size());
102: assertEquals(Integer.MAX_VALUE, q.getMaxResults());
103: q.setMaxResults(max);
104: assertEquals(max, q.getMaxResults());
105: final List<OWLClassE> res = q.getResultList();
106: assertNotNull(res);
107: assertFalse(res.isEmpty());
108: assertEquals(max, res.size());
109: }
110:
111: @Test(expected = IllegalArgumentException.class)
112: public void testSetMaxResultsNegative() {
113: logger.debug("Test: set maximum number of results. Negative argument.");
114: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
115: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(query, OWLClassE.class);
116: q.setMaxResults(-1);
117: }
118:
119: @Test
120: public void testSetMaxResultsZero() {
121: logger.debug("Test: set maximum number of results. Zero argument.");
122: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
123: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(query, OWLClassE.class);
124: q.setMaxResults(0);
125: final List<OWLClassE> res = q.getResultList();
126: assertNotNull(res);
127: assertTrue(res.isEmpty());
128: }
129:
130: @Test
131: public void testGetSingleResult() {
132: logger.debug("Test: get single result.");
133: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
134: final String query =
135: "SELECT ?x WHERE { ?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#A-stringAttribute> ?aString .}";
136: final TypedQuery<OWLClassA> q = getEntityManager().createNativeQuery(query, OWLClassA.class);
137: q.setParameter("aString", a.getStringAttribute(), "en");
138: final OWLClassA res = q.getSingleResult();
139: assertNotNull(res);
140: assertEquals(a.getUri(), res.getUri());
141: }
142:
143: @Test(expected = NoUniqueResultException.class)
144: public void testGetSingleResultMultiples() {
145: logger.debug("Test: get single result. No unique result.");
146: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
147: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(query, OWLClassE.class);
148: q.getSingleResult();
149: }
150:
151: @Test(expected = NoResultException.class)
152: public void testGetSingleResultNoResult() {
153: logger.debug("Test: get single result. No result.");
154: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassX> . }";
155: final TypedQuery<OWLClassE> q = getEntityManager().createNativeQuery(query, OWLClassE.class);
156: q.getSingleResult();
157: }
158:
159: @Test(expected = NullPointerException.class)
160: public void testCreateQueryNullQuery() {
161: logger.debug("Test: create query. Null query passed.");
162: getEntityManager().createNativeQuery(null, OWLClassA.class);
163: }
164:
165: @Test(expected = NullPointerException.class)
166: public void testCreateQueryNullClass() {
167: logger.debug("Test: create query. Null result class passed.");
168: final String query = "SELECT ?x WHERE { ?x ?y ?z .}";
169: getEntityManager().createNativeQuery(query, (Class<OWLClassA>) null);
170: }
171:
172: @Test
173: public void askQueryReturnsTrue() {
174: logger.debug("Test: execute a ASK query which returns true.");
175: final String query = "ASK { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassA> . }";
176: final TypedQuery<Boolean> q = getEntityManager().createNativeQuery(query, Boolean.class);
177: final Boolean res = q.getSingleResult();
178: assertNotNull(res);
179: assertTrue(res);
180: }
181:
182: @Test
183: public void askQueryReturnsFalse() {
184: logger.debug("Test: execute a ASK query which returns false.");
185: final String query = "ASK { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassX> . }";
186: final TypedQuery<Boolean> q = getEntityManager().createNativeQuery(query, Boolean.class);
187: final List<Boolean> res = q.getResultList();
188: assertNotNull(res);
189: assertEquals(1, res.size());
190: assertFalse(res.get(0));
191: }
192:
193: @Ignore
194: @Test
195: public void askQueryAgainstTransactionalOntologyContainsUncommittedChangesAsWell() throws Exception {
196: logger.debug("Test: execute an ASK query which returns changes yet to be committed in transaction.");
197: final OWLClassE e = new OWLClassE();
198: getEntityManager().getTransaction().begin();
199: try {
200: getEntityManager().persist(e);
201: final TypedQuery<Boolean> query = getEntityManager().createNativeQuery(
202: "ASK { ?individual a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }",
203: Boolean.class).setParameter("individual", e.getUri());
204: final Boolean res = query.getSingleResult();
205: assertTrue(res);
206: } finally {
207: getEntityManager().getTransaction().rollback();
208: }
209: }
210:
211: @Test
212: public void askQueryWithPositionParameter() {
213: logger.debug("Test: execute an ASK query which returns true, query contains positional parameter.");
214: final String query = "ASK { ?x a $1 . }";
215: final URI paramValue = URI.create(OWLClassA.class.getAnnotation(OWLClass.class).iri());
216: final TypedQuery<Boolean> q = getEntityManager().createNativeQuery(query, Boolean.class)
217: .setParameter(1, paramValue);
218: final Boolean res = q.getSingleResult();
219: assertNotNull(res);
220: assertTrue(res);
221: }
222: }