-
Notifications
You must be signed in to change notification settings - Fork 14
/
RetrieveOperationsWithInheritanceRunner.java
282 lines (241 loc) · 13.4 KB
/
RetrieveOperationsWithInheritanceRunner.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Copyright (C) 2019 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.runner;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.oom.exceptions.AmbiguousEntityTypeException;
import cz.cvut.kbss.jopa.test.*;
import cz.cvut.kbss.jopa.test.environment.DataAccessor;
import cz.cvut.kbss.jopa.test.environment.Generators;
import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
import cz.cvut.kbss.jopa.test.environment.Triple;
import cz.cvut.kbss.jopa.vocabulary.RDF;
import cz.cvut.kbss.jopa.vocabulary.RDFS;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import static org.junit.jupiter.api.Assertions.*;
public abstract class RetrieveOperationsWithInheritanceRunner extends BaseInheritanceRunner {
public RetrieveOperationsWithInheritanceRunner(Logger logger, PersistenceFactory persistenceFactory,
DataAccessor dataAccessor) {
super(logger, persistenceFactory, dataAccessor);
}
@Test
void findReadsAttributesOfMappedSuperclass() throws Exception {
final Collection<Triple> data = new ArrayList<>();
entityQ.setUri(Generators.generateUri());
data.add(
new Triple(entityQ.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_Q)));
data.add(new Triple(entityQ.getUri(), URI.create(RDFS.LABEL), entityQ.getLabel()));
data.add(
new Triple(entityQ.getUri(), URI.create(Vocabulary.P_Q_PARENT_STRING_ATTRIBUTE),
entityQ.getParentString()));
data.add(new Triple(entityQ.getUri(), URI.create(Vocabulary.P_Q_STRING_ATTRIBUTE),
entityQ.getStringAttribute()));
data.addAll(triplesForA());
data.add(new Triple(entityQ.getUri(), URI.create(Vocabulary.P_HAS_OWL_CLASS_A), entityA.getUri()));
final EntityManager em = getEntityManager("findReadsAttributesOfMappedSuperclass", false);
persistTestData(data, em);
final OWLClassQ result = em.find(OWLClassQ.class, entityQ.getUri());
assertNotNull(result);
assertEquals(entityQ.getStringAttribute(), result.getStringAttribute());
assertEquals(entityQ.getLabel(), result.getLabel());
assertEquals(entityQ.getParentString(), result.getParentString());
assertNotNull(result.getOwlClassA());
assertEquals(entityQ.getOwlClassA().getUri(), result.getOwlClassA().getUri());
}
private Collection<Triple> triplesForA() {
final Collection<Triple> data = new ArrayList<>();
entityA.setUri(Generators.generateUri());
data.add(
new Triple(entityA.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_A)));
data.add(new Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE),
entityA.getStringAttribute()));
return data;
}
@Test
void findReadsAttributesOfEntitySuperclass() throws Exception {
final Collection<Triple> data = triplesForEntityT();
final EntityManager em = getEntityManager("findReadsAttributesOfEntitySuperclass", false);
persistTestData(data, em);
final OWLClassT result = em.find(OWLClassT.class, entityT.getUri());
assertNotNull(result);
assertEquals(entityT.getName(), result.getName());
assertEquals(entityT.getDescription(), result.getDescription());
assertEquals(entityT.getIntAttribute(), result.getIntAttribute());
assertNotNull(result.getOwlClassA());
assertEquals(entityA.getUri(), result.getOwlClassA().getUri());
}
private Collection<Triple> triplesForEntityT() {
final Collection<Triple> data = new ArrayList<>();
entityT.setUri(Generators.generateUri());
data.add(new Triple(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_T)));
data.add(new Triple(entityT.getUri(), URI.create(RDFS.LABEL), entityT.getName()));
data.add(new Triple(entityT.getUri(), URI.create(Vocabulary.DC_DESCRIPTION), entityT.getDescription()));
data.add(new Triple(entityT.getUri(), URI.create(Vocabulary.P_T_INTEGER_ATTRIBUTE), entityT.getIntAttribute()));
data.addAll(triplesForA());
data.add(new Triple(entityT.getUri(), URI.create(Vocabulary.P_HAS_OWL_CLASS_A), entityA.getUri()));
return data;
}
@Test
void findLoadsSuperclassInstanceWhenRequestedAndClassAssertionIsPresent() throws Exception {
final EntityManager em = getEntityManager("findLoadsSuperclassInstanceWhenRequestedAndClassAssertionIsPresent",
false);
final Collection<Triple> data = triplesForEntityT();
data.add(new Triple(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
persistTestData(data, em);
final OWLClassS result = em.find(OWLClassS.class, entityT.getUri());
assertNotNull(result);
assertEquals(entityT.getName(), result.getName());
assertEquals(entityT.getDescription(), result.getDescription());
assertTrue(result.getTypes().contains(Vocabulary.C_OWL_CLASS_T));
}
@Test
void findLoadsSubclassWhenSuperclassIsPassedInAndTypeCorrespondsToSubclass() throws Exception {
final Collection<Triple> data = triplesForEntityT();
final EntityManager em = getEntityManager(
"findLoadsSubclassWhenSuperclassIsPassedInAndTypeCorrespondsToSubclass",
false);
persistTestData(data, em);
final OWLClassS result = em.find(OWLClassS.class, entityT.getUri());
assertNotNull(result);
assertTrue(result instanceof OWLClassT);
verifyEntityTAttributes((OWLClassT) result);
}
private void verifyEntityTAttributes(OWLClassT result) {
assertEquals(entityT.getName(), result.getName());
assertEquals(entityT.getDescription(), result.getDescription());
assertEquals(entityT.getIntAttribute(), result.getIntAttribute());
assertEquals(entityT.getOwlClassA().getUri(), result.getOwlClassA().getUri());
}
@Test
void findLoadsSubclassOfAbstractParent() throws Exception {
final Collection<Triple> data = new ArrayList<>();
entityT.setUri(Generators.generateUri());
data.add(new Triple(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
data.add(new Triple(entityT.getUri(), URI.create(RDFS.LABEL), entityT.getName()));
data.add(new Triple(entityT.getUri(), URI.create(Vocabulary.DC_DESCRIPTION), entityT.getDescription()));
final EntityManager em = getEntityManager("findLoadsSubclassOfAbstractParent", false);
persistTestData(data, em);
final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
assertNotNull(result);
assertTrue(result instanceof OWLClassS);
final OWLClassS sResult = (OWLClassS) result;
assertEquals(entityT.getName(), sResult.getName());
assertEquals(entityT.getDescription(), sResult.getDescription());
}
@Test
void findLoadsMostConcreteSubclassOfAbstractAncestor() throws Exception {
final Collection<Triple> data = triplesForEntityT();
data.add(new Triple(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
final EntityManager em = getEntityManager("findLoadsMostConcreteSubclassOfAbstractAncestor", false);
persistTestData(data, em);
final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
assertNotNull(result);
assertTrue(result instanceof OWLClassT);
verifyEntityTAttributes((OWLClassT) result);
}
@Test
void findReturnsNullWhenMatchingClassIsAbstract() throws Exception {
final Collection<Triple> data = triplesForEntityT();
data.remove(new Triple(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_T)));
data.add(new Triple(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S_PARENT)));
final EntityManager em = getEntityManager("findReturnsNullWhenMatchingClassIsAbstract", false);
persistTestData(data, em);
assertNull(em.find(OWLClassSParent.class, entityT.getUri()));
}
@Test
void findReturnsMostSpecificSubtypeWhenReturnTypeIsAbstractAncestor() throws Exception {
final Collection<Triple> data = triplesForEntityT();
data.add(new Triple(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
data.add(new Triple(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S_PARENT)));
final EntityManager em = getEntityManager("findReturnsMostSpecificSubtypeWhenReturnTypeIsAbstractAncestor",
false);
persistTestData(data, em);
final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
assertNotNull(result);
assertTrue(result instanceof OWLClassT);
verifyEntityTAttributes((OWLClassT) result);
}
@Test
void findLoadsMostSpecificSubclassFromCache() {
this.em = getEntityManager("findLoadsMostSpecificSubclassFromCache", true);
persist(entityT, entityA);
final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
assertNotNull(result);
assertTrue(result instanceof OWLClassT);
verifyEntityTAttributes((OWLClassT) result);
}
@Test
void findLoadsInstanceOfSuperclassFromCacheWhenTypeMatchesAndIsSpecifiedAsReturnType() {
this.em = getEntityManager("findLoadsInstanceOfSuperclassWhenTypeMatchesAndIsSpecifiedAsReturnType", true);
persist(entityT, entityA);
final OWLClassS result = em.find(OWLClassS.class, entityT.getUri());
assertNotNull(result);
assertEquals(entityT.getName(), result.getName());
assertEquals(entityT.getDescription(), result.getDescription());
}
@Test
void findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypes() throws Exception {
final Collection<Triple> data = triplesForEntityT();
data.add(new Triple(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_U)));
this.em = getEntityManager("findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypes", false);
persistTestData(data, em);
assertThrows(AmbiguousEntityTypeException.class, () -> em.find(OWLClassS.class, entityT.getUri()));
}
@Test
void findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypesInCache() {
this.em = getEntityManager("findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypesInCache",
true);
persist(entityT, entityA);
em.clear();
final OWLClassU sameIndividual = new OWLClassU();
sameIndividual.setUri(entityT.getUri());
sameIndividual.setName(entityT.getName());
persist(sameIndividual);
assertTrue(em.getEntityManagerFactory().getCache()
.contains(OWLClassT.class, entityT.getUri(), new EntityDescriptor()));
assertTrue(em.getEntityManagerFactory().getCache()
.contains(OWLClassU.class, sameIndividual.getUri(), new EntityDescriptor()));
assertThrows(AmbiguousEntityTypeException.class, () -> em.find(OWLClassS.class, entityT.getUri()));
}
@Test
void loadingEntityLoadsExactMatchOfPolymorphicAttribute() {
this.em = getEntityManager("loadingEntityLoadsExactMatchOfPolymorphicAttribute", false);
final OWLClassS s = new OWLClassS();
s.setName("s");
s.setDescription("S - description");
entityU.setOwlClassS(s);
persist(entityU, s);
final OWLClassU result = em.find(OWLClassU.class, entityU.getUri());
assertNotNull(result);
assertNotNull(result.getOwlClassS());
assertEquals(s.getUri(), result.getOwlClassS().getUri());
assertEquals(s.getName(), result.getOwlClassS().getName());
assertEquals(s.getDescription(), result.getOwlClassS().getDescription());
}
@Test
void loadingEntityLoadsCorrectSubtypeInPolymorphicAttribute() {
this.em = getEntityManager("loadingEntityLoadsCorrectSubtypeInPolymorphicAttribute", false);
persist(entityU, entityT, entityA);
final OWLClassU result = em.find(OWLClassU.class, entityU.getUri());
assertNotNull(result.getOwlClassS());
assertTrue(result.getOwlClassS() instanceof OWLClassT);
final OWLClassT tResult = (OWLClassT) result.getOwlClassS();
verifyEntityTAttributes(tResult);
assertNotNull(tResult.getOwlClassA());
assertEquals(entityA.getUri(), tResult.getOwlClassA().getUri());
}
}