Skip to contentMethod: testReplaceSimpleList()
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.test.runner;
16:
17: import cz.cvut.kbss.jopa.exceptions.IntegrityConstraintViolatedException;
18: import cz.cvut.kbss.jopa.exceptions.OWLInferredAttributeModifiedException;
19: import cz.cvut.kbss.jopa.exceptions.RollbackException;
20: import cz.cvut.kbss.jopa.test.*;
21: import cz.cvut.kbss.jopa.test.environment.Generators;
22: import org.junit.Test;
23: import org.slf4j.Logger;
24:
25: import java.lang.reflect.Field;
26: import java.net.MalformedURLException;
27: import java.net.URI;
28: import java.net.URL;
29: import java.util.*;
30: import java.util.stream.IntStream;
31:
32: import static org.hamcrest.core.Is.isA;
33: import static org.junit.Assert.*;
34:
35: public abstract class UpdateOperationsRunner extends BaseRunner {
36:
37: private OWLClassA entityA2;
38: private OWLClassF entityF;
39: private OWLClassJ entityJ;
40: private OWLClassO entityO;
41:
42: public UpdateOperationsRunner(Logger logger) {
43: super(logger);
44: initialize();
45: }
46:
47: private void initialize() {
48: this.entityF = new OWLClassF();
49: entityF.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityF"));
50: this.entityA2 = new OWLClassA();
51: entityA2.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA2"));
52: this.entityJ = new OWLClassJ();
53: entityJ.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityJ"));
54: final Set<OWLClassA> set = new HashSet<>(2);
55: set.add(entityA);
56: set.add(entityA2);
57: entityJ.setOwlClassA(set);
58: this.entityO = new OWLClassO(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityO"));
59: final Set<OWLClassE> es = new HashSet<>();
60: for (int i = 0; i < 5; i++) {
61: final OWLClassE e = new OWLClassE();
62: e.setStringAttribute("StringAttributeNo" + i);
63: es.add(e);
64: }
65: entityO.setOwlClassESet(es);
66: }
67:
68: @Test
69: public void testUpdateDataLeaveLazy() throws Exception {
70: logger.debug(
71: "Test: update data property. Leaves lazily loaded field empty and checks that after commit the field's value hasn't changed.");
72: this.em = getEntityManager("UpdateDataProperty", false);
73: entityB.setProperties(Generators.createProperties());
74: persist(entityB);
75:
76: em.getTransaction().begin();
77: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
78: assertNotNull(b);
79: final Field propsField = OWLClassB.class.getDeclaredField("properties");
80: propsField.setAccessible(true);
81: assertNull(propsField.get(b));
82: final String newString = "NewString";
83: b.setStringAttribute(newString);
84: em.getTransaction().commit();
85:
86: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
87: assertNotNull(res);
88: assertEquals(newString, res.getStringAttribute());
89: assertNotNull(res.getProperties());
90: assertEquals(entityB.getProperties(), res.getProperties());
91: }
92:
93: @Test
94: public void testUpdateDataPropertySetNull() {
95: logger.debug("Test: update data property. Set it to null.");
96: this.em = getEntityManager("UpdateDataPropertyToNull", true);
97: persist(entityA);
98:
99: em.getTransaction().begin();
100: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
101: assertNotNull(a);
102: assertNotNull(a.getStringAttribute());
103: a.setStringAttribute(null);
104: em.getTransaction().commit();
105:
106: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
107: assertNotNull(res);
108: assertNull(res.getStringAttribute());
109: assertEquals(entityA.getTypes(), res.getTypes());
110: }
111:
112: @Test
113: public void testUpdateReference() {
114: logger.debug("Test: update reference to entity.");
115: this.em = getEntityManager("UpdateReference", true);
116: persist(entityD, entityI);
117: // em.persist(entityA, ctx);
118:
119: final OWLClassA newA = new OWLClassA();
120: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
121: newA.setStringAttribute("newA");
122: em.getTransaction().begin();
123: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
124: assertNotNull(d.getOwlClassA());
125: d.setOwlClassA(newA);
126: final OWLClassI i = em.find(OWLClassI.class, entityI.getUri());
127: assertNotNull(i.getOwlClassA());
128: i.setOwlClassA(newA);
129: em.persist(newA);
130: em.getTransaction().commit();
131:
132: final OWLClassA resA1 = em.find(OWLClassA.class, newA.getUri());
133: assertNotNull(resA1);
134: final OWLClassD resD = em.find(OWLClassD.class, d.getUri());
135: assertEquals(resA1.getUri(), resD.getOwlClassA().getUri());
136: //                assertSame(resD.getOwlClassA(), resA1);
137: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
138: final OWLClassI resI = em.find(OWLClassI.class, i.getUri());
139: assertEquals(newA.getUri(), resI.getOwlClassA().getUri());
140: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
141: assertEquals(resA1.getUri(), resI.getOwlClassA().getUri());
142: //                assertEquals(resA1, resI.getOwlClassA());
143: }
144:
145: @Test
146: public void testMergeSet() throws Exception {
147: logger.debug("Test: merge set property.");
148: this.em = getEntityManager("MergeSet", false);
149: persist(entityJ);
150: em.clear();
151:
152: entityJ.getOwlClassA().forEach(a -> a.setStringAttribute("NEWVALUE"));
153:
154: em.getTransaction().begin();
155: OWLClassJ merged = em.merge(entityJ);
156:
157: for (final OWLClassA a : merged.getOwlClassA()) {
158: assertEquals(a.getStringAttribute(), "NEWVALUE");
159: }
160: em.getTransaction().commit();
161: }
162:
163: @Test
164: public void testMergeDetachedWithChanges() {
165: logger.debug("Test: merge detached entity with changes.");
166: this.em = getEntityManager("UpdateDetached", true);
167: persist(entityA);
168:
169: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
170: assertTrue(em.contains(a));
171: em.detach(a);
172: assertFalse(em.contains(a));
173: final String newType = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#AddedType";
174: a.getTypes().add(newType);
175: em.getTransaction().begin();
176: final OWLClassA merged = em.merge(a);
177: assertTrue(merged.getTypes().contains(newType));
178: em.getTransaction().commit();
179:
180: final OWLClassA res = em.find(OWLClassA.class, a.getUri());
181: assertEquals(a.getTypes().size(), res.getTypes().size());
182: assertTrue(res.getTypes().containsAll(a.getTypes()));
183: }
184:
185: @Test
186: public void testMergeDetachedCascade() {
187: logger.debug("Test: merge detached with cascade.");
188: this.em = getEntityManager("UpdateCascade", true);
189: em.getTransaction().begin();
190: em.persist(entityH);
191: assertTrue(em.contains(entityA));
192: em.getTransaction().commit();
193:
194: em.getTransaction().begin();
195: final OWLClassH h = em.find(OWLClassH.class, entityH.getUri());
196: assertNotNull(h.getOwlClassA());
197: em.detach(h);
198: assertFalse(em.contains(h));
199: assertFalse(em.contains(h.getOwlClassA()));
200: final String newStr = "newStringAttribute";
201: h.getOwlClassA().setStringAttribute(newStr);
202: final OWLClassH merged = em.merge(h);
203: assertEquals(newStr, merged.getOwlClassA().getStringAttribute());
204: em.getTransaction().commit();
205:
206: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
207: assertNotNull(res);
208: assertEquals(newStr, res.getStringAttribute());
209: }
210:
211: @Test
212: public void testMergeDetachedWithObjectPropertyChange() {
213: logger.debug("Test: merge detached with object property change.");
214: this.em = getEntityManager("UpdateDetachedWithOPChange", true);
215: persist(entityD, entityA);
216:
217: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
218: em.detach(d);
219: d.setOwlClassA(entityA2);
220: em.getTransaction().begin();
221: em.merge(d);
222: em.persist(entityA2);
223: em.getTransaction().commit();
224:
225: final OWLClassD res = em.find(OWLClassD.class, entityD.getUri());
226: assertNotNull(res);
227: assertEquals(entityA2.getUri(), res.getOwlClassA().getUri());
228: assertEquals(entityA2.getStringAttribute(), res.getOwlClassA().getStringAttribute());
229: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
230: assertNotNull(resA);
231: assertEquals(entityA.getStringAttribute(), resA.getStringAttribute());
232: assertEquals(entityA.getTypes(), resA.getTypes());
233: }
234:
235: @Test
236: public void testRemoveFromSimpleList() {
237: logger.debug("Test: remove entity from simple list. (But keep it in the ontology.)");
238: this.em = getEntityManager("UpdateRemoveFromSimpleList", true);
239: entityC.setSimpleList(Generators.createSimpleList());
240: persistEntityWithList();
241:
242: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
243: assertNotNull(c);
244: em.getTransaction().begin();
245: final OWLClassA a = c.getSimpleList().get(1);
246: c.getSimpleList().remove(a);
247: em.getTransaction().commit();
248:
249: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
250: assertNotNull(resA);
251: final OWLClassC resC = em.find(OWLClassC.class, c.getUri());
252: assertEquals(c.getSimpleList().size(), resC.getSimpleList().size());
253: assertEquals(entityC.getSimpleList().size() - 1, resC.getSimpleList().size());
254: for (OWLClassA aa : resC.getSimpleList()) {
255: assertFalse(resA.getUri().equals(aa.getUri()));
256: }
257: }
258:
259: private void persistEntityWithList() {
260: em.getTransaction().begin();
261: em.persist(entityC);
262: if (entityC.getSimpleList() != null) {
263: entityC.getSimpleList().forEach(em::persist);
264: }
265: if (entityC.getReferencedList() != null) {
266: entityC.getReferencedList().forEach(em::persist);
267: }
268: em.getTransaction().commit();
269: }
270:
271: @Test
272: public void testAddToSimpleList() {
273: logger.debug("Test: add entity to simple list.");
274: this.em = getEntityManager("UpdateAddToSimpleList", true);
275: entityC.setSimpleList(Generators.createSimpleList());
276: em.getTransaction().begin();
277: em.persist(entityC);
278: entityC.getSimpleList().forEach(em::persist);
279: em.persist(entityA);
280: em.getTransaction().commit();
281:
282: em.getTransaction().begin();
283: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
284: assertNotNull(c);
285: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
286: assertNotNull(a);
287: assertFalse(c.getSimpleList().contains(a));
288: c.getSimpleList().add(a);
289: em.getTransaction().commit();
290:
291: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
292: assertEquals(c.getSimpleList().size(), resC.getSimpleList().size());
293: assertEquals(entityC.getSimpleList().size() + 1, resC.getSimpleList().size());
294: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
295: assertNotNull(resA);
296: assertTrue(resC.getSimpleList().contains(resA));
297: }
298:
299: @Test
300: public void testClearSimpleList() {
301: logger.debug("Test: clear a simple list (but keep the entities in ontology).");
302: this.em = getEntityManager("UpdateClearSimpleList", true);
303: entityC.setSimpleList(Generators.createSimpleList());
304: persistEntityWithList();
305:
306: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
307: assertNotNull(c);
308: assertFalse(c.getSimpleList().isEmpty());
309: em.getTransaction().begin();
310: c.getSimpleList().clear();
311: em.getTransaction().commit();
312:
313: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
314: assertNotNull(resC);
315: assertTrue(resC.getSimpleList() == null || resC.getSimpleList().isEmpty());
316: for (OWLClassA a : entityC.getSimpleList()) {
317: assertNotNull(em.find(OWLClassA.class, a.getUri()));
318: }
319: }
320:
321: @Test
322: public void testReplaceSimpleList() {
323: logger.debug("Test: replace simple list with a new one.");
324: this.em = getEntityManager("UpdateReplaceSimpleList", true);
325: entityC.setSimpleList(Generators.createSimpleList());
326: persistEntityWithList();
327:
328: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
329: final List<OWLClassA> newList = new ArrayList<>(1);
330: newList.add(entityA);
331: em.getTransaction().begin();
332: em.persist(entityA);
333: c.setSimpleList(newList);
334: em.getTransaction().commit();
335:
336: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
337: assertNotNull(resC);
338: assertEquals(newList.size(), resC.getSimpleList().size());
339: boolean found;
340:• for (OWLClassA a : newList) {
341: found = false;
342:• for (OWLClassA aa : resC.getSimpleList()) {
343:• if (a.getUri().equals(aa.getUri())) {
344: found = true;
345: break;
346: }
347: }
348: assertTrue(found);
349: }
350:• for (OWLClassA a : entityC.getSimpleList()) {
351: assertNotNull(em.find(OWLClassA.class, a.getUri()));
352: }
353: }
354:
355: @Test
356: public void testRemoveFromReferencedList() {
357: logger.debug("Test: remove entity from referenced list. (But keep it in the ontology.");
358: this.em = getEntityManager("UpdateRemoveFromReferencedList", true);
359: entityC.setReferencedList(Generators.createReferencedList());
360: persistEntityWithList();
361:
362: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
363: assertNotNull(c);
364: em.getTransaction().begin();
365: final OWLClassA a = c.getReferencedList().get(Generators.randomInt(c.getReferencedList().size()));
366: c.getReferencedList().remove(a);
367: em.getTransaction().commit();
368:
369: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
370: assertNotNull(resA);
371: final OWLClassC resC = em.find(OWLClassC.class, c.getUri());
372: assertEquals(c.getReferencedList().size(), resC.getReferencedList().size());
373: assertEquals(entityC.getReferencedList().size() - 1, resC.getReferencedList().size());
374: for (OWLClassA aa : resC.getReferencedList()) {
375: assertFalse(resA.getUri().equals(aa.getUri()));
376: }
377: }
378:
379: @Test
380: public void testAddToReferencedList() {
381: logger.debug("Test: add entity to Referenced list.");
382: this.em = getEntityManager("UpdateAddToReferencedList", true);
383: entityC.setReferencedList(Generators.createReferencedList());
384: persistEntityWithList();
385:
386: em.getTransaction().begin();
387: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
388: assertNotNull(c);
389: em.persist(entityA);
390: c.getReferencedList().add(entityA);
391: em.getTransaction().commit();
392:
393: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
394: assertEquals(c.getReferencedList().size(), resC.getReferencedList().size());
395: assertEquals(entityC.getReferencedList().size() + 1, resC.getReferencedList().size());
396: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
397: assertNotNull(resA);
398: assertTrue(resC.getReferencedList().contains(resA));
399: }
400:
401: @Test
402: public void testClearReferencedList() {
403: logger.debug("Test: clear referenced list (but keep the entities in ontology).");
404: this.em = getEntityManager("UpdateClearReferencedList", true);
405: entityC.setReferencedList(Generators.createReferencedList());
406: persistEntityWithList();
407:
408: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
409: assertNotNull(c);
410: assertFalse(c.getReferencedList().isEmpty());
411: em.getTransaction().begin();
412: c.setReferencedList(null);
413: em.getTransaction().commit();
414:
415: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
416: assertNotNull(resC);
417: assertNull(resC.getReferencedList());
418: for (OWLClassA a : entityC.getReferencedList()) {
419: assertNotNull(em.find(OWLClassA.class, a.getUri()));
420: }
421: }
422:
423: @Test
424: public void testReplaceReferencedList() {
425: logger.debug("Test: replace referenced list with a new one.");
426: this.em = getEntityManager("UpdateReplaceReferencedList", true);
427: entityC.setReferencedList(Generators.createReferencedList());
428: persistEntityWithList();
429:
430: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
431: final List<OWLClassA> newList = new ArrayList<>(1);
432: newList.add(entityA);
433: em.getTransaction().begin();
434: em.persist(entityA);
435: c.setReferencedList(newList);
436: em.getTransaction().commit();
437:
438: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
439: assertNotNull(resC);
440: assertEquals(newList.size(), resC.getReferencedList().size());
441: boolean found;
442: for (OWLClassA a : newList) {
443: found = false;
444: for (OWLClassA aa : resC.getReferencedList()) {
445: if (a.getUri().equals(aa.getUri())) {
446: found = true;
447: break;
448: }
449: }
450: assertTrue(found);
451: }
452: for (OWLClassA a : entityC.getReferencedList()) {
453: assertNotNull(em.find(OWLClassA.class, a.getUri()));
454: }
455: }
456:
457: @Test
458: public void testAddNewToProperties() {
459: logger.debug("Test: add a new property value to entity's properties.");
460: this.em = getEntityManager("UpdateAddNewToProperties", false);
461: entityB.setProperties(Generators.createProperties());
462: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
463: expected.putAll(entityB.getProperties());
464: persist(entityB);
465: em.clear();
466:
467: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
468: assertNotNull(b);
469: assertEquals(expected.size(), b.getProperties().size());
470: em.getTransaction().begin();
471: b.getProperties().put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyFour",
472: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup"));
473: expected.putAll(b.getProperties());
474: em.getTransaction().commit();
475:
476: final OWLClassB res = em.find(OWLClassB.class, b.getUri());
477: assertNotNull(res);
478: assertEquals(expected.size(), res.getProperties().size());
479: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
480: assertTrue(res.getProperties().containsKey(e.getKey()));
481: final Set<String> s = e.getValue();
482: final Set<String> resS = res.getProperties().get(e.getKey());
483: assertEquals(s.size(), resS.size());
484: assertTrue(s.containsAll(resS));
485: }
486: }
487:
488: @Test
489: public void testAddPropertyValue() {
490: logger.debug("Test: add another value to an existing property.");
491: this.em = getEntityManager("UpdateAddPropertyValue", false);
492: entityB.setProperties(Generators.createProperties());
493: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
494: final String prop = entityB.getProperties().keySet().iterator().next();
495: expected.putAll(entityB.getProperties());
496: persist(entityB);
497: em.clear();
498:
499: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
500: assertNotNull(b);
501: assertEquals(expected.size(), b.getProperties().size());
502: em.getTransaction().begin();
503: b.getProperties().get(prop).add("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup");
504: expected.putAll(b.getProperties());
505: em.getTransaction().commit();
506:
507: final OWLClassB res = em.find(OWLClassB.class, b.getUri());
508: assertNotNull(res);
509: assertEquals(expected.size(), res.getProperties().size());
510: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
511: assertTrue(res.getProperties().containsKey(e.getKey()));
512: final Set<String> s = e.getValue();
513: final Set<String> resS = res.getProperties().get(e.getKey());
514: assertEquals(s.size(), resS.size());
515: if (e.getKey().equals(prop)) {
516: assertTrue(s.size() > 1);
517: for (String val : s) {
518: assertTrue(resS.contains(val));
519: }
520: } else {
521: assertTrue(resS.contains(s.iterator().next()));
522: }
523: }
524: }
525:
526: @Test
527: public void testAddPropertyValueDetached() {
528: logger.debug("Test: add another value to an existing property.");
529: this.em = getEntityManager("UpdateAddPropertyValueDetached", false);
530: entityB.setProperties(Generators.createProperties());
531: final String prop = entityB.getProperties().keySet().iterator().next();
532: final String newPropertyValue = "http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue";
533: persist(entityB);
534: em.clear();
535:
536: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
537: b.getProperties().get(prop).add(newPropertyValue);
538: em.detach(b);
539: em.getTransaction().begin();
540: em.merge(b);
541: em.getTransaction().commit();
542:
543: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
544: assertNotNull(res);
545: assertTrue(res.getProperties().get(prop).contains(newPropertyValue));
546: }
547:
548: @Test
549: public void testAddNewTypedPropertyWithValues() {
550: logger.debug("Test: add a new property with values. Typed.");
551: this.em = getEntityManager("UpdateAddNewToPropertiesTyped", false);
552: entityP.setProperties(Generators.createTypedProperties());
553: persist(entityP);
554: em.clear();
555:
556: final OWLClassP p = em.find(OWLClassP.class, entityP.getUri());
557: em.getTransaction().begin();
558: final URI property = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newProperty");
559: p.getProperties().put(property, new HashSet<>(Arrays.asList(1, "Two", new Date())));
560: em.getTransaction().commit();
561:
562: em.clear();
563:
564: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
565: assertNotNull(res);
566: assertTrue(res.getProperties().containsKey(property));
567: assertEquals(p.getProperties(), res.getProperties());
568: }
569:
570: @Test
571: public void testAddTypedPropertyValue() {
572: logger.debug("Test: add an existing property value. Typed.");
573: this.em = getEntityManager("UpdateAddPropertyValueTyped", false);
574: entityP.setProperties(Generators.createTypedProperties());
575: persist(entityP);
576: em.clear();
577:
578: final OWLClassP p = em.find(OWLClassP.class, entityP.getUri());
579: em.getTransaction().begin();
580: final URI property = p.getProperties().keySet().iterator().next();
581: final Object value = generateValueForProperty(p, property);
582: p.getProperties().get(property).add(value);
583: em.getTransaction().commit();
584:
585: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
586: assertTrue(res.getProperties().get(property).contains(value));
587: assertEquals(p.getProperties(), res.getProperties());
588: }
589:
590: private Object generateValueForProperty(OWLClassP instance, URI property) {
591: final Set<Object> values = instance.getProperties().get(property);
592: assert values != null && !values.isEmpty();
593: if (values.iterator().next() instanceof URI) {
594: return URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue");
595: } else {
596: return "StringValue";
597: }
598: }
599:
600: @Test(expected = OWLInferredAttributeModifiedException.class)
601: public void testModifyInferredAttribute() {
602: logger.debug("Test: modify an inferred attribute.");
603: this.em = getEntityManager("ModifyInferredAttribute", false);
604: persist(entityF);
605:
606: em.getTransaction().begin();
607: final OWLClassF f = em.find(OWLClassF.class, entityF.getUri());
608: assertNotNull(f);
609: f.setSecondStringAttribute("otherValue");
610: em.getTransaction().commit();
611: }
612:
613: @Test
614: public void testModifyAttributesOfBasicTypes() {
615: logger.debug("Test: modify attributes of basic Java types (Integer, Boolean etc.).");
616: this.em = getEntityManager("ModifyBasicTypeAttributes", false);
617: persist(entityM);
618:
619: em.getTransaction().begin();
620: final OWLClassM m = em.find(OWLClassM.class, entityM.getKey());
621: m.setBooleanAttribute(!entityM.getBooleanAttribute());
622: m.setDoubleAttribute(m.getDoubleAttribute() - 100.0);
623: m.setLongAttribute(m.getLongAttribute() + 100L);
624: m.setDateAttribute(new Date(System.currentTimeMillis() + 10000));
625: em.getTransaction().commit();
626:
627: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
628: assertEquals(m.getBooleanAttribute(), res.getBooleanAttribute());
629: assertEquals(m.getLongAttribute(), res.getLongAttribute());
630: assertEquals(m.getDoubleAttribute(), res.getDoubleAttribute());
631: assertEquals(m.getDateAttribute(), res.getDateAttribute());
632: }
633:
634: @Test
635: public void testModifyEnumAttribute() {
636: logger.debug("Test: modify enum attribute.");
637: this.em = getEntityManager("ModifyEnumAttribute", false);
638: assertNotNull(entityM.getEnumAttribute());
639: persist(entityM);
640:
641: final OWLClassM.Severity updated = OWLClassM.Severity.LOW;
642: em.getTransaction().begin();
643: final OWLClassM m = em.find(OWLClassM.class, entityM.getKey());
644: m.setEnumAttribute(updated);
645: em.getTransaction().commit();
646:
647: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
648: assertNotNull(res);
649: assertEquals(updated, res.getEnumAttribute());
650: }
651:
652: @Test
653: public void testCascadeOfNewlyPersistedOnMerge() {
654: logger.debug("Test: cascade merging of an object that has been just persisted.");
655: this.em = getEntityManager("CascadeMergeWithNewlyPersisted", true);
656: em.getTransaction().begin();
657: em.persist(entityO);
658: entityO.getOwlClassESet().forEach(em::persist);
659: em.getTransaction().commit();
660:
661: final OWLClassE newE = new OWLClassE();
662: newE.setStringAttribute("NewlyAddedE");
663: final OWLClassO toUpdate = em.find(OWLClassO.class, entityO.getUri());
664: em.detach(toUpdate);
665:
666: em.getTransaction().begin();
667: toUpdate.getOwlClassESet().add(newE);
668: em.persist(newE);
669: final OWLClassO merged = em.merge(toUpdate);
670: assertTrue(merged.getOwlClassESet().contains(newE));
671: em.getTransaction().commit();
672:
673: final OWLClassE resE = em.find(OWLClassE.class, newE.getUri());
674: assertNotNull(resE);
675: final OWLClassO resO = em.find(OWLClassO.class, entityO.getUri());
676: assertEquals(entityO.getOwlClassESet().size() + 1, resO.getOwlClassESet().size());
677: }
678:
679: @Test
680: public void modificationsOfCollectionAfterCascadeMergeAreWrittenOnCommit() {
681: logger.debug(
682: "Test: modify collection after cascade merge and check that the changes have been propagated on commit.");
683: this.em = getEntityManager("ModifyCollectionAfterCascadeMerge", true);
684: em.getTransaction().begin();
685: em.persist(entityJ);
686: em.getTransaction().commit();
687:
688: final OWLClassA newA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
689: em.getTransaction().begin();
690: final OWLClassJ merged = em.merge(entityJ);
691: merged.getOwlClassA().add(newA);
692: em.persist(newA);
693: em.getTransaction().commit();
694:
695: final OWLClassJ result = em.find(OWLClassJ.class, entityJ.getUri());
696: assertEquals(merged.getOwlClassA().size(), result.getOwlClassA().size());
697: boolean found = false;
698: for (OWLClassA a : result.getOwlClassA()) {
699: if (a.getUri().equals(newA.getUri())) {
700: found = true;
701: break;
702: }
703: }
704: assertTrue(found);
705: }
706:
707: @Test
708: public void testAddPropertiesWhenTheyWereNullOriginally() {
709: logger.debug("Test: add properties on update when the field was originally null.");
710: this.em = getEntityManager("AddPropertiesToNullOriginals", true);
711: em.getTransaction().begin();
712: assertNull(entityB.getProperties());
713: em.persist(entityB);
714: em.getTransaction().commit();
715:
716: final OWLClassB update = em.find(OWLClassB.class, entityB.getUri());
717: em.detach(update);
718: final Map<String, Set<String>> properties = Generators.createProperties(2);
719: update.setProperties(properties);
720: em.getTransaction().begin();
721: em.merge(update);
722: em.getTransaction().commit();
723:
724: final OWLClassB result = em.find(OWLClassB.class, entityB.getUri());
725: assertNotNull(result.getProperties());
726: assertEquals(properties, result.getProperties());
727: }
728:
729: @Test
730: public void testUpdatePlainLiteralObjectPropertyValueToAnotherIndividual() {
731: logger.debug("Test: update plain literal object property value to a different individual.");
732: this.em = getEntityManager("UpdatePlainLiteralObjectPropertyValue", true);
733: em.getTransaction().begin();
734: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#oldIndividual"));
735: em.persist(entityP);
736: em.getTransaction().commit();
737:
738: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
739: final URI newValue = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newIndividual");
740: em.getTransaction().begin();
741: update.setIndividualUri(newValue);
742: em.getTransaction().commit();
743:
744: final OWLClassP result = em.find(OWLClassP.class, entityP.getUri());
745: assertEquals(newValue, result.getIndividualUri());
746: }
747:
748: @Test
749: public void testUpdatePlainLiteralObjectPropertyValueToNull() {
750: logger.debug("Test: update plain literal object property value to null.");
751: this.em = getEntityManager("UpdatePlainLiteralObjectPropertyValueToNull", true);
752: em.getTransaction().begin();
753: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#SomeIndividual"));
754: em.persist(entityP);
755: em.getTransaction().commit();
756:
757: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
758: assertNotNull(update.getIndividualUri());
759: em.getTransaction().begin();
760: update.setIndividualUri(null);
761: em.getTransaction().commit();
762:
763: final OWLClassP result = em.find(OWLClassP.class, entityP.getUri());
764: assertNull(result.getIndividualUri());
765: }
766:
767: @Test
768: public void testUpdatePluralPlainLiteralObjectPropertyAttribute() throws MalformedURLException {
769: logger.debug("Test: update plural plain literal object property attribute.");
770: this.em = getEntityManager("UpdatePluralPlainLiteralObjectPropertyValue", true);
771: entityP.setIndividuals(Generators.createUrls());
772: persist(entityP);
773:
774: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
775: em.getTransaction().begin();
776: final URL added = new URL("http://krizik.felk.cvut.cz/ontologies/jopa#added");
777: final URL removed = entityP.getIndividuals().iterator().next();
778: update.getIndividuals().add(added);
779: update.getIndividuals().remove(removed);
780: em.getTransaction().commit();
781:
782: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
783: assertNotNull(res);
784: assertEquals(update.getIndividuals(), res.getIndividuals());
785: }
786:
787: @Test
788: public void testUpdateSimpleListOfIdentifiersByAddingNewItems() {
789: this.em = getEntityManager("UpdateSimpleListOfIdentifiersByAddingItems", true);
790: entityP.setSimpleList(Generators.createListOfIdentifiers());
791: persist(entityP);
792:
793: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
794: em.getTransaction().begin();
795: for (int i = 0; i < Generators.randomPositiveInt(10); i++) {
796: final URI u = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#Added-" + i);
797: // Insert at random position
798: update.getSimpleList().add(Generators.randomInt(update.getSimpleList().size()), u);
799: }
800: em.getTransaction().commit();
801:
802: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
803: assertNotNull(res);
804: assertEquals(update.getSimpleList(), res.getSimpleList());
805: }
806:
807: @Test
808: public void testUpdateReferencedListByRemovingAndAddingItems() {
809: this.em = getEntityManager("UpdateReferencedListByRemovingAndAddingItems", true);
810: entityP.setReferencedList(Generators.createListOfIdentifiers());
811: persist(entityP);
812:
813: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
814: em.getTransaction().begin();
815: for (int i = 0; i < Generators.randomPositiveInt(10); i++) {
816: final URI u = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#Added-" + i);
817: // We might even overwrite items set in previous iterations, but it does not matter. JOPA should handle it
818: update.getReferencedList().set(Generators.randomInt(update.getReferencedList().size()), u);
819: }
820: em.getTransaction().commit();
821:
822: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
823: assertNotNull(res);
824: assertEquals(update.getReferencedList(), res.getReferencedList());
825: }
826:
827: @Test
828: public void testUpdateStringAnnotationPropertyValue() {
829: this.em = getEntityManager("UpdateStringAnnotationPropertyValue", true);
830: entityN.setAnnotationProperty("PropertyValue");
831: persist(entityN);
832:
833: final String newValue = "newValue";
834: final OWLClassN update = em.find(OWLClassN.class, entityN.getId());
835: assertNotNull(update);
836: em.getTransaction().begin();
837: update.setAnnotationProperty(newValue);
838: em.getTransaction().commit();
839:
840: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
841: assertEquals(newValue, res.getAnnotationProperty());
842: }
843:
844: @Test
845: public void testUpdateAnnotationPropertyWithUriValueToDifferentValue() {
846: this.em = getEntityManager("UpdateAnnotationPropertyWithUriValueToDifferentValue", true);
847: entityN.setAnnotationUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#value"));
848: persist(entityN);
849:
850: final OWLClassN update = em.find(OWLClassN.class, entityN.getId());
851: assertNotNull(update);
852: em.getTransaction().begin();
853: final URI newUri = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newValue");
854: update.setAnnotationUri(newUri);
855: em.getTransaction().commit();
856:
857: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
858: assertEquals(newUri, res.getAnnotationUri());
859: }
860:
861: @Test
862: public void testUpdateUriTypes() {
863: this.em = getEntityManager("UpdateUriTypes", true);
864: entityP.setTypes(Generators.createUriTypes());
865: persist(entityP);
866:
867: em.getTransaction().begin();
868: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
869: final Iterator<URI> it = update.getTypes().iterator();
870: while (it.hasNext()) {
871: it.next();
872: if (Generators.randomBoolean()) {
873: it.remove();
874: }
875: }
876: update.getTypes().addAll(Generators.createUriTypes());
877: em.getTransaction().commit();
878:
879: final OWLClassP result = em.find(OWLClassP.class, entityP.getUri());
880: assertEquals(update.getTypes().size(), result.getTypes().size());
881: assertTrue(update.getTypes().containsAll(result.getTypes()));
882: }
883:
884: @Test
885: public void updateWithInvalidInstanceThrowsIntegrityConstraintsViolation() {
886: thrown.expect(RollbackException.class);
887: thrown.expectCause(isA(IntegrityConstraintViolatedException.class));
888:
889: this.em = getEntityManager("UpdateWithInvalidInstanceThrowsICViolation", true);
890: persist(entityN);
891:
892: entityN.setStringAttribute(null);
893: em.getTransaction().begin();
894: em.merge(entityN);
895: em.getTransaction().commit();
896: }
897:
898: @Test
899: public void addingValuesToDatatypePropertyCollectionAddsThemIntoRepository() {
900: this.em = getEntityManager("addingValuesToDatatypePropertyCollectionAddsThemIntoRepository", false);
901: persist(entityM);
902:
903: IntStream.generate(Generators::randomInt).limit(7).forEach(entityM.getIntegerSet()::add);
904: em.getTransaction().begin();
905: em.merge(entityM);
906: em.getTransaction().commit();
907:
908: final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
909: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
910: }
911:
912: @Test
913: public void removingValuesFromDatatypePropertyCollectionRemovesThemFromRepository() {
914: this.em = getEntityManager("removingValuesFromDatatypePropertyCollectionRemovesThemFromRepository", false);
915: persist(entityM);
916:
917: final Iterator<Integer> it = entityM.getIntegerSet().iterator();
918: while (it.hasNext()) {
919: it.next();
920: if (Generators.randomBoolean()) {
921: it.remove();
922: }
923: }
924: // Make sure there is at least one element
925: entityM.getIntegerSet().add(Generators.randomInt());
926:
927: em.getTransaction().begin();
928: em.merge(entityM);
929: em.getTransaction().commit();
930:
931: final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
932: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
933: }
934: }