Skip to content

Method: persistNullThrowsNPX()

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