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