Skip to content

Method: removeDeletesAllPropertyAssertionsMappedByEntity()

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.model.annotations.Id;
18: import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty;
19: import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
20: import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
21: import cz.cvut.kbss.jopa.test.*;
22: import cz.cvut.kbss.jopa.test.environment.Generators;
23: import cz.cvut.kbss.jopa.test.environment.TestEnvironmentUtils;
24: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
25: import org.junit.Ignore;
26: import org.junit.Test;
27: import org.slf4j.Logger;
28:
29: import java.lang.reflect.Field;
30: import java.net.URI;
31: import java.util.*;
32:
33: import static org.junit.Assert.*;
34:
35: public abstract class DeleteOperationsRunner extends BaseRunner {
36:
37: public DeleteOperationsRunner(Logger logger) {
38: super(logger);
39: }
40:
41: @Test
42: public void testRemoveSimple() {
43: this.em = getEntityManager("SimpleRemove", false);
44: persist(entityA);
45:
46: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
47: assertNotNull(a);
48: em.getTransaction().begin();
49: em.remove(a);
50: em.getTransaction().commit();
51:
52: assertNull(em.find(OWLClassA.class, entityA.getUri()));
53: verifyIndividualWasRemoved(entityA.getUri());
54: }
55:
56: // TODO First we need to resolve referential integrity
57: @Ignore
58: @Test
59: public void testRemoveReference() {
60: this.em = getEntityManager("RemoveReference", false);
61: persist(entityD, entityA);
62:
63: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
64: assertNotNull(a);
65: em.getTransaction().begin();
66: em.remove(a);
67: em.getTransaction().commit();
68:
69: final OWLClassD res = em.find(OWLClassD.class, entityD.getUri());
70: assertNotNull(res);
71: assertNull(em.find(OWLClassA.class, entityA.getUri()));
72: }
73:
74: @Test
75: public void testRemoveCascade() {
76: this.em = getEntityManager("RemoveCascade", false);
77: em.getTransaction().begin();
78: em.persist(entityG);
79: assertTrue(em.contains(entityG));
80: assertTrue(em.contains(entityH));
81: assertTrue(em.contains(entityA));
82: em.getTransaction().commit();
83:
84: em.getTransaction().begin();
85: final OWLClassG g = em.find(OWLClassG.class, entityG.getUri());
86: final OWLClassH h = em.find(OWLClassH.class, entityH.getUri());
87: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
88: assertNotNull(g);
89: assertNotNull(h);
90: assertNotNull(a);
91: assertTrue(em.contains(g));
92: assertTrue(em.contains(h));
93: assertTrue(em.contains(a));
94: assertNotNull(g);
95: em.remove(g);
96: assertFalse(em.contains(g));
97: assertFalse(em.contains(h));
98: assertFalse(em.contains(a));
99: em.getTransaction().commit();
100:
101: assertNull(em.find(OWLClassG.class, entityG.getUri()));
102: assertNull(em.find(OWLClassH.class, entityH.getUri()));
103: assertNull(em.find(OWLClassA.class, entityA.getUri()));
104: verifyIndividualWasRemoved(entityG.getUri());
105: verifyIndividualWasRemoved(entityH.getUri());
106: verifyIndividualWasRemoved(entityA.getUri());
107: }
108:
109: @Test(expected = IllegalArgumentException.class)
110: public void testRemoveDetached() {
111: this.em = getEntityManager("RemoveDetached", false);
112: assertNull(entityE.getUri());
113: persist(entityE);
114: assertNotNull(entityE.getUri());
115:
116: em.getTransaction().begin();
117: final OWLClassE e = em.find(OWLClassE.class, entityE.getUri());
118: assertNotNull(e);
119: assertTrue(em.contains(e));
120: em.detach(e);
121: assertFalse(em.contains(e));
122: em.remove(e);
123: }
124:
125: @Test
126: public void testRemoveFromSimpleList() {
127: this.em = getEntityManager("RemoveFromSimpleList", false);
128: final int size = 5;
129: entityC.setSimpleList(Generators.createSimpleList(size));
130: em.getTransaction().begin();
131: em.persist(entityC);
132: entityC.getSimpleList().forEach(em::persist);
133: em.getTransaction().commit();
134:
135: final int randIndex = Generators.randomInt(size);
136: final OWLClassA a = em.find(OWLClassA.class, entityC.getSimpleList().get(randIndex).getUri());
137: assertNotNull(a);
138: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
139: assertNotNull(c);
140: em.getTransaction().begin();
141: // We have to remove A from the simple list as well because otherwise we would break the chain in instances
142: assertTrue(c.getSimpleList().remove(a));
143: em.remove(a);
144: em.getTransaction().commit();
145:
146: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
147: assertNull(resA);
148: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
149: boolean found = false;
150: for (OWLClassA aa : resC.getSimpleList()) {
151: if (aa.getUri().equals(a.getUri())) {
152: found = true;
153: break;
154: }
155: }
156: assertFalse(found);
157: }
158:
159: @Test
160: public void testRemoveFromReferencedList() {
161: this.em = getEntityManager("RemoveFromReferencedList", false);
162: final int size = 10;
163: entityC.setReferencedList(Generators.createReferencedList(size));
164: em.getTransaction().begin();
165: em.persist(entityC);
166: entityC.getReferencedList().forEach(em::persist);
167: em.getTransaction().commit();
168:
169: final int randIndex = Generators.randomInt(size);
170: final OWLClassA a = em.find(OWLClassA.class, entityC.getReferencedList().get(randIndex).getUri());
171: assertNotNull(a);
172: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
173: assertNotNull(c);
174: em.getTransaction().begin();
175: // We have to remove A from the referenced list as well because otherwise we would break the chain in instances
176: assertTrue(c.getReferencedList().remove(a));
177: em.remove(a);
178: em.getTransaction().commit();
179:
180: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
181: assertNull(resA);
182: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
183: boolean found = false;
184: for (OWLClassA aa : resC.getReferencedList()) {
185: if (aa.getUri().equals(a.getUri())) {
186: found = true;
187: break;
188: }
189: }
190: assertFalse(found);
191: }
192:
193: @Test
194: public void testRemoveListOwner() {
195: this.em = getEntityManager("RemoveListOwner", false);
196: entityC.setSimpleList(Generators.createSimpleList());
197: entityC.setReferencedList(Generators.createReferencedList());
198: em.getTransaction().begin();
199: em.persist(entityC);
200: entityC.getSimpleList().forEach(em::persist);
201: entityC.getReferencedList().forEach(em::persist);
202: em.getTransaction().commit();
203:
204: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
205: assertNotNull(c);
206: em.getTransaction().begin();
207: em.remove(c);
208: em.getTransaction().commit();
209:
210: em.getEntityManagerFactory().getCache().evictAll();
211: for (OWLClassA a : entityC.getSimpleList()) {
212: assertNotNull(em.find(OWLClassA.class, a.getUri()));
213: }
214: for (OWLClassA a : entityC.getReferencedList()) {
215: assertNotNull(em.find(OWLClassA.class, a.getUri()));
216: }
217: }
218:
219: @Test
220: public void testRemoveNotYetCommitted() {
221: this.em = getEntityManager("RemoveNotYetCommitted", false);
222: em.getTransaction().begin();
223: em.persist(entityE);
224: assertTrue(em.contains(entityE));
225: em.remove(entityE);
226: assertFalse(em.contains(entityE));
227: em.getTransaction().commit();
228:
229: final OWLClassE res = em.find(OWLClassE.class, entityE.getUri());
230: assertNull(res);
231: }
232:
233: @Test
234: public void testRemoveMergedWithCascading() {
235: this.em = getEntityManager("CascadeMergeAndRemove", false);
236: em.getTransaction().begin();
237: em.persist(entityG);
238: assertTrue(em.contains(entityA));
239: assertTrue(em.contains(entityG));
240: assertTrue(em.contains(entityH));
241: em.getTransaction().commit();
242: em.clear();
243:
244: final OWLClassG toDetach = em.find(OWLClassG.class, entityG.getUri());
245: assertNotNull(toDetach);
246: em.detach(toDetach);
247: assertFalse(em.contains(toDetach));
248: assertFalse(em.contains(toDetach.getOwlClassH()));
249: assertFalse(em.contains(toDetach.getOwlClassH().getOwlClassA()));
250:
251: em.getTransaction().begin();
252: final OWLClassG toRemove = em.merge(toDetach);
253: em.remove(toRemove);
254: em.getTransaction().commit();
255:
256: assertNull(em.find(OWLClassG.class, entityG.getUri()));
257: assertNull(em.find(OWLClassH.class, entityH.getUri()));
258: assertNull(em.find(OWLClassA.class, entityA.getUri()));
259: }
260:
261: @Test
262: public void removeDeletesAllPropertyAssertionsMappedByEntity() throws Exception {
263: this.em = getEntityManager("RemoveDeletesAllMappedAttributes", false);
264: em.getTransaction().begin();
265: entityC.setSimpleList(Generators.createSimpleList(5));
266: em.persist(entityC);
267: entityC.getSimpleList().forEach(em::persist);
268: em.getTransaction().commit();
269:
270: final OWLClassC toRemove = em.find(OWLClassC.class, entityC.getUri());
271: em.getTransaction().begin();
272: em.remove(toRemove);
273: em.getTransaction().commit();
274:
275: final List<String> properties = resolveCProperties();
276:• for (String prop : properties) {
277: assertFalse(
278: em.createNativeQuery("ASK WHERE { ?x ?p ?o .}", Boolean.class).setParameter("x", entityC.getUri())
279: .setParameter("p", URI.create(prop)).getSingleResult());
280: }
281: }
282:
283: private List<String> resolveCProperties() throws Exception {
284: final List<String> lst = new ArrayList<>();
285: for (Field f : OWLClassC.class.getDeclaredFields()) {
286: if (f.getAnnotation(Id.class) != null || EntityPropertiesUtils.isFieldTransient(f)) {
287: continue;
288: }
289: if (f.getAnnotation(OWLDataProperty.class) != null) {
290: lst.add(f.getAnnotation(OWLDataProperty.class).iri());
291: } else if (f.getAnnotation(OWLObjectProperty.class) != null) {
292: lst.add(f.getAnnotation(OWLObjectProperty.class).iri());
293: } else if (f.getAnnotation(OWLAnnotationProperty.class) != null) {
294: lst.add(f.getAnnotation(OWLAnnotationProperty.class).iri());
295: }
296: }
297: return lst;
298: }
299:
300: @Test
301: public void testRemoveUnmappedPropertyValue() {
302: entityB.setProperties(Generators.createProperties());
303: this.em = getEntityManager("RemoveUnmappedPropertyValue", false);
304: em.getTransaction().begin();
305: em.persist(entityB);
306: em.getTransaction().commit();
307:
308: final String property = entityB.getProperties().keySet().iterator().next();
309: final Set<String> values = entityB.getProperties().get(property);
310: assertTrue(values.size() > 0);
311: final String valueToRemove = values.iterator().next();
312: em.getTransaction().begin();
313: final OWLClassB toUpdate = em.find(OWLClassB.class, entityB.getUri());
314: assertNotNull(toUpdate.getProperties());
315: assertTrue(toUpdate.getProperties().containsKey(property));
316: assertTrue(toUpdate.getProperties().get(property).contains(valueToRemove));
317: toUpdate.getProperties().get(property).remove(valueToRemove);
318: em.getTransaction().commit();
319:
320: final OWLClassB result = em.find(OWLClassB.class, entityB.getUri());
321: assertNotNull(result.getProperties());
322: assertTrue(result.getProperties().containsKey(property));
323: assertEquals(values.size() - 1, result.getProperties().get(property).size());
324: assertFalse(result.getProperties().get(property).contains(valueToRemove));
325: }
326:
327: @Test
328: public void testRemoveAllValuesOfUnmappedProperty() {
329: entityB.setProperties(Generators.createProperties());
330: this.em = getEntityManager("RemoveAllValuesOfUnmappedProperty", false);
331: em.getTransaction().begin();
332: em.persist(entityB);
333: em.getTransaction().commit();
334:
335: final String property = entityB.getProperties().keySet().iterator().next();
336: em.getTransaction().begin();
337: final OWLClassB toUpdate = em.find(OWLClassB.class, entityB.getUri());
338: assertNotNull(toUpdate.getProperties());
339: assertTrue(toUpdate.getProperties().containsKey(property));
340: toUpdate.getProperties().remove(property);
341: em.getTransaction().commit();
342:
343: final OWLClassB result = em.find(OWLClassB.class, entityB.getUri());
344: assertNotNull(result.getProperties());
345: assertFalse(result.getProperties().containsKey(property));
346: }
347:
348: @Test
349: public void testRemoveTypedUnmappedPropertyValue() {
350: this.em = getEntityManager("RemoveUnmappedPropertyValueTyped", false);
351: entityP.setProperties(Generators.createTypedProperties(10));
352: persist(entityP);
353:
354: em.getTransaction().begin();
355: final OWLClassP toUpdate = em.find(OWLClassP.class, entityP.getUri());
356: for (Set<Object> set : toUpdate.getProperties().values()) {
357: final Iterator<Object> it = set.iterator();
358: while (it.hasNext()) {
359: it.next();
360: if (Generators.randomBoolean()) {
361: it.remove();
362: }
363: }
364: }
365: em.getTransaction().commit();
366: toUpdate.getProperties().keySet().removeIf(property -> toUpdate.getProperties().get(property).isEmpty());
367: if (toUpdate.getProperties().isEmpty()) {
368: toUpdate.setProperties(null);
369: }
370:
371: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
372: assertEquals(toUpdate.getProperties(), res.getProperties());
373: }
374:
375: @Test
376: public void testRemoveAllValuesOfTypedUnmappedProperty() {
377: this.em = getEntityManager("RemoveAllValuesOfUnmappedPropertyTyped", false);
378: entityP.setProperties(Generators.createTypedProperties(15));
379: persist(entityP);
380:
381: final OWLClassP toUpdate = em.find(OWLClassP.class, entityP.getUri());
382: em.detach(toUpdate);
383: // Copy the keys to prevent concurrent modification
384: final Set<URI> keys = new HashSet<>(toUpdate.getProperties().keySet());
385: keys.stream().filter(k -> Generators.randomBoolean())
386: .forEach(key -> toUpdate.getProperties().remove(key));
387: em.getTransaction().begin();
388: final OWLClassP merged = em.merge(toUpdate);
389: assertTrue(TestEnvironmentUtils.arePropertiesEqual(toUpdate.getProperties(), merged.getProperties()));
390: em.getTransaction().commit();
391:
392: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
393: assertTrue(TestEnvironmentUtils.arePropertiesEqual(toUpdate.getProperties(), res.getProperties()));
394: }
395:
396: @Test
397: public void testRemoveAllValuesOfPluralPlainIdentifierObjectProperty() {
398: this.em = getEntityManager("RemoveAllValuesOfPluralPlainIdentifierOP", false);
399: entityP.setIndividuals(Generators.createUrls());
400: persist(entityP);
401:
402: final OWLClassP toUpdate = em.find(OWLClassP.class, entityP.getUri());
403: em.getTransaction().begin();
404: toUpdate.getIndividuals().clear();
405: em.getTransaction().commit();
406:
407: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
408: assertNull(res.getIndividuals());
409: }
410:
411: @Test
412: public void testSetAnnotationPropertyValueToNull() {
413: this.em = getEntityManager("SetAnnotationPropertyValueToNull", false);
414: entityN.setAnnotationProperty("annotationPropertyValue");
415: persist(entityN);
416:
417: final OWLClassN update = em.find(OWLClassN.class, entityN.getId());
418: assertNotNull(update.getAnnotationProperty());
419: em.getTransaction().begin();
420: update.setAnnotationProperty(null);
421: em.getTransaction().commit();
422:
423: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
424: assertNull(res.getAnnotationProperty());
425: }
426:
427: @Test
428: public void testSetAnnotationPropertyValueContainingUriToNull() {
429: this.em = getEntityManager("SetAnnotationPropertyValueContainingUriToNull", false);
430: entityN.setAnnotationUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#annotationPropertyValue"));
431: persist(entityN);
432:
433: final OWLClassN update = em.find(OWLClassN.class, entityN.getId());
434: assertNotNull(update.getAnnotationUri());
435: em.getTransaction().begin();
436: update.setAnnotationUri(null);
437: em.getTransaction().commit();
438:
439: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
440: assertNull(res.getAnnotationUri());
441: }
442:
443: @Test
444: public void testClearUriTypes() {
445: this.em = getEntityManager("ClearUriTypes", false);
446: entityP.setTypes(Generators.createUriTypes());
447: persist(entityP);
448:
449: em.getTransaction().begin();
450: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
451: update.getTypes().clear();
452: em.getTransaction().commit();
453:
454: final OWLClassP result = em.find(OWLClassP.class, entityP.getUri());
455: assertNull(result.getTypes());
456: }
457:
458: @Test
459: public void testRemoveDetachedWithCascadedReferenceUsingMergeAndRemove() {
460: this.em = getEntityManager("RemoveDetachedEntityWithCascadedReferenceUsingMergeAndRemove", true);
461: persist(entityH);
462: assertNotNull(em.find(OWLClassH.class, entityH.getUri()));
463: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
464: final OWLClassH h = new OWLClassH();
465: h.setUri(entityH.getUri());
466: final OWLClassA a = new OWLClassA();
467: a.setUri(entityA.getUri());
468: a.setStringAttribute(entityA.getStringAttribute());
469: a.setTypes(new HashSet<>(entityA.getTypes()));
470: h.setOwlClassA(a);
471:
472: em.clear();
473: em.getTransaction().begin();
474: final OWLClassH toRemove = em.merge(h);
475: em.remove(toRemove);
476: em.getTransaction().commit();
477:
478: assertNull(em.find(OWLClassH.class, entityH.getUri()));
479: assertNull(em.find(OWLClassA.class, entityA.getUri()));
480: }
481:
482: @Test
483: public void removeEntityTwiceInOneTransactionRemovesIt() {
484: this.em = getEntityManager("RemoveDetachedEntityWithCascadedReferenceUsingMergeAndRemove", true);
485: persist(entityA);
486:
487: em.getTransaction().begin();
488: final OWLClassA toRemove = em.merge(entityA);
489: em.remove(toRemove);
490: assertFalse(em.contains(toRemove));
491: em.remove(toRemove);
492: assertFalse(em.contains(toRemove));
493: em.getTransaction().commit();
494:
495: assertNull(em.find(OWLClassA.class, entityA.getUri()));
496: }
497:
498: @Test
499: public void settingDatatypeCollectionToNullRemovesAllValues() {
500: this.em = getEntityManager("settingDatatypeCollectionToNullRemovesAllValues", true);
501: persist(entityM);
502:
503: em.getTransaction().begin();
504: final OWLClassM toClear = em.find(OWLClassM.class, entityM.getKey());
505: toClear.setIntegerSet(null);
506: em.getTransaction().commit();
507:
508: final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
509: assertNull(result.getIntegerSet());
510: verifyDatatypePropertiesRemoved();
511: }
512:
513: private void verifyDatatypePropertiesRemoved() {
514: for (Integer value : entityM.getIntegerSet()) {
515: assertFalse(em.createNativeQuery("ASK { ?x ?p ?v . }", Boolean.class)
516: .setParameter("x", URI.create(entityM.getKey()))
517: .setParameter("p", URI.create(Vocabulary.p_m_IntegerSet)).setParameter("v", value)
518: .getSingleResult());
519: }
520: }
521:
522: @Test
523: public void clearingDatatypeCollectionRemovesAllValues() {
524: this.em = getEntityManager("clearingDatatypeCollectionRemovesAllValues", true);
525: persist(entityM);
526:
527: em.getTransaction().begin();
528: final OWLClassM toClear = em.find(OWLClassM.class, entityM.getKey());
529: toClear.getIntegerSet().clear();
530: em.getTransaction().commit();
531:
532: final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
533: // Could be the cached variant, which contains empty collection, or loaded from ontology, which contains null
534: assertTrue(result.getIntegerSet() == null || result.getIntegerSet().isEmpty());
535: verifyDatatypePropertiesRemoved();
536: }
537:
538: @Test
539: public void removingNewlyPersistedInstanceRemovesPendingPersistsAndAllowsTransactionToFinish() {
540: this.em = getEntityManager("removingNewlyPersistedInstanceRemovesPendingPersistsAndAllowsTransactionToFinish",
541: true);
542: em.getTransaction().begin();
543: em.persist(entityD);
544: em.remove(entityD);
545: em.getTransaction().commit();
546:
547: assertNull(em.find(OWLClassD.class, entityD.getUri()));
548: assertNull(em.find(OWLClassA.class, entityA.getUri()));
549: }
550:
551: @Test
552: public void removingNewlyPersistedInstanceRemovesPendingListReferencesAndAllowsTransactionToFinish() {
553: this.em = getEntityManager(
554: "removingNewlyPersistedInstanceRemovesPendingListReferencesAndAllowsTransactionToFinish", true);
555: em.getTransaction().begin();
556: entityC.setSimpleList(Generators.createSimpleList());
557: entityC.setReferencedList(Generators.createReferencedList());
558: em.persist(entityC);
559: em.remove(entityC);
560: em.getTransaction().commit();
561:
562: assertNull(em.find(OWLClassC.class, entityC.getUri()));
563: entityC.getSimpleList().forEach(a -> assertNull(em.find(OWLClassA.class, a.getUri())));
564: entityC.getReferencedList().forEach(a -> assertNull(em.find(OWLClassA.class, a.getUri())));
565: }
566:
567: @Test
568: public void removingListItemsFromNewlyPersistedOwnerRemovesThemFromPendingReferencesAndAllowsTransactionToFinish() {
569: this.em = getEntityManager(
570: "removingListItemsFromNewlyPersistedOwnerRemovesThemFromPendingReferencesAndAllowsTransactionToFinish",
571: false);
572: em.getTransaction().begin();
573: entityC.setSimpleList(Generators.createSimpleList());
574: entityC.setReferencedList(Generators.createReferencedList());
575: em.persist(entityC);
576: entityC.getSimpleList().clear();
577: entityC.getReferencedList().clear();
578: em.getTransaction().commit();
579:
580: final OWLClassC result = em.find(OWLClassC.class, entityC.getUri());
581: assertNotNull(result);
582: assertNull(result.getSimpleList());
583: assertNull(result.getReferencedList());
584: }
585: }