-
Notifications
You must be signed in to change notification settings - Fork 14
/
RetrieveOperationsRunner.java
435 lines (382 loc) · 20.1 KB
/
RetrieveOperationsRunner.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
* 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.JOPAPersistenceProperties;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.model.query.TypedQuery;
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.Quad;
import cz.cvut.kbss.jopa.vocabulary.RDF;
import cz.cvut.kbss.ontodriver.ReloadableDataSource;
import cz.cvut.kbss.ontodriver.config.OntoDriverProperties;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
public abstract class RetrieveOperationsRunner extends BaseRunner {
public RetrieveOperationsRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
super(logger, persistenceFactory, dataAccessor);
}
@Test
void testRetrieveSimple() {
this.em = getEntityManager("RetrieveSimple", false);
persist(entityA);
em.getEntityManagerFactory().getCache().evictAll();
final OWLClassA res = findRequired(OWLClassA.class, entityA.getUri());
assertEquals(entityA.getUri(), res.getUri());
assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
assertTrue(entityA.getTypes().containsAll(res.getTypes()));
assertTrue(em.contains(res));
}
@Test
void testRetrieveWithLazyAttribute() throws Exception {
this.em = getEntityManager("RetrieveLazy", false);
persist(entityI);
final OWLClassI resI = findRequired(OWLClassI.class, entityI.getUri());
final Field f = OWLClassI.class.getDeclaredField("owlClassA");
f.setAccessible(true);
Object value = f.get(resI);
assertNull(value);
assertNotNull(resI.getOwlClassA());
value = f.get(resI);
assertNotNull(value);
assertEquals(entityA.getUri(), resI.getOwlClassA().getUri());
assertTrue(em.contains(resI.getOwlClassA()));
}
@Test
void testRetrieveWithGeneratedId() {
this.em = getEntityManager("RetrieveGenerated", false);
em.getTransaction().begin();
final int size = 10;
final List<OWLClassE> lst = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
final OWLClassE e = new OWLClassE();
e.setStringAttribute("blablabla" + i);
assertNull(e.getUri());
em.persist(e);
assertNotNull(e.getUri());
lst.add(e);
}
em.getTransaction().commit();
em.clear();
for (OWLClassE e : lst) {
final OWLClassE res = findRequired(OWLClassE.class, e.getUri());
assertEquals(e.getStringAttribute(), res.getStringAttribute());
}
}
@Test
void findByUnknownIdReturnsNull() {
this.em = getEntityManager("RetrieveNotExisting", false);
final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
assertNull(res);
}
@Test
void testRefreshInstance() {
this.em = getEntityManager("Refresh", false);
persist(entityD, entityA);
final OWLClassA newA = new OWLClassA();
newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
newA.setStringAttribute("newA");
final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
final OWLClassA a = findRequired(OWLClassA.class, entityA.getUri());
assertEquals(d.getOwlClassA(), a);
d.setOwlClassA(newA);
em.refresh(d);
assertEquals(a.getUri(), d.getOwlClassA().getUri());
}
@Test
void refreshingNotManagedIsIllegal() {
this.em = getEntityManager("RefreshNotManaged", false);
persist(entityA);
findRequired(OWLClassA.class, entityA.getUri());
final OWLClassA newA = new OWLClassA();
newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
newA.setStringAttribute("newA");
assertThrows(IllegalArgumentException.class, () -> em.refresh(newA));
}
@Test
void findOfEntityWithExistingIdButDifferentTypeReturnsNull() {
this.em = getEntityManager("RetrieveDifferentType", false);
persist(entityA);
final OWLClassB res = em.find(OWLClassB.class, entityA.getUri());
assertNull(res);
}
@Test
void testRefreshInstanceWithUnmappedProperties() {
this.em = getEntityManager("RefreshEntityWithProperties", false);
final Map<URI, Set<Object>> properties = Generators.createTypedProperties();
entityP.setProperties(properties);
persist(entityP);
final OWLClassP p = findRequired(OWLClassP.class, entityP.getUri());
p.getProperties().put(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#addedProperty"),
Collections.singleton("Test"));
assertNotEquals(properties, p.getProperties());
em.refresh(p);
assertEquals(properties, p.getProperties());
}
@Test
void plainIdentifierAttributeIsAlwaysLoadedEagerly() throws Exception {
this.em = getEntityManager("PlainIdentifiersAreLoadedEagerly", false);
entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#plainIdentifier"));
entityP.setIndividuals(Collections.singleton(new URL("http://krizik.felk.cvut.cz/ontologies/jopa#url")));
persist(entityP);
final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
final Field singularField = OWLClassP.class.getDeclaredField("individualUri");
singularField.setAccessible(true);
assertNotNull(singularField.get(res));
final Field pluralField = OWLClassP.class.getDeclaredField("individuals");
pluralField.setAccessible(true);
assertNotNull(pluralField.get(res));
}
@Test
void readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance() {
this.em = getEntityManager("readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance", true);
persist(entityN);
final OWLClassN resultOne = findRequired(OWLClassN.class, entityN.getId());
final OWLClassN resultTwo = findRequired(OWLClassN.class, entityN.getId());
assertSame(resultOne, resultTwo);
}
@Test
void retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues() {
this.em = getEntityManager("retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues", false);
final OWLClassV v = new OWLClassV();
v.setProperties(Generators.createProperties());
v.setThings(new HashSet<>());
for (int i = 0; i < Generators.randomPositiveInt(5, 10); i++) {
final Thing thing = new Thing();
thing.setName("thing" + i);
thing.setDescription("description of a thing. Number " + i);
thing.setTypes(Collections.singleton(Vocabulary.C_OWL_CLASS_A));
v.getThings().add(thing);
}
em.getTransaction().begin();
em.persist(v);
v.getThings().forEach(em::persist);
em.getTransaction().commit();
em.clear();
final OWLClassV result = findRequired(OWLClassV.class, v.getUri());
assertEquals(v.getProperties(), result.getProperties());
final Set<String> expectedUris = v.getThings().stream().map(Thing::getUri).collect(Collectors.toSet());
assertEquals(v.getThings().size(), result.getThings().size());
result.getThings().forEach(t -> assertTrue(expectedUris.contains(t.getUri())));
}
@Test
void retrieveGetsStringAttributeWithCorrectLanguageWhenItIsSpecifiedInDescriptor() throws Exception {
this.em = getEntityManager("retrieveGetsStringAttributeWithCorrectLanguageWhenItIsSpecifiedInDescriptor",
false);
persist(entityA);
final String value = "v cestine";
final String lang = "cs";
persistTestData(Collections
.singleton(new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, lang)), em);
final Descriptor descriptor = new EntityDescriptor();
descriptor.setLanguage(lang);
final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptor);
assertNotNull(result);
assertEquals(value, result.getStringAttribute());
assertEquals(entityA.getTypes(), result.getTypes());
}
@Test
void retrieveGetsStringAttributesWithDifferentLanguageTagsSpecifiedInDescriptor() throws Exception {
this.em = getEntityManager("retrieveGetsStringAttributesWithDifferentLanguageTagsSpecifiedInDescriptor", false);
entityN.setAnnotationProperty("english annotation");
entityN.setStringAttribute("english string");
persist(entityN);
final String csAnnotation = "anotace cesky";
final String csString = "retezec cesky";
final Set<Quad> testData = new HashSet<>();
testData.add(new Quad(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STR_ANNOTATION_PROPERTY),
csAnnotation, "cs"));
testData.add(
new Quad(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STRING_ATTRIBUTE), csString, "cs"));
persistTestData(testData, em);
final Descriptor descriptor = new EntityDescriptor();
descriptor.setAttributeLanguage(OWLClassN.class.getDeclaredField("annotationProperty"), "en");
descriptor.setAttributeLanguage(OWLClassN.class.getDeclaredField("stringAttribute"), "cs");
final OWLClassN result = em.find(OWLClassN.class, entityN.getId(), descriptor);
assertEquals(entityN.getAnnotationProperty(), result.getAnnotationProperty());
assertEquals(csString, result.getStringAttribute());
}
@Test
void retrieveAllowsToOverridePULevelLanguageSpecification() throws Exception {
this.em = getEntityManager("retrieveAllowsToOverridePULevelLanguageSpecification", false);
entityA.setStringAttribute(null);
// PU-level language is en
persist(entityA);
final String value = "cestina";
persistTestData(Collections
.singleton(new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, "cs")), em);
final OWLClassA resOne = findRequired(OWLClassA.class, entityA.getUri());
assertNull(resOne.getStringAttribute());
em.clear();
final Descriptor descriptor = new EntityDescriptor();
descriptor.setLanguage("cs");
final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
assertEquals(value, resTwo.getStringAttribute());
}
@Test
void retrieveLoadsStringLiteralWithCorrectLanguageTagWhenCachedValueHasDifferentLanguageTag()
throws Exception {
this.em = getEntityManager(
"retrieveLoadsStringLiteralWithCorrectLanguageTagWhenCachedValueHasDifferentLanguageTag", true);
persist(entityA); // persisted with @en
final String value = "cestina";
persistTestData(Collections
.singleton(new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value, "cs")), em);
final OWLClassA resOne = findRequired(OWLClassA.class, entityA.getUri());
assertEquals(entityA.getStringAttribute(), resOne.getStringAttribute());
em.clear();
final Descriptor descriptor = new EntityDescriptor();
descriptor.setLanguage("cs");
final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
assertEquals(value, resTwo.getStringAttribute());
}
@Test
public void reloadAllowsToReloadFileStorageContent() throws Exception {
final Map<String, String> props = new HashMap<>();
final File storage = Files.createTempFile("reload-driver-test", ".owl").toFile();
storage.deleteOnExit();
final String initialContent = "<?xml version=\"1.0\"?>\n" +
"<rdf:RDF\n" +
" xmlns:owl = \"http://www.w3.org/2002/07/owl#\"\n" +
" xmlns:rdf = \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" +
"<owl:Ontology rdf:about=\"http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine\"></owl:Ontology>" +
"</rdf:RDF>";
Files.write(storage.toPath(), initialContent.getBytes());
props.put(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY, storage.getAbsolutePath());
props.put(JOPAPersistenceProperties.ONTOLOGY_URI_KEY, "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine");
props.put(OntoDriverProperties.USE_TRANSACTIONAL_ONTOLOGY, Boolean.toString(false));
addFileStorageProperties(props);
this.em = getEntityManager("reloadAllowsToReloadFileStorageContent", false, props);
final String subject = "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#CaliforniaRegion";
final String type = "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#Region";
em.getTransaction().begin();
final TypedQuery<Boolean> query =
em.createNativeQuery("ASK { ?x a ?y . }", Boolean.class).setParameter("x", URI.create(subject))
.setParameter("y", URI.create(type));
assertFalse(query.getSingleResult());
replaceFileContents(storage);
final ReloadableDataSource ds = em.getEntityManagerFactory().unwrap(ReloadableDataSource.class);
ds.reload();
// Need to force OWLAPI driver to open a new ontology snapshot with the reloaded data
em.getTransaction().commit();
assertTrue(query.getSingleResult());
}
private void replaceFileContents(File target) throws IOException {
try (final InputStream is = new URL("https://www.w3.org/TR/2003/PR-owl-guide-20031209/wine").openStream()) {
Files.copy(is, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
protected abstract void addFileStorageProperties(Map<String, String> properties);
@Test
void getReferenceRetrievesReferenceToInstanceWithDataPropertiesWhoseAttributesAreLoadedLazily()
throws Exception {
this.em = getEntityManager(
"getReferenceRetrievesReferenceToInstanceWithDataPropertiesWhoseAttributesAreLoadedLazily", false);
persist(entityM);
final OWLClassM result = em.getReference(OWLClassM.class, entityM.getKey());
assertNotNull(result);
assertTrue(em.contains(result));
final Field intAttField = OWLClassM.class.getDeclaredField("intAttribute");
intAttField.setAccessible(true);
assertNull(intAttField.get(result));
assertNotNull(result.getIntAttribute());
assertEquals(entityM.getBooleanAttribute(), result.getBooleanAttribute());
assertEquals(entityM.getIntAttribute(), result.getIntAttribute());
assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
assertEquals(entityM.getDateAttribute(), result.getDateAttribute());
assertEquals(entityM.getEnumAttribute(), result.getEnumAttribute());
}
@Test
void getReferenceRetrievesReferenceToInstanceWithObjectPropertiesWhoseAttributesAreLoadedLazily() {
this.em = getEntityManager(
"getReferenceRetrievesReferenceToInstanceWithObjectPropertiesWhoseAttributesAreLoadedLazily", false);
persist(entityG);
final OWLClassG gResult = em.getReference(OWLClassG.class, entityG.getUri());
assertNotNull(gResult);
assertNotNull(gResult.getOwlClassH());
assertEquals(entityH.getUri(), gResult.getOwlClassH().getUri());
assertNotNull(gResult.getOwlClassH().getOwlClassA());
assertEquals(entityA.getUri(), gResult.getOwlClassH().getOwlClassA().getUri());
assertEquals(entityA.getStringAttribute(), gResult.getOwlClassH().getOwlClassA().getStringAttribute());
}
@Test
void getReferenceRetrievesReferenceToInstanceWhenCacheIsEnabled() {
this.em = getEntityManager("getReferenceRetrievesReferenceToInstanceWhenCacheIsEnabled", true);
persist(entityA);
final OWLClassA result = em.getReference(OWLClassA.class, entityA.getUri());
assertNotNull(result);
assertEquals(entityA.getUri(), result.getUri());
assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
}
@Test
void loadingEntityWithLexicalFormAttributeLoadsLexicalFormOfLiteral() throws Exception {
this.em = getEntityManager("loadingEntityWithLexicalFormAttributeLoadsLexicalFormOfLiteral", false);
persist(entityM);
final Integer value = 117;
persistTestData(Collections
.singleton(new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_lexicalForm), value)),
em);
final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
assertEquals(value.toString(), result.getLexicalForm());
}
@Test
void loadingEntityWithSimpleLiteralLoadsSimpleLiteralValue() throws Exception {
this.em = getEntityManager("loadingEntityWithSimpleLiteralLoadsSimpleLiteralValue", false);
final String value = "test";
persistTestData(Arrays.asList(
new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_simpleLiteral), value, (String) null)),
em);
final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
assertEquals(value, result.getSimpleLiteral());
}
@Test
void loadEntityWithSimpleLiteralLoadsAlsoLanguageTaggedValue() throws Exception {
this.em = getEntityManager("loadEntityWithSimpleLiteralLoadsAlsoLanguageTaggedValue", false);
final String value = "test";
persistTestData(Arrays.asList(
new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_simpleLiteral), value, "en")), em);
final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
assertEquals(value, result.getSimpleLiteral());
}
@Test
void loadEntitySupportsCollectionAttribute() throws Exception {
this.em = getEntityManager("loadEntitySupportsCollectionAttribute", false);
persistTestData(Arrays.asList(
new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_StringCollection), "value", "en"),
new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_StringCollection), "valueTwo", "en")),
em);
final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
assertNotNull(result.getStringCollection());
assertFalse(result.getStringCollection().isEmpty());
assertThat(result.getStringCollection(), hasItems("value", "valueTwo"));
}
}