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