Skip to contentMethod: testPersistInstanceWithPlainIdentifierObjectPropertyValue()
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.exception.IdentifierNotSetException;
21: import cz.cvut.kbss.jopa.exceptions.OWLEntityExistsException;
22: import cz.cvut.kbss.jopa.exceptions.RollbackException;
23: import cz.cvut.kbss.jopa.model.EntityManager;
24: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
25: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
26: import cz.cvut.kbss.jopa.test.OWLClassA;
27: import cz.cvut.kbss.jopa.test.OWLClassB;
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.OWLClassH;
32: import cz.cvut.kbss.jopa.test.OWLClassK;
33: import cz.cvut.kbss.jopa.test.OWLClassM;
34: import cz.cvut.kbss.jopa.test.OWLClassN;
35: import cz.cvut.kbss.jopa.test.OWLClassP;
36: import cz.cvut.kbss.jopa.test.OWLClassWithQueryAttr;
37: import cz.cvut.kbss.jopa.test.OWLClassWithQueryAttr2;
38: import cz.cvut.kbss.jopa.test.OWLClassWithQueryAttr3;
39: import cz.cvut.kbss.jopa.test.OWLClassWithQueryAttr4;
40: import cz.cvut.kbss.jopa.test.OWLClassWithQueryAttr5;
41: import cz.cvut.kbss.jopa.test.OWLClassX;
42: import cz.cvut.kbss.jopa.test.Vocabulary;
43: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
44: import cz.cvut.kbss.jopa.test.environment.Generators;
45: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
46: import cz.cvut.kbss.jopa.test.environment.Quad;
47: import cz.cvut.kbss.jopa.test.environment.TestEnvironment;
48: import cz.cvut.kbss.jopa.vocabulary.XSD;
49: import org.junit.jupiter.api.Test;
50: import org.slf4j.Logger;
51:
52: import java.net.URI;
53: import java.net.URL;
54: import java.time.LocalDate;
55: import java.time.LocalDateTime;
56: import java.time.ZoneOffset;
57: import java.time.temporal.ChronoUnit;
58: import java.util.ArrayList;
59: import java.util.Arrays;
60: import java.util.Collections;
61: import java.util.HashMap;
62: import java.util.HashSet;
63: import java.util.List;
64: import java.util.Map;
65: import java.util.Set;
66: import java.util.stream.Collectors;
67: import java.util.stream.IntStream;
68:
69: import static org.junit.jupiter.api.Assertions.assertEquals;
70: import static org.junit.jupiter.api.Assertions.assertFalse;
71: import static org.junit.jupiter.api.Assertions.assertNotNull;
72: import static org.junit.jupiter.api.Assertions.assertNull;
73: import static org.junit.jupiter.api.Assertions.assertThrows;
74: import static org.junit.jupiter.api.Assertions.assertTrue;
75:
76: public abstract class CreateOperationsRunner extends BaseRunner {
77:
78: protected CreateOperationsRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
79: super(logger, persistenceFactory, dataAccessor);
80: }
81:
82: @Test
83: void testPersistWithGeneratedId() {
84: this.em = getEntityManager("PersistWithGenerated", false);
85: assertNull(entityE.getUri());
86: persist(entityE);
87:
88: assertNotNull(entityE.getUri());
89: final OWLClassE resE = findRequired(OWLClassE.class, entityE.getUri());
90: assertEquals(entityE.getStringAttribute(), resE.getStringAttribute());
91: }
92:
93: @Test
94: void persistingEntityWithoutIdAndWithoutGeneratedIdThrowsException() {
95: this.em = getEntityManager("PersistWithoutId", false);
96: final OWLClassB b = new OWLClassB();
97: b.setStringAttribute("someValue");
98: assertThrows(IdentifierNotSetException.class, () -> persist(b));
99: }
100:
101: @Test
102: void persistNullThrowsNPX() {
103: this.em = getEntityManager("PersistNull", false);
104: em.getTransaction().begin();
105: assertThrows(NullPointerException.class, () -> em.persist(null));
106: }
107:
108: @Test
109: void testPersistAndRollbackChanges() {
110: this.em = getEntityManager("PersistRollback", false);
111: em.getTransaction().begin();
112: em.persist(entityE);
113: assertTrue(em.contains(entityE));
114: em.getTransaction().rollback();
115:
116: assertFalse(em.contains(entityE));
117: assertNull(em.find(entityE.getClass(), entityE.getUri()));
118: }
119:
120: @Test
121: void persistingInRollbackOnlyThrowsExceptionOnCommit() {
122: this.em = getEntityManager("PersistRollbackOnly", false);
123: em.getTransaction().begin();
124: em.getTransaction().setRollbackOnly();
125: em.persist(entityE);
126: assertThrows(RollbackException.class, () -> em.getTransaction().commit());
127: }
128:
129: @Test
130: void testPersistWithCascade() {
131: this.em = getEntityManager("PersistWithCascade", false);
132: persist(entityG);
133:
134: final OWLClassA resA2 = findRequired(OWLClassA.class, entityA.getUri());
135: final OWLClassH resH = findRequired(OWLClassH.class, entityH.getUri());
136: assertEquals(resH.getOwlClassA(), resA2);
137: final OWLClassG resG = findRequired(OWLClassG.class, entityG.getUri());
138: assertEquals(resG.getOwlClassH(), resH);
139: assertEquals(resG.getOwlClassH().getOwlClassA(), resA2);
140: }
141:
142: @Test
143: void persistingOnlyOnePartOfRelationWithoutCascadeThrowsRollbackException() {
144: this.em = getEntityManager("PersistWithoutCascade", false);
145: assertThrows(RollbackException.class, () -> persist(entityD));
146: }
147:
148: @Test
149: void persistingDetachedEntityIsIllegal() {
150: this.em = getEntityManager("PersistDetached", false);
151: persist(entityA);
152:
153: final OWLClassA det = findRequired(OWLClassA.class, entityA.getUri());
154: em.getTransaction().begin();
155: em.detach(det);
156: assertThrows(OWLEntityExistsException.class, () -> em.persist(det));
157: }
158:
159: @Test
160: void testPersistWithProperties() throws Exception {
161: this.em = getEntityManager("PersistWithProperties", false);
162: final Map<String, Set<String>> props = new HashMap<>(3);
163: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyOne", Collections
164: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Individual10"));
165: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyTwo", Collections
166: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/SomeEntity"));
167: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyThree",
168: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityG"));
169: final Map<String, Set<String>> expected = new HashMap<>(4);
170: expected.putAll(props);
171: entityB.setProperties(props);
172: persist(entityB);
173: em.clear();
174:
175: final OWLClassB res = findRequired(OWLClassB.class, entityB.getUri());
176: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
177: assertNotNull(res.getProperties());
178: assertFalse(res.getProperties().isEmpty());
179: assertEquals(expected.size(), res.getProperties().size());
180: assertEquals(expected, res.getProperties());
181: final List<Quad> expectedStatements = new ArrayList<>();
182: props.forEach((k, vs) -> vs.forEach(v -> expectedStatements.add(new Quad(entityB.getUri(), URI.create(k), v, (String) null))));
183: verifyStatementsPresent(expectedStatements, em);
184: }
185:
186: @Test
187: void testPersistWithEmptyProperties() {
188: this.em = getEntityManager("PersistWithPropertiesEmpty", false);
189: entityB.setProperties(Collections.emptyMap());
190: em.getTransaction().begin();
191: em.persist(entityB);
192: assertTrue(em.contains(entityB));
193: em.getTransaction().commit();
194: em.clear();
195:
196: final OWLClassB b = findRequired(OWLClassB.class, entityB.getUri());
197: assertEquals(entityB.getUri(), b.getUri());
198: assertEquals(entityB.getStringAttribute(), b.getStringAttribute());
199: assertTrue(b.getProperties().isEmpty());
200: }
201:
202: @Test
203: void persistTwoInstancesOfDifferentClassesWithSameUriIntoTheSamePersistenceContextIsIllegal() {
204: this.em = getEntityManager("PersistURITwiceInDifferentClassesSamePC", false);
205: final URI pk = URI.create("http://krizik.felk.cvut.cz/jopa/onto/sameEntity");
206: final OWLClassA a = new OWLClassA();
207: a.setUri(pk);
208: final OWLClassB b = new OWLClassB();
209: b.setUri(pk);
210: em.getTransaction().begin();
211: em.persist(a);
212: assertThrows(OWLEntityExistsException.class, () -> em.persist(b));
213: }
214:
215: @Test
216: void persistTwoInstancesOfDifferentClassesWithSameUriIntoDifferentPersistenceContextsIsLegal() {
217: this.em = getEntityManager("PersistURITwiceInDifferentClassesDifferentPCs", false);
218: final URI uri = URI.create("http://krizik.felk.cvut.cz/jopa/onto/sameEntity");
219: entityA.setUri(uri);
220: entityB.setUri(uri);
221: persist(entityA);
222: try (EntityManager emTwo = em.getEntityManagerFactory().createEntityManager()) {
223: persist(entityB);
224:
225: assertNotNull(emTwo.find(OWLClassA.class, entityA.getUri()));
226: assertNotNull(em.find(OWLClassB.class, entityB.getUri()));
227: }
228: }
229:
230: @Test
231: void testPersistEntityWithBasicTypeAttributes() {
232: this.em = getEntityManager("PersistEntityWithBasicTypeAttributes", false);
233: persist(entityM);
234: em.clear();
235:
236: final OWLClassM res = findRequired(OWLClassM.class, entityM.getKey());
237: assertEquals(entityM.getKey(), res.getKey());
238: assertEquals(entityM.getBooleanAttribute(), res.getBooleanAttribute());
239: assertEquals(entityM.getIntAttribute(), res.getIntAttribute());
240: assertEquals(entityM.getLongAttribute(), res.getLongAttribute());
241: assertEquals(entityM.getFloatAttribute(), res.getFloatAttribute());
242: assertEquals(entityM.getDoubleAttribute(), res.getDoubleAttribute());
243: assertEquals(entityM.getDateAttribute(), res.getDateAttribute());
244: }
245:
246: @Test
247: void testPersistAndUpdateAttributeBeforeCommit() {
248: this.em = getEntityManager("PersistAndUpdateBeforeCommit", false);
249: final String updatedValue = "updatedStringAttributeValue";
250: em.getTransaction().begin();
251: em.persist(entityA);
252: entityA.setStringAttribute(updatedValue);
253: em.getTransaction().commit();
254: em.clear();
255:
256: final OWLClassA res = findRequired(OWLClassA.class, entityA.getUri());
257: assertEquals(updatedValue, res.getStringAttribute());
258: }
259:
260: @Test
261: void testPersistEntityWithEnumAttribute() {
262: this.em = getEntityManager("PersistEntityWithEnum", false);
263: persist(entityM);
264:
265: final OWLClassM res = findRequired(OWLClassM.class, entityM.getKey());
266: assertEquals(entityM.getEnumAttribute(), res.getEnumAttribute());
267: }
268:
269: @Test
270: void testPersistTypedProperties() throws Exception {
271: this.em = getEntityManager("PersistTypedProperties", false);
272: entityP.setProperties(Generators.createTypedProperties());
273: persist(entityP);
274: em.clear();
275:
276: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
277: assertEquals(entityP.getProperties(), res.getProperties());
278: final List<Quad> expectedStatements = new ArrayList<>();
279: entityP.getProperties()
280: .forEach((k, vs) -> vs.forEach(v -> expectedStatements.add(new Quad(entityP.getUri(), k, v, (String) null))));
281: verifyStatementsPresent(expectedStatements, em);
282: }
283:
284: @Test
285: void testPersistInstanceWithPlainIdentifierObjectPropertyValue() {
286: this.em = getEntityManager("PersistInstanceWithIdentifierObjectPropertyValue", false);
287: final URI value = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#individualAAA");
288: entityP.setIndividualUri(value);
289: persist(entityP);
290: em.clear();
291:
292: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
293: assertEquals(value, res.getIndividualUri());
294: }
295:
296: @Test
297: void testPersistInstanceWithPluralObjectPropertyAttributeRepresentedByUrls() {
298: this.em = getEntityManager("PersistInstanceWithPluralIdentifierObjectPropertyValue", false);
299: final Set<URL> urls = Generators.createUrls();
300: entityP.setIndividuals(urls);
301: persist(entityP);
302: em.clear();
303:
304: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
305: assertEquals(urls, res.getIndividuals());
306: }
307:
308: @Test
309: void annotationPropertyValueIsSavedAsStringUnlessItsTypeIsUri() throws Exception {
310: this.em = getEntityManager("PersistInstanceWithAnnotationPropertyValues", false);
311: final String apValue = Generators.generateUri().toString();
312: final URI apUriValue = Generators.generateUri();
313: entityN.setAnnotationProperty(apValue);
314: entityN.setAnnotationUri(apUriValue);
315: persist(entityN);
316: em.clear();
317: assertNotNull(entityN.getId());
318:
319: final URI id = URI.create(entityN.getId());
320: verifyStatementsPresent(Set.of(
321: new Quad(id, URI.create(Vocabulary.P_N_STR_ANNOTATION_PROPERTY), apValue, TestEnvironment.PERSISTENCE_LANGUAGE),
322: new Quad(id, URI.create(Vocabulary.P_N_URI_ANNOTATION_PROPERTY), apUriValue)
323: ), em);
324: final OWLClassN res = findRequired(OWLClassN.class, entityN.getId());
325: assertEquals(apValue, res.getAnnotationProperty());
326: assertEquals(apUriValue, res.getAnnotationUri());
327: }
328:
329: @Test
330: void persistEntityWithNonNullGeneratedIdentifierDoesNotRewriteIdentifier() {
331: this.em = getEntityManager("PersistEntityWithNonNullGeneratedIdentifiersDoesNotRewriteIdentifier", false);
332: final URI u = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityELives");
333: entityE.setUri(u);
334: persist(entityE);
335:
336: assertEquals(u, entityE.getUri());
337: final OWLClassE res = findRequired(OWLClassE.class, u);
338: assertEquals(u, res.getUri());
339: assertEquals(entityE.getStringAttribute(), res.getStringAttribute());
340: }
341:
342: @Test
343: void persistEntityAndReferenceWithNonNullGeneratedIdentifiersDoesNotRewriteThem() {
344: this.em = getEntityManager("PersistEntityAndReferenceWithNonNullGeneratedIdentifiersDoesNotRewriteThem", false);
345: final URI uK = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityKLives");
346: final URI uE = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityELives");
347: final OWLClassK entityK = new OWLClassK();
348: entityK.setUri(uK);
349: entityK.setOwlClassE(entityE);
350: entityE.setUri(uE);
351: persist(entityK, entityE);
352:
353: assertEquals(uK, entityK.getUri());
354: assertEquals(uE, entityE.getUri());
355: final OWLClassK resK = findRequired(OWLClassK.class, uK);
356: assertEquals(uE, resK.getOwlClassE().getUri());
357: final OWLClassE resE = findRequired(OWLClassE.class, uE);
358: assertEquals(uE, resE.getUri());
359: }
360:
361: @Test
362: void testPersistEntityWithUriTypes() {
363: this.em = getEntityManager("PersistEntityWithUriTypes", false);
364: entityP.setTypes(Generators.createUriTypes());
365: persist(entityP);
366: em.clear();
367:
368: final OWLClassP result = findRequired(OWLClassP.class, entityP.getUri());
369: assertEquals(entityP.getTypes().size(), result.getTypes().size());
370: assertTrue(entityP.getTypes().containsAll(result.getTypes()));
371: }
372:
373: @Test
374: void persistEntityWithDatatypePropertyCollectionPersistsAllValues() {
375: assertFalse(entityM.getIntegerSet().isEmpty());
376: this.em = getEntityManager("PersistEntityWithDatatypePropertyCollection", false);
377: persist(entityM);
378:
379: assertNotNull(entityM.getKey());
380: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
381: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
382: }
383:
384: @Test
385: void persistSetsStringLiteralLanguageTagAccordingToDescriptor() throws Exception {
386: this.em = getEntityManager("persistSetsStringLiteralLanguageTagAccordingToDescriptor", false);
387: em.getTransaction().begin();
388: final Descriptor descriptor = new EntityDescriptor();
389: descriptor.setAttributeLanguage(em.getMetamodel().entity(OWLClassA.class)
390: .getDeclaredAttribute("stringAttribute"), "cs");
391: em.persist(entityA, descriptor);
392: em.getTransaction().commit();
393:
394: verifyStatementsPresent(Collections.singleton(
395: new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), entityA.getStringAttribute(),
396: "cs")), em);
397: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
398: }
399:
400: @Test
401: void persistSetsStringLiteralLanguageTagToGloballyConfiguredValueWhenDescriptorDoesNotSpecifyIt()
402: throws Exception {
403: this.em = getEntityManager(
404: "persistSetsStringLiteralLanguageTagToGloballyConfiguredValueWhenDescriptorDoesNotSpecifyIt", false);
405: persist(entityA);
406:
407: verifyStatementsPresent(Collections.singleton(
408: new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), entityA.getStringAttribute(),
409: "en")), em);
410: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
411: }
412:
413: @Test
414: void persistAllowsOverridingGlobalLanguageWithLocalEmptyTag() throws Exception {
415: this.em = getEntityManager("persistAllowsOverridingGlobalLanguageWithLocalEmptyTag", false);
416: em.getTransaction().begin();
417: final Descriptor descriptor = new EntityDescriptor();
418: descriptor.setAttributeLanguage(em.getMetamodel().entity(OWLClassA.class)
419: .getDeclaredAttribute("stringAttribute"), null);
420: em.persist(entityA, descriptor);
421: em.getTransaction().commit();
422:
423: verifyStatementsPresent(Collections.singleton(
424: new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), entityA.getStringAttribute(),
425: (String) null)), em);
426: final OWLClassA result = findRequired(OWLClassA.class, entityA.getUri());
427: // The string attribute should be loaded even though PU language is set to en, because the persisted value has no lang tag
428: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
429: }
430:
431: @Test
432: void persistAllowsToSpecifyLanguageTagPerEntityAndOverrideItOnAttributeLevel() throws Exception {
433: this.em = getEntityManager("persistAllowsToSpecifyLanguageTagPerEntityAndOverrideItOnAttributeLevel", false);
434: entityN.setStringAttribute("retezec v cestine");
435: entityN.setAnnotationProperty("entity descriptor ist in Deutsch");
436: final Descriptor descriptor = new EntityDescriptor();
437: descriptor.setLanguage("de");
438: descriptor.setAttributeLanguage(em.getMetamodel().entity(OWLClassN.class)
439: .getDeclaredAttribute("stringAttribute"), "cs");
440:
441: em.getTransaction().begin();
442: em.persist(entityN, descriptor);
443: em.getTransaction().commit();
444:
445: final Set<Quad> statements = new HashSet<>(4);
446: statements.add(new Quad(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STR_ANNOTATION_PROPERTY),
447: entityN.getAnnotationProperty(), "de"));
448: statements.add(new Quad(URI.create(entityN.getId()), URI.create(Vocabulary.P_N_STRING_ATTRIBUTE),
449: entityN.getStringAttribute(), "cs"));
450: verifyStatementsPresent(statements, em);
451:
452: final OWLClassN result = em.find(OWLClassN.class, entityN.getId(), descriptor);
453: assertEquals(entityN.getAnnotationProperty(), result.getAnnotationProperty());
454: assertEquals(entityN.getStringAttribute(), result.getStringAttribute());
455: }
456:
457: @Test
458: void persistSavesInstanceWithReferenceToExistingInstance() {
459: this.em = getEntityManager("persistSavesInstanceWithReferenceToExistingInstance", false);
460: persist(entityA);
461:
462: em.clear();
463: persist(entityD);
464:
465: final OWLClassD resultD = findRequired(OWLClassD.class, entityD.getUri());
466: assertNotNull(resultD.getOwlClassA());
467: }
468:
469: @Test
470: void persistSupportsLocalDateTimeApi() {
471: this.em = getEntityManager("persistSupportsLocalDateTimeApi", false);
472: final OWLClassX entityX = new OWLClassX();
473: final LocalDate date = LocalDate.now();
474: entityX.setLocalDate(date);
475: // Truncate to millis to prevent problems with storage precision
476: final LocalDateTime dateTime = LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS);
477: entityX.setLocalDateTime(dateTime);
478: persist(entityX);
479:
480: final OWLClassX result = findRequired(OWLClassX.class, entityX.getUri());
481: assertEquals(date, result.getLocalDate());
482: assertEquals(dateTime, result.getLocalDateTime());
483: }
484:
485: @Test
486: void persistSupportsPluralAnnotationProperties() {
487: this.em = getEntityManager("persistSupportsPluralAnnotationProperties", false);
488: final Set<String> annotations = IntStream.range(0, 5).mapToObj(i -> "Source" + i).collect(Collectors.toSet());
489: entityN.setPluralAnnotationProperty(annotations);
490: persist(entityN);
491:
492: final OWLClassN result = findRequired(OWLClassN.class, entityN.getId());
493: assertEquals(annotations, result.getPluralAnnotationProperty());
494: }
495:
496: @Test
497: void persistSupportsSavingSimpleLiteralValue() {
498: this.em = getEntityManager("persistSupportsSavingSimpleLiteralValue", false);
499: final String value = "test";
500: entityM.setSimpleLiteral(value);
501: persist(entityM);
502:
503: verifyValueDatatype(URI.create(entityM.getKey()), Vocabulary.p_m_simpleLiteral, XSD.STRING);
504: }
505:
506: @Test
507: void persistSupportsSavingUriAsSimpleLiteralValue() {
508: this.em = getEntityManager("persistSupportsSavingUriAsSimpleLiteralValue", false);
509: final URI value = Generators.generateUri();
510: entityM.setSimpleLiteralUri(value);
511: persist(entityM);
512:
513: verifyValueDatatype(URI.create(entityM.getKey()), Vocabulary.p_m_simpleLiteralUri, XSD.STRING);
514: }
515:
516: @Test
517: void persistSupportsDataCollectionAttribute() throws Exception {
518: this.em = getEntityManager("persistSupportsDataCollectionAttribute", false);
519: assertFalse(entityM.getStringCollection().isEmpty());
520: persist(entityM);
521:
522: verifyStatementsPresent(entityM.getStringCollection().stream()
523: .map(s -> new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_StringCollection), s, "en"))
524: .collect(Collectors.toSet()), em);
525: }
526:
527: @Test
528: void testPersistEntityWithQueryAttr() {
529: this.em = getEntityManager("PersistWithQueryAttr", false);
530:
531: em.getTransaction().begin();
532: em.persist(entityWithQueryAttr);
533: assertTrue(em.contains(entityWithQueryAttr));
534: em.getTransaction().commit();
535: em.clear();
536:
537: final OWLClassWithQueryAttr resultEntity =
538: findRequired(OWLClassWithQueryAttr.class, entityWithQueryAttr.getUri());
539: assertEquals(entityWithQueryAttr.getUri(), resultEntity.getUri());
540: assertEquals(entityWithQueryAttr.getStringAttribute(), resultEntity.getStringAttribute());
541: assertEquals(entityWithQueryAttr.getStringAttribute(), resultEntity.getStringQueryAttribute());
542: }
543:
544: @Test
545: void testPersistEntityWithManagedTypeQueryAttr() {
546: this.em = getEntityManager("PersistWithManagedTypeQueryAttr", false);
547:
548: persist(entityA);
549:
550: em.clear();
551: em.getTransaction().begin();
552: em.persist(entityWithQueryAttr2);
553: assertTrue(em.contains(entityWithQueryAttr2));
554: em.getTransaction().commit();
555:
556: final OWLClassWithQueryAttr2 resultEntity =
557: findRequired(OWLClassWithQueryAttr2.class, entityWithQueryAttr2.getUri());
558: assertEquals(entityWithQueryAttr2.getUri(), resultEntity.getUri());
559: assertNotNull(resultEntity.getEntityAttribute());
560: assertNotNull(resultEntity.getEntityQueryAttribute());
561: assertEquals(entityWithQueryAttr2.getEntityAttribute(), resultEntity.getEntityAttribute());
562: assertEquals(entityWithQueryAttr2.getEntityAttribute(), resultEntity.getEntityQueryAttribute());
563: }
564:
565: @Test
566: void testPersistEntityWithPluralQueryAttr() {
567: this.em = getEntityManager("PersistWithPluralQueryAttr", false);
568:
569: Set<String> pluralAttribute = new HashSet<>();
570: pluralAttribute.add("a");
571: pluralAttribute.add("b");
572: pluralAttribute.add("c");
573:
574: entityWithQueryAttr3.setPluralAttribute(pluralAttribute);
575:
576: em.getTransaction().begin();
577: em.persist(entityWithQueryAttr3);
578: assertTrue(em.contains(entityWithQueryAttr3));
579: em.getTransaction().commit();
580: em.clear();
581:
582: final OWLClassWithQueryAttr3 resultEntity =
583: findRequired(OWLClassWithQueryAttr3.class, entityWithQueryAttr3.getUri());
584: assertEquals(entityWithQueryAttr3.getUri(), resultEntity.getUri());
585: assertEquals(entityWithQueryAttr3.getPluralAttribute(), resultEntity.getPluralAttribute());
586: assertEquals(entityWithQueryAttr3.getPluralAttribute(), resultEntity.getPluralQueryAttribute());
587: }
588:
589: @Test
590: void testPersistEntityWithPluralManagedTypeQueryAttr() {
591: this.em = getEntityManager("PersistEntityWithPluralManagedTypeQueryAttr", false);
592:
593: entityWithQueryAttr5.setPluralAttribute(Collections.singleton(entityA));
594:
595: em.getTransaction().begin();
596: em.persist(entityWithQueryAttr5);
597: assertTrue(em.contains(entityWithQueryAttr5));
598: assertTrue(em.contains(entityA));
599: em.getTransaction().commit();
600: em.clear();
601:
602: final OWLClassWithQueryAttr5 resultEntity =
603: findRequired(OWLClassWithQueryAttr5.class, entityWithQueryAttr5.getUri());
604: assertEquals(entityWithQueryAttr5.getUri(), resultEntity.getUri());
605: assertEquals(entityWithQueryAttr5.getPluralAttribute(), resultEntity.getPluralAttribute());
606: assertEquals(entityWithQueryAttr5.getPluralAttribute(), resultEntity.getPluralQueryAttribute());
607: }
608:
609: @Test
610: void testPersistEntityWithPluralMultipleManagedTypesQueryAttr() {
611: this.em = getEntityManager("PersistEntityWithPluralMultipleManagedTypesQueryAttr", false);
612:
613: Set<OWLClassA> simpleSet = Generators.createSimpleSet(20);
614: entityWithQueryAttr5.setPluralAttribute(simpleSet);
615:
616: em.getTransaction().begin();
617: em.persist(entityWithQueryAttr5);
618: assertTrue(em.contains(entityWithQueryAttr5));
619: for (OWLClassA e : simpleSet) {
620: assertTrue(em.contains(e));
621: }
622: em.getTransaction().commit();
623: em.clear();
624:
625: final OWLClassWithQueryAttr5 resultEntity =
626: findRequired(OWLClassWithQueryAttr5.class, entityWithQueryAttr5.getUri());
627: assertEquals(entityWithQueryAttr5.getUri(), resultEntity.getUri());
628: assertEquals(entityWithQueryAttr5.getPluralAttribute(), resultEntity.getPluralAttribute());
629: assertEquals(entityWithQueryAttr5.getPluralAttribute(), resultEntity.getPluralQueryAttribute());
630: }
631:
632: @Test
633: void testPersistEntityWithASKQueryAttr() {
634: this.em = getEntityManager("PersistWithASKQueryAttr", false);
635:
636: em.getTransaction().begin();
637: em.persist(entityWithQueryAttr4);
638: assertTrue(em.contains(entityWithQueryAttr4));
639: em.getTransaction().commit();
640: em.clear();
641:
642: final OWLClassWithQueryAttr4 resultEntity =
643: findRequired(OWLClassWithQueryAttr4.class, entityWithQueryAttr4.getUri());
644: assertEquals(entityWithQueryAttr4.getUri(), resultEntity.getUri());
645: assertEquals(entityWithQueryAttr4.getStringAttribute(), resultEntity.getStringAttribute());
646: assertEquals(true, resultEntity.getAskQueryAttribute());
647: }
648:
649: @Test
650: void persistSupportsMappingEnumsToSimpleLiterals() throws Exception {
651: this.em = getEntityManager("persistSupportsMappingEnumsToSimpleLiterals", false);
652:
653: persist(entityM);
654:
655: verifyStatementsPresent(Collections.singleton(
656: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_enumSimpleLiteralAttribute),
657: entityM.getEnumSimpleLiteral().name(), (String) null)), em);
658: }
659:
660: @Test
661: void persistSupportsUsingExplicitCustomConverter() throws Exception {
662: this.em = getEntityManager("persistSupportsUsingExplicitCustomConverter", false);
663: final ZoneOffset value = ZoneOffset.ofHours(2);
664: entityM.setWithConverter(value);
665:
666: persist(entityM);
667:
668: verifyStatementsPresent(Collections.singleton(
669: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_withConverter),
670: entityM.getWithConverter().getId(), (String) null)), em);
671: }
672:
673: @Test
674: void persistSupportsAnnotationPropertyValueMappedToSimpleLiteral() throws Exception {
675: this.em = getEntityManager("persistSupportsAnnotationPropertyValueMappedToSimpleLiteral", false);
676: entityM.setSimpleLiteral("test:value");
677: entityM.setAnnotationSimpleLiteral("test:value");
678: persist(entityM);
679:
680: verifyStatementsPresent(Arrays.asList(
681: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_annotationSimpleLiteral),
682: entityM.getAnnotationSimpleLiteral(), (String) null),
683: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_simpleLiteral),
684: entityM.getSimpleLiteral(), (String) null)), em);
685: }
686: }