-
Notifications
You must be signed in to change notification settings - Fork 14
/
BaseRunner.java
290 lines (260 loc) · 12 KB
/
BaseRunner.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
283
284
285
286
287
288
289
290
/**
* Copyright (C) 2020 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.Descriptor;
import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
import cz.cvut.kbss.jopa.test.*;
import cz.cvut.kbss.jopa.test.environment.DataAccessor;
import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
import cz.cvut.kbss.jopa.test.environment.Quad;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.function.Executable;
import org.slf4j.Logger;
import java.net.URI;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
public abstract class BaseRunner {
protected static final URI CONTEXT_ONE = URI.create("http://krizik.felk.cvut.cz/jopa/contexts#One");
protected static final URI CONTEXT_TWO = URI.create("http://krizik.felk.cvut.cz/jopa/contexts#Two");
protected final Logger logger;
protected EntityManager em;
protected OWLClassA entityA;
protected OWLClassB entityB;
protected OWLClassC entityC;
protected OWLClassD entityD;
// Generated IRI
protected OWLClassE entityE;
// Two relationships, cascade
protected OWLClassG entityG;
protected OWLClassH entityH;
// Lazy reference to OWLClassA
protected OWLClassI entityI;
protected OWLClassM entityM;
protected OWLClassN entityN;
protected OWLClassP entityP;
// Mapped superclass
protected OWLClassQ entityQ;
// Query based attribute
protected OWLClassWithQueryAttr entityWithQueryAttr;
protected OWLClassWithQueryAttr2 entityWithQueryAttr2;
protected OWLClassWithQueryAttr3 entityWithQueryAttr3;
protected OWLClassWithQueryAttr4 entityWithQueryAttr4;
protected OWLClassWithQueryAttr5 entityWithQueryAttr5;
protected OWLClassWithQueryAttr6 entityWithQueryAttr6;
protected final DataAccessor dataAccessor;
protected final PersistenceFactory persistenceFactory;
public BaseRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
assert logger != null;
this.logger = logger;
this.persistenceFactory = persistenceFactory;
this.dataAccessor = dataAccessor;
init();
}
/**
* Initializes the test entities in the following manner:
* <pre>
* <ul>
* <li>entityA contains non-empty types</li>
* <li>entityB's properties are null</li>
* <li>entityC's simple and referenced lists are null</li>
* <li>entityD's reference to OWLClassA is set to entityA</li>
* <li>entityE's URI is left null for ID generation</li>
* <li>entityG's reference to OWLClassH is set to entityH</li>
* <li>entityH's reference to OWLClassA is set to entityA</li>
* <li>entityI's reference to OWLClassA is set to entityA</li>
* <li>entityM has all fields set to some values</li>
* <li>entityN has required fields set</li>
* <li>entityP's properties and uri are null</li>
* <li>entityQ's reference to OWLClassA is set to entityA</li>
* </ul>
* </pre>
*/
private void init() {
this.entityA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
entityA.setStringAttribute("entityAStringAttribute");
final Set<String> types = new HashSet<>();
types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassU");
entityA.setTypes(types);
this.entityB = new OWLClassB();
entityB.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityB"));
entityB.setStringAttribute("entityBStringAttribute");
this.entityC = new OWLClassC();
entityC.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityC"));
this.entityD = new OWLClassD(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityD"));
entityD.setOwlClassA(entityA);
this.entityE = new OWLClassE();
entityE.setStringAttribute("entityEStringAttribute");
this.entityH = new OWLClassH();
entityH.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityH"));
entityH.setOwlClassA(entityA);
this.entityG = new OWLClassG();
entityG.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityG"));
entityG.setOwlClassH(entityH);
this.entityI = new OWLClassI();
entityI.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityI"));
entityI.setOwlClassA(entityA);
this.entityM = new OWLClassM();
entityM.initializeTestValues(true);
this.entityN = new OWLClassN();
entityN.setStringAttribute("entityNStringAttribute");
this.entityP = new OWLClassP();
this.entityQ = new OWLClassQ();
entityQ.setStringAttribute("entityQStringAttribute");
entityQ.setParentString("entityQParentStringAttribute");
entityQ.setLabel("entityQLabel");
entityQ.setOwlClassA(entityA);
this.entityWithQueryAttr = new OWLClassWithQueryAttr();
entityWithQueryAttr.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr"));
entityWithQueryAttr.setStringAttribute("EntityWithQueryAttrStringAttribute");
this.entityWithQueryAttr2 = new OWLClassWithQueryAttr2();
entityWithQueryAttr2.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr2"));
entityWithQueryAttr2.setEntityAttribute(entityA);
this.entityWithQueryAttr3 = new OWLClassWithQueryAttr3();
entityWithQueryAttr3.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr3"));
this.entityWithQueryAttr4 = new OWLClassWithQueryAttr4();
entityWithQueryAttr4.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr4"));
entityWithQueryAttr4.setStringAttribute("EntityWithQueryAttrStringAttribute4");
this.entityWithQueryAttr5 = new OWLClassWithQueryAttr5();
entityWithQueryAttr5.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr5"));
this.entityWithQueryAttr6 = new OWLClassWithQueryAttr6();
entityWithQueryAttr6.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr6"));
}
@AfterEach
public void tearDown() {
if (em != null && em.isOpen()) {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
em.getEntityManagerFactory().close();
}
}
/**
* Persists the specified instance(s) in a separate transaction.
*
* @param entity Entity to persist
*/
protected void persist(Object... entity) {
transactional(() -> {
for (Object ent : entity) {
em.persist(ent);
}
});
}
/**
* Runs the specified action in a transaction on the current entity manager.
*
* @param action The code to run
*/
protected void transactional(Runnable action) {
em.getTransaction().begin();
action.run();
em.getTransaction().commit();
}
/**
* Runs the specified action in a transaction on the current entity manager.
* <p>
* This allows using also actions which throw checked exceptions.
*
* @param action The code to run
* @see #transactional(Runnable)
*/
protected void transactionalThrowing(Executable action) {
try {
em.getTransaction().begin();
action.execute();
em.getTransaction().commit();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
/**
* Verifies that no statements with the specified individual as subject exist in the ontology any more.
*
* @param identifier Individual identifier
*/
protected void verifyIndividualWasRemoved(URI identifier) {
// TODO There is a bug in OWL2Query - the query returns true, because it finds the top object and data property assertion for an individual
// which doesn't exist anymore (but is a part of the query)
final boolean remains = em.createNativeQuery("ASK WHERE { ?instance ?y ?z . }", Boolean.class)
.setParameter("instance", identifier).getSingleResult();
assertFalse(remains);
}
protected EntityManager getEntityManager(String repositoryName, boolean cacheEnabled) {
return getEntityManager(repositoryName, cacheEnabled, Collections.emptyMap());
}
protected EntityManager getEntityManager(String repositoryName, boolean cacheEnabled,
Map<String, String> properties) {
return persistenceFactory.getEntityManager(repositoryName, cacheEnabled, properties);
}
protected void persistTestData(Collection<Quad> data, EntityManager em) throws Exception {
dataAccessor.persistTestData(data, em);
}
protected void verifyStatementsPresent(Collection<Quad> expected, EntityManager em) throws Exception {
dataAccessor.verifyDataPresent(expected, em);
}
protected void verifyStatementsNotPresent(Collection<Quad> notExpected, EntityManager em) throws Exception {
dataAccessor.verifyDataNotPresent(notExpected, em);
}
// Utility methods to reduce duplication
/**
* Persists the specified instance of {@link OWLClassC} together will all items in the lists (if specified).
*
* @param instance Instace to persist
*/
void persistCWithLists(OWLClassC instance) {
transactional(() -> {
em.persist(instance);
if (instance.getSimpleList() != null) {
instance.getSimpleList().forEach(em::persist);
}
if (instance.getReferencedList() != null) {
instance.getReferencedList().forEach(em::persist);
}
});
}
/**
* Finds instance and checks for it not being {@code null}.
*
* @return The found instance
*/
<T> T findRequired(Class<T> type, Object identifier) {
final T result = em.find(type, identifier);
assertNotNull(result);
return result;
}
/**
* Finds instance and checks for it not being {@code null}.
*
* @return The found instance
*/
<T> T findRequired(Class<T> type, Object identifier, Descriptor descriptor) {
final T result = em.find(type, identifier, descriptor);
assertNotNull(result);
return result;
}
<T> void verifyExists(Class<T> type, Object identifier) {
assertNotNull(em.find(type, identifier));
}
protected void verifyValueDatatype(URI identifier, String property, String expectedDatatype) {
assertTrue(em.createNativeQuery("ASK WHERE { ?x ?property ?value . " +
"FILTER (DATATYPE(?value) = ?datatype) }", Boolean.class)
.setParameter("x", identifier)
.setParameter("property", URI.create(property))
.setParameter("datatype", URI.create(expectedDatatype)).getSingleResult());
}
protected <T> FieldSpecification<? super T, ?> fieldSpecification(Class<T> cls, String attName) {
return em.getMetamodel().entity(cls).getFieldSpecification(attName);
}
}