Skip to content

Method: testRetrieveEntityWithManagedTypeQueryAttr()

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.test.runner;
19:
20: import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
21: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
22: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
23: import cz.cvut.kbss.jopa.model.query.TypedQuery;
24: import cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy;
25: import cz.cvut.kbss.jopa.test.OWLClassA;
26: import cz.cvut.kbss.jopa.test.OWLClassB;
27: import cz.cvut.kbss.jopa.test.OWLClassC;
28: import cz.cvut.kbss.jopa.test.OWLClassD;
29: import cz.cvut.kbss.jopa.test.OWLClassE;
30: import cz.cvut.kbss.jopa.test.OWLClassG;
31: import cz.cvut.kbss.jopa.test.OWLClassI;
32: import cz.cvut.kbss.jopa.test.OWLClassM;
33: import cz.cvut.kbss.jopa.test.OWLClassN;
34: import cz.cvut.kbss.jopa.test.OWLClassP;
35: import cz.cvut.kbss.jopa.test.OWLClassV;
36: import cz.cvut.kbss.jopa.test.OWLClassWithQueryAttr;
37: import cz.cvut.kbss.jopa.test.OWLClassWithQueryAttr2;
38: import cz.cvut.kbss.jopa.test.OWLClassWithQueryAttr6;
39: import cz.cvut.kbss.jopa.test.OWLClassWithUrn;
40: import cz.cvut.kbss.jopa.test.Thing;
41: import cz.cvut.kbss.jopa.test.Vocabulary;
42: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
43: import cz.cvut.kbss.jopa.test.environment.Generators;
44: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
45: import cz.cvut.kbss.jopa.test.environment.Quad;
46: import cz.cvut.kbss.jopa.vocabulary.RDF;
47: import cz.cvut.kbss.ontodriver.ReloadableDataSource;
48: import cz.cvut.kbss.ontodriver.config.OntoDriverProperties;
49: import org.junit.jupiter.api.Test;
50: import org.slf4j.Logger;
51:
52: import java.io.File;
53: import java.io.IOException;
54: import java.io.InputStream;
55: import java.lang.reflect.Field;
56: import java.lang.reflect.Method;
57: import java.net.URI;
58: import java.net.URL;
59: import java.nio.file.Files;
60: import java.nio.file.StandardCopyOption;
61: import java.time.LocalDate;
62: import java.util.ArrayList;
63: import java.util.Arrays;
64: import java.util.Collections;
65: import java.util.HashMap;
66: import java.util.HashSet;
67: import java.util.List;
68: import java.util.Map;
69: import java.util.Set;
70: import java.util.stream.Collectors;
71:
72: import static org.hamcrest.CoreMatchers.hasItems;
73: import static org.hamcrest.CoreMatchers.instanceOf;
74: import static org.hamcrest.MatcherAssert.assertThat;
75: import static org.hamcrest.Matchers.not;
76: import static org.junit.jupiter.api.Assertions.assertEquals;
77: import static org.junit.jupiter.api.Assertions.assertFalse;
78: import static org.junit.jupiter.api.Assertions.assertInstanceOf;
79: import static org.junit.jupiter.api.Assertions.assertNotEquals;
80: import static org.junit.jupiter.api.Assertions.assertNotNull;
81: import static org.junit.jupiter.api.Assertions.assertNull;
82: import static org.junit.jupiter.api.Assertions.assertSame;
83: import static org.junit.jupiter.api.Assertions.assertThrows;
84: import static org.junit.jupiter.api.Assertions.assertTrue;
85:
86:
87: public abstract class RetrieveOperationsRunner extends BaseRunner {
88:
89: public RetrieveOperationsRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
90: super(logger, persistenceFactory, dataAccessor);
91: }
92:
93: @Test
94: void testRetrieveSimple() {
95: this.em = getEntityManager("RetrieveSimple", false);
96: persist(entityA);
97:
98: em.getEntityManagerFactory().getCache().evictAll();
99: final OWLClassA res = findRequired(OWLClassA.class, entityA.getUri());
100: assertEquals(entityA.getUri(), res.getUri());
101: assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
102: assertTrue(entityA.getTypes().containsAll(res.getTypes()));
103: assertTrue(em.contains(res));
104: }
105:
106: @Test
107: void testRetrieveWithLazyAttribute() {
108: this.em = getEntityManager("RetrieveLazy", false);
109: persist(entityI);
110:
111: final OWLClassI resI = findRequired(OWLClassI.class, entityI.getUri());
112: assertInstanceOf(LazyLoadingProxy.class, resI.getOwlClassA());
113: // Trigger lazy loading
114: assertNotNull(resI.getOwlClassA().getUri());
115: assertEquals(entityA.getUri(), resI.getOwlClassA().getUri());
116: assertTrue(em.contains(resI.getOwlClassA()));
117: }
118:
119: @Test
120: void testRetrieveWithGeneratedId() {
121: this.em = getEntityManager("RetrieveGenerated", false);
122: em.getTransaction().begin();
123: final int size = 10;
124: final List<OWLClassE> lst = new ArrayList<>(size);
125: for (int i = 0; i < size; i++) {
126: final OWLClassE e = new OWLClassE();
127: e.setStringAttribute("blablabla" + i);
128: assertNull(e.getUri());
129: em.persist(e);
130: assertNotNull(e.getUri());
131: lst.add(e);
132: }
133: em.getTransaction().commit();
134:
135: em.clear();
136: for (OWLClassE e : lst) {
137: final OWLClassE res = findRequired(OWLClassE.class, e.getUri());
138: assertEquals(e.getStringAttribute(), res.getStringAttribute());
139: }
140: }
141:
142: @Test
143: void findByUnknownIdReturnsNull() {
144: this.em = getEntityManager("RetrieveNotExisting", false);
145: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
146: assertNull(res);
147: }
148:
149: @Test
150: void testRefreshInstance() {
151: this.em = getEntityManager("Refresh", false);
152: persist(entityD, entityA);
153:
154: final OWLClassA newA = new OWLClassA();
155: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
156: newA.setStringAttribute("newA");
157: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
158: final OWLClassA a = findRequired(OWLClassA.class, entityA.getUri());
159: assertEquals(d.getOwlClassA(), a);
160: d.setOwlClassA(newA);
161: em.refresh(d);
162: assertEquals(a.getUri(), d.getOwlClassA().getUri());
163: }
164:
165: @Test
166: void refreshingNotManagedIsIllegal() {
167: this.em = getEntityManager("RefreshNotManaged", false);
168: persist(entityA);
169:
170: findRequired(OWLClassA.class, entityA.getUri());
171: final OWLClassA newA = new OWLClassA();
172: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
173: newA.setStringAttribute("newA");
174: assertThrows(IllegalArgumentException.class, () -> em.refresh(newA));
175: }
176:
177: @Test
178: void findOfEntityWithExistingIdButDifferentTypeReturnsNull() {
179: this.em = getEntityManager("RetrieveDifferentType", false);
180: persist(entityA);
181:
182: final OWLClassB res = em.find(OWLClassB.class, entityA.getUri());
183: assertNull(res);
184: }
185:
186: @Test
187: void testRefreshInstanceWithUnmappedProperties() {
188: this.em = getEntityManager("RefreshEntityWithProperties", false);
189: final Map<URI, Set<Object>> properties = Generators.createTypedProperties();
190: entityP.setProperties(properties);
191: persist(entityP);
192:
193: final OWLClassP p = findRequired(OWLClassP.class, entityP.getUri());
194: p.getProperties().put(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#addedProperty"),
195: Collections.singleton("Test"));
196: assertNotEquals(properties, p.getProperties());
197: em.refresh(p);
198: assertEquals(properties, p.getProperties());
199: }
200:
201: @Test
202: void plainIdentifierAttributeIsAlwaysLoadedEagerly() throws Exception {
203: this.em = getEntityManager("PlainIdentifiersAreLoadedEagerly", false);
204: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#plainIdentifier"));
205: entityP.setIndividuals(Collections.singleton(new URL("http://krizik.felk.cvut.cz/ontologies/jopa#url")));
206: persist(entityP);
207:
208: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
209: final Field singularField = OWLClassP.class.getDeclaredField("individualUri");
210: singularField.setAccessible(true);
211: assertNotNull(singularField.get(res));
212: final Field pluralField = OWLClassP.class.getDeclaredField("individuals");
213: pluralField.setAccessible(true);
214: assertNotNull(pluralField.get(res));
215: }
216:
217: @Test
218: void readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance() {
219: this.em = getEntityManager("readingIndividualWithStringIdTwiceInPersistenceContextReturnsSameInstance", true);
220: persist(entityN);
221:
222: final OWLClassN resultOne = findRequired(OWLClassN.class, entityN.getId());
223: final OWLClassN resultTwo = findRequired(OWLClassN.class, entityN.getId());
224: assertSame(resultOne, resultTwo);
225: }
226:
227: @Test
228: void retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues() {
229: this.em = getEntityManager("retrieveLoadsUnmappedPropertiesTogetherWithObjectPropertyValues", false);
230: final OWLClassV v = new OWLClassV();
231: v.setProperties(Generators.createProperties());
232: v.setThings(new HashSet<>());
233: for (int i = 0; i < Generators.randomPositiveInt(5, 10); i++) {
234: final Thing thing = new Thing();
235: thing.setName("thing" + i);
236: thing.setDescription("description of a thing. Number " + i);
237: thing.setTypes(Collections.singleton(Vocabulary.C_OWL_CLASS_A));
238: v.getThings().add(thing);
239: }
240: em.getTransaction().begin();
241: em.persist(v);
242: v.getThings().forEach(em::persist);
243: em.getTransaction().commit();
244: em.clear();
245:
246: final OWLClassV result = findRequired(OWLClassV.class, v.getUri());
247: assertEquals(v.getProperties(), result.getProperties());
248: final Set<String> expectedUris = v.getThings().stream().map(Thing::getUri).collect(Collectors.toSet());
249: assertEquals(v.getThings().size(), result.getThings().size());
250: result.getThings().forEach(t -> assertTrue(expectedUris.contains(t.getUri())));
251: }
252:
253: @Test
254: void retrieveGetsStringAttributeWithCorrectLanguageWhenItIsSpecifiedInDescriptor() throws Exception {
255: this.em = getEntityManager("retrieveGetsStringAttributeWithCorrectLanguageWhenItIsSpecifiedInDescriptor",
256: false);
257: persist(entityA);
258: final String value = "v cestine";
259: final String lang = "cs";
260: persistTestData(Collections
261: .singleton(
262: new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value,
263: lang)), em);
264:
265: final Descriptor descriptor = new EntityDescriptor();
266: descriptor.setLanguage(lang);
267:
268: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptor);
269: assertNotNull(result);
270: assertEquals(value, result.getStringAttribute());
271: assertEquals(entityA.getTypes(), result.getTypes());
272: }
273:
274: @Test
275: void retrieveGetsStringAttributesWithDifferentLanguageTagsSpecifiedInDescriptor() throws Exception {
276: this.em = getEntityManager("retrieveGetsStringAttributesWithDifferentLanguageTagsSpecifiedInDescriptor", false);
277: entityN.setAnnotationProperty("english annotation");
278: entityN.setStringAttribute("english string");
279: persist(entityN);
280: final String csAnnotation = "anotace cesky";
281: final String csString = "retezec cesky";
282: final Set<Quad> testData = new HashSet<>();
283: testData.add(new Quad(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STR_ANNOTATION_PROPERTY),
284: csAnnotation, "cs"));
285: testData.add(
286: new Quad(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STRING_ATTRIBUTE), csString, "cs"));
287: persistTestData(testData, em);
288:
289: final Descriptor descriptor = new EntityDescriptor();
290: descriptor.setAttributeLanguage(
291: em.getMetamodel().entity(OWLClassN.class).getDeclaredAttribute("annotationProperty"), "en");
292: descriptor
293: .setAttributeLanguage(em.getMetamodel().entity(OWLClassN.class).getDeclaredAttribute("stringAttribute"),
294: "cs");
295: final OWLClassN result = em.find(OWLClassN.class, entityN.getId(), descriptor);
296: assertEquals(entityN.getAnnotationProperty(), result.getAnnotationProperty());
297: assertEquals(csString, result.getStringAttribute());
298: }
299:
300: @Test
301: void retrieveAllowsToOverridePULevelLanguageSpecification() throws Exception {
302: this.em = getEntityManager("retrieveAllowsToOverridePULevelLanguageSpecification", false);
303: entityA.setStringAttribute(null);
304: // PU-level language is en
305: persist(entityA);
306: final String value = "cestina";
307: persistTestData(Collections
308: .singleton(
309: new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value,
310: "cs")), em);
311:
312: final OWLClassA resOne = findRequired(OWLClassA.class, entityA.getUri());
313: assertNull(resOne.getStringAttribute());
314: em.clear();
315:
316: final Descriptor descriptor = new EntityDescriptor();
317: descriptor.setLanguage("cs");
318: final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
319: assertEquals(value, resTwo.getStringAttribute());
320: }
321:
322: @Test
323: void retrieveLoadsStringLiteralWithCorrectLanguageTagWhenCachedValueHasDifferentLanguageTag()
324: throws Exception {
325: this.em = getEntityManager(
326: "retrieveLoadsStringLiteralWithCorrectLanguageTagWhenCachedValueHasDifferentLanguageTag", true);
327: persist(entityA); // persisted with @en
328: final String value = "cestina";
329: persistTestData(Collections
330: .singleton(
331: new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), value,
332: "cs")), em);
333:
334: final OWLClassA resOne = findRequired(OWLClassA.class, entityA.getUri());
335: assertEquals(entityA.getStringAttribute(), resOne.getStringAttribute());
336: em.clear();
337:
338: final Descriptor descriptor = new EntityDescriptor();
339: descriptor.setLanguage("cs");
340: final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
341: assertEquals(value, resTwo.getStringAttribute());
342: }
343:
344: @Test
345: public void reloadAllowsToReloadFileStorageContent() throws Exception {
346: final Map<String, String> props = new HashMap<>();
347: final File storage = Files.createTempFile("reload-driver-test", ".owl").toFile();
348: storage.deleteOnExit();
349: final String initialContent = """
350: <?xml version="1.0"?>
351: <rdf:RDF
352: xmlns:owl = "http://www.w3.org/2002/07/owl#"
353: xmlns:rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#">\
354: <owl:Ontology rdf:about="http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine"></owl:Ontology>\
355: </rdf:RDF>""";
356: Files.write(storage.toPath(), initialContent.getBytes());
357: props.put(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY, storage.toURI().toString());
358: props.put(JOPAPersistenceProperties.ONTOLOGY_URI_KEY, "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine");
359: props.put(OntoDriverProperties.USE_TRANSACTIONAL_ONTOLOGY, Boolean.toString(false));
360: addFileStorageProperties(props);
361: this.em = getEntityManager("reloadAllowsToReloadFileStorageContent", false, props);
362: final String subject = "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#CaliforniaRegion";
363: final String type = "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#Region";
364: em.getTransaction().begin();
365: final TypedQuery<Boolean> query =
366: em.createNativeQuery("ASK { ?x a ?y . }", Boolean.class).setParameter("x", URI.create(subject))
367: .setParameter("y", URI.create(type));
368: assertFalse(query.getSingleResult());
369: replaceFileContents(storage);
370:
371: final ReloadableDataSource ds = em.getEntityManagerFactory().unwrap(ReloadableDataSource.class);
372: ds.reload();
373: // Need to force OWLAPI driver to open a new ontology snapshot with the reloaded data
374: em.getTransaction().commit();
375: assertTrue(query.getSingleResult());
376: }
377:
378: private static void replaceFileContents(File target) throws IOException {
379: try (final InputStream is = new URL("https://www.w3.org/TR/2003/PR-owl-guide-20031209/wine").openStream()) {
380: Files.copy(is, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
381: }
382: }
383:
384: protected abstract void addFileStorageProperties(Map<String, String> properties);
385:
386: @Test
387: void getReferenceRetrievesReferenceToInstanceWithDataPropertiesWhoseAttributesAreLoadedLazily() throws Exception {
388: this.em = getEntityManager(
389: "getReferenceRetrievesReferenceToInstanceWithDataPropertiesWhoseAttributesAreLoadedLazily", false);
390: persist(entityM);
391: final OWLClassM result = em.getReference(OWLClassM.class, entityM.getKey());
392: assertNotNull(result);
393: assertTrue(em.contains(result));
394: final Field intAttField = OWLClassM.class.getDeclaredField("intAttribute");
395: intAttField.setAccessible(true);
396: assertNull(intAttField.get(result));
397: assertNotNull(result.getIntAttribute());
398: assertEquals(entityM.getBooleanAttribute(), result.getBooleanAttribute());
399: assertEquals(entityM.getIntAttribute(), result.getIntAttribute());
400: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
401: assertEquals(entityM.getDateAttribute(), result.getDateAttribute());
402: assertEquals(entityM.getEnumAttribute(), result.getEnumAttribute());
403: }
404:
405: @Test
406: void getReferenceRetrievesReferenceToInstanceWithObjectPropertiesWhoseAttributesAreLoadedLazily() {
407: this.em = getEntityManager(
408: "getReferenceRetrievesReferenceToInstanceWithObjectPropertiesWhoseAttributesAreLoadedLazily", false);
409: persist(entityG);
410: final OWLClassG gResult = em.getReference(OWLClassG.class, entityG.getUri());
411: assertNotNull(gResult);
412: assertNotNull(gResult.getOwlClassH());
413: assertEquals(entityH.getUri(), gResult.getOwlClassH().getUri());
414: assertNotNull(gResult.getOwlClassH().getOwlClassA());
415: assertEquals(entityA.getUri(), gResult.getOwlClassH().getOwlClassA().getUri());
416: assertEquals(entityA.getStringAttribute(), gResult.getOwlClassH().getOwlClassA().getStringAttribute());
417: }
418:
419: @Test
420: void getReferenceRetrievesReferenceToInstanceWhenCacheIsEnabled() {
421: this.em = getEntityManager("getReferenceRetrievesReferenceToInstanceWhenCacheIsEnabled", true);
422: persist(entityA);
423:
424: final OWLClassA result = em.getReference(OWLClassA.class, entityA.getUri());
425: assertNotNull(result);
426: assertEquals(entityA.getUri(), result.getUri());
427: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
428: }
429:
430: @Test
431: void loadingEntityWithLexicalFormAttributeLoadsLexicalFormOfLiteral() throws Exception {
432: this.em = getEntityManager("loadingEntityWithLexicalFormAttributeLoadsLexicalFormOfLiteral", false);
433: persist(entityM);
434: final Integer value = 117;
435: persistTestData(Collections
436: .singleton(
437: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_lexicalForm),
438: value)),
439: em);
440: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
441: assertEquals(value.toString(), result.getLexicalForm());
442: }
443:
444: @Test
445: void loadingEntityWithSimpleLiteralLoadsSimpleLiteralValue() throws Exception {
446: this.em = getEntityManager("loadingEntityWithSimpleLiteralLoadsSimpleLiteralValue", false);
447: final String value = "test";
448: persistTestData(Arrays.asList(
449: new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
450: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_simpleLiteral), value, (String) null)),
451: em);
452:
453: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
454: assertEquals(value, result.getSimpleLiteral());
455: }
456:
457: @Test
458: void loadingEntityWithSimpleLiteralUriLoadsSimpleLiteralValue() throws Exception {
459: this.em = getEntityManager("loadingEntityWithSimpleLiteralUriLoadsSimpleLiteralValue", false);
460: final URI value = Generators.generateUri();
461: persistTestData(Arrays.asList(
462: new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
463: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_simpleLiteralUri), value.toString(), (String) null)),
464: em);
465:
466: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
467: assertEquals(value, result.getSimpleLiteralUri());
468: }
469:
470: @Test
471: void loadEntityWithSimpleLiteralLoadsAlsoLanguageTaggedValue() throws Exception {
472: this.em = getEntityManager("loadEntityWithSimpleLiteralLoadsAlsoLanguageTaggedValue", false);
473: final String value = "test";
474: persistTestData(Arrays.asList(
475: new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
476: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_simpleLiteral), value, "en")), em);
477:
478: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
479: assertEquals(value, result.getSimpleLiteral());
480: }
481:
482: @Test
483: void loadEntitySupportsCollectionAttribute() throws Exception {
484: this.em = getEntityManager("loadEntitySupportsCollectionAttribute", false);
485: persistTestData(Arrays.asList(
486: new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
487: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_StringCollection), "value", "en"),
488: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_StringCollection), "valueTwo", "en")),
489: em);
490:
491: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
492: assertNotNull(result.getStringCollection());
493: assertFalse(result.getStringCollection().isEmpty());
494: assertThat(result.getStringCollection(), hasItems("value", "valueTwo"));
495: }
496:
497: @Test
498: void testRetrieveEntityWithQueryAttr() {
499: this.em = getEntityManager("RetrieveWithQueryAttr", false);
500:
501: persist(entityWithQueryAttr);
502:
503: em.getEntityManagerFactory().getCache().evictAll();
504: final OWLClassWithQueryAttr res = findRequired(OWLClassWithQueryAttr.class, entityWithQueryAttr.getUri());
505: assertEquals(entityWithQueryAttr.getUri(), res.getUri());
506: assertEquals(entityWithQueryAttr.getStringAttribute(), res.getStringAttribute());
507: assertEquals(entityWithQueryAttr.getStringAttribute(), res.getStringQueryAttribute());
508: assertTrue(em.contains(res));
509: }
510:
511: @Test
512: void testRetrieveEntityWithManagedTypeQueryAttr() {
513: this.em = getEntityManager("RetrieveWithManagedTypeQueryAttr", false);
514:
515: persist(entityWithQueryAttr2, entityA);
516:
517: final OWLClassWithQueryAttr2 res = findRequired(OWLClassWithQueryAttr2.class, entityWithQueryAttr2.getUri());
518: assertEquals(entityWithQueryAttr2.getUri(), res.getUri());
519: assertEquals(entityWithQueryAttr2.getEntityAttribute(), res.getEntityAttribute());
520: assertEquals(entityWithQueryAttr2.getEntityAttribute(), res.getEntityQueryAttribute());
521: assertTrue(em.contains(res));
522: }
523:
524: @Test
525: void testRetrieveWithLazyQueryAttribute() throws Exception {
526: this.em = getEntityManager("RetrieveLazyQueryAttr", false);
527:
528: entityWithQueryAttr6.setOwlClassA(entityA);
529: entityD.setOwlClassA(entityA);
530:
531:
532: persist(entityWithQueryAttr6, entityD);
533:
534: final OWLClassWithQueryAttr6 res = findRequired(OWLClassWithQueryAttr6.class, entityWithQueryAttr6.getUri());
535: final Field f = OWLClassWithQueryAttr6.class.getDeclaredField("lazyQueryAttribute");
536: f.setAccessible(true);
537: Object value = f.get(res);
538: assertInstanceOf(LazyLoadingProxy.class, value);
539: assertNotNull(res.getLazyQueryAttribute());
540: value = f.get(res);
541: assertNotNull(value);
542: assertEquals(entityD.getUri(), res.getLazyQueryAttribute().getUri());
543: }
544:
545: @Test
546: void testSupportForUrnIrisInClassAndProperty() {
547: this.em = getEntityManager("testSupportForUrnIrisInClassAndProperty", false);
548: final OWLClassWithUrn instance = new OWLClassWithUrn();
549: instance.setUri(Generators.generateUri());
550: instance.setLabel("Test instance with URN");
551: instance.setCreated(LocalDate.now());
552: persist(instance);
553:
554: final OWLClassWithUrn result = findRequired(OWLClassWithUrn.class, instance.getUri());
555: assertEquals(instance.getLabel(), result.getLabel());
556: assertEquals(instance.getCreated(), result.getCreated());
557: }
558:
559: @Test
560: void testSupportForUrnIdentifier() {
561: this.em = getEntityManager("testSupportForUrnIdentifier", false);
562: final URI id = URI.create("urn:jopa:instance:test-instance");
563: final OWLClassWithUrn instance = new OWLClassWithUrn();
564: instance.setUri(id);
565: persist(instance);
566:
567: findRequired(OWLClassWithUrn.class, id);
568: }
569:
570: @Test
571: void retrieveSupportsMappingEnumsToSimpleLiterals() throws Exception {
572: this.em = getEntityManager("retrieveSupportsMappingEnumsToSimpleLiterals", false);
573: persistTestData(List.of(
574: new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
575: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_enumSimpleLiteralAttribute),
576: entityM.getEnumSimpleLiteral().name(), (String) null)
577: ), em);
578:
579: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
580: assertEquals(entityM.getEnumSimpleLiteral(), result.getEnumSimpleLiteral());
581: }
582:
583: @Test
584: void retrieveViaReflectiveGetter() throws Exception {
585: this.em = getEntityManager("retrieveViaReflectiveGetter", false);
586: transactional(() -> {
587: em.persist(entityC);
588: entityC.setReferencedList(List.of(entityA));
589: entityC.getReferencedList().forEach(em::persist);
590: });
591: em.clear();
592:
593: final OWLClassC result = em.find(OWLClassC.class, entityC.getUri());
594: final Method getter = OWLClassC.class.getDeclaredMethod("getReferencedList");
595: final Object value = getter.invoke(result);
596: assertNotNull(value);
597: assertFalse(((List) value).isEmpty());
598:
599: assertEquals(1, ((List) value).size());
600: assertThat(result.getReferencedList(), not(instanceOf(LazyLoadingProxy.class)));
601: }
602:
603: @Test
604: void lazilyLoadedAttributeIsNullWhenThereIsNoReferenceToLoad() {
605: this.em = getEntityManager("lazilyLoadedAttributeIsNullWhenThereIsNoReferenceToLoad", false);
606: final EntityDescriptor descriptor = new EntityDescriptor(Generators.generateUri());
607: entityI.setOwlClassA(null);
608: transactional(() -> em.persist(entityI, descriptor));
609:
610: final OWLClassI result = findRequired(OWLClassI.class, entityI.getUri());
611: assertNull(result.getOwlClassA());
612: }
613: }