Skip to content

Method: testPersistWithGeneratedIntoContext()

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.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.test.*;
22: import cz.cvut.kbss.jopa.test.environment.Generators;
23: import org.junit.Test;
24: import org.slf4j.Logger;
25:
26: import java.net.URI;
27: import java.util.Map;
28: import java.util.Set;
29:
30: import static org.junit.Assert.*;
31:
32: public abstract class CreateOperationsMultiContextRunner extends BaseRunner {
33:
34: private OWLClassF entityF;
35: private OWLClassK entityK;
36:
37: public CreateOperationsMultiContextRunner(Logger logger) {
38: super(logger);
39: initialize();
40: }
41:
42: private void initialize() {
43: this.entityF = new OWLClassF();
44: entityF.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityF"));
45: this.entityK = new OWLClassK();
46: entityK.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityK"));
47: }
48:
49: @Test
50: public void testPersistDataPropertyIntoContext() throws Exception {
51: logger.debug("Test: persist an entity into the default context and its data property into a different one.");
52: this.em = getEntityManager("MultiPersistDataPropertyIntoContext", false);
53: final Descriptor aDescriptor = new EntityDescriptor();
54: aDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_ONE);
55: em.getTransaction().begin();
56: em.persist(entityA, aDescriptor);
57: em.getTransaction().commit();
58:
59: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
60: assertNotNull(res);
61: assertEquals(entityA.getUri(), res.getUri());
62: assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
63: assertEquals(entityA.getTypes().size(), res.getTypes().size());
64: assertTrue(entityA.getTypes().containsAll(res.getTypes()));
65: }
66:
67: @Test
68: public void testPersistObjectPropertyIntoContext() throws Exception {
69: logger.debug(
70: "Test: persist entity into one context and its object property into another, along with its own attributes.");
71: this.em = getEntityManager("MultiPersistObjectPropertyIntoContext", false);
72: final Descriptor dDescriptor = new EntityDescriptor();
73: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
74: dDescriptor.addAttributeDescriptor(OWLClassD.class.getDeclaredField("owlClassA"), aDescriptor);
75: em.getTransaction().begin();
76: em.persist(entityD, dDescriptor);
77: em.persist(entityA, aDescriptor);
78: em.getTransaction().commit();
79:
80: final OWLClassD resD = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
81: assertNotNull(resD);
82: assertNotNull(resD.getOwlClassA());
83: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
84: assertNotNull(resA);
85: assertEquals(resD.getOwlClassA().getUri(), resA.getUri());
86: assertEquals(resD.getOwlClassA().getStringAttribute(), resA.getStringAttribute());
87: assertTrue(resD.getOwlClassA().getTypes().containsAll(resA.getTypes()));
88: }
89:
90: @Test
91: public void testPersistWithGeneratedIntoContext() {
92: logger.debug("Test: persist entity with generated ID into a context.");
93: this.em = getEntityManager("MultiPersistWithGeneratedIntoContext", false);
94: final Descriptor eDescriptor = new EntityDescriptor(CONTEXT_ONE);
95: em.getTransaction().begin();
96: em.persist(entityE, eDescriptor);
97: em.getTransaction().commit();
98:
99: final OWLClassE res = em.find(OWLClassE.class, entityE.getUri(), eDescriptor);
100: assertNotNull(res);
101: assertEquals(entityE.getUri(), res.getUri());
102: assertEquals(entityE.getStringAttribute(), res.getStringAttribute());
103: }
104:
105: @Test(expected = OWLEntityExistsException.class)
106: public void testPersistTwiceIntoOneContext() {
107: this.em = getEntityManager("MultiPersistTwiceIntoOneContext", false);
108: logger.debug("Test: persist an entity twice into the same context.");
109: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
110: em.getTransaction().begin();
111: em.persist(entityA, aDescriptor);
112: em.getTransaction().commit();
113:
114: assertNotNull(em.find(OWLClassA.class, entityA.getUri(), aDescriptor));
115: em.getTransaction().begin();
116: em.persist(entityA, aDescriptor);
117: em.getTransaction().commit();
118: }
119:
120: @Test
121: public void testPersistTwiceIntoDifferentContexts() {
122: this.em = getEntityManager("MultiPersistTwiceIntoDifferentContexts", false);
123: logger.debug("Test: persist an entity into two different contexts.");
124: final Descriptor aDescriptorOne = new EntityDescriptor(CONTEXT_ONE);
125: final Descriptor aDescriptorTwo = new EntityDescriptor(CONTEXT_TWO);
126: em.getTransaction().begin();
127: em.persist(entityA, aDescriptorOne);
128: em.persist(entityA, aDescriptorTwo);
129: em.getTransaction().commit();
130:
131: final OWLClassA resOne = em.find(OWLClassA.class, entityA.getUri(), aDescriptorOne);
132: assertNotNull(resOne);
133: final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), aDescriptorTwo);
134: assertNotNull(resTwo);
135: assertNotSame(resOne, resTwo);
136: assertEquals(resOne.getUri(), resTwo.getUri());
137: assertEquals(resOne.getStringAttribute(), resTwo.getStringAttribute());
138: }
139:
140: @Test
141: public void testPersistPropertiesIntoDifferentContext() throws Exception {
142: this.em = getEntityManager("MultiPersistPropertiesIntoDifferentContext", false);
143: logger.debug("Test: persist an entity and persist its properties into a different context.");
144: final Descriptor bDescriptor = new EntityDescriptor();
145: entityB.setProperties(Generators.createProperties(10));
146: bDescriptor.addAttributeContext(OWLClassB.class.getDeclaredField("properties"), CONTEXT_ONE);
147: em.getTransaction().begin();
148: em.persist(entityB, bDescriptor);
149: em.getTransaction().commit();
150:
151: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri(), bDescriptor);
152: assertNotNull(res);
153: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
154: assertEquals(entityB.getProperties().size(), res.getProperties().size());
155: for (Map.Entry<String, Set<String>> e : res.getProperties().entrySet()) {
156: assertTrue(entityB.getProperties().containsKey(e.getKey()));
157: assertEquals(e.getValue(), entityB.getProperties().get(e.getKey()));
158: }
159: }
160:
161: @Test
162: public void testPersistCascadeIntoThreeContexts() throws Exception {
163: this.em = getEntityManager("MultiPersistCascadeIntoThreeContexts", false);
164: logger.debug("Test: persist three entities in cascaded relationship, each into a different context.");
165: final Descriptor gDescriptor = new EntityDescriptor();
166: final Descriptor hDescriptor = new EntityDescriptor(CONTEXT_ONE);
167: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
168: hDescriptor.addAttributeDescriptor(OWLClassH.class.getDeclaredField("owlClassA"), aDescriptor);
169: gDescriptor.addAttributeDescriptor(OWLClassG.class.getDeclaredField("owlClassH"), hDescriptor);
170: em.getTransaction().begin();
171: em.persist(entityG, gDescriptor);
172: assertTrue(em.contains(entityG));
173: assertTrue(em.contains(entityH));
174: assertTrue(em.contains(entityA));
175: em.getTransaction().commit();
176:
177: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
178: assertNotNull(resA);
179: final OWLClassH resH = em.find(OWLClassH.class, entityH.getUri(), hDescriptor);
180: assertNotNull(resH);
181: assertSame(resA, resH.getOwlClassA());
182: final OWLClassG resG = em.find(OWLClassG.class, entityG.getUri(), gDescriptor);
183: assertNotNull(resG);
184: assertSame(resH, resG.getOwlClassH());
185: }
186:
187: @Test
188: public void testPersistSetWithAttributeContexts() throws Exception {
189: logger.debug(
190: "Test: persist entity with simple set, the set will be in a different context and attributes of its element in another.");
191: this.em = getEntityManager("MultiPersistSetWithAttributeContexts", false);
192: entityF.setSimpleSet(Generators.createSimpleSet(20));
193: final Descriptor fDescriptor = new EntityDescriptor();
194: final Descriptor setDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_ONE,
195: OWLClassF.class.getDeclaredField("simpleSet"));
196: fDescriptor.addAttributeDescriptor(OWLClassF.class.getDeclaredField("simpleSet"), setDescriptor);
197: setDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_TWO);
198: setDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("types"), CONTEXT_TWO);
199: em.getTransaction().begin();
200: em.persist(entityF, fDescriptor);
201: for (OWLClassA a : entityF.getSimpleSet()) {
202: em.persist(a, setDescriptor);
203: }
204: em.getTransaction().commit();
205:
206: final OWLClassF resF = em.find(OWLClassF.class, entityF.getUri(), fDescriptor);
207: assertNotNull(resF);
208: assertEquals(entityF.getSimpleSet().size(), resF.getSimpleSet().size());
209: for (OWLClassA a : resF.getSimpleSet()) {
210: final OWLClassA resA = em.find(OWLClassA.class, a.getUri(), setDescriptor);
211: assertNotNull(resA);
212: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
213: assertEquals(a.getTypes(), resA.getTypes());
214: }
215: }
216:
217: @Test
218: public void testPersistEntityWithObjectPropertyWithGeneratedIdentifierAndPutTheReferenceIntoContext()
219: throws Exception {
220: logger.debug("Test: persist entity with reference to an entity with generated identifier. "
221: + "The identifier should be generated automatically before the referenced entity itself is persisted.");
222: this.em = getEntityManager("PersistEntityWithObjectPropertyWithGeneratedIdentifierContexts", true);
223: entityK.setOwlClassE(entityE);
224: assertNull(entityE.getUri());
225: final Descriptor eDescriptor = new EntityDescriptor(CONTEXT_TWO);
226: final Descriptor kDescriptor = new EntityDescriptor(CONTEXT_ONE);
227: kDescriptor.addAttributeDescriptor(OWLClassK.class.getDeclaredField("owlClassE"), eDescriptor);
228: em.getTransaction().begin();
229: em.persist(entityK, kDescriptor);
230: assertNotNull(entityE.getUri());
231: em.persist(entityE, eDescriptor);
232: em.getTransaction().commit();
233:
234: final OWLClassE resE = em.find(OWLClassE.class, entityE.getUri(), eDescriptor);
235: assertNotNull(resE);
236: assertEquals(entityE.getStringAttribute(), resE.getStringAttribute());
237: final OWLClassK resK = em.find(OWLClassK.class, entityK.getUri(), kDescriptor);
238: assertNotNull(resK);
239: assertEquals(resE, resK.getOwlClassE());
240: }
241: }