Skip to content

Method: testSimpleFindAll()

1: /**
2: * Copyright (C) 2022 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.test.OWLClassA;
18: import cz.cvut.kbss.jopa.test.OWLClassD;
19: import cz.cvut.kbss.jopa.test.OWLClassT;
20: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
21: import cz.cvut.kbss.jopa.test.environment.Generators;
22: import cz.cvut.kbss.jopa.test.query.QueryTestEnvironment;
23: import org.junit.jupiter.api.Test;
24: import org.slf4j.Logger;
25:
26: import java.util.Comparator;
27: import java.util.List;
28:
29: import static org.hamcrest.MatcherAssert.assertThat;
30: import static org.hamcrest.Matchers.lessThanOrEqualTo;
31: import static org.junit.jupiter.api.Assertions.*;
32:
33: public abstract class SoqlRunner extends BaseQueryRunner {
34:
35: protected SoqlRunner(Logger logger, DataAccessor dataAccessor) {
36: super(logger, dataAccessor);
37: }
38:
39: @Test
40: public void testSimpleFindAll() {
41: final List<OWLClassA> expected = QueryTestEnvironment.getData(OWLClassA.class);
42: final List<OWLClassA> result = getEntityManager().createQuery("SELECT a FROM OWLClassA a", OWLClassA.class)
43: .getResultList();
44: assertEquals(expected.size(), result.size());
45:• for (OWLClassA a : result) {
46: assertNotNull(a.getStringAttribute());
47: assertTrue(expected.stream().anyMatch(aa -> aa.getUri().equals(a.getUri())));
48: }
49: }
50:
51: @Test
52: public void testSimpleCount() {
53: final List<OWLClassA> expected = QueryTestEnvironment.getData(OWLClassA.class);
54: final Object result = getEntityManager().createQuery("SELECT DISTINCT COUNT(a) FROM OWLClassA a", Integer.class)
55: .getSingleResult();
56: assertEquals(expected.size(), result);
57: }
58:
59: @Test
60: public void testFindByDataPropertyAttribute() {
61: final OWLClassA expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassA.class));
62: final OWLClassA result = getEntityManager()
63: .createQuery("SELECT a FROM OWLClassA a WHERE a.stringAttribute = :str", OWLClassA.class)
64: .setParameter("str", expected.getStringAttribute(), "en").getSingleResult();
65: assertEquals(expected.getUri(), result.getUri());
66: assertEquals(expected.getStringAttribute(), result.getStringAttribute());
67: assertEquals(expected.getTypes(), result.getTypes());
68: }
69:
70: @Test
71: public void testFindByDataNotPropertyAttribute() {
72: final OWLClassA unexpected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassA.class));
73: final List<OWLClassA> result = getEntityManager()
74: .createQuery("SELECT a FROM OWLClassA a WHERE NOT a.stringAttribute = :str", OWLClassA.class)
75: .setParameter("str", unexpected.getStringAttribute(), "en").getResultList();
76: for (OWLClassA item : result) {
77: assertNotEquals(unexpected.getUri(), item.getUri());
78: assertNotEquals(unexpected.getStringAttribute(), item.getStringAttribute());
79: }
80: }
81:
82: @Test
83: public void testFindByDataNotPropertyAttributeAndPropertyAttribute() {
84: final OWLClassT unexpected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
85: final int intThreshold = QueryTestEnvironment.getData(OWLClassT.class).size() / 2;
86: final List<OWLClassT> result = getEntityManager()
87: .createQuery("SELECT t FROM OWLClassT t WHERE NOT t.owlClassA = :a AND t.intAttribute < :intAtt",
88: OWLClassT.class)
89: .setParameter("a", unexpected.getOwlClassA().getUri())
90: .setParameter("intAtt", intThreshold).getResultList();
91: assertFalse(result.isEmpty());
92: for (OWLClassT item : result) {
93: assertNotEquals(unexpected.getUri(), item.getUri());
94: assertTrue(intThreshold > item.getIntAttribute());
95: }
96: }
97:
98: @Test
99: public void testFindByObjectPropertyAttribute() {
100: final OWLClassD expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassD.class));
101: final OWLClassD result = getEntityManager()
102: .createQuery("SELECT d FROM OWLClassD d WHERE d.owlClassA = :a", OWLClassD.class)
103: .setParameter("a", expected.getOwlClassA().getUri()).getSingleResult();
104: assertEquals(expected.getUri(), result.getUri());
105: assertEquals(expected.getOwlClassA().getUri(), result.getOwlClassA().getUri());
106: }
107:
108: @Test
109: public void testFindByConjunctionOfAttributes() {
110: final OWLClassT sample = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
111: final List<OWLClassT> result = getEntityManager()
112: .createQuery("SELECT t FROM OWLClassT t WHERE t.owlClassA = :a AND t.intAttribute <= :intAtt",
113: OWLClassT.class)
114: .setParameter("a", sample.getOwlClassA().getUri())
115: .setParameter("intAtt", sample.getIntAttribute()).getResultList();
116: assertFalse(result.isEmpty());
117: for (OWLClassT item : result) {
118: assertEquals(sample.getOwlClassA().getUri(), item.getOwlClassA().getUri());
119: assertThat(item.getIntAttribute(), lessThanOrEqualTo(sample.getIntAttribute()));
120: }
121: }
122:
123: @Test
124: public void testOrderBy() {
125: final List<OWLClassT> expected = QueryTestEnvironment.getData(OWLClassT.class);
126: expected.sort(Comparator.comparing(OWLClassT::getIntAttribute));
127: final List<OWLClassT> result = getEntityManager()
128: .createQuery("SELECT t FROM OWLClassT t ORDER BY t.intAttribute", OWLClassT.class).getResultList();
129: assertEquals(expected.size(), result.size());
130: for (OWLClassT t : result) {
131: assertTrue(expected.stream().anyMatch(tt -> tt.getUri().equals(t.getUri())));
132: }
133: }
134:
135: @Test
136: public void testFindByDisjunctionOfAttributes() {
137: final OWLClassT sample = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
138: final List<OWLClassT> result = getEntityManager()
139: .createQuery("SELECT t FROM OWLClassT t WHERE t.owlClassA = :a OR t.intAttribute <= :intAtt",
140: OWLClassT.class)
141: .setParameter("a", sample.getOwlClassA().getUri())
142: .setParameter("intAtt", sample.getIntAttribute()).getResultList();
143: assertFalse(result.isEmpty());
144: for (OWLClassT item : result) {
145: boolean matches = item.getOwlClassA().getUri().equals(sample.getOwlClassA().getUri());
146: matches |= item.getIntAttribute() <= sample.getIntAttribute();
147: assertTrue(matches);
148: }
149: }
150:
151: @Test
152: public void testFindByTransitiveAttributeValue() {
153: final OWLClassD expected = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassD.class));
154: final OWLClassD result = getEntityManager()
155: .createQuery("SELECT d FROM OWLClassD d WHERE d.owlClassA.stringAttribute = :stringAtt",
156: OWLClassD.class)
157: .setParameter("stringAtt", expected.getOwlClassA().getStringAttribute(), "en").getSingleResult();
158: assertEquals(expected.getUri(), result.getUri());
159: assertEquals(expected.getOwlClassA().getUri(), result.getOwlClassA().getUri());
160: }
161: }