-
Notifications
You must be signed in to change notification settings - Fork 14
/
PolymorphicSelectQueryRunner.java
131 lines (118 loc) · 5.67 KB
/
PolymorphicSelectQueryRunner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.test.query.runner;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.test.OWLClassS;
import cz.cvut.kbss.jopa.test.OWLClassSParent;
import cz.cvut.kbss.jopa.test.OWLClassT;
import cz.cvut.kbss.jopa.test.Vocabulary;
import cz.cvut.kbss.jopa.test.environment.Generators;
import cz.cvut.kbss.jopa.test.query.QueryTestEnvironment;
import cz.cvut.kbss.jopa.vocabulary.RDFS;
import org.junit.After;
import org.junit.Test;
import org.slf4j.Logger;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import static org.junit.Assert.*;
public abstract class PolymorphicSelectQueryRunner extends BaseQueryRunner {
protected PolymorphicSelectQueryRunner(Logger logger) {
super(logger);
}
@After
public void tearDown() {
getEntityManager().clear();
getEntityManager().getEntityManagerFactory().getCache().evictAll();
}
@Test
public void selectLoadsInstanceOfMostConcreteSubclassOfAbstractEntity() {
final OWLClassT t = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
final EntityManager em = getEntityManager();
final OWLClassSParent result =
em.createNativeQuery("SELECT ?x WHERE { ?x ?hasName ?name . }", OWLClassSParent.class)
.setParameter("hasName", URI.create(RDFS.LABEL))
.setParameter("name", t.getName(), "en").getSingleResult();
assertNotNull(result);
assertTrue(result instanceof OWLClassT);
verifyEntityTAttributes(t, (OWLClassT) result);
}
private void verifyEntityTAttributes(OWLClassT expected, OWLClassT actual) {
assertEquals(expected.getUri(), actual.getUri());
assertEquals(expected.getName(), actual.getName());
assertEquals(expected.getDescription(), actual.getDescription());
assertEquals(expected.getIntAttribute(), actual.getIntAttribute());
assertEquals(expected.getOwlClassA().getUri(), actual.getOwlClassA().getUri());
}
@Test
public void selectLoadsInstanceOfMostConcreteSubclassOfConcreteEntity() {
final OWLClassT t = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
final EntityManager em = getEntityManager();
final OWLClassS result =
em.createNativeQuery("SELECT ?x WHERE { ?x ?hasName ?name . }", OWLClassS.class)
.setParameter("hasName", URI.create(RDFS.LABEL))
.setParameter("name", t.getName(), "en").getSingleResult();
assertNotNull(result);
assertTrue(result instanceof OWLClassT);
verifyEntityTAttributes(t, (OWLClassT) result);
}
@Test
public void selectByTypeLoadsAllIndividualsAsMostConcreteSubclassInstances() {
final List<OWLClassT> data = QueryTestEnvironment.getData(OWLClassT.class);
final EntityManager em = getEntityManager();
// This will cause the type resolver to have to do some work
em.getTransaction().begin();
data.forEach(t -> {
t.setTypes(new HashSet<>(Arrays.asList(Vocabulary.C_OWL_CLASS_S_PARENT, Vocabulary.C_OWL_CLASS_S)));
em.merge(t);
});
em.getTransaction().commit();
final List<OWLClassSParent> result =
em.createNativeQuery("SELECT ?x WHERE { ?x a ?type . }", OWLClassSParent.class)
.setParameter("type", URI.create(
Vocabulary.C_OWL_CLASS_S_PARENT)).getResultList();
assertEquals(data.size(), result.size());
boolean found;
for (OWLClassT t : data) {
found = false;
for (OWLClassSParent tt : result) {
if (t.getUri().equals(tt.getUri())) {
found = true;
assertTrue(tt instanceof OWLClassT);
verifyEntityTAttributes(t, (OWLClassT) tt);
break;
}
}
assertTrue(found);
}
}
@Test
public void selectLoadsInstanceAsGivenTypeWhenItIsConcreteAndFoundInTypesOfIndividual() {
final OWLClassT t = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
final EntityManager em = getEntityManager();
em.getTransaction().begin();
t.setTypes(Collections.singleton(Vocabulary.C_OWL_CLASS_S));
em.merge(t);
em.getTransaction().commit();
final OWLClassS result = em.createNativeQuery("SELECT ?x WHERE { ?x ?hasInt ?int. }", OWLClassS.class)
.setParameter("hasInt", URI.create(Vocabulary.P_T_INTEGER_ATTRIBUTE))
.setParameter("int", t.getIntAttribute()).getSingleResult();
assertNotNull(result);
assertFalse(result instanceof OWLClassT);
assertEquals(t.getName(), result.getName());
assertEquals(t.getDescription(), result.getDescription());
assertTrue(result.getTypes().contains(Vocabulary.C_OWL_CLASS_T));
}
}