Skip to content

Method: testPersistReferencedList()

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.util.Collections;
25: import java.util.HashMap;
26: import java.util.Map;
27: import java.util.Set;
28:
29: import static org.junit.Assert.*;
30:
31: public abstract class CreateOperationsRunner extends BaseRunner {
32:
33: protected CreateOperationsRunner(Logger logger) {
34: super(logger);
35: }
36:
37: @Test
38: public void testPersistWithGenerated() {
39: logger.debug("Test: persist into all contexts, also with generated id.");
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 testPersistWithoutId() {
57: logger.debug("Test: persist without id. No ID generation specified.");
58: this.em = getEntityManager("PersistWithoutId", false);
59: final OWLClassB b = new OWLClassB();
60: b.setStringAttribute("someValue");
61: persist(b);
62: }
63:
64: @Test(expected = NullPointerException.class)
65: public void testPersistNull() {
66: logger.debug("Test: persist null.");
67: this.em = getEntityManager("PersistNull", false);
68: em.getTransaction().begin();
69: em.persist(null);
70: }
71:
72: @Test
73: public void testPersistRollback() {
74: logger.debug("Test: persist and then rollback the transaction.");
75: this.em = getEntityManager("PersistRollback", false);
76: em.getTransaction().begin();
77: em.persist(entityE);
78: assertTrue(em.contains(entityE));
79: em.getTransaction().rollback();
80:
81: assertFalse(em.contains(entityE));
82: assertNull(em.find(entityE.getClass(), entityE.getUri()));
83: }
84:
85: @Test(expected = RollbackException.class)
86: public void testPersistRollbackOnly() {
87: logger.debug("Test: set transaction as rollback only and the try persisting an entity.");
88: this.em = getEntityManager("PersistRollbackOnly", false);
89: em.getTransaction().begin();
90: em.getTransaction().setRollbackOnly();
91: em.persist(entityE);
92: em.getTransaction().commit();
93: }
94:
95: @Test
96: public void testPersistCascade() {
97: logger.debug("Test: persist with cascade over two relationships.");
98: this.em = getEntityManager("PersistWithCascade", false);
99: persist(entityG);
100:
101: final OWLClassA resA2 = em.find(OWLClassA.class, entityA.getUri());
102: assertNotNull(resA2);
103: final OWLClassH resH = em.find(OWLClassH.class, entityH.getUri());
104: assertNotNull(resH);
105: assertEquals(resH.getOwlClassA(), resA2);
106: final OWLClassG resG = em.find(OWLClassG.class, entityG.getUri());
107: assertNotNull(resG);
108: assertEquals(resG.getOwlClassH(), resH);
109: assertEquals(resG.getOwlClassH().getOwlClassA(), resA2);
110: }
111:
112: @Test(expected = OWLEntityExistsException.class)
113: public void testPersistTwiceInOne() {
114: logger.debug("Test: persist twice into one context.");
115: this.em = getEntityManager("PersistTwice", false);
116: persist(entityB, entityB);
117: }
118:
119: @Test(expected = RollbackException.class)
120: public void testPersistWithoutCascade() {
121: logger.debug("Test: try persisting relationship not marked as cascade.");
122: this.em = getEntityManager("PersistWithoutCascade", false);
123: persist(entityD);
124: }
125:
126: @Test(expected = OWLEntityExistsException.class)
127: public void testPersistDetached() {
128: logger.debug("Test: persist detached entity. Should throw entity exists exception.");
129: this.em = getEntityManager("PersistDetached", false);
130: persist(entityA);
131:
132: final OWLClassA det = em.find(OWLClassA.class, entityA.getUri());
133: assertNotNull(det);
134: em.detach(det);
135: em.persist(det);
136: }
137:
138: @Test
139: public void testPersistSimpleList() {
140: logger.debug("Test: persist entity with simple list.");
141: this.em = getEntityManager("PersistSimpleList", false);
142: entityC.setSimpleList(Generators.createSimpleList(10));
143: em.getTransaction().begin();
144: em.persist(entityC);
145: entityC.getSimpleList().forEach(em::persist);
146: em.getTransaction().commit();
147:
148: final OWLClassA a = em.find(OWLClassA.class, entityC.getSimpleList().get(1).getUri());
149: assertNotNull(a);
150: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
151: assertNotNull(c);
152: assertNotNull(c.getSimpleList());
153: assertFalse(c.getSimpleList().isEmpty());
154: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
155: assertTrue(c.getSimpleList().contains(a));
156: }
157:
158: @Test(expected = RollbackException.class)
159: public void testPersistSimpleListNoCascade() {
160: logger.debug("Test: persist entity with simple list, but don't persist the referenced entities.");
161: this.em = getEntityManager("PersistSimpleListNoCascade", false);
162: entityC.setSimpleList(Generators.createSimpleList(10));
163: persist(entityC);
164: }
165:
166: @Test
167: public void testPersistReferencedList() {
168: logger.debug("Test: persist entity with referenced list.");
169: this.em = getEntityManager("PersistReferencedList", false);
170: entityC.setReferencedList(Generators.createReferencedList(5));
171: em.getTransaction().begin();
172: em.persist(entityC);
173: entityC.getReferencedList().forEach(em::persist);
174: assertTrue(em.contains(entityC));
175: assertTrue(em.contains(entityC.getReferencedList().get(0)));
176: em.getTransaction().commit();
177:
178: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
179: assertNotNull(c);
180: assertNotNull(c.getReferencedList());
181: assertFalse(c.getReferencedList().isEmpty());
182: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
183:• for (OWLClassA a : entityC.getReferencedList()) {
184: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
185: assertNotNull(resA);
186: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
187: assertTrue(c.getReferencedList().contains(resA));
188: }
189: }
190:
191: @Test(expected = RollbackException.class)
192: public void testPersistReferencedListNoCascade() {
193: logger.debug("Test: persist entity with referenced list. Don't persist the referenced entities.");
194: this.em = getEntityManager("PersistReferencedListNoCascade", false);
195: entityC.setReferencedList(Generators.createReferencedList(5));
196: persist(entityC);
197: }
198:
199: @Test
200: public void testPersistSimpleAndReferencedList() {
201: logger.debug("Test: persist entity with both simple and referenced list.");
202: this.em = getEntityManager("PersistSimpleAndReferencedList", false);
203: entityC.setReferencedList(Generators.createReferencedList(5));
204: entityC.setSimpleList(Generators.createSimpleList(5));
205: em.getTransaction().begin();
206: em.persist(entityC);
207: entityC.getSimpleList().forEach(em::persist);
208: entityC.getReferencedList().forEach(em::persist);
209: em.getTransaction().commit();
210:
211: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
212: assertNotNull(c);
213: assertNotNull(c.getSimpleList());
214: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
215: assertNotNull(c.getReferencedList());
216: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
217: for (OWLClassA a : entityC.getSimpleList()) {
218: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
219: assertNotNull(resA);
220: assertTrue(c.getSimpleList().contains(resA));
221: }
222: for (OWLClassA a : entityC.getReferencedList()) {
223: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
224: assertNotNull(resA);
225: assertTrue(c.getReferencedList().contains(resA));
226: }
227: }
228:
229: @Test
230: public void testPersistProperties() {
231: logger.debug("Test: persist entity with properties.");
232: this.em = getEntityManager("PersistWithProperties", false);
233: final Map<String, Set<String>> props = new HashMap<>(3);
234: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyOne", Collections
235: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Individual10"));
236: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyTwo", Collections
237: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/SomeEntity"));
238: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyThree",
239: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityG"));
240: final Map<String, Set<String>> expected = new HashMap<>(4);
241: expected.putAll(props);
242: entityB.setProperties(props);
243: persist(entityB);
244: em.clear();
245:
246: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
247: assertNotNull(res);
248: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
249: assertNotNull(res.getProperties());
250: assertFalse(res.getProperties().isEmpty());
251: assertEquals(expected.size(), res.getProperties().size());
252: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
253: assertTrue(res.getProperties().containsKey(e.getKey()));
254: final Set<String> s = e.getValue();
255: final Set<String> resS = res.getProperties().get(e.getKey());
256: assertNotNull(resS);
257: assertEquals(1, resS.size());
258: assertEquals(s.iterator().next(), resS.iterator().next());
259: }
260: }
261:
262: @Test
263: public void testPersistPropertiesEmpty() {
264: logger.debug("Test: persist entity with properties. The properties will be an empty map.");
265: this.em = getEntityManager("PersistWithPropertiesEmpty", false);
266: entityB.setProperties(Collections.emptyMap());
267: em.getTransaction().begin();
268: em.persist(entityB);
269: assertTrue(em.contains(entityB));
270: em.getTransaction().commit();
271: em.clear();
272:
273: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
274: assertNotNull(b);
275: assertEquals(entityB.getUri(), b.getUri());
276: assertEquals(entityB.getStringAttribute(), b.getStringAttribute());
277: assertNull(b.getProperties());
278: }
279:
280: @Test(expected = OWLEntityExistsException.class)
281: public void persistURITwiceInDifferentClasses() {
282: logger.debug("Test: persist two different entities (of different types) with the same URI.");
283: this.em = getEntityManager("PersistURITwiceInDifferentClasses", false);
284: final URI pk = URI.create("http://krizik.felk.cvut.cz/jopa/onto/sameEntity");
285: final OWLClassA a = new OWLClassA();
286: a.setUri(pk);
287: final OWLClassB b = new OWLClassB();
288: b.setUri(pk);
289: em.getTransaction().begin();
290: em.persist(a);
291: em.persist(b);
292: em.getTransaction().commit();
293: }
294:
295: @Test
296: public void testPersistEntityWithBasicTypeAttributes() {
297: logger.debug("Test: persist entity with attributes of basic types (Integer, Boolean etc.).");
298: this.em = getEntityManager("PersistEntityWithBasicTypeAttributes", false);
299: persist(entityM);
300: em.clear();
301:
302: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
303: assertNotNull(res);
304: assertEquals(entityM.getKey(), res.getKey());
305: assertEquals(entityM.getBooleanAttribute(), res.getBooleanAttribute());
306: assertEquals(entityM.getIntAttribute(), res.getIntAttribute());
307: assertEquals(entityM.getLongAttribute(), res.getLongAttribute());
308: assertEquals(entityM.getDoubleAttribute(), res.getDoubleAttribute());
309: assertEquals(entityM.getDateAttribute(), res.getDateAttribute());
310: }
311:
312: @Test
313: public void testPersistAndUpdateAttributeBeforeCommit() {
314: logger.debug("Test: persist entity, set attribute value and then commit.");
315: this.em = getEntityManager("PersistAndUpdateBeforeCommit", false);
316: final String updatedValue = "updatedStringAttributeValue";
317: em.getTransaction().begin();
318: em.persist(entityA);
319: entityA.setStringAttribute(updatedValue);
320: em.getTransaction().commit();
321: em.clear();
322:
323: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
324: assertNotNull(res);
325: assertEquals(updatedValue, res.getStringAttribute());
326: }
327:
328: @Test
329: public void testPersistEntityWithEnumAttribute() {
330: logger.debug("Test: persist entity with enum attribute.");
331: this.em = getEntityManager("PersistEntityWithEnum", false);
332: persist(entityM);
333:
334: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
335: assertNotNull(res);
336: assertEquals(entityM.getEnumAttribute(), res.getEnumAttribute());
337: }
338:
339: @Test
340: public void testPersistTypedProperties() {
341: logger.debug("Test: persist entity with typed properties.");
342: this.em = getEntityManager("PersistTypedProperties", false);
343: entityP.setProperties(Generators.createTypedProperties());
344: em.getTransaction().begin();
345: em.persist(entityP);
346: em.getTransaction().commit();
347: em.clear();
348:
349: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
350: assertNotNull(res);
351: assertEquals(entityP.getProperties(), res.getProperties());
352: }
353: }