Skip to content

Method: testSelectByObjectProperty()

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.EntityManager;
20: import cz.cvut.kbss.jopa.model.annotations.OWLClass;
21: import cz.cvut.kbss.jopa.model.query.Query;
22: import cz.cvut.kbss.jopa.test.OWLClassA;
23: import cz.cvut.kbss.jopa.test.OWLClassB;
24: import cz.cvut.kbss.jopa.test.OWLClassD;
25: import cz.cvut.kbss.jopa.test.OWLClassE;
26: import cz.cvut.kbss.jopa.test.query.QueryTestEnvironment;
27: import org.junit.Test;
28: import org.slf4j.Logger;
29:
30: import java.net.URI;
31: import java.util.List;
32: import java.util.Set;
33: import java.util.stream.Collectors;
34:
35: import static org.junit.Assert.*;
36:
37: public abstract class QueryRunner extends BaseQueryRunner {
38:
39: protected QueryRunner(Logger logger) {
40: super(logger);
41: }
42:
43: @Test
44: public void testSelectByType() {
45: logger.debug("Test: get URIs of individuals of a certain type.");
46: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#TypeA> . }";
47: final Query q = getEntityManager().createNativeQuery(query);
48: final List<OWLClassA> as = QueryTestEnvironment.getDataByContext(null, OWLClassA.class);
49: assertNotNull(q);
50:
51: final List res = q.getResultList();
52:
53: assertNotNull(res);
54: assertFalse(res.isEmpty());
55: assertEquals(as.size(), res.size());
56: boolean found;
57: for (OWLClassA a : as) {
58: found = false;
59: for (Object row : res) {
60: if (a.getUri().equals(row)) {
61: found = true;
62: break;
63: }
64: }
65: assertTrue(found);
66: }
67: }
68:
69: @Test
70: public void testSelectByDataProperty() {
71: logger.debug("Test: select data property values.");
72: final String query = "SELECT ?y WHERE { ?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#B-stringAttribute> ?y . }";
73: final Query q = getEntityManager().createNativeQuery(query);
74: final Set<String> exp = QueryTestEnvironment.getDataByContext(null, OWLClassB.class).stream()
75: .map(OWLClassB::getStringAttribute).collect(Collectors.toSet());
76:
77: final List res = q.getResultList();
78:
79: assertNotNull(res);
80: assertFalse(res.isEmpty());
81: assertEquals(exp.size(), res.size());
82: for (Object lst2 : res) {
83: assertTrue(lst2 instanceof String);
84: // False means we got the expected value
85: assertFalse(exp.add((String) lst2));
86: }
87: }
88:
89: @Test
90: public void testSelectByObjectProperty() {
91: logger.debug("Test: select object property values.");
92: final OWLClassD d = QueryTestEnvironment.getData(OWLClassD.class).get(0);
93: final String query = "SELECT ?x WHERE { ?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#hasA> ?y . }";
94: final Query q = getEntityManager().createNativeQuery(query);
95: q.setParameter("y", d.getOwlClassA().getUri());
96:
97: final List res = q.getResultList();
98:
99: assertEquals(1, res.size());
100: final Object subRes = res.get(0);
101: assertEquals(d.getUri(), subRes);
102: }
103:
104: @Test
105: public void testSelectTypes() {
106: logger.debug("Test: select individual's types.");
107: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
108: final Set<String> types = a.getTypes();
109: types.add(a.getClass().getAnnotation(OWLClass.class).iri());
110: final String query = "SELECT ?x WHERE { ?instance a ?x . }";
111: final Query q = getEntityManager().createNativeQuery(query);
112: q.setParameter("instance", a.getUri());
113:
114: final List res = q.getResultList();
115: // The result can contain more types (inference)
116: assertTrue(res.size() >= types.size());
117: for (String type : types) {
118: assertTrue(res.contains(URI.create(type)));
119: }
120: }
121:
122: @Test
123: public void testSetMaxResults() {
124: logger.debug("Test: set maximum number of results.");
125: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
126: final Query q = getEntityManager().createNativeQuery(query);
127: final int max = 5;
128: assertTrue(max < QueryTestEnvironment.getData(OWLClassE.class).size());
129: assertEquals(Integer.MAX_VALUE, q.getMaxResults());
130: q.setMaxResults(max);
131: assertEquals(max, q.getMaxResults());
132:
133: final List res = q.getResultList();
134: assertNotNull(res);
135: assertFalse(res.isEmpty());
136: assertEquals(max, res.size());
137: }
138:
139: @Test(expected = IllegalArgumentException.class)
140: public void testSetMaxResultsNegative() {
141: logger.debug("Test: set maximum number of results. Negative argument.");
142: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
143: final Query q = getEntityManager().createNativeQuery(query);
144: q.setMaxResults(-1);
145: }
146:
147: @Test
148: public void testSetMaxResultsZero() {
149: logger.debug("Test: set maximum number of results. Zero argument.");
150: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
151: final Query q = getEntityManager().createNativeQuery(query);
152: q.setMaxResults(0);
153:
154: final List res = q.getResultList();
155: assertNotNull(res);
156: assertTrue(res.isEmpty());
157: }
158:
159: @Test
160: public void testGetSingleResult() {
161: logger.debug("Test: get single result.");
162: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
163: final String query =
164: "SELECT ?x WHERE { ?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#A-stringAttribute> ?y .}";
165: final Query q = getEntityManager().createNativeQuery(query);
166: q.setParameter("y", a.getStringAttribute(), "en");
167:
168: final Object res = q.getSingleResult();
169: assertNotNull(res);
170: assertEquals(a.getUri(), res);
171: }
172:
173: @Test(expected = NoUniqueResultException.class)
174: public void testGetSingleResultMultiples() {
175: logger.debug("Test: get single result. No unique result.");
176: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> . }";
177: final Query q = getEntityManager().createNativeQuery(query);
178: q.getSingleResult();
179: }
180:
181: @Test(expected = NoResultException.class)
182: public void testGetSingleResultNoResult() {
183: logger.debug("Test: get single result. No result.");
184: final String query = "SELECT ?x WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassX> . }";
185: final Query q = getEntityManager().createNativeQuery(query);
186: q.getSingleResult();
187: }
188:
189: @Test
190: public void testSelectQueryWithPositionalParameters() {
191: logger.debug("Test: select query using positional parameters.");
192: final OWLClassA a = QueryTestEnvironment.getData(OWLClassA.class).get(0);
193: final String query =
194: "SELECT ?x WHERE { ?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#A-stringAttribute> $ .}";
195: final Query q = getEntityManager().createNativeQuery(query);
196: q.setParameter(1, a.getStringAttribute(), "en");
197:
198: final Object res = q.getSingleResult();
199: assertNotNull(res);
200: assertEquals(a.getUri(), res);
201: }
202:
203: @Test
204: public void testSelectWithOptionalReturnsNullInUnfilledColumns() throws Exception {
205: logger.debug("Test: select query with optional. The result should have nulls in places of empty values.");
206: final String query =
207: "SELECT ?x ?s WHERE { ?x a <http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassE> ." +
208: " OPTIONAL {?x <http://krizik.felk.cvut.cz/ontologies/jopa/attributes#E-stringAttribute> ?s . } }";
209: final OWLClassE e = new OWLClassE();
210: final EntityManager em = getEntityManager();
211: em.getTransaction().begin();
212: em.persist(e);
213: em.getTransaction().commit();
214: final Query q = em.createNativeQuery(query);
215:
216: final List result = q.getResultList();
217: assertFalse(result.isEmpty());
218: for (Object row : result) {
219: final Object[] rowArr = (Object[]) row;
220: if (rowArr[0].equals(e.getUri().toString())) {
221: assertNull(rowArr[1]);
222: }
223: }
224: }
225: }