-
Notifications
You must be signed in to change notification settings - Fork 2
/
DefaultInstanceBuilderTest.java
400 lines (354 loc) · 16.9 KB
/
DefaultInstanceBuilderTest.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
/**
* Copyright (C) 2017 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.jsonld.deserialization;
import cz.cvut.kbss.jopa.model.annotations.Id;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.common.CollectionType;
import cz.cvut.kbss.jsonld.deserialization.util.TargetClassResolver;
import cz.cvut.kbss.jsonld.environment.Generator;
import cz.cvut.kbss.jsonld.environment.TestUtil;
import cz.cvut.kbss.jsonld.environment.Vocabulary;
import cz.cvut.kbss.jsonld.environment.model.Employee;
import cz.cvut.kbss.jsonld.environment.model.Organization;
import cz.cvut.kbss.jsonld.environment.model.Person;
import cz.cvut.kbss.jsonld.environment.model.User;
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.*;
import static org.junit.Assert.*;
public class DefaultInstanceBuilderTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private InstanceBuilder deserializer = new DefaultInstanceBuilder(new TargetClassResolver(TestUtil.getDefaultTypeMap()));
@Test
public void getCurrentRootReturnsNullIfThereIsNoRoot() {
assertNull(deserializer.getCurrentRoot());
}
@Test
public void openObjectCreatesNewInstanceOfSpecifiedClass() {
deserializer.openObject(Person.class);
final Object res = deserializer.getCurrentRoot();
assertNotNull(res);
assertTrue(res instanceof Person);
}
@Test
public void openCollectionCreatesSet() {
deserializer.openCollection(CollectionType.SET);
assertTrue(deserializer.getCurrentRoot() instanceof Set);
}
@Test
public void openCollectionInCollectionPushesOriginalCollectionToStack() throws Exception {
deserializer.openCollection(CollectionType.SET);
deserializer.openCollection(CollectionType.SET);
assertTrue(deserializer.getCurrentRoot() instanceof Set);
assertFalse(getOpenInstances().isEmpty());
assertTrue(getOpenInstances().peek().getInstance() instanceof Set);
assertNotSame(deserializer.getCurrentRoot(), getOpenInstances().peek().getInstance());
}
@Test
public void openObjectAddsObjectToCurrentlyOpenCollectionAndBecomesCurrentInstance() throws Exception {
deserializer.openCollection(CollectionType.SET);
deserializer.openObject(Employee.class);
final Object root = deserializer.getCurrentRoot();
assertTrue(root instanceof Employee);
assertFalse(getOpenInstances().isEmpty());
final Object stackTop = getOpenInstances().peek().getInstance();
assertTrue(stackTop instanceof Set);
final Set<?> set = (Set<?>) stackTop;
assertEquals(1, set.size());
assertSame(root, set.iterator().next());
}
@SuppressWarnings("unchecked")
private Stack<InstanceContext<?>> getOpenInstances() throws Exception {
final Field stackField = DefaultInstanceBuilder.class.getDeclaredField("openInstances");
stackField.setAccessible(true);
return (Stack<InstanceContext<?>>) stackField.get(deserializer);
}
private InstanceContext<?> getCurrentInstance() throws Exception {
final Field instField = DefaultInstanceBuilder.class.getDeclaredField("currentInstance");
instField.setAccessible(true);
return (InstanceContext<?>) instField.get(deserializer);
}
@Test
public void closeObjectPopsPreviousInstanceFromTheStack() throws Exception {
deserializer.openCollection(CollectionType.SET);
final Object originalRoot = deserializer.getCurrentRoot();
assertTrue(originalRoot instanceof Set);
deserializer.openObject(Employee.class);
assertFalse(getOpenInstances().isEmpty());
deserializer.closeObject();
assertTrue(getOpenInstances().isEmpty());
assertSame(originalRoot, deserializer.getCurrentRoot());
}
@Test
public void closeObjectDoesNothingForRootObject() throws Exception {
deserializer.openObject(Person.class);
assertTrue(getOpenInstances().isEmpty());
final Object root = deserializer.getCurrentRoot();
deserializer.closeObject();
assertTrue(getOpenInstances().isEmpty());
assertSame(root, deserializer.getCurrentRoot());
}
@Test
public void addValueAddsValuesToCurrentlyOpenCollection() {
final List<Integer> items = new ArrayList<>();
for (int i = 0; i < Generator.randomCount(10); i++) {
items.add(Generator.randomCount(Integer.MAX_VALUE));
}
deserializer.openCollection(CollectionType.LIST);
items.forEach(item -> deserializer.addValue(item));
assertTrue(deserializer.getCurrentRoot() instanceof List);
final List<?> result = (List<?>) deserializer.getCurrentRoot();
for (int i = 0; i < items.size(); i++) {
assertEquals(items.get(i), result.get(i));
}
}
@Test
public void openObjectByPropertyCreatesObjectOfCorrectType() {
deserializer.openObject(Employee.class);
deserializer.openObject(Vocabulary.IS_MEMBER_OF, Collections.singletonList(Vocabulary.ORGANIZATION));
assertTrue(deserializer.getCurrentRoot() instanceof Organization);
}
@Test
public void openObjectByPropertySetsNewInstanceAsFieldValueAndReplacesCurrentContext() throws Exception {
deserializer.openObject(Employee.class);
deserializer.openObject(Vocabulary.IS_MEMBER_OF, Collections.singletonList(Vocabulary.ORGANIZATION));
assertFalse(getOpenInstances().isEmpty());
final Object top = getOpenInstances().peek().getInstance();
assertTrue(top instanceof Employee);
assertNotNull(((Employee) top).getEmployer());
}
@Test
public void addValueSetsPropertyValue() {
deserializer.openObject(User.class);
final String firstName = "Catherine";
final String lastName = "Halsey";
deserializer.addValue(Vocabulary.FIRST_NAME, firstName);
deserializer.addValue(Vocabulary.LAST_NAME, lastName);
deserializer.addValue(Vocabulary.IS_ADMIN, true);
final User res = (User) deserializer.getCurrentRoot();
assertEquals(firstName, res.getFirstName());
assertEquals(lastName, res.getLastName());
assertTrue(res.getAdmin());
}
@Test
public void openCollectionByPropertyCreatesCollectionOfCorrectType() throws Exception {
deserializer.openObject(Organization.class);
deserializer.openCollection(Vocabulary.HAS_MEMBER);
assertFalse(getOpenInstances().isEmpty());
assertTrue(deserializer.getCurrentRoot() instanceof Set);
}
@Test
public void openCollectionByPropertySetsFieldValueAndPushesCurrentInstanceToStack() throws Exception {
deserializer.openObject(Organization.class);
deserializer.openCollection(Vocabulary.HAS_MEMBER);
assertFalse(getOpenInstances().isEmpty());
assertTrue(getOpenInstances().peek().getInstance() instanceof Organization);
final Organization org = (Organization) getOpenInstances().peek().getInstance();
assertSame(deserializer.getCurrentRoot(), org.getEmployees());
}
@Test
public void closeCollectionPopsLastInstanceFromStack() throws Exception {
deserializer.openObject(Organization.class);
final Object originalRoot = deserializer.getCurrentRoot();
deserializer.openCollection(Vocabulary.HAS_MEMBER);
assertFalse(getOpenInstances().isEmpty());
deserializer.closeCollection();
assertTrue(getOpenInstances().isEmpty());
assertSame(originalRoot, deserializer.getCurrentRoot());
}
@Test
public void closeCollectionDoesNothingWhenItIsTheTopElement() throws Exception {
deserializer.openCollection(CollectionType.SET);
assertTrue(getOpenInstances().isEmpty());
final Object root = deserializer.getCurrentRoot();
deserializer.closeCollection();
assertTrue(getOpenInstances().isEmpty());
assertSame(root, deserializer.getCurrentRoot());
}
@Test
public void objectIsStoredInKnownInstancesWhenItsIdIsRead() throws Exception {
final User user = Generator.generateUser();
deserializer.openObject(User.class);
deserializer.addValue(JsonLd.ID, user.getUri().toString());
assertTrue(getKnownInstances().containsKey(user.getUri().toString()));
assertTrue(getKnownInstances().get(user.getUri().toString()) instanceof User);
}
@SuppressWarnings("unchecked")
private Map<String, Object> getKnownInstances() throws Exception {
final Field field = DefaultInstanceBuilder.class.getDeclaredField("knownInstances");
field.setAccessible(true);
return (Map<String, Object>) field.get(deserializer);
}
@Test
public void openCollectionCreatesTypesContextForTypesField() throws Exception {
deserializer.openObject(User.class);
deserializer.openCollection(JsonLd.TYPE);
final InstanceContext<?> result = getCurrentInstance();
assertTrue(result instanceof TypesContext);
final TypesContext<?, ?> ctx = (TypesContext<?, ?>) result;
assertEquals(String.class, ctx.getItemType());
}
@Test
public void isPropertyMappedReturnsTrueForMappedProperty() {
deserializer.openObject(Person.class);
assertTrue(deserializer.isPropertyMapped(Vocabulary.FIRST_NAME));
}
@Test
public void isPropertyMappedReturnsFalseForUnknownProperty() {
deserializer.openObject(Organization.class);
assertFalse(deserializer.isPropertyMapped(Vocabulary.IS_ADMIN));
}
@Test
public void isPropertyMappedReturnsTrueForType() {
deserializer.openObject(Person.class);
assertTrue(deserializer.isPropertyMapped(JsonLd.TYPE));
}
@Test
public void isPluralReturnsTrueForType() {
deserializer.openObject(Person.class);
assertTrue(deserializer.isPlural(JsonLd.TYPE));
}
@Test
public void openCollectionOpensDummyCollectionContextForTypeInInstanceWithoutTypesField() throws Exception {
deserializer.openObject(Person.class);
assertTrue(getCurrentInstance() instanceof SingularObjectContext);
deserializer.openCollection(JsonLd.TYPE);
assertTrue(getCurrentInstance() instanceof DummyCollectionInstanceContext);
deserializer.closeCollection();
assertTrue(getCurrentInstance() instanceof SingularObjectContext);
}
@Test
public void addingTypesToInstanceWithoutTypesFieldDoesNothing() {
deserializer.openObject(Person.class);
deserializer.openCollection(JsonLd.TYPE);
deserializer.addValue(Vocabulary.EMPLOYEE);
deserializer.addValue(Vocabulary.USER);
deserializer.closeCollection();
assertNotNull(deserializer.getCurrentRoot());
}
@Test
public void addValueOpensCollectionAndAddsSingleItemToItWhenSingleValueForPluralAttributeIsSpecified() {
deserializer.openObject(Organization.class);
final String value = "Mjolnir-IV";
deserializer.addValue(Vocabulary.BRAND, value);
final Organization object = (Organization) deserializer.getCurrentRoot();
assertEquals(1, object.getBrands().size());
assertTrue(object.getBrands().contains(value));
}
@Test
public void openCollectionCreatesPropertiesContextAndSetsNewPropertiesMapOnTargetInstanceWhenItDoesNotExist() {
deserializer.openObject(Person.class);
final Person instance = (Person) deserializer.getCurrentRoot();
assertNull(instance.getProperties());
deserializer.openCollection(Vocabulary.IS_ADMIN);
assertNotNull(instance.getProperties());
}
@Test
public void openCollectionReusesPropertiesInstanceWhenItAlreadyExistsOnTargetInstance() {
deserializer.openObject(Person.class);
final Person instance = (Person) deserializer.getCurrentRoot();
assertNull(instance.getProperties());
deserializer.openCollection(Vocabulary.IS_ADMIN);
final Map<String, Set<String>> propsMap = instance.getProperties();
deserializer.closeCollection();
deserializer.openCollection(Vocabulary.USERNAME);
assertSame(propsMap, instance.getProperties());
}
@Test
public void openObjectCreatesIdentifierContextWhenTargetTypeIsIdentifier() throws Exception {
deserializer.openCollection(CollectionType.LIST);
deserializer.openObject(URI.class);
final InstanceContext<?> ctx = getCurrentInstance();
assertTrue(ctx instanceof NodeReferenceContext);
}
@Test
public void openObjectCreatesIdentifierContextForPropertyWithPlainIdentifierTargetFieldType() throws Exception {
deserializer.openObject(Organization.class);
deserializer.openObject(Vocabulary.ORIGIN, Collections.emptyList());
final InstanceContext<?> ctx = getCurrentInstance();
assertTrue(ctx instanceof NodeReferenceContext);
}
@Test
public void addNodeReferenceForPropertySetsPlainIdentifierObjectPropertyValue() {
deserializer.openObject(Organization.class);
final String uri = Generator.generateUri().toString();
deserializer.addNodeReference(Vocabulary.ORIGIN, uri);
final Organization object = (Organization) deserializer.getCurrentRoot();
assertEquals(uri, object.getCountry().toString());
}
@Test
public void addNodeReferenceForPropertySetsFieldValueToKnownInstance() throws Exception {
final Organization org = Generator.generateOrganization();
getKnownInstances().put(org.getUri().toString(), org);
deserializer.openObject(Employee.class);
deserializer.addNodeReference(Vocabulary.IS_MEMBER_OF, org.getUri().toString());
final Employee object = (Employee) deserializer.getCurrentRoot();
assertEquals(org, object.getEmployer());
}
@Test
public void addNodeReferenceForPropertyThrowsDeserializationExceptionWhenNoKnownInstanceIsFound() {
final String nodeId = Generator.generateUri().toString();
thrown.expect(JsonLdDeserializationException.class);
thrown.expectMessage(
"Node with IRI " + nodeId + " cannot be referenced, because it has not been encountered yet.");
deserializer.openObject(Employee.class);
deserializer.addNodeReference(Vocabulary.IS_MEMBER_OF, nodeId);
}
@Test
public void addNodeReferenceAddsPlainIdentifierObjectPropertyValueToCollection() {
final URI nodeId = Generator.generateUri();
deserializer.openObject(Event.class);
deserializer.openCollection(Vocabulary.HAS_EVENT_TYPE);
deserializer.addNodeReference(nodeId.toString());
deserializer.closeCollection();
final Event object = (Event) deserializer.getCurrentRoot();
assertTrue(object.eventTypes.contains(nodeId));
}
@OWLClass(iri = Generator.URI_BASE + "Event")
public static class Event {
@Id
private URI uri;
@OWLObjectProperty(iri = Vocabulary.HAS_EVENT_TYPE)
private Set<URI> eventTypes;
}
@Test
public void addNodeReferenceAddsKnownInstanceWithMatchingIdentifier() throws Exception {
final Employee employee = Generator.generateEmployee();
getKnownInstances().put(employee.getUri().toString(), employee);
deserializer.openObject(Organization.class);
deserializer.openCollection(Vocabulary.HAS_MEMBER);
deserializer.addNodeReference(employee.getUri().toString());
deserializer.closeCollection();
final Organization object = (Organization) deserializer.getCurrentRoot();
assertTrue(object.getEmployees().contains(employee));
}
@Test
public void addNodeReferenceThrowsDeserializationExceptionWhenNoKnownInstanceIsFound() {
final String nodeId = Generator.generateUri().toString();
thrown.expect(JsonLdDeserializationException.class);
thrown.expectMessage(
"Node with IRI " + nodeId + " cannot be referenced, because it has not been encountered yet.");
deserializer.openObject(Organization.class);
deserializer.openCollection(Vocabulary.HAS_MEMBER);
deserializer.addNodeReference(nodeId);
}
}