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