-
Notifications
You must be signed in to change notification settings - Fork 14
/
CollectionValueMergerTest.java
192 lines (170 loc) · 8.78 KB
/
CollectionValueMergerTest.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
/**
* Copyright (C) 2020 Czech Technical University in Prague
* <p>
* 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.
* <p>
* 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.merge;
import cz.cvut.kbss.jopa.environment.OWLClassA;
import cz.cvut.kbss.jopa.environment.OWLClassC;
import cz.cvut.kbss.jopa.environment.OWLClassF;
import cz.cvut.kbss.jopa.environment.Vocabulary;
import cz.cvut.kbss.jopa.environment.utils.Generators;
import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks;
import cz.cvut.kbss.jopa.model.MetamodelImpl;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.model.metamodel.EntityType;
import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class CollectionValueMergerTest {
@Mock
private UnitOfWorkImpl uow;
@Mock
private MetamodelImpl metamodel;
private Descriptor descriptor;
private CollectionValueMerger merger;
@Before
public void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
final MetamodelMocks mocks = new MetamodelMocks();
mocks.setMocks(metamodel);
when(uow.getMetamodel()).thenReturn(metamodel);
when(uow.isEntityType(any())).thenAnswer(invocation -> {
final Class<?> arg = (Class<?>) invocation.getArguments()[0];
try {
return metamodel.entity(arg) != null;
} catch (IllegalArgumentException e) {
return false;
}
});
this.descriptor = new EntityDescriptor();
this.merger = new CollectionValueMerger(uow, new ManagedTypeValueMerger(uow));
}
@Test
public void mergeMergesUpdatedValuesIntoOriginalSetOfNonManagedTypes() {
final OWLClassA orig = Generators.generateOwlClassAInstance();
final OWLClassA merge = new OWLClassA(orig);
final String addedType = Vocabulary.CLASS_BASE + "AddedType";
final String removedType = merge.getTypes().iterator().next();
merge.getTypes().remove(removedType);
merge.getTypes().add(addedType);
final EntityType<OWLClassA> et = metamodel.entity(OWLClassA.class);
final FieldSpecification<? super OWLClassA, ?> typesSpec = et.getTypes();
merger.mergeValue(typesSpec, orig, orig.getTypes(), merge.getTypes(), descriptor);
assertTrue(orig.getTypes().contains(addedType));
assertFalse(orig.getTypes().contains(removedType));
}
@Test
public void mergeReplacesNullCollectionWithMergedOne() {
final OWLClassA orig = Generators.generateOwlClassAInstance();
final OWLClassA merge = new OWLClassA(orig);
orig.setTypes(null);
final EntityType<OWLClassA> et = metamodel.entity(OWLClassA.class);
final FieldSpecification<? super OWLClassA, ?> typesSpec = et.getTypes();
merger.mergeValue(typesSpec, orig, orig.getTypes(), merge.getTypes(), descriptor);
assertNotNull(orig.getTypes());
assertEquals(orig.getTypes(), merge.getTypes());
}
@Test
public void mergeReplacesOriginalCollectionWithNullWhenMergedHasNull() {
final OWLClassA orig = Generators.generateOwlClassAInstance();
final OWLClassA merge = new OWLClassA(orig);
merge.setTypes(null);
final EntityType<OWLClassA> et = metamodel.entity(OWLClassA.class);
final FieldSpecification<? super OWLClassA, ?> typesSpec = et.getTypes();
merger.mergeValue(typesSpec, orig, orig.getTypes(), merge.getTypes(), descriptor);
assertNull(orig.getTypes());
}
@Test
public void mergeLoadsReferencesUsedInMergedCollectionFromStorage() throws Exception {
final OWLClassF orig = new OWLClassF(Generators.createIndividualIdentifier());
orig.setSimpleSet(IntStream.range(0, 10).mapToObj(i -> Generators.generateOwlClassAInstance())
.collect(Collectors.toSet()));
orig.getSimpleSet()
.forEach(a -> when(uow.readObject(OWLClassA.class, a.getUri(), descriptor)).thenReturn(new OWLClassA(a)));
final OWLClassF merged = new OWLClassF(orig.getUri());
merged.setSimpleSet(new HashSet<>());
final Iterator<OWLClassA> it = orig.getSimpleSet().iterator();
int i = 0;
while (it.hasNext()) {
if (i % 2 == 0) {
merged.getSimpleSet().add(it.next());
} else {
it.next();
}
i++;
}
for (int j = 0; j < Generators.randomPositiveInt(5); j++) {
final OWLClassA a = Generators.generateOwlClassAInstance();
merged.getSimpleSet().add(a);
when(uow.readObject(OWLClassA.class, a.getUri(), descriptor)).thenReturn(new OWLClassA(a));
}
final EntityType<OWLClassF> et = metamodel.entity(OWLClassF.class);
final FieldSpecification<? super OWLClassF, ?> att = et
.getFieldSpecification(OWLClassF.getSimpleSetField().getName());
merger.mergeValue(att, orig, orig.getSimpleSet(), merged.getSimpleSet(), descriptor);
assertEquals(merged.getSimpleSet().size(), orig.getSimpleSet().size());
merged.getSimpleSet().forEach(a -> verify(uow).readObject(OWLClassA.class, a.getUri(), descriptor));
}
@Test
public void mergeReplacesOriginalCollectionWithEmptyOneWhenMergedCollectionIsEmpty() throws Exception {
final OWLClassF orig = new OWLClassF(Generators.createIndividualIdentifier());
orig.setSimpleSet(IntStream.range(0, 10).mapToObj(i -> Generators.generateOwlClassAInstance())
.collect(Collectors.toSet()));
final OWLClassF merged = new OWLClassF(orig.getUri());
merged.setSimpleSet(Collections.emptySet());
final EntityType<OWLClassF> et = metamodel.entity(OWLClassF.class);
final FieldSpecification<? super OWLClassF, ?> att = et
.getFieldSpecification(OWLClassF.getSimpleSetField().getName());
merger.mergeValue(att, orig, orig.getSimpleSet(), merged.getSimpleSet(), descriptor);
assertNotNull(orig.getSimpleSet());
assertTrue(orig.getSimpleSet().isEmpty());
}
@Test
public void mergeLoadsReferencesUsedInMergedListFromStorage() throws Exception {
final OWLClassC orig = new OWLClassC(Generators.createIndividualIdentifier());
orig.setReferencedList(IntStream.range(0, 10).mapToObj(i -> Generators.generateOwlClassAInstance())
.collect(Collectors.toList()));
orig.getReferencedList()
.forEach(a -> when(uow.readObject(OWLClassA.class, a.getUri(), descriptor)).thenReturn(new OWLClassA(a)));
final OWLClassC merged = new OWLClassC(orig.getUri());
merged.setReferencedList(new ArrayList<>());
for (int i = 0; i < orig.getReferencedList().size(); i++) {
if (i % 3 == 0) {
merged.getReferencedList().add(orig.getReferencedList().get(i));
}
}
for (int i = 0; i < Generators.randomPositiveInt(5); i++) {
final OWLClassA a = Generators.generateOwlClassAInstance();
merged.getReferencedList().add(a);
when(uow.readObject(OWLClassA.class, a.getUri(), descriptor)).thenReturn(new OWLClassA(a));
}
final EntityType<OWLClassC> et = metamodel.entity(OWLClassC.class);
final FieldSpecification<? super OWLClassC, ?> att = et
.getFieldSpecification(OWLClassC.getRefListField().getName());
merger.mergeValue(att, orig, orig.getReferencedList(), merged.getReferencedList(), descriptor);
assertEquals(merged.getReferencedList().size(), orig.getReferencedList().size());
merged.getReferencedList().forEach(a -> verify(uow).readObject(OWLClassA.class, a.getUri(), descriptor));
}
}