Skip to contentMethod: testPersistPropertiesIntoDifferentContext()
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.exceptions.OWLEntityExistsException;
21: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
22: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
23: import cz.cvut.kbss.jopa.model.descriptors.ObjectPropertyCollectionDescriptor;
24: import cz.cvut.kbss.jopa.model.query.TypedQuery;
25: import cz.cvut.kbss.jopa.test.*;
26: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
27: import cz.cvut.kbss.jopa.test.environment.Generators;
28: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
29: import cz.cvut.kbss.jopa.test.environment.Quad;
30: import org.junit.jupiter.api.Test;
31: import org.slf4j.Logger;
32:
33: import java.net.URI;
34: import java.util.*;
35:
36: import static org.junit.jupiter.api.Assertions.*;
37:
38: public abstract class CreateOperationsMultiContextRunner extends BaseRunner {
39:
40: private OWLClassF entityF;
41: private OWLClassK entityK;
42:
43: private final EntityDescriptor cOneDescriptor = new EntityDescriptor(CONTEXT_ONE);
44: private final EntityDescriptor cTwoDescriptor = new EntityDescriptor(CONTEXT_TWO);
45:
46: public CreateOperationsMultiContextRunner(Logger logger, PersistenceFactory persistenceFactory,
47: DataAccessor dataAccessor) {
48: super(logger, persistenceFactory, dataAccessor);
49: initialize();
50: }
51:
52: private void initialize() {
53: this.entityF = new OWLClassF();
54: entityF.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityF"));
55: this.entityK = new OWLClassK();
56: entityK.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityK"));
57: }
58:
59: @Test
60: void testPersistDataPropertyIntoContext() {
61: this.em = getEntityManager("MultiPersistDataPropertyIntoContext", false);
62: final Descriptor aDescriptor = new EntityDescriptor();
63: aDescriptor.addAttributeContext(OWLClassA_.stringAttribute, CONTEXT_ONE);
64: transactional(() -> em.persist(entityA, aDescriptor));
65:
66: final OWLClassA res = findRequired(OWLClassA.class, entityA.getUri(), aDescriptor);
67: assertEquals(entityA.getUri(), res.getUri());
68: assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
69: assertEquals(entityA.getTypes().size(), res.getTypes().size());
70: assertTrue(entityA.getTypes().containsAll(res.getTypes()));
71: }
72:
73: @Test
74: void testPersistObjectPropertyIntoContext() {
75: this.em = getEntityManager("MultiPersistObjectPropertyIntoContext", false);
76: final Descriptor dDescriptor = new EntityDescriptor(false);
77: dDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassD.class, "owlClassA"), cOneDescriptor);
78: transactional(() -> {
79: em.persist(entityD, dDescriptor);
80: em.persist(entityA, cOneDescriptor);
81: });
82:
83: final OWLClassD resD = findRequired(OWLClassD.class, entityD.getUri(), dDescriptor);
84: assertNotNull(resD.getOwlClassA());
85: final OWLClassA resA = findRequired(OWLClassA.class, entityA.getUri(), cOneDescriptor);
86: assertEquals(resD.getOwlClassA().getUri(), resA.getUri());
87: assertEquals(resD.getOwlClassA().getStringAttribute(), resA.getStringAttribute());
88: assertTrue(resD.getOwlClassA().getTypes().containsAll(resA.getTypes()));
89: }
90:
91: @Test
92: void persistWithObjectPropertySavesAssertionIntoSubjectContextByDefault() {
93: this.em = getEntityManager("persistWithObjectPropertySavesAssertionIntoSubjectContextByDefault", false);
94: cOneDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassD.class, "owlClassA"), cTwoDescriptor);
95: transactional(() -> {
96: em.persist(entityD, cOneDescriptor);
97: em.persist(entityA, cTwoDescriptor);
98: });
99:
100: final TypedQuery<Boolean> query = em.createNativeQuery("ASK {GRAPH ?g {?d ?hasA ?a .}}", Boolean.class)
101: .setParameter("g", CONTEXT_ONE).setParameter("d", entityD.getUri())
102: .setParameter("hasA", URI.create(Vocabulary.P_HAS_OWL_CLASS_A))
103: .setParameter("a", entityA.getUri());
104: assertTrue(query.getSingleResult());
105: query.setParameter("g", CONTEXT_TWO);
106: assertFalse(query.getSingleResult());
107: }
108:
109: @Test
110: void persistWithPluralObjectPropertySavesAssertionIntoSubjectContextByDefault() {
111: this.em = getEntityManager("persistWithObjectPropertySavesAssertionIntoSubjectContextByDefault", false);
112: final ObjectPropertyCollectionDescriptor opDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
113: fieldSpecification(OWLClassF.class, "simpleSet"));
114: entityF.setSimpleSet(Collections.singleton(entityA));
115: transactional(() -> em.persist(entityA, cTwoDescriptor));
116:
117: cOneDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassF.class, "simpleSet"), opDescriptor);
118: transactional(() -> em.persist(entityF, cOneDescriptor));
119:
120: final OWLClassF result = findRequired(OWLClassF.class, entityF.getUri());
121: assertEquals(1, result.getSimpleSet().size());
122: assertEquals(entityA.getUri(), result.getSimpleSet().iterator().next().getUri());
123:
124: final TypedQuery<Boolean> query = em.createNativeQuery("ASK {GRAPH ?g {?f ?hasA ?a .}}", Boolean.class)
125: .setParameter("g", CONTEXT_ONE).setParameter("f", entityF.getUri())
126: .setParameter("hasA", URI.create(Vocabulary.P_F_HAS_SIMPLE_SET))
127: .setParameter("a", entityA.getUri());
128: assertTrue(query.getSingleResult());
129: query.setParameter("g", CONTEXT_TWO);
130: assertFalse(query.getSingleResult());
131: }
132:
133: @Test
134: void testPersistWithGeneratedIntoContext() {
135: this.em = getEntityManager("MultiPersistWithGeneratedIntoContext", false);
136: transactional(() -> em.persist(entityE, cOneDescriptor));
137:
138: final OWLClassE res = findRequired(OWLClassE.class, entityE.getUri(), cOneDescriptor);
139: assertEquals(entityE.getUri(), res.getUri());
140: assertEquals(entityE.getStringAttribute(), res.getStringAttribute());
141: }
142:
143: @Test
144: void testPersistReferenceIntoContextAndThenOwnerIntoDefault() {
145: this.em = getEntityManager("ReferenceIntoContextThenOwnerIntoDefault", false);
146: transactional(() -> em.persist(entityA, cOneDescriptor));
147:
148: em.clear();
149: transactional(() -> {
150: entityD.setOwlClassA(entityA);
151: em.persist(entityD);
152: });
153:
154: final OWLClassD res = findRequired(OWLClassD.class, entityD.getUri());
155: assertNotNull(res.getOwlClassA());
156: }
157:
158: @Test
159: void persistTwiceIntoSameContextIsInvalid() {
160: this.em = getEntityManager("MultiPersistTwiceIntoOneContext", false);
161: transactional(() -> em.persist(entityA, cOneDescriptor));
162:
163: assertThrows(OWLEntityExistsException.class, () -> transactional(() -> em.persist(entityA, cOneDescriptor)));
164: }
165:
166: @Test
167: void persistTwiceIntoDifferentContextsIsLegal() {
168: this.em = getEntityManager("MultiPersistTwiceIntoDifferentContexts", false);
169: transactional(() -> em.persist(entityA, cOneDescriptor));
170:
171: transactional(() -> em.persist(entityA, cTwoDescriptor));
172:
173:
174: final OWLClassA resOne = findRequired(OWLClassA.class, entityA.getUri(), cOneDescriptor);
175: final OWLClassA resTwo = findRequired(OWLClassA.class, entityA.getUri(), cTwoDescriptor);
176: assertNotSame(resOne, resTwo);
177: assertEquals(resOne.getUri(), resTwo.getUri());
178: assertEquals(resOne.getStringAttribute(), resTwo.getStringAttribute());
179: }
180:
181: @Test
182: void testPersistPropertiesIntoDifferentContext() {
183: this.em = getEntityManager("MultiPersistPropertiesIntoDifferentContext", false);
184: final Descriptor bDescriptor = new EntityDescriptor();
185: entityB.setProperties(Generators.createProperties(10));
186: bDescriptor.addAttributeContext(em.getMetamodel().entity(OWLClassB.class).getProperties(), CONTEXT_ONE);
187: transactional(() -> em.persist(entityB, bDescriptor));
188:
189: final OWLClassB res = findRequired(OWLClassB.class, entityB.getUri(), bDescriptor);
190: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
191: assertEquals(entityB.getProperties().size(), res.getProperties().size());
192:• for (Map.Entry<String, Set<String>> e : res.getProperties().entrySet()) {
193: assertTrue(entityB.getProperties().containsKey(e.getKey()));
194: assertEquals(e.getValue(), entityB.getProperties().get(e.getKey()));
195: }
196: }
197:
198: @Test
199: void testPersistCascadeIntoThreeContexts() {
200: this.em = getEntityManager("MultiPersistCascadeIntoThreeContexts", false);
201: final Descriptor gDescriptor = new EntityDescriptor(false);
202: final Descriptor hDescriptor = new EntityDescriptor(CONTEXT_ONE, false);
203: hDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassH.class, "owlClassA"), cTwoDescriptor);
204: gDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassG.class, "owlClassH"), hDescriptor);
205: transactional(() -> {
206: em.persist(entityG, gDescriptor);
207: assertTrue(em.contains(entityG));
208: assertTrue(em.contains(entityH));
209: assertTrue(em.contains(entityA));
210: });
211:
212: final OWLClassA resA = findRequired(OWLClassA.class, entityA.getUri(), cTwoDescriptor);
213: final OWLClassH resH = findRequired(OWLClassH.class, entityH.getUri(), hDescriptor);
214: assertSame(resA, resH.getOwlClassA());
215: final OWLClassG resG = findRequired(OWLClassG.class, entityG.getUri(), gDescriptor);
216: assertSame(resH, resG.getOwlClassH());
217: }
218:
219: @Test
220: void persistCascadeIntoThreeContextSavesOPAssertionAlwaysIntoSubjectContextWhenConfigured() {
221: this.em = getEntityManager(
222: "persistCascadeIntoThreeContextSavesOPAssertionAlwaysIntoSubjectContextWhenConfigured", false);
223: final Descriptor gDescriptor = new EntityDescriptor();
224: cOneDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassH.class, "owlClassA"), cTwoDescriptor);
225: gDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassG.class, "owlClassH"), cOneDescriptor);
226: transactional(() -> {
227: em.persist(entityG, gDescriptor);
228: assertTrue(em.contains(entityG));
229: assertTrue(em.contains(entityH));
230: assertTrue(em.contains(entityA));
231: });
232:
233: assertTrue(em.createNativeQuery("ASK { ?s ?p ?o . }", Boolean.class)
234: .setParameter("s", entityG.getUri())
235: .setParameter("p", URI.create(Vocabulary.P_HAS_H))
236: .setParameter("o", entityH.getUri()).getSingleResult());
237: final TypedQuery<Boolean> query = em.createNativeQuery("ASK {GRAPH ?g { ?s ?p ?o . }}", Boolean.class)
238: .setParameter("g", CONTEXT_ONE)
239: .setParameter("s", entityG.getUri())
240: .setParameter("p", URI.create(Vocabulary.P_HAS_H))
241: .setParameter("o", entityH.getUri());
242: assertFalse(query.getSingleResult());
243: query.setParameter("s", entityH.getUri())
244: .setParameter("p", URI.create(Vocabulary.P_HAS_OWL_CLASS_A))
245: .setParameter("o", entityA.getUri());
246: assertTrue(query.getSingleResult());
247: query.setParameter("g", CONTEXT_TWO);
248: assertFalse(query.getSingleResult());
249: }
250:
251: @Test
252: void testPersistSetWithAttributeContexts() {
253: this.em = getEntityManager("MultiPersistSetWithAttributeContexts", false);
254: entityF.setSimpleSet(Generators.createSimpleSet(20));
255: final Descriptor fDescriptor = new EntityDescriptor(false);
256: final ObjectPropertyCollectionDescriptor setDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_ONE,
257: fieldSpecification(OWLClassF.class, "simpleSet"), false);
258: fDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassF.class, "simpleSet"), setDescriptor);
259: setDescriptor.addAttributeContext(fieldSpecification(OWLClassA.class, "stringAttribute"), CONTEXT_TWO);
260: setDescriptor.addAttributeContext(em.getMetamodel().entity(OWLClassA.class).getTypes(), CONTEXT_TWO);
261: transactional(() -> {
262: em.persist(entityF, fDescriptor);
263: for (OWLClassA a : entityF.getSimpleSet()) {
264: em.persist(a, setDescriptor.getElementDescriptor());
265: }
266: });
267:
268: final OWLClassF resF = findRequired(OWLClassF.class, entityF.getUri(), fDescriptor);
269: assertEquals(entityF.getSimpleSet().size(), resF.getSimpleSet().size());
270: for (OWLClassA a : resF.getSimpleSet()) {
271: final OWLClassA resA = findRequired(OWLClassA.class, a.getUri(), setDescriptor);
272: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
273: assertEquals(a.getTypes(), resA.getTypes());
274: }
275: }
276:
277: @Test
278: void testPersistEntityWithObjectPropertyWithGeneratedIdentifierAndPutTheReferenceIntoContext() {
279: this.em = getEntityManager("PersistEntityWithObjectPropertyWithGeneratedIdentifierContexts", true);
280: entityK.setOwlClassE(entityE);
281: assertNull(entityE.getUri());
282: cOneDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassK.class, "owlClassE"), cTwoDescriptor);
283: transactional(() -> {
284: em.persist(entityK, cOneDescriptor);
285: em.persist(entityE, cTwoDescriptor);
286: assertNotNull(entityE.getUri());
287: });
288:
289: final OWLClassE resE = findRequired(OWLClassE.class, entityE.getUri(), cTwoDescriptor);
290: assertEquals(entityE.getStringAttribute(), resE.getStringAttribute());
291: final OWLClassK resK = findRequired(OWLClassK.class, entityK.getUri(), cOneDescriptor);
292: assertEquals(resE, resK.getOwlClassE());
293: }
294:
295: @Test
296: void testPersistEntityWithMappedSuperclassPuttingReferenceIntoDifferentContext() {
297: this.em = getEntityManager("PersistEntityWithMappedSuperclassReferenceInContext", true);
298: cOneDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassQ.class, "owlClassA"), cTwoDescriptor);
299: cOneDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassQ.class, "owlClassA"), cTwoDescriptor);
300: transactional(() -> {
301: em.persist(entityQ, cOneDescriptor);
302: em.persist(entityA, cTwoDescriptor);
303: });
304:
305: final OWLClassA resA = findRequired(OWLClassA.class, entityA.getUri(), cTwoDescriptor);
306: final OWLClassQ resQ = findRequired(OWLClassQ.class, entityQ.getUri(), cOneDescriptor);
307: assertEquals(entityQ.getStringAttribute(), resQ.getStringAttribute());
308: assertEquals(entityQ.getParentString(), resQ.getParentString());
309: assertEquals(entityQ.getLabel(), resQ.getLabel());
310: assertNotNull(resQ.getOwlClassA());
311: assertEquals(resA, resQ.getOwlClassA());
312: }
313:
314: @Test
315: void persistReferenceIntoContextBeforeOwnerDoesNotThrowUnpersistedChangeException() {
316: this.em =
317: getEntityManager("persistReferenceIntoContextBeforeOwnerDoesNotThrowUnpersistedChangeException", false);
318: cOneDescriptor.addAttributeDescriptor(fieldSpecification(OWLClassD.class, "owlClassA"),
319: new EntityDescriptor((URI) null));
320: persist(entityA);
321: transactional(() -> em.persist(entityD, cOneDescriptor));
322:
323: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri(), cOneDescriptor);
324: assertNotNull(result.getOwlClassA());
325: assertEquals(entityA.getStringAttribute(), result.getOwlClassA().getStringAttribute());
326: }
327:
328: /**
329: * Bug #58
330: */
331: @Test
332: void persistEntityIntoContextAndAttributeIntoDefaultWorksCorrectly() {
333: this.em = getEntityManager("persistEntityIntoContextAndAttributeIntoDefaultWorksCorrectly", false);
334: cOneDescriptor.addAttributeContext(OWLClassA_.stringAttribute, null);
335: transactional(() -> em.persist(entityA, cOneDescriptor));
336:
337: transactional(() -> {
338: try {
339: verifyStatementsPresent(Collections.singleton(
340: new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE),
341: entityA.getStringAttribute())), em);
342: verifyStatementsNotPresent(Collections.singleton(
343: new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE),
344: entityA.getStringAttribute(), CONTEXT_ONE)), em);
345: } catch (Exception e) {
346: throw new RuntimeException(e);
347: }
348: });
349: }
350:
351: @Test
352: void persistSupportsMultipleContextsSpecifiedForReferencedEntities() {
353: this.em = getEntityManager("persistSupportsMultipleContextsSpecifiedForReferencedEntities", true);
354: final OWLClassA entityA2 = new OWLClassA(Generators.generateUri(), "second instance");
355: transactional(() -> {
356: em.persist(entityA, cOneDescriptor);
357: em.persist(entityA2, cTwoDescriptor);
358: });
359:
360: final Descriptor descriptor = new EntityDescriptor();
361: descriptor.addAttributeContext(fieldSpecification(OWLClassF.class, "simpleSet"), CONTEXT_ONE)
362: .addAttributeContext(fieldSpecification(OWLClassF.class, "simpleSet"), CONTEXT_TWO);
363: entityF.setSimpleSet(new HashSet<>(Arrays.asList(entityA, entityA2)));
364: transactional(() -> em.persist(entityF, descriptor));
365:
366: final OWLClassF result = findRequired(OWLClassF.class, entityF.getUri(), descriptor);
367: assertEquals(2, result.getSimpleSet().size());
368: assertTrue(result.getSimpleSet().stream().anyMatch(a -> a.getUri().equals(entityA.getUri())));
369: assertTrue(result.getSimpleSet().stream().anyMatch(a -> a.getUri().equals(entityA2.getUri())));
370: }
371: }