Skip to content

Method: changesDoneAfterInstanceRefreshAreCommittedToRepository()

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.exceptions.InferredAttributeModifiedException;
16: import cz.cvut.kbss.jopa.exceptions.IntegrityConstraintViolatedException;
17: import cz.cvut.kbss.jopa.exceptions.RollbackException;
18: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
19: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
20: import cz.cvut.kbss.jopa.oom.exceptions.UnpersistedChangeException;
21: import cz.cvut.kbss.jopa.test.*;
22: import cz.cvut.kbss.jopa.test.environment.*;
23: import cz.cvut.kbss.jopa.vocabulary.XSD;
24: import org.junit.jupiter.api.Test;
25: import org.slf4j.Logger;
26:
27: import java.lang.reflect.Field;
28: import java.net.MalformedURLException;
29: import java.net.URI;
30: import java.net.URL;
31: import java.time.Instant;
32: import java.util.*;
33: import java.util.stream.Collectors;
34: import java.util.stream.IntStream;
35:
36: import static org.hamcrest.CoreMatchers.containsString;
37: import static org.hamcrest.CoreMatchers.instanceOf;
38: import static org.hamcrest.MatcherAssert.assertThat;
39: import static org.junit.jupiter.api.Assertions.*;
40:
41: public abstract class UpdateOperationsRunner extends BaseRunner {
42:
43: private OWLClassA entityA2;
44: private OWLClassF entityF;
45: private OWLClassJ entityJ;
46: private OWLClassO entityO;
47:
48: public UpdateOperationsRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
49: super(logger, persistenceFactory, dataAccessor);
50: initialize();
51: }
52:
53: private void initialize() {
54: this.entityF = new OWLClassF();
55: entityF.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityF"));
56: this.entityA2 = new OWLClassA();
57: entityA2.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA2"));
58: this.entityJ = new OWLClassJ();
59: entityJ.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityJ"));
60: final Set<OWLClassA> set = new HashSet<>(2);
61: set.add(entityA);
62: set.add(entityA2);
63: entityJ.setOwlClassA(set);
64: this.entityO = new OWLClassO(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityO"));
65: final Set<OWLClassE> es = new HashSet<>();
66: for (int i = 0; i < 5; i++) {
67: final OWLClassE e = new OWLClassE();
68: e.setStringAttribute("StringAttributeNo" + i);
69: es.add(e);
70: }
71: entityO.setOwlClassESet(es);
72: }
73:
74: @Test
75: void testUpdateDataLeaveLazy() throws Exception {
76: this.em = getEntityManager("UpdateDataProperty", false);
77: entityB.setProperties(Generators.createProperties());
78: persist(entityB);
79:
80: em.getTransaction().begin();
81: final OWLClassB b = findRequired(OWLClassB.class, entityB.getUri());
82: final Field propsField = OWLClassB.class.getDeclaredField("properties");
83: propsField.setAccessible(true);
84: assertNull(propsField.get(b));
85: final String newString = "NewString";
86: b.setStringAttribute(newString);
87: em.getTransaction().commit();
88:
89: final OWLClassB res = findRequired(OWLClassB.class, entityB.getUri());
90: assertEquals(newString, res.getStringAttribute());
91: assertNotNull(res.getProperties());
92: assertEquals(entityB.getProperties(), res.getProperties());
93: }
94:
95: @Test
96: void testUpdateDataPropertySetNull() {
97: this.em = getEntityManager("UpdateDataPropertyToNull", true);
98: persist(entityA);
99:
100: em.getTransaction().begin();
101: final OWLClassA a = findRequired(OWLClassA.class, entityA.getUri());
102: assertNotNull(a.getStringAttribute());
103: a.setStringAttribute(null);
104: em.getTransaction().commit();
105:
106: final OWLClassA res = findRequired(OWLClassA.class, entityA.getUri());
107: assertNull(res.getStringAttribute());
108: assertEquals(entityA.getTypes(), res.getTypes());
109: }
110:
111: @Test
112: void testUpdateReference() {
113: this.em = getEntityManager("UpdateReference", true);
114: persist(entityD, entityI);
115:
116: final OWLClassA newA = new OWLClassA();
117: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
118: newA.setStringAttribute("newA");
119: em.getTransaction().begin();
120: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
121: assertNotNull(d.getOwlClassA());
122: d.setOwlClassA(newA);
123: final OWLClassI i = findRequired(OWLClassI.class, entityI.getUri());
124: assertNotNull(i.getOwlClassA());
125: i.setOwlClassA(newA);
126: em.persist(newA);
127: em.getTransaction().commit();
128:
129: final OWLClassA resA1 = findRequired(OWLClassA.class, newA.getUri());
130: final OWLClassD resD = findRequired(OWLClassD.class, d.getUri());
131: assertEquals(resA1.getUri(), resD.getOwlClassA().getUri());
132: verifyExists(OWLClassA.class, entityA.getUri());
133: final OWLClassI resI = findRequired(OWLClassI.class, i.getUri());
134: assertEquals(newA.getUri(), resI.getOwlClassA().getUri());
135: verifyExists(OWLClassA.class, entityA.getUri());
136: assertEquals(resA1.getUri(), resI.getOwlClassA().getUri());
137: }
138:
139: @Test
140: void mergedInstanceWithChangesInCascadedPluralReferenceAttributeContainsUpdatedValues() {
141: this.em = getEntityManager("MergeSet", false);
142: persist(entityJ);
143: em.clear();
144:
145: entityJ.getOwlClassA().forEach(a -> a.setStringAttribute("NEWVALUE"));
146:
147: em.getTransaction().begin();
148: OWLClassJ merged = em.merge(entityJ);
149:
150: for (final OWLClassA a : merged.getOwlClassA()) {
151: assertEquals(a.getStringAttribute(), "NEWVALUE");
152: }
153: em.getTransaction().commit();
154: }
155:
156: @Test
157: void testMergeDetachedWithChanges() {
158: this.em = getEntityManager("UpdateDetached", true);
159: persist(entityA);
160:
161: final OWLClassA a = findRequired(OWLClassA.class, entityA.getUri());
162: assertTrue(em.contains(a));
163: em.detach(a);
164: assertFalse(em.contains(a));
165: final String newType = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#AddedType";
166: a.getTypes().add(newType);
167: em.getTransaction().begin();
168: final OWLClassA merged = em.merge(a);
169: assertTrue(merged.getTypes().contains(newType));
170: em.getTransaction().commit();
171:
172: final OWLClassA res = findRequired(OWLClassA.class, a.getUri());
173: assertEquals(a.getTypes().size(), res.getTypes().size());
174: assertTrue(res.getTypes().containsAll(a.getTypes()));
175: }
176:
177: @Test
178: void testMergeDetachedCascade() {
179: this.em = getEntityManager("UpdateCascade", true);
180: em.getTransaction().begin();
181: em.persist(entityH);
182: assertTrue(em.contains(entityA));
183: em.getTransaction().commit();
184:
185: em.getTransaction().begin();
186: final OWLClassH h = findRequired(OWLClassH.class, entityH.getUri());
187: assertNotNull(h.getOwlClassA());
188: em.detach(h);
189: assertFalse(em.contains(h));
190: assertFalse(em.contains(h.getOwlClassA()));
191: final String newStr = "newStringAttribute";
192: h.getOwlClassA().setStringAttribute(newStr);
193: final OWLClassH merged = em.merge(h);
194: assertEquals(newStr, merged.getOwlClassA().getStringAttribute());
195: em.getTransaction().commit();
196:
197: final OWLClassA res = findRequired(OWLClassA.class, entityA.getUri());
198: assertEquals(newStr, res.getStringAttribute());
199: }
200:
201: @Test
202: void testMergeDetachedWithObjectPropertyChange() {
203: this.em = getEntityManager("UpdateDetachedWithOPChange", true);
204: persist(entityD, entityA);
205:
206: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
207: em.detach(d);
208: d.setOwlClassA(entityA2);
209: em.getTransaction().begin();
210: em.merge(d);
211: em.persist(entityA2);
212: em.getTransaction().commit();
213:
214: final OWLClassD res = findRequired(OWLClassD.class, entityD.getUri());
215: assertEquals(entityA2.getUri(), res.getOwlClassA().getUri());
216: assertEquals(entityA2.getStringAttribute(), res.getOwlClassA().getStringAttribute());
217: final OWLClassA resA = findRequired(OWLClassA.class, entityA.getUri());
218: assertEquals(entityA.getStringAttribute(), resA.getStringAttribute());
219: assertEquals(entityA.getTypes(), resA.getTypes());
220: }
221:
222: @Test
223: void testRemoveFromSimpleList() {
224: this.em = getEntityManager("UpdateRemoveFromSimpleList", true);
225: entityC.setSimpleList(Generators.createSimpleList());
226: persistCWithLists(entityC);
227:
228: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri());
229: em.getTransaction().begin();
230: final OWLClassA a = c.getSimpleList().get(1);
231: c.getSimpleList().remove(a);
232: em.getTransaction().commit();
233:
234: final OWLClassA resA = findRequired(OWLClassA.class, a.getUri());
235: final OWLClassC resC = findRequired(OWLClassC.class, c.getUri());
236: assertEquals(c.getSimpleList().size(), resC.getSimpleList().size());
237: assertEquals(entityC.getSimpleList().size() - 1, resC.getSimpleList().size());
238: for (OWLClassA aa : resC.getSimpleList()) {
239: assertNotEquals(resA.getUri(), aa.getUri());
240: }
241: }
242:
243: @Test
244: void testAddToSimpleList() {
245: this.em = getEntityManager("UpdateAddToSimpleList", true);
246: entityC.setSimpleList(Generators.createSimpleList());
247: em.getTransaction().begin();
248: em.persist(entityC);
249: entityC.getSimpleList().forEach(em::persist);
250: em.persist(entityA);
251: em.getTransaction().commit();
252:
253: em.getTransaction().begin();
254: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri());
255: final OWLClassA a = findRequired(OWLClassA.class, entityA.getUri());
256: assertFalse(c.getSimpleList().contains(a));
257: c.getSimpleList().add(a);
258: em.getTransaction().commit();
259:
260: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri());
261: assertEquals(c.getSimpleList().size(), resC.getSimpleList().size());
262: assertEquals(entityC.getSimpleList().size() + 1, resC.getSimpleList().size());
263: final OWLClassA resA = findRequired(OWLClassA.class, entityA.getUri());
264: assertTrue(resC.getSimpleList().contains(resA));
265: }
266:
267: @Test
268: void testClearSimpleList() {
269: this.em = getEntityManager("UpdateClearSimpleList", true);
270: entityC.setSimpleList(Generators.createSimpleList());
271: persistCWithLists(entityC);
272:
273: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri());
274: assertFalse(c.getSimpleList().isEmpty());
275: em.getTransaction().begin();
276: c.getSimpleList().clear();
277: em.getTransaction().commit();
278:
279: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri());
280: assertTrue(resC.getSimpleList() == null || resC.getSimpleList().isEmpty());
281: for (OWLClassA a : entityC.getSimpleList()) {
282: verifyExists(OWLClassA.class, a.getUri());
283: }
284: }
285:
286: @Test
287: void testReplaceSimpleList() {
288: this.em = getEntityManager("UpdateReplaceSimpleList", true);
289: entityC.setSimpleList(Generators.createSimpleList());
290: persistCWithLists(entityC);
291:
292: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri());
293: final List<OWLClassA> newList = new ArrayList<>(1);
294: newList.add(entityA);
295: em.getTransaction().begin();
296: em.persist(entityA);
297: c.setSimpleList(newList);
298: em.getTransaction().commit();
299:
300: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri());
301: assertEquals(newList.size(), resC.getSimpleList().size());
302: boolean found;
303: for (OWLClassA a : newList) {
304: found = false;
305: for (OWLClassA aa : resC.getSimpleList()) {
306: if (a.getUri().equals(aa.getUri())) {
307: found = true;
308: break;
309: }
310: }
311: assertTrue(found);
312: }
313: for (OWLClassA a : entityC.getSimpleList()) {
314: assertNotNull(em.find(OWLClassA.class, a.getUri()));
315: }
316: }
317:
318: @Test
319: void testRemoveFromReferencedList() {
320: this.em = getEntityManager("UpdateRemoveFromReferencedList", true);
321: entityC.setReferencedList(Generators.createReferencedList());
322: persistCWithLists(entityC);
323:
324: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri());
325: em.getTransaction().begin();
326: final OWLClassA a = c.getReferencedList().get(Generators.randomInt(c.getReferencedList().size()));
327: c.getReferencedList().remove(a);
328: em.getTransaction().commit();
329:
330: final OWLClassA resA = findRequired(OWLClassA.class, a.getUri());
331: final OWLClassC resC = findRequired(OWLClassC.class, c.getUri());
332: assertEquals(c.getReferencedList().size(), resC.getReferencedList().size());
333: assertEquals(entityC.getReferencedList().size() - 1, resC.getReferencedList().size());
334: for (OWLClassA aa : resC.getReferencedList()) {
335: assertNotEquals(resA.getUri(), aa.getUri());
336: }
337: }
338:
339: @Test
340: void testAddToReferencedList() {
341: this.em = getEntityManager("UpdateAddToReferencedList", true);
342: entityC.setReferencedList(Generators.createReferencedList());
343: persistCWithLists(entityC);
344:
345: em.getTransaction().begin();
346: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri());
347: em.persist(entityA);
348: c.getReferencedList().add(entityA);
349: em.getTransaction().commit();
350:
351: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri());
352: assertEquals(c.getReferencedList().size(), resC.getReferencedList().size());
353: assertEquals(entityC.getReferencedList().size() + 1, resC.getReferencedList().size());
354: final OWLClassA resA = findRequired(OWLClassA.class, entityA.getUri());
355: assertTrue(resC.getReferencedList().contains(resA));
356: }
357:
358: @Test
359: void testClearReferencedList() {
360: this.em = getEntityManager("UpdateClearReferencedList", true);
361: entityC.setReferencedList(Generators.createReferencedList());
362: persistCWithLists(entityC);
363:
364: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri());
365: assertFalse(c.getReferencedList().isEmpty());
366: em.getTransaction().begin();
367: c.setReferencedList(null);
368: em.getTransaction().commit();
369:
370: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri());
371: assertNull(resC.getReferencedList());
372: for (OWLClassA a : entityC.getReferencedList()) {
373: verifyExists(OWLClassA.class, a.getUri());
374: }
375: }
376:
377: @Test
378: void testReplaceReferencedList() {
379: this.em = getEntityManager("UpdateReplaceReferencedList", true);
380: entityC.setReferencedList(Generators.createReferencedList());
381: persistCWithLists(entityC);
382:
383: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri());
384: final List<OWLClassA> newList = new ArrayList<>(1);
385: newList.add(entityA);
386: em.getTransaction().begin();
387: em.persist(entityA);
388: c.setReferencedList(newList);
389: em.getTransaction().commit();
390:
391: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri());
392: assertEquals(newList.size(), resC.getReferencedList().size());
393: boolean found;
394: for (OWLClassA a : newList) {
395: found = false;
396: for (OWLClassA aa : resC.getReferencedList()) {
397: if (a.getUri().equals(aa.getUri())) {
398: found = true;
399: break;
400: }
401: }
402: assertTrue(found);
403: }
404: for (OWLClassA a : entityC.getReferencedList()) {
405: verifyExists(OWLClassA.class, a.getUri());
406: }
407: }
408:
409: @Test
410: void testAddNewToProperties() {
411: this.em = getEntityManager("UpdateAddNewToProperties", false);
412: entityB.setProperties(Generators.createProperties());
413: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
414: expected.putAll(entityB.getProperties());
415: persist(entityB);
416: em.clear();
417:
418: final OWLClassB b = findRequired(OWLClassB.class, entityB.getUri());
419: assertEquals(expected.size(), b.getProperties().size());
420: em.getTransaction().begin();
421: b.getProperties().put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyFour",
422: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup"));
423: expected.putAll(b.getProperties());
424: em.getTransaction().commit();
425:
426: final OWLClassB res = findRequired(OWLClassB.class, b.getUri());
427: assertEquals(expected.size(), res.getProperties().size());
428: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
429: assertTrue(res.getProperties().containsKey(e.getKey()));
430: final Set<String> s = e.getValue();
431: final Set<String> resS = res.getProperties().get(e.getKey());
432: assertEquals(s.size(), resS.size());
433: assertTrue(s.containsAll(resS));
434: }
435: }
436:
437: @Test
438: void testAddPropertyValue() {
439: this.em = getEntityManager("UpdateAddPropertyValue", false);
440: entityB.setProperties(Generators.createProperties());
441: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
442: final String prop = entityB.getProperties().keySet().iterator().next();
443: expected.putAll(entityB.getProperties());
444: persist(entityB);
445: em.clear();
446:
447: final OWLClassB b = findRequired(OWLClassB.class, entityB.getUri());
448: assertEquals(expected.size(), b.getProperties().size());
449: em.getTransaction().begin();
450: b.getProperties().get(prop).add("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup");
451: expected.putAll(b.getProperties());
452: em.getTransaction().commit();
453:
454: final OWLClassB res = findRequired(OWLClassB.class, b.getUri());
455: assertEquals(expected.size(), res.getProperties().size());
456: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
457: assertTrue(res.getProperties().containsKey(e.getKey()));
458: final Set<String> s = e.getValue();
459: final Set<String> resS = res.getProperties().get(e.getKey());
460: assertEquals(s.size(), resS.size());
461: if (e.getKey().equals(prop)) {
462: assertTrue(s.size() > 1);
463: for (String val : s) {
464: assertTrue(resS.contains(val));
465: }
466: } else {
467: assertTrue(resS.contains(s.iterator().next()));
468: }
469: }
470: }
471:
472: @Test
473: void testAddPropertyValueDetached() {
474: this.em = getEntityManager("UpdateAddPropertyValueDetached", false);
475: entityB.setProperties(Generators.createProperties());
476: final String prop = entityB.getProperties().keySet().iterator().next();
477: final String newPropertyValue = "http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue";
478: persist(entityB);
479: em.clear();
480:
481: final OWLClassB b = findRequired(OWLClassB.class, entityB.getUri());
482: b.getProperties().get(prop).add(newPropertyValue);
483: em.detach(b);
484: em.getTransaction().begin();
485: em.merge(b);
486: em.getTransaction().commit();
487:
488: final OWLClassB res = findRequired(OWLClassB.class, entityB.getUri());
489: assertTrue(res.getProperties().get(prop).contains(newPropertyValue));
490: }
491:
492: @Test
493: void testAddNewTypedPropertyWithValues() {
494: this.em = getEntityManager("UpdateAddNewToPropertiesTyped", false);
495: entityP.setProperties(Generators.createTypedProperties());
496: persist(entityP);
497: em.clear();
498:
499: final OWLClassP p = findRequired(OWLClassP.class, entityP.getUri());
500: em.getTransaction().begin();
501: final URI property = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newProperty");
502: p.getProperties().put(property, new HashSet<>(Arrays.asList(1, "Two", new Date())));
503: em.getTransaction().commit();
504:
505: em.clear();
506:
507: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
508: assertTrue(res.getProperties().containsKey(property));
509: assertEquals(p.getProperties(), res.getProperties());
510: }
511:
512: @Test
513: void testAddTypedPropertyValue() {
514: this.em = getEntityManager("UpdateAddPropertyValueTyped", false);
515: entityP.setProperties(Generators.createTypedProperties());
516: persist(entityP);
517: em.clear();
518:
519: final OWLClassP p = findRequired(OWLClassP.class, entityP.getUri());
520: em.getTransaction().begin();
521: final URI property = p.getProperties().keySet().iterator().next();
522: final Object value = generateValueForProperty(p, property);
523: p.getProperties().get(property).add(value);
524: em.getTransaction().commit();
525:
526: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
527: assertTrue(res.getProperties().get(property).contains(value));
528: assertEquals(p.getProperties(), res.getProperties());
529: }
530:
531: private static Object generateValueForProperty(OWLClassP instance, URI property) {
532: final Set<Object> values = instance.getProperties().get(property);
533: assert values != null && !values.isEmpty();
534: if (values.iterator().next() instanceof URI) {
535: return URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue");
536: } else {
537: return "StringValue";
538: }
539: }
540:
541: @Test
542: void testModifyInferredAttribute() {
543: this.em = getEntityManager("ModifyInferredAttribute", false);
544: persist(entityF);
545:
546: em.getTransaction().begin();
547: final OWLClassF f = findRequired(OWLClassF.class, entityF.getUri());
548: assertThrows(InferredAttributeModifiedException.class, () -> f.setSecondStringAttribute("otherValue"));
549: }
550:
551: @Test
552: void testModifyAttributesOfBasicTypes() {
553: this.em = getEntityManager("ModifyBasicTypeAttributes", false);
554: persist(entityM);
555:
556: em.getTransaction().begin();
557: final OWLClassM m = findRequired(OWLClassM.class, entityM.getKey());
558: m.setBooleanAttribute(!entityM.getBooleanAttribute());
559: m.setFloatAttribute(m.getFloatAttribute() - 50.0F);
560: m.setDoubleAttribute(m.getDoubleAttribute() - 100.0);
561: m.setLongAttribute(m.getLongAttribute() + 100L);
562: m.setDateAttribute(new Date(System.currentTimeMillis() + 10000));
563: em.getTransaction().commit();
564:
565: final OWLClassM res = findRequired(OWLClassM.class, entityM.getKey());
566: assertEquals(m.getBooleanAttribute(), res.getBooleanAttribute());
567: assertEquals(m.getLongAttribute(), res.getLongAttribute());
568: assertEquals(m.getFloatAttribute(), res.getFloatAttribute());
569: assertEquals(m.getDoubleAttribute(), res.getDoubleAttribute());
570: assertEquals(m.getDateAttribute(), res.getDateAttribute());
571: }
572:
573: @Test
574: void testModifyEnumAttribute() {
575: this.em = getEntityManager("ModifyEnumAttribute", false);
576: assertNotNull(entityM.getEnumAttribute());
577: persist(entityM);
578:
579: final OWLClassM.Severity updated = OWLClassM.Severity.LOW;
580: em.getTransaction().begin();
581: final OWLClassM m = findRequired(OWLClassM.class, entityM.getKey());
582: m.setEnumAttribute(updated);
583: em.getTransaction().commit();
584:
585: final OWLClassM res = findRequired(OWLClassM.class, entityM.getKey());
586: assertEquals(updated, res.getEnumAttribute());
587: }
588:
589: @Test
590: void testCascadeOfNewlyPersistedOnMerge() {
591: this.em = getEntityManager("CascadeMergeWithNewlyPersisted", true);
592: em.getTransaction().begin();
593: em.persist(entityO);
594: entityO.getOwlClassESet().forEach(em::persist);
595: em.getTransaction().commit();
596:
597: final OWLClassE newE = new OWLClassE();
598: newE.setStringAttribute("NewlyAddedE");
599: final OWLClassO toUpdate = em.find(OWLClassO.class, entityO.getUri());
600: em.detach(toUpdate);
601:
602: em.getTransaction().begin();
603: toUpdate.getOwlClassESet().add(newE);
604: em.persist(newE);
605: final OWLClassO merged = em.merge(toUpdate);
606: assertTrue(merged.getOwlClassESet().contains(newE));
607: em.getTransaction().commit();
608:
609: verifyExists(OWLClassE.class, newE.getUri());
610: final OWLClassO resO = findRequired(OWLClassO.class, entityO.getUri());
611: assertEquals(entityO.getOwlClassESet().size() + 1, resO.getOwlClassESet().size());
612: }
613:
614: @Test
615: void modificationsOfCollectionAfterCascadeMergeAreWrittenOnCommit() {
616: this.em = getEntityManager("ModifyCollectionAfterCascadeMerge", true);
617: em.getTransaction().begin();
618: em.persist(entityJ);
619: em.getTransaction().commit();
620:
621: final OWLClassA newA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
622: em.getTransaction().begin();
623: final OWLClassJ merged = em.merge(entityJ);
624: merged.getOwlClassA().add(newA);
625: em.persist(newA);
626: em.getTransaction().commit();
627:
628: final OWLClassJ result = findRequired(OWLClassJ.class, entityJ.getUri());
629: assertEquals(merged.getOwlClassA().size(), result.getOwlClassA().size());
630: boolean found = false;
631: for (OWLClassA a : result.getOwlClassA()) {
632: if (a.getUri().equals(newA.getUri())) {
633: found = true;
634: break;
635: }
636: }
637: assertTrue(found);
638: }
639:
640: @Test
641: void testAddPropertiesWhenTheyWereNullOriginally() {
642: this.em = getEntityManager("AddPropertiesToNullOriginals", true);
643: em.getTransaction().begin();
644: assertNull(entityB.getProperties());
645: em.persist(entityB);
646: em.getTransaction().commit();
647:
648: final OWLClassB update = findRequired(OWLClassB.class, entityB.getUri());
649: em.detach(update);
650: final Map<String, Set<String>> properties = Generators.createProperties(2);
651: update.setProperties(properties);
652: em.getTransaction().begin();
653: em.merge(update);
654: em.getTransaction().commit();
655:
656: final OWLClassB result = findRequired(OWLClassB.class, entityB.getUri());
657: assertNotNull(result.getProperties());
658: assertEquals(properties, result.getProperties());
659: }
660:
661: @Test
662: void testUpdatePlainLiteralObjectPropertyValueToAnotherIndividual() {
663: this.em = getEntityManager("UpdatePlainLiteralObjectPropertyValue", true);
664: em.getTransaction().begin();
665: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#oldIndividual"));
666: em.persist(entityP);
667: em.getTransaction().commit();
668:
669: final OWLClassP update = findRequired(OWLClassP.class, entityP.getUri());
670: final URI newValue = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newIndividual");
671: em.getTransaction().begin();
672: update.setIndividualUri(newValue);
673: em.getTransaction().commit();
674:
675: final OWLClassP result = findRequired(OWLClassP.class, entityP.getUri());
676: assertEquals(newValue, result.getIndividualUri());
677: }
678:
679: @Test
680: void testUpdatePlainLiteralObjectPropertyValueToNull() {
681: this.em = getEntityManager("UpdatePlainLiteralObjectPropertyValueToNull", true);
682: em.getTransaction().begin();
683: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#SomeIndividual"));
684: em.persist(entityP);
685: em.getTransaction().commit();
686:
687: final OWLClassP update = findRequired(OWLClassP.class, entityP.getUri());
688: assertNotNull(update.getIndividualUri());
689: em.getTransaction().begin();
690: update.setIndividualUri(null);
691: em.getTransaction().commit();
692:
693: final OWLClassP result = findRequired(OWLClassP.class, entityP.getUri());
694: assertNull(result.getIndividualUri());
695: }
696:
697: @Test
698: void testUpdatePluralPlainLiteralObjectPropertyAttribute() throws MalformedURLException {
699: this.em = getEntityManager("UpdatePluralPlainLiteralObjectPropertyValue", true);
700: entityP.setIndividuals(Generators.createUrls());
701: persist(entityP);
702:
703: final OWLClassP update = findRequired(OWLClassP.class, entityP.getUri());
704: em.getTransaction().begin();
705: final URL added = new URL("http://krizik.felk.cvut.cz/ontologies/jopa#added");
706: final URL removed = entityP.getIndividuals().iterator().next();
707: update.getIndividuals().add(added);
708: update.getIndividuals().remove(removed);
709: em.getTransaction().commit();
710:
711: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
712: assertEquals(update.getIndividuals(), res.getIndividuals());
713: }
714:
715: @Test
716: void testUpdateSimpleListOfIdentifiersByAddingNewItems() {
717: this.em = getEntityManager("UpdateSimpleListOfIdentifiersByAddingItems", true);
718: entityP.setSimpleList(Generators.createListOfIdentifiers());
719: persist(entityP);
720:
721: final OWLClassP update = findRequired(OWLClassP.class, entityP.getUri());
722: em.getTransaction().begin();
723: for (int i = 0; i < Generators.randomPositiveInt(5, 10); i++) {
724: final URI u = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#Added-" + i);
725: // Insert at random position
726: update.getSimpleList().add(Generators.randomInt(update.getSimpleList().size()), u);
727: }
728: em.getTransaction().commit();
729:
730: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
731: assertEquals(update.getSimpleList(), res.getSimpleList());
732: }
733:
734: @Test
735: void testUpdateReferencedListByRemovingAndAddingItems() {
736: this.em = getEntityManager("UpdateReferencedListByRemovingAndAddingItems", true);
737: entityP.setReferencedList(Generators.createListOfIdentifiers());
738: persist(entityP);
739:
740: final OWLClassP update = findRequired(OWLClassP.class, entityP.getUri());
741: em.getTransaction().begin();
742: for (int i = 0; i < Generators.randomPositiveInt(5, 10); i++) {
743: final URI u = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#Added-" + i);
744: // We might even overwrite items set in previous iterations, but it does not matter. JOPA should handle it
745: update.getReferencedList().set(Generators.randomInt(update.getReferencedList().size()), u);
746: }
747: em.getTransaction().commit();
748:
749: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
750: assertEquals(update.getReferencedList(), res.getReferencedList());
751: }
752:
753: @Test
754: void testUpdateStringAnnotationPropertyValue() {
755: this.em = getEntityManager("UpdateStringAnnotationPropertyValue", true);
756: entityN.setAnnotationProperty("PropertyValue");
757: persist(entityN);
758:
759: final String newValue = "newValue";
760: final OWLClassN update = findRequired(OWLClassN.class, entityN.getId());
761: assertNotNull(update);
762: em.getTransaction().begin();
763: update.setAnnotationProperty(newValue);
764: em.getTransaction().commit();
765:
766: final OWLClassN res = findRequired(OWLClassN.class, entityN.getId());
767: assertEquals(newValue, res.getAnnotationProperty());
768: }
769:
770: @Test
771: void testUpdateAnnotationPropertyWithUriValueToDifferentValue() {
772: this.em = getEntityManager("UpdateAnnotationPropertyWithUriValueToDifferentValue", true);
773: entityN.setAnnotationUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#value"));
774: persist(entityN);
775:
776: final OWLClassN update = findRequired(OWLClassN.class, entityN.getId());
777: assertNotNull(update);
778: em.getTransaction().begin();
779: final URI newUri = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newValue");
780: update.setAnnotationUri(newUri);
781: em.getTransaction().commit();
782:
783: final OWLClassN res = findRequired(OWLClassN.class, entityN.getId());
784: assertEquals(newUri, res.getAnnotationUri());
785: }
786:
787: @Test
788: void testUpdateUriTypes() {
789: this.em = getEntityManager("UpdateUriTypes", true);
790: entityP.setTypes(Generators.createUriTypes());
791: persist(entityP);
792:
793: em.getTransaction().begin();
794: final OWLClassP update = findRequired(OWLClassP.class, entityP.getUri());
795: final Iterator<URI> it = update.getTypes().iterator();
796: while (it.hasNext()) {
797: it.next();
798: if (Generators.randomBoolean()) {
799: it.remove();
800: }
801: }
802: update.getTypes().addAll(Generators.createUriTypes());
803: em.getTransaction().commit();
804:
805: final OWLClassP result = findRequired(OWLClassP.class, entityP.getUri());
806: assertEquals(update.getTypes().size(), result.getTypes().size());
807: assertTrue(update.getTypes().containsAll(result.getTypes()));
808: }
809:
810: @Test
811: void updateWithInvalidInstanceThrowsIntegrityConstraintsViolation() {
812: this.em = getEntityManager("UpdateWithInvalidInstanceThrowsICViolation", true);
813: persist(entityN);
814:
815: entityN.setStringAttribute(null);
816: em.getTransaction().begin();
817: final RollbackException ex = assertThrows(RollbackException.class, () -> {
818: em.merge(entityN);
819: em.getTransaction().commit();
820: });
821: assertThat(ex.getCause(), instanceOf(IntegrityConstraintViolatedException.class));
822: }
823:
824: @Test
825: void addingValuesToDatatypePropertyCollectionAddsThemIntoRepository() {
826: this.em = getEntityManager("addingValuesToDatatypePropertyCollectionAddsThemIntoRepository", false);
827: persist(entityM);
828:
829: IntStream.generate(Generators::randomInt).limit(7).forEach(entityM.getIntegerSet()::add);
830: em.getTransaction().begin();
831: em.merge(entityM);
832: em.getTransaction().commit();
833:
834: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
835: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
836: }
837:
838: @Test
839: void removingValuesFromDatatypePropertyCollectionRemovesThemFromRepository() {
840: this.em = getEntityManager("removingValuesFromDatatypePropertyCollectionRemovesThemFromRepository", false);
841: persist(entityM);
842:
843: final Iterator<Integer> it = entityM.getIntegerSet().iterator();
844: while (it.hasNext()) {
845: it.next();
846: if (Generators.randomBoolean()) {
847: it.remove();
848: }
849: }
850: // Make sure there is at least one element
851: entityM.getIntegerSet().add(Generators.randomInt());
852:
853: em.getTransaction().begin();
854: em.merge(entityM);
855: em.getTransaction().commit();
856:
857: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
858: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
859: }
860:
861: @Test
862: void mergeReplacesReferenceWithInstanceFromRepository() {
863: this.em = getEntityManager("mergeReplacesReferenceWithInstanceFromRepository", true);
864: persist(entityD, entityA);
865:
866: final String originalString = entityA.getStringAttribute();
867: entityA.setStringAttribute("updatedStringAttribute");
868: em.getTransaction().begin();
869: final OWLClassD merged = em.merge(entityD);
870: assertEquals(originalString, merged.getOwlClassA().getStringAttribute());
871: em.getTransaction().commit();
872:
873: final OWLClassA result = findRequired(OWLClassA.class, entityA.getUri());
874: assertEquals(originalString, result.getStringAttribute());
875: }
876:
877: @Test
878: void mergeMergesUpdatedDataPropertyFields() {
879: this.em = getEntityManager("mergeMergesUpdatedDataPropertyFields", true);
880: persist(entityA);
881: final Set<String> newTypes =
882: Generators.createUriTypes().stream().map(URI::toString).collect(Collectors.toSet());
883: entityA.setTypes(newTypes);
884: final String newStringAtt = "newStringAttribute";
885: entityA.setStringAttribute(newStringAtt);
886:
887: em.getTransaction().begin();
888: final OWLClassA merged = em.merge(entityA);
889: assertEquals(newStringAtt, merged.getStringAttribute());
890: assertTrue(TestEnvironmentUtils.typesEqual(newTypes, merged.getTypes()));
891: em.getTransaction().commit();
892:
893: final OWLClassA result = findRequired(OWLClassA.class, entityA.getUri());
894: assertEquals(newStringAtt, result.getStringAttribute());
895: assertTrue(TestEnvironmentUtils.typesEqual(newTypes, result.getTypes()));
896: }
897:
898: @Test
899: void mergeMergesUpdateToObjectPropertyField() {
900: this.em = getEntityManager("mergeMergesUpdateToObjectPropertyField", true);
901: persist(entityD, entityA, entityA2);
902:
903: em.getTransaction().begin();
904: entityD.setOwlClassA(entityA2);
905: final OWLClassD merged = em.merge(entityD);
906: assertEquals(entityA2.getUri(), merged.getOwlClassA().getUri());
907: em.getTransaction().commit();
908:
909: final OWLClassD dResult = findRequired(OWLClassD.class, entityD.getUri());
910: assertEquals(entityA2.getUri(), dResult.getOwlClassA().getUri());
911: final OWLClassA aReference = findRequired(OWLClassA.class, entityA2.getUri());
912: assertSame(dResult.getOwlClassA(), aReference);
913: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
914: }
915:
916: @Test
917: void updateSupportsSavingStringLiteralWithDifferentLanguageTag() throws Exception {
918: this.em = getEntityManager("updateSupportsSavingStringLiteralWithDifferentLanguageTag", false);
919: persist(entityA);
920:
921: entityA.setStringAttribute("hodnota v cestine");
922: final Descriptor descriptor = new EntityDescriptor();
923: descriptor.setAttributeLanguage(OWLClassA.class.getDeclaredField("stringAttribute"), "cs");
924: em.getTransaction().begin();
925: em.merge(entityA, descriptor);
926: em.getTransaction().commit();
927:
928: final OWLClassA resultOne = findRequired(OWLClassA.class, entityA.getUri());
929: assertNull(resultOne.getStringAttribute());
930: em.clear();
931: final OWLClassA resultTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
932: assertEquals(entityA.getStringAttribute(), resultTwo.getStringAttribute());
933: }
934:
935: @Test
936: void mergeDetachedRemovesObsoleteDescriptorInSecondLevelCache() {
937: this.em = getEntityManager("mergeDetachedReplacesObsoleteDescriptorInSecondLevelCache", true);
938: final Descriptor descriptorOne = new EntityDescriptor();
939: descriptorOne.setLanguage("en");
940: em.getTransaction().begin();
941: em.persist(entityA, descriptorOne);
942: em.getTransaction().commit();
943: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorOne));
944:
945: final Descriptor descriptorTwo = new EntityDescriptor();
946: descriptorTwo.setLanguage("cs");
947: assertNotEquals(descriptorOne, descriptorTwo);
948: entityA.setStringAttribute("cesky");
949: em.getTransaction().begin();
950: em.merge(entityA, descriptorTwo);
951: em.getTransaction().commit();
952:
953: assertFalse(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorOne));
954: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptorTwo);
955: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
956: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorTwo));
957: }
958:
959: @Test
960: void mergeUpdatesCacheEventWhenDescriptorContainsOnlyAttributeLanguageSetting() throws Exception {
961: this.em = getEntityManager("mergeUpdatesCacheEventWhenDescriptorContainsOnlyAttributeLanguageSetting", true);
962: persist(entityA);
963:
964: final Descriptor descriptorTwo = new EntityDescriptor();
965: descriptorTwo.setAttributeLanguage(OWLClassA.class.getDeclaredField("stringAttribute"), "cs");
966: entityA.setStringAttribute("cesky");
967: em.getTransaction().begin();
968: em.merge(entityA, descriptorTwo);
969: em.getTransaction().commit();
970:
971: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptorTwo);
972: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
973: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorTwo));
974: }
975:
976: @Test
977: void updateRemovesPendingAssertionWhenItIsReplacedByAnotherValue() {
978: this.em = getEntityManager("updateRemovesPendingAssertionWhenItIsReplacedByAnotherValue", true);
979: entityD.setOwlClassA(null);
980: persist(entityD, entityA2);
981:
982: em.getTransaction().begin();
983: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
984: d.setOwlClassA(entityA);
985: d.setOwlClassA(findRequired(OWLClassA.class, entityA2.getUri()));
986: em.getTransaction().commit();
987:
988: final OWLClassD dResult = findRequired(OWLClassD.class, entityD.getUri());
989: assertNotNull(dResult.getOwlClassA());
990: assertEquals(entityA2.getUri(), dResult.getOwlClassA().getUri());
991: assertNull(em.find(OWLClassA.class, entityA.getUri()));
992: }
993:
994: @Test
995: void updatesKeepsPendingAssertionPluralAttribute() {
996: this.em = getEntityManager("updatesKeepsPendingAssertionPluralAttribute", true);
997: final OWLClassL entityL = new OWLClassL(Generators.generateUri());
998: entityL.setSet(new HashSet<>());
999: entityL.getSet().add(entityA2);
1000: entityL.setSimpleList(Collections.singletonList(entityA2));
1001: entityL.setSingleA(entityA2);
1002: entityL.setUri(Generators.generateUri());
1003: persist(entityL, entityA2);
1004:
1005: em.getTransaction().begin();
1006: final OWLClassL inst = findRequired(OWLClassL.class, entityL.getUri());
1007: inst.getSet().add(entityA);
1008: final RollbackException ex = assertThrows(RollbackException.class, () -> em.getTransaction().commit());
1009: assertThat(ex.getCause(), instanceOf(UnpersistedChangeException.class));
1010: assertThat(ex.getMessage(), containsString(entityA.toString()));
1011: }
1012:
1013: @Test
1014: void mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributePreviouslyMerged() {
1015: this.em = getEntityManager("mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributePreviouslyMerged",
1016: true);
1017: persist(entityD, entityA);
1018:
1019: final String newString = "updatedString";
1020: final OWLClassD dUpdate = new OWLClassD(entityD.getUri());
1021: final OWLClassA aUpdate = new OWLClassA(entityA.getUri());
1022: aUpdate.setStringAttribute(newString);
1023: aUpdate.setTypes(new HashSet<>(entityA.getTypes()));
1024: dUpdate.setOwlClassA(aUpdate);
1025: em.clear();
1026: em.getTransaction().begin();
1027: final OWLClassD orig = findRequired(OWLClassD.class, entityD.getUri());
1028: em.detach(orig);
1029: em.find(OWLClassA.class, entityA.getUri());
1030: em.merge(aUpdate);
1031: em.merge(dUpdate);
1032: em.getTransaction().commit();
1033: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
1034: assertEquals(newString, result.getOwlClassA().getStringAttribute());
1035: }
1036:
1037: @Test
1038: void mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributeMergedLater() {
1039: this.em = getEntityManager("mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributeMergedLater",
1040: true);
1041: persist(entityD, entityA);
1042:
1043: final String newString = "updatedString";
1044: final OWLClassD dUpdate = new OWLClassD(entityD.getUri());
1045: final OWLClassA aUpdate = new OWLClassA(entityA.getUri());
1046: aUpdate.setStringAttribute(newString);
1047: aUpdate.setTypes(new HashSet<>(entityA.getTypes()));
1048: dUpdate.setOwlClassA(aUpdate);
1049: em.clear();
1050: em.getTransaction().begin();
1051: final OWLClassD orig = findRequired(OWLClassD.class, entityD.getUri());
1052: em.detach(orig);
1053: em.merge(dUpdate);
1054: em.merge(aUpdate);
1055: em.getTransaction().commit();
1056: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
1057: assertEquals(newString, result.getOwlClassA().getStringAttribute());
1058: }
1059:
1060: @Test
1061: void mergeCorrectlyMergesChangesOnPluralObjectPropertyIntoCache() {
1062: this.em = getEntityManager("mergeCorrectlyMergesChangesOnPluralObjectPropertyIntoCache", true);
1063: final OWLClassL entityL = new OWLClassL(Generators.generateUri());
1064: entityL.setSet(new HashSet<>());
1065: entityL.getSet().add(entityA);
1066: entityL.getSet().add(entityA2);
1067: entityL.setSimpleList(Collections.singletonList(entityA2));
1068: entityL.setSingleA(entityA2);
1069: persist(entityL, entityA, entityA2);
1070:
1071: final OWLClassL toUpdate = new OWLClassL(entityL.getUri());
1072: toUpdate.setSet(new HashSet<>(entityL.getSet()));
1073: toUpdate.getSet().remove(entityA2);
1074: toUpdate.setSimpleList(Collections.singletonList(entityA2));
1075: toUpdate.setSingleA(entityA2);
1076: em.getTransaction().begin();
1077: final OWLClassL original = findRequired(OWLClassL.class, entityL.getUri());
1078: assertEquals(2, original.getSet().size());
1079: for (OWLClassA a : original.getSet()) {
1080: if (a.getUri().equals(entityA2.getUri())) {
1081: original.getSet().remove(a);
1082: break;
1083: }
1084: }
1085: toUpdate.getSet().forEach(a -> em.merge(a));
1086: em.merge(toUpdate);
1087: em.getTransaction().commit();
1088:
1089: final OWLClassL result = findRequired(OWLClassL.class, entityL.getUri());
1090: assertEquals(1, result.getSet().size());
1091: }
1092:
1093: @Test
1094: void updateKeepsPendingListReferenceWhenItemIsAddedToIt() {
1095: this.em = getEntityManager("updateKeepsPendingListReferenceWhenItemIsAddedToIt", true);
1096: final OWLClassK entityK = new OWLClassK();
1097: entityK.setSimpleList(generateEInstances(5));
1098: entityK.setReferencedList(generateEInstances(6));
1099: em.getTransaction().begin();
1100: em.persist(entityK);
1101: entityK.getSimpleList().forEach(e -> {
1102: assertNull(e.getUri());
1103: em.persist(e);
1104: });
1105: entityK.getReferencedList().forEach(e -> {
1106: assertNull(e.getUri());
1107: em.persist(e);
1108: });
1109: em.getTransaction().commit();
1110:
1111: final OWLClassE addedSimple = new OWLClassE();
1112: addedSimple.setStringAttribute("addedSimple");
1113: final OWLClassE addedReferenced = new OWLClassE();
1114: addedReferenced.setStringAttribute("addedReferenced");
1115: em.getTransaction().begin();
1116: final OWLClassK update = em.find(OWLClassK.class, entityK.getUri());
1117: update.getSimpleList().add(addedSimple);
1118: update.getReferencedList().add(addedReferenced);
1119: assertNull(addedSimple.getUri());
1120: assertNull(addedReferenced.getUri());
1121: em.persist(addedSimple);
1122: em.persist(addedReferenced);
1123: em.getTransaction().commit();
1124:
1125: final OWLClassK result = findRequired(OWLClassK.class, entityK.getUri());
1126: assertEquals(addedSimple.getUri(), result.getSimpleList().get(result.getSimpleList().size() - 1).getUri());
1127: assertEquals(addedReferenced.getUri(),
1128: result.getReferencedList().get(result.getReferencedList().size() - 1).getUri());
1129: }
1130:
1131: private static List<OWLClassE> generateEInstances(int count) {
1132: return IntStream.range(0, count).mapToObj(i -> {
1133: final OWLClassE e = new OWLClassE();
1134: e.setStringAttribute("instance" + i);
1135: return e;
1136: }).collect(Collectors.toList());
1137: }
1138:
1139: @Test
1140: void mergeRemovedThrowsIllegalArgumentException() {
1141: this.em = getEntityManager("mergeRemovedThrowsIllegalArgument", true);
1142: persist(entityA);
1143:
1144: em.getTransaction().begin();
1145: final OWLClassA toRemove = findRequired(OWLClassA.class, entityA.getUri());
1146: em.remove(toRemove);
1147: final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> em.merge(toRemove));
1148: assertThat(ex.getMessage(), containsString("removed"));
1149: }
1150:
1151: @Test
1152: void changesDoneAfterInstanceRefreshAreCommittedToRepository() {
1153: this.em = getEntityManager("changesDoneAfterInstanceRefreshAreCommittedToRepository", true);
1154: persist(entityD, entityA, entityA2);
1155:
1156: em.getTransaction().begin();
1157: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
1158: final OWLClassA origA = d.getOwlClassA();
1159: final OWLClassA newA = new OWLClassA(Generators.generateUri());
1160: newA.setStringAttribute("newAWithStringAttribute");
1161: d.setOwlClassA(em.find(OWLClassA.class, entityA2.getUri()));
1162: em.refresh(d);
1163: assertEquals(origA, d.getOwlClassA());
1164: d.setOwlClassA(newA);
1165: em.persist(newA);
1166: em.getTransaction().commit();
1167:
1168: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
1169: assertEquals(newA.getUri(), result.getOwlClassA().getUri());
1170: assertEquals(newA.getStringAttribute(), result.getOwlClassA().getStringAttribute());
1171: }
1172:
1173: @Test
1174: void refreshOfPluralObjectPropertyReplacesCollectionWithOriginalAndAllowsFurtherChanges() {
1175: this.em = getEntityManager("refreshOfPluralObjectPropertyReplacesCollectionWithOriginalAndAllowsFurtherChanges",
1176: true);
1177: persist(entityJ);
1178:
1179: em.getTransaction().begin();
1180: final OWLClassJ j = findRequired(OWLClassJ.class, entityJ.getUri());
1181: j.getOwlClassA().clear();
1182: em.refresh(j);
1183: final OWLClassA newA = new OWLClassA(Generators.generateUri());
1184: newA.setStringAttribute("newAWithStringAttribute");
1185: j.getOwlClassA().add(newA);
1186: em.persist(newA);
1187: em.getTransaction().commit();
1188:
1189: final OWLClassJ result = findRequired(OWLClassJ.class, entityJ.getUri());
1190: assertEquals(entityJ.getOwlClassA().size() + 1, result.getOwlClassA().size());
1191: final Set<URI> aUris = new HashSet<>(Arrays.asList(entityA.getUri(), entityA2.getUri(), newA.getUri()));
1192: result.getOwlClassA().forEach(a -> assertTrue(aUris.contains(a.getUri())));
1193: }
1194:
1195: /**
1196: * Bug #33
1197: */
1198: @Test
1199: void mergeWithUpdatedPropertyValueRemovesOriginalAssertion() {
1200: this.em = getEntityManager("mergeWithUpdatedPropertyValueRemovesOriginalAssertion", false);
1201: persist(entityH, entityA);
1202:
1203: em.getTransaction().begin();
1204: entityH.setOwlClassA(entityA2);
1205: em.merge(entityH);
1206: em.getTransaction().commit();
1207:
1208: final OWLClassH result = findRequired(OWLClassH.class, entityH.getUri());
1209: assertEquals(entityA2.getUri(), result.getOwlClassA().getUri());
1210: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
1211: }
1212:
1213: /**
1214: * Bug #33, but for plural attributes
1215: */
1216: @Test
1217: void mergeWithUpdatedPropertyValueRemovesOriginalAssertionsFromPluralAttribute() {
1218: this.em = getEntityManager("mergeWithUpdatedPropertyValueRemovesOriginalAssertionsFromPluralAttribute", false);
1219: persist(entityJ);
1220:
1221: assertEquals(2, entityJ.getOwlClassA().size());
1222: final OWLClassA newA = new OWLClassA(Generators.generateUri());
1223: entityJ.setOwlClassA(Collections.singleton(newA));
1224: em.getTransaction().begin();
1225: em.merge(entityJ);
1226: em.getTransaction().commit();
1227:
1228: final OWLClassJ result = findRequired(OWLClassJ.class, entityJ.getUri());
1229: assertEquals(1, result.getOwlClassA().size());
1230: assertEquals(newA.getUri(), result.getOwlClassA().iterator().next().getUri());
1231: }
1232:
1233: @Test
1234: void updateSupportsJava8Instant() {
1235: this.em = getEntityManager("updateSupportsJava8Instant", true);
1236: final OWLClassX entityX = new OWLClassX();
1237: entityX.setInstant(Instant.now());
1238: persist(entityX);
1239:
1240: final Instant newInstant = Instant.ofEpochMilli(System.currentTimeMillis() + 10000);
1241: em.getTransaction().begin();
1242: final OWLClassX toUpdate = findRequired(OWLClassX.class, entityX.getUri());
1243: toUpdate.setInstant(newInstant);
1244: em.getTransaction().commit();
1245:
1246: final OWLClassX cachedResult = findRequired(OWLClassX.class, entityX.getUri());
1247: assertEquals(newInstant, cachedResult.getInstant());
1248: em.getEntityManagerFactory().getCache().evict(OWLClassX.class);
1249: final OWLClassX result = findRequired(OWLClassX.class, entityX.getUri());
1250: assertEquals(newInstant, result.getInstant());
1251: }
1252:
1253: @Test
1254: void updateSupportsPluralAnnotationProperty() {
1255: this.em = getEntityManager("updateSupportsPluralAnnotationProperty", true);
1256: final Set<String> annotations = IntStream.range(0, 5).mapToObj(i -> "Source" + i).collect(Collectors.toSet());
1257: entityN.setPluralAnnotationProperty(annotations);
1258: persist(entityN);
1259:
1260: final String added = "Added Source";
1261: final String removed = annotations.iterator().next();
1262: annotations.remove(removed);
1263: annotations.add(added);
1264: entityN.getPluralAnnotationProperty().remove(removed);
1265: entityN.getPluralAnnotationProperty().add(added);
1266: em.getTransaction().begin();
1267: final OWLClassN merged = em.merge(entityN);
1268: assertEquals(annotations, merged.getPluralAnnotationProperty());
1269: em.getTransaction().commit();
1270:
1271: final OWLClassN result = findRequired(OWLClassN.class, entityN.getId());
1272: assertEquals(annotations, result.getPluralAnnotationProperty());
1273: }
1274:
1275: @Test
1276: void updateSupportsSettingSubclassOnPolymorphicAttribute() {
1277: this.em = getEntityManager("updateSupportsSettingSubclassOnPolymorphicAttribute", true);
1278: final OWLClassU u = new OWLClassU();
1279: final OWLClassU reference = new OWLClassU();
1280: em.getTransaction().begin();
1281: em.persist(reference);
1282: em.getTransaction().commit();
1283:
1284: em.getTransaction().begin();
1285: final OWLClassU managedRef = em.merge(reference);
1286: u.setOwlClassS(managedRef);
1287: em.persist(u);
1288: em.getTransaction().commit();
1289:
1290: final OWLClassU result = findRequired(OWLClassU.class, u.getUri());
1291: assertEquals(reference.getUri(), result.getOwlClassS().getUri());
1292: }
1293:
1294: @Test
1295: void updateSupportsUpdatingSimpleLiteralValue() {
1296: this.em = getEntityManager("updateSupportsUpdatingSimpleLiteralValue", true);
1297: entityM.setSimpleLiteral("test original");
1298: persist(entityM);
1299:
1300: updateSimpleLiteralAndVerify();
1301: }
1302:
1303: private void updateSimpleLiteralAndVerify() {
1304: em.getTransaction().begin();
1305: final String newValue = "new test value";
1306: final OWLClassM toUpdate = findRequired(OWLClassM.class, entityM.getKey());
1307: toUpdate.setSimpleLiteral(newValue);
1308: em.getTransaction().commit();
1309:
1310: verifyValueDatatype(URI.create(entityM.getKey()), Vocabulary.p_m_simpleLiteral, XSD.STRING);
1311: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
1312: assertEquals(newValue, result.getSimpleLiteral());
1313: }
1314:
1315: @Test
1316: void updateOverwritesLanguageStringWithSimpleLiteralForSimpleLiteralAttribute() throws Exception {
1317: this.em = getEntityManager("updateOverwritesLanguageStringWithSimpleLiteralForSimpleLiteralAttribute", true);
1318: persist(entityM);
1319: final Collection<Quad> originalQuad = Collections.singleton(
1320: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_simpleLiteral), "test", "en"));
1321: persistTestData(originalQuad, em);
1322:
1323: updateSimpleLiteralAndVerify();
1324: verifyStatementsNotPresent(originalQuad, em);
1325: }
1326:
1327: @Test
1328: void updateSupportCollectionAttribute() {
1329: this.em = getEntityManager("updateSupportCollectionAttribute", true);
1330: final OWLClassX x = new OWLClassX();
1331: x.setACollection(new HashSet<>(Arrays.asList(entityA, entityA2)));
1332:
1333: final OWLClassA newA = new OWLClassA();
1334: newA.setUri(Generators.generateUri());
1335: newA.setStringAttribute("stringAttributeeee");
1336: em.getTransaction().begin();
1337: em.persist(newA);
1338: x.setACollection(Collections.singletonList(newA));
1339: final OWLClassX merged = em.merge(x);
1340: assertFalse(merged.getACollection().isEmpty());
1341: em.getTransaction().commit();
1342:
1343: final OWLClassX result = findRequired(OWLClassX.class, x.getUri());
1344: assertEquals(1, result.getACollection().size());
1345: assertEquals(newA.getUri(), result.getACollection().iterator().next().getUri());
1346: }
1347: }