-
Notifications
You must be signed in to change notification settings - Fork 14
/
StringPropertiesFieldStrategyTest.java
353 lines (299 loc) · 16.3 KB
/
StringPropertiesFieldStrategyTest.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
/**
* 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.oom;
import cz.cvut.kbss.jopa.environment.OWLClassB;
import cz.cvut.kbss.jopa.environment.utils.Generators;
import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks;
import cz.cvut.kbss.jopa.environment.utils.TestEnvironmentUtils;
import cz.cvut.kbss.jopa.exceptions.InvalidAssertionIdentifierException;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.ontodriver.model.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
public class StringPropertiesFieldStrategyTest {
private static final URI PK = Generators.createIndividualIdentifier();
@Mock
private EntityMappingHelper mapperMock;
private PropertiesFieldStrategy<OWLClassB> strategy;
private AxiomValueGatherer gatherer;
private OWLClassB entity;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
MetamodelMocks mocks = new MetamodelMocks();
this.entity = new OWLClassB();
entity.setUri(PK);
entity.setStringAttribute("someString");
final Descriptor descriptor = new EntityDescriptor();
this.strategy = new PropertiesFieldStrategy<>(mocks.forOwlClassB().entityType(),
mocks.forOwlClassB().propertiesSpec(), descriptor, mapperMock);
this.gatherer = new AxiomValueGatherer(NamedResource.create(PK), null);
gatherer.addValue(Assertion.createClassAssertion(false), new Value<>(PK), null);
}
@Test
public void extractsValuesForPersist() throws Exception {
final int propCount = 5;
entity.setProperties(Generators.generateStringProperties(propCount, propCount));
when(mapperMock.getOriginalInstance(entity)).thenReturn(null);
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> res = OOMTestUtils.getPropertiesToAdd(gatherer);
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(entity.getProperties(), res));
assertNull(OOMTestUtils.getPropertiesToRemove(gatherer));
}
@Test
public void extractsNothingWhenThereAreNoPropertiesForPersist() throws Exception {
when(mapperMock.getOriginalInstance(entity)).thenReturn(null);
strategy.buildAxiomValuesFromInstance(entity, gatherer);
assertNull(OOMTestUtils.getPropertiesToAdd(gatherer));
assertNull(OOMTestUtils.getPropertiesToRemove(gatherer));
}
@Test
public void extractsNothingWhenThereAreNotPropertiesToAddAndNoneInOriginal() throws Exception {
when(mapperMock.getOriginalInstance(entity)).thenReturn(createOriginal());
strategy.buildAxiomValuesFromInstance(entity, gatherer);
assertNull(OOMTestUtils.getPropertiesToAdd(gatherer));
assertNull(OOMTestUtils.getPropertiesToRemove(gatherer));
}
@Test
public void extractFieldValueSkipsNullPropertyIdentifier() throws Exception {
final int propCount = 5;
final Map<String, Set<String>> props = Generators.generateStringProperties(propCount, propCount);
entity.setProperties(new HashMap<>(props));
entity.getProperties().put(null, Collections.singleton("Test"));
when(mapperMock.getOriginalInstance(entity)).thenReturn(null);
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> res = OOMTestUtils.getPropertiesToAdd(gatherer);
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(props, res));
assertNull(OOMTestUtils.getPropertiesToRemove(gatherer));
}
@Test
public void extractFieldValueSkipsNullSetOfPropertyValues() throws Exception {
final Map<String, Set<String>> props = Generators.generateStringProperties();
entity.setProperties(new HashMap<>(props));
entity.getProperties().put("http://krizik.felk.cvut.cz/ontologies/nullProperty", null);
when(mapperMock.getOriginalInstance(entity)).thenReturn(null);
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> res = OOMTestUtils.getPropertiesToAdd(gatherer);
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(props, res));
assertNull(OOMTestUtils.getPropertiesToRemove(gatherer));
}
@Test
public void extractFieldValueSkipsNullPropertyValue() throws Exception {
final Map<String, Set<String>> props = Generators.generateStringProperties();
entity.setProperties(new HashMap<>());
props.forEach((key, val) -> entity.getProperties().put(key, new HashSet<>(val)));
final String property = entity.getProperties().keySet().iterator().next();
entity.getProperties().get(property).add(null);
when(mapperMock.getOriginalInstance(entity)).thenReturn(null);
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> res = OOMTestUtils.getPropertiesToAdd(gatherer);
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(props, res));
assertNull(OOMTestUtils.getPropertiesToRemove(gatherer));
}
private OWLClassB createOriginal() {
final OWLClassB original = new OWLClassB();
original.setUri(PK);
original.setStringAttribute(entity.getStringAttribute());
if (entity.getProperties() != null) {
final Map<String, Set<String>> copy = new HashMap<>();
for (Map.Entry<String, Set<String>> e : entity.getProperties().entrySet()) {
copy.put(e.getKey(), new HashSet<>(e.getValue()));
}
original.setProperties(copy);
}
return original;
}
@Test(expected = InvalidAssertionIdentifierException.class)
public void throwsExceptionWhenPropertyIsNotAValidURI() throws Exception {
entity.setProperties(Generators.generateStringProperties(1, 1));
entity.getProperties().put("blabla^", Collections.emptySet());
when(mapperMock.getOriginalInstance(entity)).thenReturn(null);
strategy.buildAxiomValuesFromInstance(entity, gatherer);
}
@Test
public void removesAllPropertiesWhenSetToNull() throws Exception {
final Map<String, Set<String>> properties = Generators.generateStringProperties();
entity.setProperties(properties);
when(mapperMock.getOriginalInstance(entity)).thenReturn(createOriginal());
entity.setProperties(null);
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> result = OOMTestUtils.getPropertiesToRemove(gatherer);
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(properties, result));
assertNull(OOMTestUtils.getPropertiesToAdd(gatherer));
}
@Test
public void removesAllPropertiesWhenCleared() throws Exception {
final Map<String, Set<String>> properties = Generators.generateStringProperties();
entity.setProperties(properties);
when(mapperMock.getOriginalInstance(entity)).thenReturn(createOriginal());
entity.setProperties(Collections.emptyMap());
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> result = OOMTestUtils.getPropertiesToRemove(gatherer);
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(properties, result));
assertNull(OOMTestUtils.getPropertiesToAdd(gatherer));
}
@Test
public void removesSeveralWholeProperties() throws Exception {
entity.setProperties(Generators.generateStringProperties(5, 5));
when(mapperMock.getOriginalInstance(entity)).thenReturn(createOriginal());
// Remove one property with values
final String property = entity.getProperties().keySet().iterator().next();
final Set<String> values = entity.getProperties().get(property);
entity.getProperties().remove(property);
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> result = OOMTestUtils.getPropertiesToRemove(gatherer);
assertTrue(TestEnvironmentUtils
.assertionsCorrespondToProperties(Collections.singletonMap(property, values), result));
assertNull(OOMTestUtils.getPropertiesToAdd(gatherer));
}
@Test
public void removesValuesOfSomeProperties() throws Exception {
entity.setProperties(Generators.generateStringProperties());
when(mapperMock.getOriginalInstance(entity)).thenReturn(createOriginal());
final Map<String, Set<String>> toRemove = new HashMap<>();
int propIndex = 0;
for (Map.Entry<String, Set<String>> e : entity.getProperties().entrySet()) {
if (propIndex++ % 2 != 0) {
toRemove.put(e.getKey(), new HashSet<>());
int valueIndex = 0;
final Iterator<String> it = e.getValue().iterator();
while (it.hasNext()) {
final String val = it.next();
if (valueIndex++ % 2 != 0) {
toRemove.get(e.getKey()).add(val);
it.remove();
}
}
}
}
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> result = OOMTestUtils.getPropertiesToRemove(gatherer);
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(toRemove, result));
assertNull(OOMTestUtils.getPropertiesToAdd(gatherer));
}
@Test
public void addsPropertyWithValues() throws Exception {
entity.setProperties(Generators.generateStringProperties(3, 3));
when(mapperMock.getOriginalInstance(entity)).thenReturn(createOriginal());
final Map<String, Set<String>> added = new HashMap<>();
added.put("http://krizik.felk.cvut.cz/ontologies/jopa#added",
new HashSet<>(Arrays.asList("one", "two", "http://three")));
entity.getProperties().putAll(added);
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> result = OOMTestUtils.getPropertiesToAdd(gatherer);
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(added, result));
assertNull(OOMTestUtils.getPropertiesToRemove(gatherer));
}
@Test
public void addsValuesForExistingProperty() throws Exception {
entity.setProperties(Generators.generateStringProperties(3, 3));
when(mapperMock.getOriginalInstance(entity)).thenReturn(createOriginal());
final Map<String, Set<String>> added = new HashMap<>();
final String property = entity.getProperties().keySet().iterator().next();
final Set<String> newValues = new HashSet<>(Arrays.asList("one", "seven", "http://krizik.felk.cvut.cz/valueS"));
added.put(property, newValues);
entity.getProperties().get(property).addAll(newValues);
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> result = OOMTestUtils.getPropertiesToAdd(gatherer);
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(added, result));
assertNull(OOMTestUtils.getPropertiesToRemove(gatherer));
}
// This is a mix of the previous tests
@Test
public void addsAndRemovesSomePropertiesWithValuesAndPropertyValues() throws Exception {
entity.setProperties(Generators.generateStringProperties(3, 3));
when(mapperMock.getOriginalInstance(entity)).thenReturn(createOriginal());
final Map<String, Set<String>> removed = prepareForRemove();
final Map<String, Set<String>> added = prepareForAdd();
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> resultAdded = OOMTestUtils.getPropertiesToAdd(gatherer);
final Map<Assertion, Set<Value<?>>> resultRemoved = OOMTestUtils.getPropertiesToRemove(gatherer);
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(added, resultAdded));
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(removed, resultRemoved));
}
@Test
public void extractsAllPropertiesForAddWhenThereWereNoneInOriginal() throws Exception {
when(mapperMock.getOriginalInstance(entity)).thenReturn(createOriginal());
assertNull(mapperMock.getOriginalInstance(entity).getProperties());
entity.setProperties(Generators.generateStringProperties(3, 3));
strategy.buildAxiomValuesFromInstance(entity, gatherer);
final Map<Assertion, Set<Value<?>>> resultAdded = OOMTestUtils.getPropertiesToAdd(gatherer);
assertTrue(TestEnvironmentUtils.assertionsCorrespondToProperties(entity.getProperties(), resultAdded));
}
private Map<String, Set<String>> prepareForAdd() {
final Map<String, Set<String>> added = new HashMap<>();
final String property = entity.getProperties().keySet().iterator().next();
final Set<String> newValues = new HashSet<>(Arrays.asList("one", "seven", "http://krizik.felk.cvut.cz/valueS"));
added.put(property, newValues);
entity.getProperties().get(property).addAll(newValues);
final Map<String, Set<String>> newProperty = Collections
.singletonMap("http://krizik.felk.cvut.cz/ontologies/jopa#newProperty",
new HashSet<>(Arrays.asList("blablaOne", "blablaTwo", "http://blabla.org")));
added.putAll(newProperty);
entity.getProperties().putAll(newProperty);
return added;
}
private Map<String, Set<String>> prepareForRemove() {
final Map<String, Set<String>> removed = new HashMap<>();
String propertyToUpdate = null;
String propertyToRemove = null;
Iterator<String> it = entity.getProperties().keySet().iterator();
while (it.hasNext()) {
propertyToUpdate = propertyToRemove;
propertyToRemove = it.next();
}
removed.put(propertyToRemove, entity.getProperties().get(propertyToRemove));
entity.getProperties().remove(propertyToRemove);
final Set<String> removedValues = new HashSet<>();
it = entity.getProperties().get(propertyToUpdate).iterator();
int ind = 0;
while (it.hasNext()) {
String val = it.next();
if (ind++ % 2 != 0) {
it.remove();
removedValues.add(val);
}
}
removed.put(propertyToUpdate, removedValues);
return removed;
}
@Test
public void buildsInstanceFieldFroAxiomValues() throws Exception {
final Map<String, Set<String>> properties = Generators.generateStringProperties();
final Collection<Axiom<?>> axioms = createAxiomsForProperties(properties);
axioms.forEach(ax -> strategy.addValueFromAxiom(ax));
strategy.buildInstanceFieldValue(entity);
assertEquals(properties, entity.getProperties());
}
private Collection<Axiom<?>> createAxiomsForProperties(Map<String, Set<String>> data) {
final NamedResource subject = NamedResource.create(PK);
final Collection<Axiom<?>> axioms = new ArrayList<>();
for (Map.Entry<String, Set<String>> e : data.entrySet()) {
axioms.addAll(e.getValue().stream()
.map(val -> new AxiomImpl<>(subject,
Assertion.createPropertyAssertion(URI.create(e.getKey()), false), new Value<>(val)))
.collect(Collectors.toList()));
}
return axioms;
}
}