Skip to content

Method: testUpdateRemoveFromSimpleListInContext()

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.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
19: import cz.cvut.kbss.jopa.model.descriptors.ObjectPropertyCollectionDescriptor;
20: import cz.cvut.kbss.jopa.test.OWLClassA;
21: import cz.cvut.kbss.jopa.test.OWLClassB;
22: import cz.cvut.kbss.jopa.test.OWLClassC;
23: import cz.cvut.kbss.jopa.test.OWLClassD;
24: import cz.cvut.kbss.jopa.test.environment.Generators;
25: import cz.cvut.kbss.jopa.test.environment.TestEnvironmentUtils;
26: import org.junit.Test;
27: import org.slf4j.Logger;
28:
29: import java.net.URI;
30: import java.util.ArrayList;
31: import java.util.Collections;
32: import java.util.Iterator;
33: import java.util.List;
34:
35: import static org.junit.Assert.*;
36:
37: public abstract class UpdateOperationsMultiContextRunner extends BaseRunner {
38:
39: public UpdateOperationsMultiContextRunner(Logger logger) {
40: super(logger);
41: }
42:
43: @Test
44: public void testUpdateDataPropertyInContext() throws Exception {
45: logger.debug("Test: update data property value which is stored in a different context that the owner.");
46: this.em = getEntityManager("MultiUpdateDataPropertyInContext", false);
47: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
48: aDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_TWO);
49: em.getTransaction().begin();
50: em.persist(entityA, aDescriptor);
51: em.getTransaction().commit();
52:
53: em.getTransaction().begin();
54: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
55: assertNotNull(a);
56: final String newAttValue = "newStringAttributeValue";
57: a.setStringAttribute(newAttValue);
58: em.getTransaction().commit();
59:
60: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
61: assertNotNull(resA);
62: assertEquals(newAttValue, resA.getStringAttribute());
63: assertEquals(entityA.getTypes(), resA.getTypes());
64: }
65:
66: @Test
67: public void testUpdateObjectPropertyToDifferentContext() throws Exception {
68: logger.debug("Test: update object property with value from different context than the previous.");
69: this.em = getEntityManager("MultiUpdateObjectPropertyToDifferent", false);
70: final Descriptor dDescriptor = new EntityDescriptor();
71: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
72: dDescriptor.addAttributeDescriptor(OWLClassD.class.getDeclaredField("owlClassA"), aDescriptor);
73: em.getTransaction().begin();
74: em.persist(entityD, dDescriptor);
75: em.persist(entityA, aDescriptor);
76: em.getTransaction().commit();
77:
78: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
79: assertNotNull(d);
80: assertNotNull(d.getOwlClassA());
81: em.getTransaction().begin();
82: final OWLClassA newA = new OWLClassA();
83: newA.setUri(URI.create("http://krizik.felk.cvut.cz/jopa/ontologies/newEntityA"));
84: newA.setStringAttribute("newAStringAttribute");
85: final Descriptor newADescriptor = new EntityDescriptor(CONTEXT_TWO);
86: em.persist(newA, newADescriptor);
87: dDescriptor.addAttributeDescriptor(OWLClassD.class.getDeclaredField("owlClassA"), newADescriptor);
88: d.setOwlClassA(newA);
89: em.getTransaction().commit();
90:
91: final OWLClassD resD = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
92: assertNotNull(resD);
93: assertEquals(newA.getUri(), resD.getOwlClassA().getUri());
94: assertEquals(newA.getStringAttribute(), resD.getOwlClassA().getStringAttribute());
95: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
96: assertNotNull(resA);
97: assertEquals(entityA.getStringAttribute(), resA.getStringAttribute());
98: }
99:
100: @Test
101: public void testUpdateAddToPropertiesInContext() throws Exception {
102: logger.debug("Test: add new property value, properties are stored in a different context.");
103: this.em = getEntityManager("MultiUpdateAddToPropertiesInContext", false);
104: entityB.setProperties(Generators.createProperties());
105: final Descriptor bDescriptor = new EntityDescriptor(CONTEXT_ONE);
106: bDescriptor.addAttributeContext(OWLClassB.class.getDeclaredField("properties"), CONTEXT_TWO);
107: em.getTransaction().begin();
108: em.persist(entityB, bDescriptor);
109: em.getTransaction().commit();
110:
111: em.getTransaction().begin();
112: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri(), bDescriptor);
113: assertNotNull(b);
114: final String newKey = "http://krizik.felk.cvut.cz/jopa/ontologies/properties/newPropertyKey";
115: final String newValue = "http://krizik.felk.cvut.cz/jopa/ontologies/newPropertyValue";
116: final String newPropertyValue = "http://krizik.felk.cvut.cz/jopa/ontologies/NewValueOfAnOldProperty";
117: final String propertyToChange = b.getProperties().keySet().iterator().next();
118: b.getProperties().put(newKey, Collections.singleton(newValue));
119: b.getProperties().get(propertyToChange).add(newPropertyValue);
120: em.getTransaction().commit();
121:
122: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri(), bDescriptor);
123: assertNotNull(res);
124: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
125: assertTrue(TestEnvironmentUtils.arePropertiesEqual(b.getProperties(), res.getProperties()));
126: }
127:
128: @Test
129: public void testUpdateAddToSimpleListInContext() throws Exception {
130: logger.debug("Test: add new element into a simple list stored in different context than its owner.");
131: this.em = getEntityManager("MultiUpdateAddToSimpleListInContext", false);
132: entityC.setSimpleList(Generators.createSimpleList(15));
133: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
134: final Descriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
135: OWLClassC.getSimpleListField());
136: cDescriptor.addAttributeDescriptor(OWLClassC.getSimpleListField(), lstDescriptor);
137: em.getTransaction().begin();
138: em.persist(entityC, cDescriptor);
139: entityC.getSimpleList().forEach(a -> em.persist(a, lstDescriptor));
140: em.getTransaction().commit();
141:
142: em.getTransaction().begin();
143: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
144: assertNotNull(c);
145: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
146: c.getSimpleList().add(entityA);
147: em.persist(entityA, lstDescriptor);
148: em.getTransaction().commit();
149:
150: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
151: assertNotNull(resC);
152: assertEquals(entityC.getSimpleList().size() + 1, resC.getSimpleList().size());
153: boolean found = false;
154: for (OWLClassA a : resC.getSimpleList()) {
155: if (a.getUri().equals(entityA.getUri())) {
156: assertEquals(entityA.getStringAttribute(), a.getStringAttribute());
157: assertEquals(entityA.getTypes(), a.getTypes());
158: found = true;
159: break;
160: }
161: }
162: assertTrue(found);
163: }
164:
165: @Test
166: public void testUpdateAddToReferencedListInContext() throws Exception {
167: logger.debug("Test: add new element into a referenced list stored in different context than its owner.");
168: this.em = getEntityManager("MultiUpdateAddToReferencedListInContext", false);
169: entityC.setReferencedList(Generators.createReferencedList(10));
170: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
171: final Descriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
172: OWLClassC.getReferencedListField());
173: cDescriptor.addAttributeDescriptor(OWLClassC.getReferencedListField(), lstDescriptor);
174: em.getTransaction().begin();
175: em.persist(entityC, cDescriptor);
176: entityC.getReferencedList().forEach(a -> em.persist(a, lstDescriptor));
177: em.getTransaction().commit();
178:
179: em.getTransaction().begin();
180: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
181: assertNotNull(c);
182: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
183: c.getReferencedList().add(entityA);
184: em.persist(entityA, lstDescriptor);
185: em.getTransaction().commit();
186:
187: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
188: assertNotNull(resC);
189: assertEquals(entityC.getReferencedList().size() + 1, resC.getReferencedList().size());
190: boolean found = false;
191: for (OWLClassA a : resC.getReferencedList()) {
192: if (a.getUri().equals(entityA.getUri())) {
193: assertEquals(entityA.getStringAttribute(), a.getStringAttribute());
194: assertEquals(entityA.getTypes(), a.getTypes());
195: found = true;
196: break;
197: }
198: }
199: assertTrue(found);
200: }
201:
202: @Test
203: public void testUpdateRemoveFromSimpleListInContext() throws Exception {
204: logger.debug("Test: remove element from simple list stored in a different context than its owner.");
205: this.em = getEntityManager("MultiUpdateRemoveFromSimpleListInContext", false);
206: entityC.setSimpleList(Generators.createSimpleList(15));
207: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
208: final Descriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
209: OWLClassC.getSimpleListField());
210: cDescriptor.addAttributeDescriptor(OWLClassC.getSimpleListField(), lstDescriptor);
211: em.getTransaction().begin();
212: em.persist(entityC, cDescriptor);
213: entityC.getSimpleList().forEach(a -> em.persist(a, lstDescriptor));
214: em.getTransaction().commit();
215:
216: em.getTransaction().begin();
217: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
218: assertNotNull(c);
219: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
220: final OWLClassA a = c.getSimpleList().get(0);
221: c.getSimpleList().remove(0);
222: em.remove(a);
223: em.getTransaction().commit();
224:
225: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
226: assertNotNull(resC);
227: assertEquals(entityC.getSimpleList().size() - 1, resC.getSimpleList().size());
228: assertNull(em.find(OWLClassA.class, a.getUri(), lstDescriptor));
229: }
230:
231: @Test
232: public void testUpdateRemoveFromReferencedListInContext() throws Exception {
233: logger.debug("Test: remove elements from referenced list stored in a different context than its owner.");
234: this.em = getEntityManager("MultiUpdateRemoveFromReferencedListInContext", false);
235: entityC.setReferencedList(Generators.createReferencedList(10));
236: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
237: final Descriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
238: OWLClassC.getReferencedListField());
239: cDescriptor.addAttributeDescriptor(OWLClassC.getReferencedListField(), lstDescriptor);
240: em.getTransaction().begin();
241: em.persist(entityC, cDescriptor);
242: entityC.getReferencedList().forEach(a -> em.persist(a, lstDescriptor));
243: em.getTransaction().commit();
244:
245: em.getTransaction().begin();
246: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
247: assertNotNull(c);
248: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
249: final List<OWLClassA> removed = new ArrayList<>();
250: int i = 0;
251: final Iterator<OWLClassA> it = c.getReferencedList().iterator();
252: while (it.hasNext()) {
253: i++;
254: final OWLClassA a = it.next();
255: if (i % 2 == 1) {
256: continue;
257: }
258: removed.add(a);
259: it.remove();
260: }
261: em.getTransaction().commit();
262:
263: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
264: assertNotNull(resC);
265: assertEquals(entityC.getReferencedList().size() - removed.size(), resC.getReferencedList()
266: .size());
267: for (OWLClassA a : removed) {
268: final OWLClassA resA = em.find(OWLClassA.class, a.getUri(), lstDescriptor);
269: assertNotNull(resA);
270: }
271: }
272: }