Skip to content

Method: persistTwiceIntoSameContextIsInvalid()

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