Skip to contentMethod: testPersistWithReferencedList()
1: /**
2: * Copyright (C) 2016 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.OWLEntityExistsException;
16: import cz.cvut.kbss.jopa.exceptions.RollbackException;
17: import cz.cvut.kbss.jopa.test.*;
18: import cz.cvut.kbss.jopa.test.environment.Generators;
19: import cz.cvut.kbss.ontodriver.exception.PrimaryKeyNotSetException;
20: import org.junit.Test;
21: import org.slf4j.Logger;
22:
23: import java.net.URI;
24: import java.net.URL;
25: import java.util.Collections;
26: import java.util.HashMap;
27: import java.util.Map;
28: import java.util.Set;
29:
30: import static org.junit.Assert.*;
31:
32: public abstract class CreateOperationsRunner extends BaseRunner {
33:
34: protected CreateOperationsRunner(Logger logger) {
35: super(logger);
36: }
37:
38: @Test
39: public void testPersistWithGeneratedId() {
40: this.em = getEntityManager("PersistWithGenerated", false);
41: persist(entityA, entityE);
42:
43: final OWLClassA resA1 = em.find(OWLClassA.class, entityA.getUri());
44: assertNotNull(resA1);
45: assertEquals(entityA.getStringAttribute(), resA1.getStringAttribute());
46: assertEquals(entityA.getTypes().size(), resA1.getTypes().size());
47: assertTrue(entityA.getTypes().containsAll(resA1.getTypes()));
48:
49: assertNotNull(entityE.getUri());
50: final OWLClassE resE = em.find(OWLClassE.class, entityE.getUri());
51: assertNotNull(resE);
52: assertEquals(entityE.getStringAttribute(), resE.getStringAttribute());
53: }
54:
55: @Test(expected = PrimaryKeyNotSetException.class)
56: public void persistingEntityWithoutIdAndWithoutGeneratedIdThrowsException() {
57: this.em = getEntityManager("PersistWithoutId", false);
58: final OWLClassB b = new OWLClassB();
59: b.setStringAttribute("someValue");
60: persist(b);
61: }
62:
63: @Test(expected = NullPointerException.class)
64: public void persistNullThrowsNPX() {
65: this.em = getEntityManager("PersistNull", false);
66: em.getTransaction().begin();
67: em.persist(null);
68: }
69:
70: @Test
71: public void testPersistAndRollbackChanges() {
72: this.em = getEntityManager("PersistRollback", false);
73: em.getTransaction().begin();
74: em.persist(entityE);
75: assertTrue(em.contains(entityE));
76: em.getTransaction().rollback();
77:
78: assertFalse(em.contains(entityE));
79: assertNull(em.find(entityE.getClass(), entityE.getUri()));
80: }
81:
82: @Test(expected = RollbackException.class)
83: public void persistingInRollbackOnlyThrowsExceptionOnCommit() {
84: this.em = getEntityManager("PersistRollbackOnly", false);
85: em.getTransaction().begin();
86: em.getTransaction().setRollbackOnly();
87: em.persist(entityE);
88: em.getTransaction().commit();
89: }
90:
91: @Test
92: public void testPersistWithCascade() {
93: this.em = getEntityManager("PersistWithCascade", false);
94: persist(entityG);
95:
96: final OWLClassA resA2 = em.find(OWLClassA.class, entityA.getUri());
97: assertNotNull(resA2);
98: final OWLClassH resH = em.find(OWLClassH.class, entityH.getUri());
99: assertNotNull(resH);
100: assertEquals(resH.getOwlClassA(), resA2);
101: final OWLClassG resG = em.find(OWLClassG.class, entityG.getUri());
102: assertNotNull(resG);
103: assertEquals(resG.getOwlClassH(), resH);
104: assertEquals(resG.getOwlClassH().getOwlClassA(), resA2);
105: }
106:
107: @Test(expected = OWLEntityExistsException.class)
108: public void persistingInstanceMultipleTimesIsNotAllowed() {
109: this.em = getEntityManager("PersistTwice", false);
110: persist(entityB, entityB);
111: }
112:
113: @Test(expected = RollbackException.class)
114: public void persistingOnlyOnePartOfRelationWithoutCascadeThrowsRollbackException() {
115: this.em = getEntityManager("PersistWithoutCascade", false);
116: persist(entityD);
117: }
118:
119: @Test(expected = OWLEntityExistsException.class)
120: public void persistingDetachedEntityIsIllegal() {
121: this.em = getEntityManager("PersistDetached", false);
122: persist(entityA);
123:
124: final OWLClassA det = em.find(OWLClassA.class, entityA.getUri());
125: assertNotNull(det);
126: em.detach(det);
127: em.persist(det);
128: }
129:
130: @Test
131: public void testPersistWithSimpleList() {
132: this.em = getEntityManager("PersistSimpleList", false);
133: entityC.setSimpleList(Generators.createSimpleList(10));
134: em.getTransaction().begin();
135: em.persist(entityC);
136: entityC.getSimpleList().forEach(em::persist);
137: em.getTransaction().commit();
138:
139: final OWLClassA a = em.find(OWLClassA.class, entityC.getSimpleList().get(1).getUri());
140: assertNotNull(a);
141: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
142: assertNotNull(c);
143: assertNotNull(c.getSimpleList());
144: assertFalse(c.getSimpleList().isEmpty());
145: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
146: assertTrue(c.getSimpleList().contains(a));
147: }
148:
149: @Test(expected = RollbackException.class)
150: public void persistingEntityWithSimpleListWithoutCascadeIsIllegal() {
151: this.em = getEntityManager("PersistSimpleListNoCascade", false);
152: entityC.setSimpleList(Generators.createSimpleList(10));
153: persist(entityC);
154: }
155:
156: @Test
157: public void testPersistWithReferencedList() {
158: this.em = getEntityManager("PersistReferencedList", false);
159: entityC.setReferencedList(Generators.createReferencedList(5));
160: em.getTransaction().begin();
161: em.persist(entityC);
162: entityC.getReferencedList().forEach(em::persist);
163: assertTrue(em.contains(entityC));
164: assertTrue(em.contains(entityC.getReferencedList().get(0)));
165: em.getTransaction().commit();
166:
167: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
168: assertNotNull(c);
169: assertNotNull(c.getReferencedList());
170: assertFalse(c.getReferencedList().isEmpty());
171: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
172:• for (OWLClassA a : entityC.getReferencedList()) {
173: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
174: assertNotNull(resA);
175: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
176: assertTrue(c.getReferencedList().contains(resA));
177: }
178: }
179:
180: @Test(expected = RollbackException.class)
181: public void persistingEntityWithReferencedListWithoutCascadeIsIllegal() {
182: this.em = getEntityManager("PersistReferencedListNoCascade", false);
183: entityC.setReferencedList(Generators.createReferencedList(5));
184: persist(entityC);
185: }
186:
187: @Test
188: public void testPersistSimpleAndReferencedList() {
189: this.em = getEntityManager("PersistSimpleAndReferencedList", false);
190: entityC.setReferencedList(Generators.createReferencedList(5));
191: entityC.setSimpleList(Generators.createSimpleList(5));
192: em.getTransaction().begin();
193: em.persist(entityC);
194: entityC.getSimpleList().forEach(em::persist);
195: entityC.getReferencedList().forEach(em::persist);
196: em.getTransaction().commit();
197:
198: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
199: assertNotNull(c);
200: assertNotNull(c.getSimpleList());
201: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
202: assertNotNull(c.getReferencedList());
203: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
204: for (OWLClassA a : entityC.getSimpleList()) {
205: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
206: assertNotNull(resA);
207: assertTrue(c.getSimpleList().contains(resA));
208: }
209: for (OWLClassA a : entityC.getReferencedList()) {
210: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
211: assertNotNull(resA);
212: assertTrue(c.getReferencedList().contains(resA));
213: }
214: }
215:
216: @Test
217: public void testPersistWithProperties() {
218: this.em = getEntityManager("PersistWithProperties", false);
219: final Map<String, Set<String>> props = new HashMap<>(3);
220: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyOne", Collections
221: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Individual10"));
222: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyTwo", Collections
223: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/SomeEntity"));
224: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyThree",
225: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityG"));
226: final Map<String, Set<String>> expected = new HashMap<>(4);
227: expected.putAll(props);
228: entityB.setProperties(props);
229: persist(entityB);
230: em.clear();
231:
232: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
233: assertNotNull(res);
234: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
235: assertNotNull(res.getProperties());
236: assertFalse(res.getProperties().isEmpty());
237: assertEquals(expected.size(), res.getProperties().size());
238: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
239: assertTrue(res.getProperties().containsKey(e.getKey()));
240: final Set<String> s = e.getValue();
241: final Set<String> resS = res.getProperties().get(e.getKey());
242: assertNotNull(resS);
243: assertEquals(1, resS.size());
244: assertEquals(s.iterator().next(), resS.iterator().next());
245: }
246: }
247:
248: @Test
249: public void testPersistWithEmptyProperties() {
250: this.em = getEntityManager("PersistWithPropertiesEmpty", false);
251: entityB.setProperties(Collections.emptyMap());
252: em.getTransaction().begin();
253: em.persist(entityB);
254: assertTrue(em.contains(entityB));
255: em.getTransaction().commit();
256: em.clear();
257:
258: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
259: assertNotNull(b);
260: assertEquals(entityB.getUri(), b.getUri());
261: assertEquals(entityB.getStringAttribute(), b.getStringAttribute());
262: assertNull(b.getProperties());
263: }
264:
265: @Test(expected = OWLEntityExistsException.class)
266: public void persistingTwoInstancesOfDifferentClassesWithSameUriIsIllegal() {
267: this.em = getEntityManager("PersistURITwiceInDifferentClasses", false);
268: final URI pk = URI.create("http://krizik.felk.cvut.cz/jopa/onto/sameEntity");
269: final OWLClassA a = new OWLClassA();
270: a.setUri(pk);
271: final OWLClassB b = new OWLClassB();
272: b.setUri(pk);
273: em.getTransaction().begin();
274: em.persist(a);
275: em.persist(b);
276: em.getTransaction().commit();
277: }
278:
279: @Test
280: public void testPersistEntityWithBasicTypeAttributes() {
281: this.em = getEntityManager("PersistEntityWithBasicTypeAttributes", false);
282: persist(entityM);
283: em.clear();
284:
285: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
286: assertNotNull(res);
287: assertEquals(entityM.getKey(), res.getKey());
288: assertEquals(entityM.getBooleanAttribute(), res.getBooleanAttribute());
289: assertEquals(entityM.getIntAttribute(), res.getIntAttribute());
290: assertEquals(entityM.getLongAttribute(), res.getLongAttribute());
291: assertEquals(entityM.getDoubleAttribute(), res.getDoubleAttribute());
292: assertEquals(entityM.getDateAttribute(), res.getDateAttribute());
293: }
294:
295: @Test
296: public void testPersistAndUpdateAttributeBeforeCommit() {
297: this.em = getEntityManager("PersistAndUpdateBeforeCommit", false);
298: final String updatedValue = "updatedStringAttributeValue";
299: em.getTransaction().begin();
300: em.persist(entityA);
301: entityA.setStringAttribute(updatedValue);
302: em.getTransaction().commit();
303: em.clear();
304:
305: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
306: assertNotNull(res);
307: assertEquals(updatedValue, res.getStringAttribute());
308: }
309:
310: @Test
311: public void testPersistEntityWithEnumAttribute() {
312: this.em = getEntityManager("PersistEntityWithEnum", false);
313: persist(entityM);
314:
315: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
316: assertNotNull(res);
317: assertEquals(entityM.getEnumAttribute(), res.getEnumAttribute());
318: }
319:
320: @Test
321: public void testPersistTypedProperties() {
322: this.em = getEntityManager("PersistTypedProperties", false);
323: entityP.setProperties(Generators.createTypedProperties());
324: em.getTransaction().begin();
325: em.persist(entityP);
326: em.getTransaction().commit();
327: em.clear();
328:
329: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
330: assertNotNull(res);
331: assertEquals(entityP.getProperties(), res.getProperties());
332: }
333:
334: @Test
335: public void testPersistInstanceWithPlainIdentifierObjectPropertyValue() {
336: this.em = getEntityManager("PersistInstanceWithIdentifierObjectPropertyValue", false);
337: final URI value = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#individualAAA");
338: entityP.setIndividualUri(value);
339: em.getTransaction().begin();
340: em.persist(entityP);
341: em.getTransaction().commit();
342: em.clear();
343:
344: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
345: assertNotNull(res);
346: assertEquals(value, res.getIndividualUri());
347: }
348:
349: @Test
350: public void testPersistInstanceWithPluralObjectPropertyAttributeRepresentedByUrls() {
351: this.em = getEntityManager("PersistInstanceWithPluralIdentifierObjectPropertyValue", false);
352: final Set<URL> urls = Generators.createUrls();
353: entityP.setIndividuals(urls);
354: em.getTransaction().begin();
355: em.persist(entityP);
356: em.getTransaction().commit();
357: em.clear();
358:
359: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
360: assertNotNull(res);
361: assertEquals(urls, res.getIndividuals());
362: }
363:
364: @Test
365: public void testPersistInstanceWithSimpleListOfIdentifiers() {
366: this.em = getEntityManager("PersistInstanceWithSimpleListOfIdentifiers", false);
367: entityP.setSimpleList(Generators.createListOfIdentifiers());
368: em.getTransaction().begin();
369: em.persist(entityP);
370: em.getTransaction().commit();
371: em.clear();
372:
373: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
374: assertNotNull(res);
375: assertEquals(entityP.getSimpleList(), res.getSimpleList());
376: }
377:
378: @Test
379: public void testPersistInstanceWithReferencedListOfIdentifiers() {
380: this.em = getEntityManager("PersistInstanceWithReferencedListOfIdentifiers", false);
381: entityP.setReferencedList(Generators.createListOfIdentifiers());
382: em.getTransaction().begin();
383: em.persist(entityP);
384: em.getTransaction().commit();
385: em.clear();
386:
387: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
388: assertNotNull(res);
389: assertEquals(entityP.getReferencedList(), res.getReferencedList());
390: }
391:
392: @Test
393: public void testPersistInstanceWithAnnotationProperties() {
394: this.em = getEntityManager("PersistInstanceWithAnnotationPropertyValues", false);
395: final String apValue = "annotationPropertyValue";
396: final URI apUriValue = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#annotationPropertyValue");
397: entityN.setAnnotationProperty(apValue);
398: entityN.setAnnotationUri(apUriValue);
399: em.getTransaction().begin();
400: em.persist(entityN);
401: em.getTransaction().commit();
402: em.clear();
403: assertNotNull(entityN.getId());
404:
405: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
406: assertEquals(apValue, res.getAnnotationProperty());
407: assertEquals(apUriValue, res.getAnnotationUri());
408: }
409:
410: @Test
411: public void persistEntityWithNonNullGeneratedIdentifierDoesNotRewriteIdentifier() {
412: this.em = getEntityManager("PersistEntityWithNonNullGeneratedIdentifiersDoesNotRewriteIdentifier", false);
413: final URI u = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityELives");
414: entityE.setUri(u);
415: em.getTransaction().begin();
416: em.persist(entityE);
417: em.getTransaction().commit();
418:
419: assertEquals(u, entityE.getUri());
420: final OWLClassE res = em.find(OWLClassE.class, u);
421: assertNotNull(res);
422: assertEquals(u, res.getUri());
423: assertEquals(entityE.getStringAttribute(), res.getStringAttribute());
424: }
425:
426: @Test
427: public void persistEntityAndReferenceWithNonNullGeneratedIdentifiersDoesNotRewriteThem() {
428: this.em = getEntityManager("PersistEntityAndReferenceWithNonNullGeneratedIdentifiersDoesNotRewriteThem", false);
429: final URI uK = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityKLives");
430: final URI uE = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityELives");
431: final OWLClassK entityK = new OWLClassK();
432: entityK.setUri(uK);
433: entityK.setOwlClassE(entityE);
434: entityE.setUri(uE);
435: em.getTransaction().begin();
436: em.persist(entityK);
437: em.persist(entityE);
438: em.getTransaction().commit();
439:
440: assertEquals(uK, entityK.getUri());
441: assertEquals(uE, entityE.getUri());
442: final OWLClassK resK = em.find(OWLClassK.class, uK);
443: assertNotNull(resK);
444: assertEquals(uE, resK.getOwlClassE().getUri());
445: final OWLClassE resE = em.find(OWLClassE.class, uE);
446: assertNotNull(resE);
447: assertEquals(uE, resE.getUri());
448: }
449: }