Skip to content

Method: testPersistEntityWithEnumAttribute()

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