Skip to contentMethod: testModifyAttributesOfBasicTypes()
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() {
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(fieldSpecification(OWLClassA.class, "stringAttribute"), "cs");
924: transactional(() -> em.merge(entityA, descriptor));
925:
926: final OWLClassA resultOne = findRequired(OWLClassA.class, entityA.getUri());
927: assertNull(resultOne.getStringAttribute());
928: em.clear();
929: final OWLClassA resultTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
930: assertEquals(entityA.getStringAttribute(), resultTwo.getStringAttribute());
931: }
932:
933: @Test
934: void mergeDetachedRemovesObsoleteDescriptorInSecondLevelCache() {
935: this.em = getEntityManager("mergeDetachedReplacesObsoleteDescriptorInSecondLevelCache", true);
936: final Descriptor descriptorOne = new EntityDescriptor();
937: descriptorOne.setLanguage("en");
938: transactional(() -> em.persist(entityA, descriptorOne));
939: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorOne));
940:
941: final Descriptor descriptorTwo = new EntityDescriptor();
942: descriptorTwo.setLanguage("cs");
943: assertNotEquals(descriptorOne, descriptorTwo);
944: entityA.setStringAttribute("cesky");
945: transactional(() -> em.merge(entityA, descriptorTwo));
946:
947: assertFalse(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorOne));
948: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptorTwo);
949: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
950: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorTwo));
951: }
952:
953: @Test
954: void mergeUpdatesCacheEventWhenDescriptorContainsOnlyAttributeLanguageSetting() {
955: this.em = getEntityManager("mergeUpdatesCacheEventWhenDescriptorContainsOnlyAttributeLanguageSetting", true);
956: persist(entityA);
957:
958: final Descriptor descriptorTwo = new EntityDescriptor();
959: descriptorTwo.setAttributeLanguage(fieldSpecification(OWLClassA.class, "stringAttribute"), "cs");
960: entityA.setStringAttribute("cesky");
961: transactional(() -> em.merge(entityA, descriptorTwo));
962:
963: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptorTwo);
964: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
965: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorTwo));
966: }
967:
968: @Test
969: void updateRemovesPendingAssertionWhenItIsReplacedByAnotherValue() {
970: this.em = getEntityManager("updateRemovesPendingAssertionWhenItIsReplacedByAnotherValue", true);
971: entityD.setOwlClassA(null);
972: persist(entityD, entityA2);
973:
974: transactional(() -> {
975: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
976: d.setOwlClassA(entityA);
977: d.setOwlClassA(findRequired(OWLClassA.class, entityA2.getUri()));
978: });
979:
980: final OWLClassD dResult = findRequired(OWLClassD.class, entityD.getUri());
981: assertNotNull(dResult.getOwlClassA());
982: assertEquals(entityA2.getUri(), dResult.getOwlClassA().getUri());
983: assertNull(em.find(OWLClassA.class, entityA.getUri()));
984: }
985:
986: @Test
987: void updatesKeepsPendingAssertionPluralAttribute() {
988: this.em = getEntityManager("updatesKeepsPendingAssertionPluralAttribute", true);
989: final OWLClassL entityL = new OWLClassL(Generators.generateUri());
990: entityL.setSet(new HashSet<>());
991: entityL.getSet().add(entityA2);
992: entityL.setSimpleList(Collections.singletonList(entityA2));
993: entityL.setSingleA(entityA2);
994: entityL.setUri(Generators.generateUri());
995: persist(entityL, entityA2);
996:
997: em.getTransaction().begin();
998: final OWLClassL inst = findRequired(OWLClassL.class, entityL.getUri());
999: inst.getSet().add(entityA);
1000: final RollbackException ex = assertThrows(RollbackException.class, () -> em.getTransaction().commit());
1001: assertThat(ex.getCause(), instanceOf(UnpersistedChangeException.class));
1002: assertThat(ex.getMessage(), containsString(entityA.toString()));
1003: }
1004:
1005: @Test
1006: void mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributePreviouslyMerged() {
1007: this.em = getEntityManager("mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributePreviouslyMerged",
1008: true);
1009: persist(entityD, entityA);
1010:
1011: final String newString = "updatedString";
1012: final OWLClassD dUpdate = new OWLClassD(entityD.getUri());
1013: final OWLClassA aUpdate = new OWLClassA(entityA.getUri());
1014: aUpdate.setStringAttribute(newString);
1015: aUpdate.setTypes(new HashSet<>(entityA.getTypes()));
1016: dUpdate.setOwlClassA(aUpdate);
1017: em.clear();
1018: transactional(() -> {
1019: final OWLClassD orig = findRequired(OWLClassD.class, entityD.getUri());
1020: em.detach(orig);
1021: em.find(OWLClassA.class, entityA.getUri());
1022: em.merge(aUpdate);
1023: em.merge(dUpdate);
1024: });
1025: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
1026: assertEquals(newString, result.getOwlClassA().getStringAttribute());
1027: }
1028:
1029: @Test
1030: void mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributeMergedLater() {
1031: this.em = getEntityManager("mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributeMergedLater",
1032: true);
1033: persist(entityD, entityA);
1034:
1035: final String newString = "updatedString";
1036: final OWLClassD dUpdate = new OWLClassD(entityD.getUri());
1037: final OWLClassA aUpdate = new OWLClassA(entityA.getUri());
1038: aUpdate.setStringAttribute(newString);
1039: aUpdate.setTypes(new HashSet<>(entityA.getTypes()));
1040: dUpdate.setOwlClassA(aUpdate);
1041: em.clear();
1042: transactional(() -> {
1043: final OWLClassD orig = findRequired(OWLClassD.class, entityD.getUri());
1044: em.detach(orig);
1045: em.merge(dUpdate);
1046: em.merge(aUpdate);
1047: });
1048: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
1049: assertEquals(newString, result.getOwlClassA().getStringAttribute());
1050: }
1051:
1052: @Test
1053: void mergeCorrectlyMergesChangesOnPluralObjectPropertyIntoCache() {
1054: this.em = getEntityManager("mergeCorrectlyMergesChangesOnPluralObjectPropertyIntoCache", true);
1055: final OWLClassL entityL = new OWLClassL(Generators.generateUri());
1056: entityL.setSet(new HashSet<>());
1057: entityL.getSet().add(entityA);
1058: entityL.getSet().add(entityA2);
1059: entityL.setSimpleList(Collections.singletonList(entityA2));
1060: entityL.setSingleA(entityA2);
1061: persist(entityL, entityA, entityA2);
1062:
1063: final OWLClassL toUpdate = new OWLClassL(entityL.getUri());
1064: toUpdate.setSet(new HashSet<>(entityL.getSet()));
1065: toUpdate.getSet().remove(entityA2);
1066: toUpdate.setSimpleList(Collections.singletonList(entityA2));
1067: toUpdate.setSingleA(entityA2);
1068: transactional(() -> {
1069: final OWLClassL original = findRequired(OWLClassL.class, entityL.getUri());
1070: assertEquals(2, original.getSet().size());
1071: for (OWLClassA a : original.getSet()) {
1072: if (a.getUri().equals(entityA2.getUri())) {
1073: original.getSet().remove(a);
1074: break;
1075: }
1076: }
1077: toUpdate.getSet().forEach(a -> em.merge(a));
1078: em.merge(toUpdate);
1079: });
1080:
1081: final OWLClassL result = findRequired(OWLClassL.class, entityL.getUri());
1082: assertEquals(1, result.getSet().size());
1083: }
1084:
1085: @Test
1086: void updateKeepsPendingListReferenceWhenItemIsAddedToIt() {
1087: this.em = getEntityManager("updateKeepsPendingListReferenceWhenItemIsAddedToIt", true);
1088: final OWLClassK entityK = new OWLClassK();
1089: entityK.setSimpleList(generateEInstances(5));
1090: entityK.setReferencedList(generateEInstances(6));
1091: em.getTransaction().begin();
1092: em.persist(entityK);
1093: entityK.getSimpleList().forEach(e -> {
1094: assertNull(e.getUri());
1095: em.persist(e);
1096: });
1097: entityK.getReferencedList().forEach(e -> {
1098: assertNull(e.getUri());
1099: em.persist(e);
1100: });
1101: em.getTransaction().commit();
1102:
1103: final OWLClassE addedSimple = new OWLClassE();
1104: addedSimple.setStringAttribute("addedSimple");
1105: final OWLClassE addedReferenced = new OWLClassE();
1106: addedReferenced.setStringAttribute("addedReferenced");
1107: em.getTransaction().begin();
1108: final OWLClassK update = em.find(OWLClassK.class, entityK.getUri());
1109: update.getSimpleList().add(addedSimple);
1110: update.getReferencedList().add(addedReferenced);
1111: assertNull(addedSimple.getUri());
1112: assertNull(addedReferenced.getUri());
1113: em.persist(addedSimple);
1114: em.persist(addedReferenced);
1115: em.getTransaction().commit();
1116:
1117: final OWLClassK result = findRequired(OWLClassK.class, entityK.getUri());
1118: assertEquals(addedSimple.getUri(), result.getSimpleList().get(result.getSimpleList().size() - 1).getUri());
1119: assertEquals(addedReferenced.getUri(),
1120: result.getReferencedList().get(result.getReferencedList().size() - 1).getUri());
1121: }
1122:
1123: private static List<OWLClassE> generateEInstances(int count) {
1124: return IntStream.range(0, count).mapToObj(i -> {
1125: final OWLClassE e = new OWLClassE();
1126: e.setStringAttribute("instance" + i);
1127: return e;
1128: }).collect(Collectors.toList());
1129: }
1130:
1131: @Test
1132: void mergeRemovedThrowsIllegalArgumentException() {
1133: this.em = getEntityManager("mergeRemovedThrowsIllegalArgument", true);
1134: persist(entityA);
1135:
1136: em.getTransaction().begin();
1137: final OWLClassA toRemove = findRequired(OWLClassA.class, entityA.getUri());
1138: em.remove(toRemove);
1139: final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> em.merge(toRemove));
1140: assertThat(ex.getMessage(), containsString("removed"));
1141: }
1142:
1143: @Test
1144: void changesDoneAfterInstanceRefreshAreCommittedToRepository() {
1145: this.em = getEntityManager("changesDoneAfterInstanceRefreshAreCommittedToRepository", true);
1146: persist(entityD, entityA, entityA2);
1147:
1148: em.getTransaction().begin();
1149: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
1150: final OWLClassA origA = d.getOwlClassA();
1151: final OWLClassA newA = new OWLClassA(Generators.generateUri());
1152: newA.setStringAttribute("newAWithStringAttribute");
1153: d.setOwlClassA(em.find(OWLClassA.class, entityA2.getUri()));
1154: em.refresh(d);
1155: assertEquals(origA, d.getOwlClassA());
1156: d.setOwlClassA(newA);
1157: em.persist(newA);
1158: em.getTransaction().commit();
1159:
1160: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
1161: assertEquals(newA.getUri(), result.getOwlClassA().getUri());
1162: assertEquals(newA.getStringAttribute(), result.getOwlClassA().getStringAttribute());
1163: }
1164:
1165: @Test
1166: void refreshOfPluralObjectPropertyReplacesCollectionWithOriginalAndAllowsFurtherChanges() {
1167: this.em = getEntityManager("refreshOfPluralObjectPropertyReplacesCollectionWithOriginalAndAllowsFurtherChanges",
1168: true);
1169: persist(entityJ);
1170:
1171: em.getTransaction().begin();
1172: final OWLClassJ j = findRequired(OWLClassJ.class, entityJ.getUri());
1173: j.getOwlClassA().clear();
1174: em.refresh(j);
1175: final OWLClassA newA = new OWLClassA(Generators.generateUri());
1176: newA.setStringAttribute("newAWithStringAttribute");
1177: j.getOwlClassA().add(newA);
1178: em.persist(newA);
1179: em.getTransaction().commit();
1180:
1181: final OWLClassJ result = findRequired(OWLClassJ.class, entityJ.getUri());
1182: assertEquals(entityJ.getOwlClassA().size() + 1, result.getOwlClassA().size());
1183: final Set<URI> aUris = new HashSet<>(Arrays.asList(entityA.getUri(), entityA2.getUri(), newA.getUri()));
1184: result.getOwlClassA().forEach(a -> assertTrue(aUris.contains(a.getUri())));
1185: }
1186:
1187: /**
1188: * Bug #33
1189: */
1190: @Test
1191: void mergeWithUpdatedPropertyValueRemovesOriginalAssertion() {
1192: this.em = getEntityManager("mergeWithUpdatedPropertyValueRemovesOriginalAssertion", false);
1193: persist(entityH, entityA);
1194:
1195: em.getTransaction().begin();
1196: entityH.setOwlClassA(entityA2);
1197: em.merge(entityH);
1198: em.getTransaction().commit();
1199:
1200: final OWLClassH result = findRequired(OWLClassH.class, entityH.getUri());
1201: assertEquals(entityA2.getUri(), result.getOwlClassA().getUri());
1202: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
1203: }
1204:
1205: /**
1206: * Bug #33, but for plural attributes
1207: */
1208: @Test
1209: void mergeWithUpdatedPropertyValueRemovesOriginalAssertionsFromPluralAttribute() {
1210: this.em = getEntityManager("mergeWithUpdatedPropertyValueRemovesOriginalAssertionsFromPluralAttribute", false);
1211: persist(entityJ);
1212:
1213: assertEquals(2, entityJ.getOwlClassA().size());
1214: final OWLClassA newA = new OWLClassA(Generators.generateUri());
1215: entityJ.setOwlClassA(Collections.singleton(newA));
1216: transactional(() -> em.merge(entityJ));
1217:
1218: final OWLClassJ result = findRequired(OWLClassJ.class, entityJ.getUri());
1219: assertEquals(1, result.getOwlClassA().size());
1220: assertEquals(newA.getUri(), result.getOwlClassA().iterator().next().getUri());
1221: }
1222:
1223: @Test
1224: void updateSupportsJava8Instant() {
1225: this.em = getEntityManager("updateSupportsJava8Instant", true);
1226: final OWLClassX entityX = new OWLClassX();
1227: entityX.setInstant(Instant.now());
1228: persist(entityX);
1229:
1230: final Instant newInstant = Instant.ofEpochMilli(System.currentTimeMillis() + 10000);
1231: em.getTransaction().begin();
1232: final OWLClassX toUpdate = findRequired(OWLClassX.class, entityX.getUri());
1233: toUpdate.setInstant(newInstant);
1234: em.getTransaction().commit();
1235:
1236: final OWLClassX cachedResult = findRequired(OWLClassX.class, entityX.getUri());
1237: assertEquals(newInstant, cachedResult.getInstant());
1238: em.getEntityManagerFactory().getCache().evict(OWLClassX.class);
1239: final OWLClassX result = findRequired(OWLClassX.class, entityX.getUri());
1240: assertEquals(newInstant, result.getInstant());
1241: }
1242:
1243: @Test
1244: void updateSupportsPluralAnnotationProperty() {
1245: this.em = getEntityManager("updateSupportsPluralAnnotationProperty", true);
1246: final Set<String> annotations = IntStream.range(0, 5).mapToObj(i -> "Source" + i).collect(Collectors.toSet());
1247: entityN.setPluralAnnotationProperty(annotations);
1248: persist(entityN);
1249:
1250: final String added = "Added Source";
1251: final String removed = annotations.iterator().next();
1252: annotations.remove(removed);
1253: annotations.add(added);
1254: entityN.getPluralAnnotationProperty().remove(removed);
1255: entityN.getPluralAnnotationProperty().add(added);
1256: em.getTransaction().begin();
1257: final OWLClassN merged = em.merge(entityN);
1258: assertEquals(annotations, merged.getPluralAnnotationProperty());
1259: em.getTransaction().commit();
1260:
1261: final OWLClassN result = findRequired(OWLClassN.class, entityN.getId());
1262: assertEquals(annotations, result.getPluralAnnotationProperty());
1263: }
1264:
1265: @Test
1266: void updateSupportsSettingSubclassOnPolymorphicAttribute() {
1267: this.em = getEntityManager("updateSupportsSettingSubclassOnPolymorphicAttribute", true);
1268: final OWLClassU u = new OWLClassU();
1269: final OWLClassU reference = new OWLClassU();
1270: persist(reference);
1271:
1272: em.getTransaction().begin();
1273: final OWLClassU managedRef = em.merge(reference);
1274: u.setOwlClassS(managedRef);
1275: em.persist(u);
1276: em.getTransaction().commit();
1277:
1278: final OWLClassU result = findRequired(OWLClassU.class, u.getUri());
1279: assertEquals(reference.getUri(), result.getOwlClassS().getUri());
1280: }
1281:
1282: @Test
1283: void updateSupportsUpdatingSimpleLiteralValue() {
1284: this.em = getEntityManager("updateSupportsUpdatingSimpleLiteralValue", true);
1285: entityM.setSimpleLiteral("test original");
1286: persist(entityM);
1287:
1288: updateSimpleLiteralAndVerify();
1289: }
1290:
1291: private void updateSimpleLiteralAndVerify() {
1292: em.getTransaction().begin();
1293: final String newValue = "new test value";
1294: final OWLClassM toUpdate = findRequired(OWLClassM.class, entityM.getKey());
1295: toUpdate.setSimpleLiteral(newValue);
1296: em.getTransaction().commit();
1297:
1298: verifyValueDatatype(URI.create(entityM.getKey()), Vocabulary.p_m_simpleLiteral, XSD.STRING);
1299: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
1300: assertEquals(newValue, result.getSimpleLiteral());
1301: }
1302:
1303: @Test
1304: void updateOverwritesLanguageStringWithSimpleLiteralForSimpleLiteralAttribute() throws Exception {
1305: this.em = getEntityManager("updateOverwritesLanguageStringWithSimpleLiteralForSimpleLiteralAttribute", true);
1306: persist(entityM);
1307: final Collection<Quad> originalQuad = Collections.singleton(
1308: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_simpleLiteral), "test", "en"));
1309: persistTestData(originalQuad, em);
1310:
1311: updateSimpleLiteralAndVerify();
1312: verifyStatementsNotPresent(originalQuad, em);
1313: }
1314:
1315: @Test
1316: void updateSupportCollectionAttribute() {
1317: this.em = getEntityManager("updateSupportCollectionAttribute", true);
1318: final OWLClassX x = new OWLClassX();
1319: x.setACollection(new HashSet<>(Arrays.asList(entityA, entityA2)));
1320:
1321: final OWLClassA newA = new OWLClassA();
1322: newA.setUri(Generators.generateUri());
1323: newA.setStringAttribute("stringAttributeeee");
1324: em.getTransaction().begin();
1325: em.persist(newA);
1326: x.setACollection(Collections.singletonList(newA));
1327: final OWLClassX merged = em.merge(x);
1328: assertFalse(merged.getACollection().isEmpty());
1329: em.getTransaction().commit();
1330:
1331: final OWLClassX result = findRequired(OWLClassX.class, x.getUri());
1332: assertEquals(1, result.getACollection().size());
1333: assertEquals(newA.getUri(), result.getACollection().iterator().next().getUri());
1334: }
1335: }