Skip to content

Method: testUpdateDataPropertyInContext()

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
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: * <p>
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.*;
21: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
22: import cz.cvut.kbss.jopa.test.environment.Generators;
23: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
24: import cz.cvut.kbss.jopa.test.environment.TestEnvironmentUtils;
25: import org.junit.jupiter.api.Test;
26: import org.slf4j.Logger;
27:
28: import java.net.URI;
29: import java.util.*;
30:
31: import static org.junit.jupiter.api.Assertions.*;
32:
33:
34: public abstract class UpdateOperationsMultiContextRunner extends BaseRunner {
35:
36: public UpdateOperationsMultiContextRunner(Logger logger, PersistenceFactory persistenceFactory,
37: DataAccessor dataAccessor) {
38: super(logger, persistenceFactory, dataAccessor);
39: }
40:
41: @Test
42: void testUpdateDataPropertyInContext() {
43: this.em = getEntityManager("MultiUpdateDataPropertyInContext", false);
44: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
45: aDescriptor.addAttributeContext(fieldSpecification(OWLClassA.class, "stringAttribute"), CONTEXT_TWO);
46: transactional(() -> em.persist(entityA, aDescriptor));
47:
48: final String newAttValue = "newStringAttributeValue";
49: transactional(() -> {
50: final OWLClassA a = findRequired(OWLClassA.class, entityA.getUri(), aDescriptor);
51: a.setStringAttribute(newAttValue);
52: });
53:
54: final OWLClassA resA = findRequired(OWLClassA.class, entityA.getUri(), aDescriptor);
55: assertEquals(newAttValue, resA.getStringAttribute());
56: assertEquals(entityA.getTypes(), resA.getTypes());
57: }
58:
59: @Test
60: void testUpdateObjectPropertyToDifferentContext() {
61: this.em = getEntityManager("MultiUpdateObjectPropertyToDifferent", false);
62: final Descriptor dDescriptor = new EntityDescriptor();
63: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
64: dDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassD.class, "owlClassA"), aDescriptor);
65: transactional(() -> {
66: em.persist(entityD, dDescriptor);
67: em.persist(entityA, aDescriptor);
68: });
69:
70: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri(), dDescriptor);
71: assertNotNull(d.getOwlClassA());
72: em.getTransaction().begin();
73: final OWLClassA newA = new OWLClassA();
74: newA.setUri(URI.create("http://krizik.felk.cvut.cz/jopa/ontologies/newEntityA"));
75: newA.setStringAttribute("newAStringAttribute");
76: final Descriptor newADescriptor = new EntityDescriptor(CONTEXT_TWO);
77: em.persist(newA, newADescriptor);
78: dDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassD.class, "owlClassA"), newADescriptor);
79: d.setOwlClassA(newA);
80: em.getTransaction().commit();
81:
82: final OWLClassD resD = findRequired(OWLClassD.class, entityD.getUri(), dDescriptor);
83: assertEquals(newA.getUri(), resD.getOwlClassA().getUri());
84: assertEquals(newA.getStringAttribute(), resD.getOwlClassA().getStringAttribute());
85: final OWLClassA resA = findRequired(OWLClassA.class, entityA.getUri(), aDescriptor);
86: assertEquals(entityA.getStringAttribute(), resA.getStringAttribute());
87: }
88:
89: @Test
90: void testUpdateAddToPropertiesInContext() {
91: this.em = getEntityManager("MultiUpdateAddToPropertiesInContext", false);
92: entityB.setProperties(Generators.createProperties());
93: final Descriptor bDescriptor = new EntityDescriptor(CONTEXT_ONE);
94: bDescriptor.addAttributeContext(em.getMetamodel().entity(OWLClassB.class).getProperties(), CONTEXT_TWO);
95: transactional(() -> em.persist(entityB, bDescriptor));
96:
97: em.getTransaction().begin();
98: final OWLClassB b = findRequired(OWLClassB.class, entityB.getUri(), bDescriptor);
99: final String newKey = "http://krizik.felk.cvut.cz/jopa/ontologies/properties/newPropertyKey";
100: final String newValue = "http://krizik.felk.cvut.cz/jopa/ontologies/newPropertyValue";
101: final String newPropertyValue = "http://krizik.felk.cvut.cz/jopa/ontologies/NewValueOfAnOldProperty";
102: final String propertyToChange = b.getProperties().keySet().iterator().next();
103: b.getProperties().put(newKey, Collections.singleton(newValue));
104: b.getProperties().get(propertyToChange).add(newPropertyValue);
105: em.getTransaction().commit();
106:
107: final OWLClassB res = findRequired(OWLClassB.class, entityB.getUri(), bDescriptor);
108: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
109: assertTrue(TestEnvironmentUtils.arePropertiesEqual(b.getProperties(), res.getProperties()));
110: }
111:
112: @Test
113: void testUpdateAddToSimpleListInContext() {
114: this.em = getEntityManager("MultiUpdateAddToSimpleListInContext", false);
115: entityC.setSimpleList(Generators.createSimpleList(15));
116: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
117: final ObjectPropertyCollectionDescriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
118: fieldSpecification(OWLClassC.class, "simpleList"));
119: cDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassC.class, "simpleList"), lstDescriptor);
120: persistCWithLists(entityC, cDescriptor, lstDescriptor);
121:
122: em.getTransaction().begin();
123: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri(), cDescriptor);
124: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
125: c.getSimpleList().add(entityA);
126: em.persist(entityA, lstDescriptor);
127: em.getTransaction().commit();
128:
129: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri(), cDescriptor);
130: assertEquals(entityC.getSimpleList().size() + 1, resC.getSimpleList().size());
131: boolean found = false;
132: for (OWLClassA a : resC.getSimpleList()) {
133: if (a.getUri().equals(entityA.getUri())) {
134: assertEquals(entityA.getStringAttribute(), a.getStringAttribute());
135: assertEquals(entityA.getTypes(), a.getTypes());
136: found = true;
137: break;
138: }
139: }
140: assertTrue(found);
141: }
142:
143: private void persistCWithLists(OWLClassC instance, Descriptor cDescriptor,
144: ObjectPropertyCollectionDescriptor listDescriptor) {
145: transactional(() -> {
146: em.persist(instance, cDescriptor);
147: if (instance.getSimpleList() != null) {
148: instance.getSimpleList().forEach(a -> em.persist(a, listDescriptor.getElementDescriptor()));
149: }
150: if (instance.getReferencedList() != null) {
151: instance.getReferencedList().forEach(a -> em.persist(a, listDescriptor.getElementDescriptor()));
152: }
153: });
154: }
155:
156: @Test
157: void testUpdateAddToReferencedListInContext() {
158: this.em = getEntityManager("MultiUpdateAddToReferencedListInContext", false);
159: entityC.setReferencedList(Generators.createReferencedList(10));
160: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
161: final ObjectPropertyCollectionDescriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
162: fieldSpecification(OWLClassC.class, "referencedList"));
163: cDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassC.class, "referencedList"), lstDescriptor);
164: persistCWithLists(entityC, cDescriptor, lstDescriptor);
165:
166: em.getTransaction().begin();
167: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri(), cDescriptor);
168: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
169: c.getReferencedList().add(entityA);
170: em.persist(entityA, lstDescriptor);
171: em.getTransaction().commit();
172:
173: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri(), cDescriptor);
174: assertEquals(entityC.getReferencedList().size() + 1, resC.getReferencedList().size());
175: boolean found = false;
176: for (OWLClassA a : resC.getReferencedList()) {
177: if (a.getUri().equals(entityA.getUri())) {
178: assertEquals(entityA.getStringAttribute(), a.getStringAttribute());
179: assertEquals(entityA.getTypes(), a.getTypes());
180: found = true;
181: break;
182: }
183: }
184: assertTrue(found);
185: }
186:
187: @Test
188: void testUpdateRemoveFromSimpleListInContext() {
189: this.em = getEntityManager("MultiUpdateRemoveFromSimpleListInContext", false);
190: entityC.setSimpleList(Generators.createSimpleList(15));
191: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
192: final ObjectPropertyCollectionDescriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
193: fieldSpecification(OWLClassC.class, "simpleList"));
194: cDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassC.class, "simpleList"), lstDescriptor);
195: persistCWithLists(entityC, cDescriptor, lstDescriptor);
196:
197: em.getTransaction().begin();
198: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri(), cDescriptor);
199: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
200: final OWLClassA a = c.getSimpleList().get(0);
201: c.getSimpleList().remove(0);
202: em.remove(a);
203: em.getTransaction().commit();
204:
205: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri(), cDescriptor);
206: assertEquals(entityC.getSimpleList().size() - 1, resC.getSimpleList().size());
207: assertNull(em.find(OWLClassA.class, a.getUri(), lstDescriptor));
208: }
209:
210: @Test
211: void testUpdateRemoveFromReferencedListInContext() {
212: this.em = getEntityManager("MultiUpdateRemoveFromReferencedListInContext", false);
213: entityC.setReferencedList(Generators.createReferencedList(10));
214: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
215: final ObjectPropertyCollectionDescriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
216: fieldSpecification(OWLClassC.class, "referencedList"));
217: cDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassC.class, "referencedList"), lstDescriptor);
218: persistCWithLists(entityC, cDescriptor, lstDescriptor);
219:
220: em.getTransaction().begin();
221: final OWLClassC c = findRequired(OWLClassC.class, entityC.getUri(), cDescriptor);
222: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
223: final List<OWLClassA> removed = new ArrayList<>();
224: int i = 0;
225: final Iterator<OWLClassA> it = c.getReferencedList().iterator();
226: while (it.hasNext()) {
227: i++;
228: final OWLClassA a = it.next();
229: if (i % 2 == 1) {
230: continue;
231: }
232: removed.add(a);
233: it.remove();
234: }
235: em.getTransaction().commit();
236:
237: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri(), cDescriptor);
238: assertEquals(entityC.getReferencedList().size() - removed.size(), resC.getReferencedList()
239: .size());
240: for (OWLClassA a : removed) {
241: final OWLClassA resA = em.find(OWLClassA.class, a.getUri(), lstDescriptor);
242: assertNotNull(resA);
243: }
244: }
245:
246: @Test
247: void testUpdatePlainIdentifierObjectPropertyValueInContext() {
248: final Descriptor pDescriptor = new EntityDescriptor(CONTEXT_ONE);
249: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/originalIndividual"));
250: this.em = getEntityManager("UpdatePlainIdentifierObjectPropertyValueInContext", true);
251: transactional(() -> em.persist(entityP, pDescriptor));
252:
253: final OWLClassP toUpdate = findRequired(OWLClassP.class, entityP.getUri());
254: em.detach(toUpdate);
255: final URI newUri = URI.create("http://krizik.felk.cvut.cz/newIndividual");
256: toUpdate.setIndividualUri(newUri);
257: transactional(() -> em.merge(toUpdate));
258:
259: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
260: assertEquals(newUri, res.getIndividualUri());
261: }
262:
263: @Test
264: void testUpdateFieldInMappedSuperclassInContext() {
265: this.em = getEntityManager("UpdateFieldInMappedSuperclassInContext", true);
266: final Descriptor qDescriptor = new EntityDescriptor(CONTEXT_ONE);
267: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
268: qDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassQ.class, "owlClassA"), aDescriptor);
269: em.getTransaction().begin();
270: em.persist(entityQ, qDescriptor);
271: em.persist(entityA, aDescriptor);
272: em.getTransaction().commit();
273:
274: entityQ.setStringAttribute("newStringAttribute");
275: entityQ.setParentString("newParentStringAttribute");
276: entityQ.setLabel("newLabel");
277: final OWLClassA newA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA2"));
278: newA.setStringAttribute("newAString");
279: entityQ.setOwlClassA(newA);
280: em.getTransaction().begin();
281: em.merge(entityQ, qDescriptor);
282: em.persist(newA, aDescriptor);
283: em.getTransaction().commit();
284:
285: final OWLClassQ res = findRequired(OWLClassQ.class, entityQ.getUri(), qDescriptor);
286: assertEquals(entityQ.getStringAttribute(), res.getStringAttribute());
287: assertEquals(entityQ.getParentString(), res.getParentString());
288: assertEquals(entityQ.getLabel(), res.getLabel());
289: assertNotNull(res.getOwlClassA());
290: assertEquals(newA.getUri(), res.getOwlClassA().getUri());
291: assertNotNull(em.find(OWLClassA.class, newA.getUri()));
292: assertNotNull(em.find(OWLClassA.class, entityA.getUri(), aDescriptor));
293: }
294:
295: @Test
296: void testUpdateObjectPropertyInContextWithAssertionInContext() {
297: this.em = getEntityManager("testUpdateObjectPropertyInContextWithAssertionInContext", true);
298: final Descriptor dDescriptor = new EntityDescriptor(CONTEXT_ONE, false);
299: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
300: dDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassD.class, "owlClassA"), aDescriptor);
301: transactional(() -> {
302: em.persist(entityA, aDescriptor);
303: em.persist(entityD, dDescriptor);
304: });
305:
306: final OWLClassA newA = new OWLClassA(Generators.generateUri());
307: newA.setStringAttribute("newString");
308: transactional(() -> {
309: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri(), dDescriptor);
310: d.setOwlClassA(newA);
311: em.persist(newA, aDescriptor);
312: });
313:
314: assertTrue(em.createNativeQuery("ASK { GRAPH ?g { ?s ?p ?o. }}", Boolean.class).setParameter("g", CONTEXT_TWO)
315: .setParameter("s", entityD.getUri()).setParameter("p", URI.create(Vocabulary.P_HAS_OWL_CLASS_A))
316: .setParameter("o", newA.getUri()).getSingleResult());
317: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri(), dDescriptor);
318: assertNotNull(result.getOwlClassA());
319: assertEquals(newA.getUri(), result.getOwlClassA().getUri());
320: }
321:
322: @Test
323: void updateSupportsObjectPropertyWithMultipleContexts() {
324: this.em = getEntityManager("updateSupportsObjectPropertyWithMultipleContexts", true);
325: final OWLClassF entityF = new OWLClassF(Generators.generateUri());
326: final OWLClassA entityA2 = new OWLClassA(Generators.generateUri(), "another a");
327: transactional(() -> {
328: em.persist(entityF);
329: em.persist(entityA, new EntityDescriptor(CONTEXT_ONE));
330: em.persist(entityA2, new EntityDescriptor(CONTEXT_TWO));
331: });
332:
333: entityF.setSimpleSet(new HashSet<>(Arrays.asList(entityA, entityA2)));
334: final Descriptor descriptor = new EntityDescriptor();
335: descriptor.addAttributeContext(fieldSpecification(OWLClassF.class, "simpleSet"), CONTEXT_ONE)
336: .addAttributeContext(fieldSpecification(OWLClassF.class, "simpleSet"), CONTEXT_TWO);
337: transactional(() -> em.merge(entityF, descriptor));
338:
339: final OWLClassF result = findRequired(OWLClassF.class, entityF.getUri(), descriptor);
340: assertEquals(2, result.getSimpleSet().size());
341: assertTrue(result.getSimpleSet().stream().anyMatch(a -> a.getUri().equals(entityA.getUri())));
342: assertTrue(result.getSimpleSet().stream().anyMatch(a -> a.getUri().equals(entityA2.getUri())));
343: }
344:
345: @Test
346: void updateWithinTransactionSupportsObjectPropertyWithMultipleContexts() {
347: this.em = getEntityManager("updateSupportsObjectPropertyWithMultipleContexts", true);
348: final OWLClassF entityF = new OWLClassF(Generators.generateUri());
349: final OWLClassA entityA2 = new OWLClassA(Generators.generateUri(), "another a");
350: transactional(() -> em.persist(entityF));
351:
352: final Descriptor descriptor = new EntityDescriptor();
353: descriptor.addAttributeContext(fieldSpecification(OWLClassF.class, "simpleSet"), CONTEXT_ONE)
354: .addAttributeContext(fieldSpecification(OWLClassF.class, "simpleSet"), CONTEXT_TWO);
355: transactional(() -> {
356: final OWLClassF toUpdate = findRequired(OWLClassF.class, entityF.getUri(), descriptor);
357: toUpdate.setSimpleSet(new HashSet<>(Arrays.asList(entityA, entityA2)));
358: em.persist(entityA, new EntityDescriptor(CONTEXT_ONE));
359: em.persist(entityA2, new EntityDescriptor(CONTEXT_TWO));
360: });
361:
362: final OWLClassF result = findRequired(OWLClassF.class, entityF.getUri(), descriptor);
363: assertEquals(2, result.getSimpleSet().size());
364: assertTrue(result.getSimpleSet().stream().anyMatch(a -> a.getUri().equals(entityA.getUri())));
365: assertTrue(result.getSimpleSet().stream().anyMatch(a -> a.getUri().equals(entityA2.getUri())));
366: }
367: }