-
Notifications
You must be signed in to change notification settings - Fork 14
/
ChangeManagerTest.java
524 lines (474 loc) · 20.6 KB
/
ChangeManagerTest.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
* Copyright (C) 2016 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.sessions.change;
import cz.cvut.kbss.jopa.environment.*;
import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks;
import cz.cvut.kbss.jopa.environment.utils.TestEnvironmentUtils;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
import cz.cvut.kbss.jopa.sessions.ChangeManager;
import cz.cvut.kbss.jopa.sessions.ChangeRecord;
import cz.cvut.kbss.jopa.sessions.MetamodelProvider;
import cz.cvut.kbss.jopa.sessions.ObjectChangeSet;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
public class ChangeManagerTest {
private static final URI DEFAULT_CONTEXT = URI.create("http://defaultContext");
private static OWLClassA testA;
private static OWLClassB testB;
private static OWLClassD testD;
private static OWLClassC testC;
private static OWLClassO testO;
private static OWLClassM testM;
private OWLClassA testAClone;
private OWLClassB testBClone;
private OWLClassC testCClone;
private OWLClassD testDClone;
private static Set<String> typesCollection;
private static Descriptor defaultDescriptor;
@Mock
private MetamodelProvider providerMock;
@Mock
private Metamodel metamodelMock;
private ChangeManager manager;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
initA();
initB();
initC();
initD();
initO();
typesCollection = new HashSet<>();
for (int i = 0; i < 10; i++) {
typesCollection.add(Integer.toString(i));
}
OWLClassF testF = new OWLClassF();
testF.setUri(URI.create("http://testF"));
testM = new OWLClassM();
testM.initializeTestValues(false);
defaultDescriptor = new EntityDescriptor(DEFAULT_CONTEXT);
}
private static void initA() {
testA = new OWLClassA();
final URI uri = URI.create("http://testA");
testA.setUri(uri);
testA.setStringAttribute("TestStringAttribute");
}
private static void initB() {
testB = new OWLClassB();
testB.setUri(URI.create("http://testB"));
testB.setStringAttribute("someString");
final Map<String, Set<String>> props = new HashMap<>();
props.put("propertyOne", Collections.singleton("valueOne"));
props.put("propertyTwo", Collections.singleton("valueTwo"));
final Set<String> multiProp = new HashSet<>();
multiProp.add("valueThree");
multiProp.add("valueFour");
props.put("propertyThree", multiProp);
testB.setProperties(props);
}
private static void initC() {
testC = new OWLClassC();
final URI pkC = URI.create("http://testC");
testC.setUri(pkC);
List<OWLClassA> refList = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
OWLClassA a = new OWLClassA();
URI pkA = URI.create("http://testARef" + (i + 1));
a.setUri(pkA);
a.setStringAttribute("stringForA" + (i + 1));
refList.add(a);
}
testC.setReferencedList(refList);
}
private static void initD() {
final URI uri2 = URI.create("http://referencedA");
final OWLClassA refA = new OWLClassA();
refA.setUri(uri2);
refA.setStringAttribute("att");
testD = new OWLClassD();
final URI uri3 = URI.create("http://testD");
testD.setUri(uri3);
testD.setOwlClassA(refA);
}
private static void initO() {
testO = new OWLClassO();
testO.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies#testO"));
testO.setStringAttribute("String");
}
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
manager = new ChangeManagerImpl(providerMock);
when(providerMock.isTypeManaged(any(Class.class))).thenAnswer(invocation -> {
final Class<?> cls = (Class<?>) invocation.getArguments()[0];
return TestEnvironmentUtils.getManagedTypes().contains(cls);
});
when(providerMock.getMetamodel()).thenReturn(metamodelMock);
final MetamodelMocks mocks = new MetamodelMocks();
mocks.setMocks(metamodelMock);
testA.setTypes(null);
initClones();
final List<OWLClassA> clonedList = new ArrayList<>(testC.getReferencedList().size());
for (OWLClassA a : testC.getReferencedList()) {
final OWLClassA newA = new OWLClassA();
newA.setUri(a.getUri());
newA.setStringAttribute(a.getStringAttribute());
clonedList.add(newA);
}
testCClone.setReferencedList(clonedList);
}
private void initClones() {
this.testAClone = new OWLClassA(testA.getUri());
testAClone.setStringAttribute(null);
testAClone.setTypes(null);
final Map<String, Set<String>> cloneProps = new HashMap<>();
for (Entry<String, Set<String>> e : testB.getProperties().entrySet()) {
final Set<String> set = e.getValue().stream().collect(Collectors.toSet());
cloneProps.put(e.getKey(), set);
}
testBClone = new OWLClassB(testB.getUri());
testBClone.setStringAttribute(testB.getStringAttribute());
testBClone.setProperties(cloneProps);
testCClone = new OWLClassC();
testCClone.setUri(testC.getUri());
testDClone = new OWLClassD(testD.getUri());
}
@Test
public void testNullHasChanges() {
assertFalse(manager.hasChanges(null, null));
}
@Test
public void testHasSimpleChanges() {
testAClone.setStringAttribute("differentStringAtribute");
assertTrue(manager.hasChanges(testA, testAClone));
}
@Test
public void testHasNoChanges() {
testAClone.setStringAttribute(testA.getStringAttribute());
assertFalse(manager.hasChanges(testA, testA));
}
@Test
public void testReferenceChange() {
final OWLClassA ref = new OWLClassA();
final URI uri = URI.create("http://newReferenceA");
ref.setUri(uri);
ref.setStringAttribute(testA.getStringAttribute());
testDClone.setOwlClassA(ref);
assertTrue(manager.hasChanges(testD, testDClone));
}
@Test
public void testCollectionEmptyHasChange() {
testAClone.setTypes(typesCollection);
testA.setTypes(new HashSet<>());
assertTrue(manager.hasChanges(testA, testAClone));
}
@Test
public void testCollectionHasChange() {
testA.setTypes(typesCollection);
Set<String> changed = new HashSet<>();
Iterator<String> it = typesCollection.iterator();
it.next();
changed.add("111");
while (it.hasNext()) {
changed.add(it.next());
}
testAClone.setTypes(changed);
assertTrue(manager.hasChanges(testA, testAClone));
}
@Test
public void testCollectionComplexHasChange() {
assertEquals(testC.getUri(), testCClone.getUri());
assertEquals(testC.getReferencedList().get(0).getUri(),
testCClone.getReferencedList().get(0).getUri());
assertEquals(testC.getReferencedList().get(5).getStringAttribute(), testCClone
.getReferencedList().get(5).getStringAttribute());
testCClone.getReferencedList().get(8).setStringAttribute("changedStringAttribute");
assertTrue(manager.hasChanges(testC, testCClone));
}
@Test
public void testHasChangeSetFromNull() {
final OWLClassA a = new OWLClassA();
a.setUri(testAClone.getUri());
// The original has string attribute null
testAClone.setStringAttribute("someString");
assertTrue(manager.hasChanges(a, testAClone));
}
@Test(expected = NullPointerException.class)
public void testCalculateChangesNull() throws Exception {
manager.calculateChanges(null);
fail("This line should not have been reached.");
}
@Test
public void testCalculateChangesNoChanges() throws Exception {
final OWLClassA a = new OWLClassA();
a.setUri(testA.getUri());
a.setStringAttribute(testA.getStringAttribute());
final ObjectChangeSet chSet = createChangeSet(testA, a);
final boolean res = manager.calculateChanges(chSet);
assertFalse(res);
assertTrue(chSet.getChanges().isEmpty());
}
@Test
public void testCalculateSimpleChanges() throws Exception {
ObjectChangeSet chSet = createChangeSet(testA, testAClone);
assertTrue(chSet.getChanges().isEmpty());
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertFalse(chSet.getChanges().isEmpty());
assertTrue(chSet.getChanges().containsKey("stringAttribute"));
}
@Test
public void testCalculateChangesPrimitives() throws Exception {
final OWLClassM testMClone = new OWLClassM();
testMClone.setIntAttribute(testM.getIntAttribute() + 117);
ObjectChangeSet chSet = createChangeSet(testM, testMClone);
assertTrue(chSet.getChanges().isEmpty());
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertFalse(chSet.getChanges().isEmpty());
assertTrue(chSet.getChanges().containsKey(OWLClassM.getIntAttributeField().getName()));
}
@Test
public void testCalculateReferenceChanges() throws Exception {
testDClone.setOwlClassA(testAClone);
ObjectChangeSet chSet = createChangeSet(testD, testDClone);
assertTrue(chSet.getChanges().isEmpty());
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertEquals(1, chSet.getChanges().size());
assertEquals(testAClone, chSet.getChanges().get(OWLClassD.getOwlClassAField().getName()).getNewValue());
}
@SuppressWarnings("unchecked")
@Test
public void testCalculateCollectionChanges() throws Exception {
testA.setTypes(typesCollection);
Set<String> newCollection = new HashSet<>(typesCollection);
newCollection.remove("8");
newCollection.add("String");
testAClone.setTypes(newCollection);
testAClone.setStringAttribute(testA.getStringAttribute());
ObjectChangeSet chSet = createChangeSet(testA, testAClone);
assertTrue(chSet.getChanges().isEmpty());
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertEquals(1, chSet.getChanges().size());
assertTrue(((Set<String>) chSet.getChanges().
get(OWLClassA.getTypesField().getName()).getNewValue()).contains("String"));
}
@Test
public void testCalculateMultipleChanges() throws Exception {
testA.setTypes(typesCollection);
Set<String> newCollection = new HashSet<>(typesCollection);
newCollection.remove("8");
newCollection.add("String");
testAClone.setTypes(newCollection);
testAClone.setStringAttribute("AnotherStringAttribute");
ObjectChangeSet chSet = createChangeSet(testA, testAClone);
assertTrue(chSet.getChanges().isEmpty());
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertEquals(2, chSet.getChanges().size());
assertTrue(chSet.getChanges().containsKey("stringAttribute"));
assertTrue(chSet.getChanges().containsKey("types"));
}
@Test
public void testCalculateChangesSetNull() throws Exception {
ObjectChangeSet chSet = createChangeSet(testA, testAClone);
assertTrue(chSet.getChanges().isEmpty());
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertFalse(chSet.getChanges().isEmpty());
assertNotNull(chSet);
assertNull(chSet.getChanges().get("stringAttribute").getNewValue());
}
@Test
public void testCalculateChangeSetFromNull() throws Exception {
final OWLClassA a = new OWLClassA();
a.setUri(testAClone.getUri());
// The original has string attribute null
testAClone.setStringAttribute("someString");
final ObjectChangeSet chSet = createChangeSet(a, testAClone);
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertEquals(1, chSet.getChanges().size());
assertEquals(1, chSet.getChanges().size());
final ChangeRecord rec = chSet.getChanges().get(OWLClassA.getStrAttField().getName());
assertNotNull(rec.getNewValue());
}
@Test
public void testCalculateChangesRemoveItemFromReferenceList() throws Exception {
testCClone.getReferencedList().remove(4);
ObjectChangeSet chSet = createChangeSet(testC, testCClone);
assertTrue(chSet.getChanges().isEmpty());
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertTrue(chSet.getChanges().containsKey("referencedList"));
assertEquals(1, chSet.getChanges().size());
}
@SuppressWarnings("unchecked")
@Test
public void testCalculateChangesAddItemToReferenceList() throws Exception {
OWLClassA add = new OWLClassA();
final URI pk = URI.create("http://addedA");
add.setUri(pk);
add.setStringAttribute("string");
testCClone.getReferencedList().add(add);
ObjectChangeSet chSet = createChangeSet(testC, testCClone);
assertTrue(chSet.getChanges().isEmpty());
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertEquals(1, chSet.getChanges().size());
final ChangeRecord r = chSet.getChanges().get(OWLClassC.getRefListField().getName());
List<OWLClassA> refs = (List<OWLClassA>) r.getNewValue();
assertEquals(add.getUri(), refs.get(10).getUri());
assertEquals(add.getStringAttribute(), refs.get(10).getStringAttribute());
}
@SuppressWarnings("unchecked")
@Test
public void testCalculateChangesChangeItemInReferenceList() throws Exception {
OWLClassA newOne = new OWLClassA();
final URI pk = URI.create("http://newOne");
newOne.setUri(pk);
newOne.setStringAttribute("newOnesString");
testCClone.getReferencedList().remove(3);
testCClone.getReferencedList().add(newOne);
ObjectChangeSet chSet = createChangeSet(testC, testCClone);
assertTrue(chSet.getChanges().isEmpty());
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertTrue(chSet.getChanges().containsKey("referencedList"));
assertEquals(1, chSet.getChanges().size());
final ChangeRecord r = chSet.getChanges().get(OWLClassC.getRefListField().getName());
List<OWLClassA> refs = (List<OWLClassA>) r.getNewValue();
assertTrue(refs.contains(newOne));
}
@Test
public void testCalculateChangesMapAddKey() throws Exception {
testBClone.getProperties().put("newProperty", Collections.singleton("propVal"));
final ObjectChangeSet chSet = createChangeSet(testB, testBClone);
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertEquals(1, chSet.getChanges().size());
assertTrue(chSet.getChanges().containsKey("properties"));
}
@Test
public void testCalculateChangesMapChangeValue() throws Exception {
final String key = testB.getProperties().keySet().iterator().next();
testBClone.getProperties().put(key, Collections.singleton("propVal"));
final ObjectChangeSet chSet = createChangeSet(testB, testBClone);
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertEquals(1, chSet.getChanges().size());
assertTrue(chSet.getChanges().containsKey("properties"));
}
@Test
public void testCalculateChangesMapAddValue() throws Exception {
final String key = testB.getProperties().keySet().iterator().next();
testBClone.getProperties().get(key).add("addedValue");
final ObjectChangeSet chSet = createChangeSet(testB, testBClone);
final boolean res = manager.calculateChanges(chSet);
assertTrue(res);
assertEquals(1, chSet.getChanges().size());
assertTrue(chSet.getChanges().containsKey("properties"));
}
private ObjectChangeSet createChangeSet(Object orig, Object clone) {
return TestEnvironmentUtils.createObjectChangeSet(orig, clone,
defaultDescriptor.getContext());
}
@Test
public void twoSetsWithSameElementsButDifferentOrderHaveNoChanges() throws Exception {
testA.setTypes(typesCollection);
testAClone.setStringAttribute(testA.getStringAttribute());
final List<String> lst = new ArrayList<>(typesCollection);
Collections.reverse(lst);
final Set<String> newTypes = new LinkedHashSet<>(lst);
testAClone.setTypes(newTypes);
assertFalse(typesCollection.iterator().next().equals(newTypes.iterator().next()));
final boolean res = manager.hasChanges(testA, testAClone);
assertFalse(res);
}
@Test
public void twoSetsWithManagedElementsWithSameIdentifiersHaveNoChanges() throws Exception {
final OWLClassF testF = new OWLClassF();
final OWLClassF cloneF = new OWLClassF();
initFAndClone(testF, cloneF);
final boolean res = manager.hasChanges(testF, cloneF);
assertFalse(res);
}
private void initFAndClone(OWLClassF orig, OWLClassF clone) {
final URI uri = URI.create("http://krizik.felk.cvut.cz/ontologies#testF");
orig.setUri(uri);
clone.setUri(uri);
orig.setSimpleSet(new HashSet<>());
clone.setSimpleSet(new HashSet<>());
orig.getSimpleSet().add(testA);
testAClone.setStringAttribute(testA.getStringAttribute());
clone.getSimpleSet().add(testAClone);
final URI aUri = URI.create("http://krizik.felk.cvut.cz/ontologies#testAA");
final OWLClassA extraA = new OWLClassA(aUri);
extraA.setStringAttribute("string");
orig.getSimpleSet().add(extraA);
final OWLClassA extraAClone = new OWLClassA(aUri);
extraAClone.setStringAttribute(extraA.getStringAttribute());
clone.getSimpleSet().add(extraAClone);
}
@Test
public void twoSetsWithManagedElementsOneElementReplacedWithNewWithoutIdHaveChanges() throws Exception {
final OWLClassF testF = new OWLClassF();
final OWLClassF cloneF = new OWLClassF();
initFAndClone(testF, cloneF);
final OWLClassA added = new OWLClassA();
added.setStringAttribute("differentString");
// OWLClassA does not have generated id, but for purposes of this test, let's assume it does
final Iterator<OWLClassA> remove = cloneF.getSimpleSet().iterator();
remove.next();
remove.remove();
cloneF.getSimpleSet().add(added);
final boolean res = manager.hasChanges(testF, cloneF);
assertTrue(res);
}
@Test
public void hasChangesIgnoresTransientFieldChanges() throws Exception {
final OWLClassO testOClone = new OWLClassO();
testOClone.setUri(testO.getUri());
testOClone.setStringAttribute(testO.getStringAttribute());
testOClone.setTransientField("Change!");
final boolean res = manager.hasChanges(testO, testOClone);
assertFalse(res);
}
@Test
public void calculateChangesIgnoresTransientFieldChanges() throws Exception {
final OWLClassO testOClone = new OWLClassO();
testOClone.setUri(testO.getUri());
testOClone.setStringAttribute(testO.getStringAttribute());
testOClone.setTransientFieldWithAnnotation("Change!");
final ObjectChangeSet changeSet = createChangeSet(testO, testOClone);
final boolean res = manager.calculateChanges(changeSet);
assertFalse(res);
assertTrue(changeSet.getChanges().isEmpty());
}
}