-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExpandedJsonLdDeserializerTest.java
513 lines (440 loc) · 22.6 KB
/
ExpandedJsonLdDeserializerTest.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
/**
* 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.expanded;
import com.github.jsonldjava.core.JsonLdProcessor;
import com.github.jsonldjava.utils.JsonUtils;
import cz.cvut.kbss.jopa.model.annotations.Properties;
import cz.cvut.kbss.jopa.model.annotations.*;
import cz.cvut.kbss.jsonld.ConfigParam;
import cz.cvut.kbss.jsonld.Configuration;
import cz.cvut.kbss.jsonld.deserialization.JsonLdDeserializer;
import cz.cvut.kbss.jsonld.environment.TestUtil;
import cz.cvut.kbss.jsonld.environment.Vocabulary;
import cz.cvut.kbss.jsonld.environment.model.*;
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
import cz.cvut.kbss.jsonld.exception.TargetTypeException;
import cz.cvut.kbss.jsonld.exception.UnknownPropertyException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.util.*;
import static cz.cvut.kbss.jsonld.environment.TestUtil.*;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
@SuppressWarnings("unused")
class ExpandedJsonLdDeserializerTest {
private static final Map<URI, User> USERS = initUsers();
private static final String ORG_NAME = "UNSC";
private static final String[] ORG_BRANDS = {"Spartan-II", "Mjolnir IV"};
private JsonLdDeserializer sut;
private static Map<URI, User> initUsers() {
final Map<URI, User> map = new HashMap<>();
map.put(HALSEY_URI, new User(HALSEY_URI, "Catherine", "Halsey", "halsey@unsc.org", true));
map.put(LASKY_URI, new User(LASKY_URI, "Thomas", "Lasky", "lasky@unsc.org", false));
map.put(PALMER_URI, new User(PALMER_URI, "Sarah", "Palmer", "palmer@unsc.org", false));
return map;
}
@BeforeEach
void setUp() {
this.sut = JsonLdDeserializer.createExpandedDeserializer();
}
@Test
void testDeserializeInstanceWithDataProperties() throws Exception {
final Object input = readAndExpand("objectWithDataProperties.json");
final User result = sut.deserialize(input, User.class);
verifyUserAttributes(USERS.get(HALSEY_URI), result);
}
private void verifyUserAttributes(User expected, User actual) {
assertEquals(expected.getUri(), actual.getUri());
assertEquals(expected.getAdmin(), actual.getAdmin());
assertEquals(expected.getFirstName(), actual.getFirstName());
assertEquals(expected.getLastName(), actual.getLastName());
assertEquals(expected.getUsername(), actual.getUsername());
}
@Test
void testDeserializeInstanceWithSingularObjectProperty() throws Exception {
final Object input = readAndExpand("objectWithSingularReference.json");
final Employee result = sut.deserialize(input, Employee.class);
verifyUserAttributes(USERS.get(HALSEY_URI), result);
assertNotNull(result.getEmployer());
verifyOrganizationAttributes(result.getEmployer());
}
private void verifyOrganizationAttributes(Organization result) {
assertEquals(TestUtil.UNSC_URI, result.getUri());
assertEquals(ORG_NAME, result.getName());
assertNotNull(result.getDateCreated());
for (String brand : ORG_BRANDS) {
assertTrue(result.getBrands().contains(brand));
}
}
@Test
void testDeserializeInstanceWithPluralObjectProperty() throws Exception {
final Object input = readAndExpand("objectWithPluralReference.json");
final Organization result = sut.deserialize(input, Organization.class);
verifyOrganizationAttributes(result);
assertEquals(3, result.getEmployees().size());
for (Employee e : result.getEmployees()) {
assertTrue(USERS.containsKey(e.getUri()));
verifyUserAttributes(USERS.get(e.getUri()), e);
}
}
@Test
void testDeserializeInstanceWithPluralObjectPropertyWithBackwardReferencesToOriginalInstance()
throws Exception {
final Object input = readAndExpand("objectWithPluralObjectPropertyWithBackwardReferences.json");
final Organization result = sut.deserialize(input, Organization.class);
verifyOrganizationAttributes(result);
assertEquals(3, result.getEmployees().size());
for (Employee e : result.getEmployees()) {
assertTrue(USERS.containsKey(e.getUri()));
verifyUserAttributes(USERS.get(e.getUri()), e);
assertNotNull(e.getEmployer());
assertSame(result, e.getEmployer());
}
}
@Test
void testDeserializeInstanceWithSingularObjectPropertyWithBackwardReference() throws Exception {
final Object input = readAndExpand("objectWithSingularObjectPropertyWithBackwardReference.json");
final Employee result = sut.deserialize(input, Employee.class);
verifyUserAttributes(USERS.get(HALSEY_URI), result);
final Organization org = result.getEmployer();
assertNotNull(org);
verifyOrganizationAttributes(org);
assertEquals(1, org.getEmployees().size());
assertSame(result, org.getEmployees().iterator().next());
}
@Test
void deserializationOfArrayWithOneInstanceReturnsProperCollection() throws Exception {
final Object input = readAndExpand("objectWithPluralReferenceContainingOneValue.json");
final Organization result = sut.deserialize(input, Organization.class);
verifyOrganizationAttributes(result);
assertEquals(1, result.getEmployees().size());
final Employee e = result.getEmployees().iterator().next();
verifyUserAttributes(USERS.get(e.getUri()), e);
}
@Test
void throwsUnknownPropertyExceptionWhenIgnoreIsNotConfiguredAndUnmappedPropertyIsEncountered()
throws Exception {
final Object input = readAndExpand("objectWithUnknownProperty.json");
final String property = "http://purl.org/dc/terms/description";
final UnknownPropertyException result = assertThrows(UnknownPropertyException.class,
() -> sut.deserialize(input, Organization.class));
assertEquals("No field matching property " + property + " was found in " + Organization.class +
" or its ancestors.", result.getMessage());
}
@Test
void skipsUnknownPropertyWhenIgnoreIsConfiguredAndUnmappedPropertyIsEncountered() throws Exception {
final Object input = readAndExpand("objectWithUnknownProperty.json");
sut.configuration().set(ConfigParam.IGNORE_UNKNOWN_PROPERTIES, Boolean.TRUE.toString());
final Organization result = sut.deserialize(input, Organization.class);
verifyOrganizationAttributes(result);
}
@Test
void deserializationResolvesReferenceInPluralPropertyWrappedInAnotherObject() throws Exception {
final Object input = readAndExpand("objectWithPluralReferenceSharingObject.json");
final Study result = sut.deserialize(input, Study.class);
assertNotNull(result.getName());
Organization org = null;
assertFalse(result.getParticipants().isEmpty());
for (Employee e : result.getParticipants()) {
if (org == null) {
org = e.getEmployer();
} else {
assertSame(org, e.getEmployer());
}
}
assertFalse(result.getMembers().isEmpty());
for (Employee e : result.getMembers()) {
assertSame(org, e.getEmployer());
}
}
@Test
void deserializationSetsValueOfTypesSpecification() throws Exception {
final Object input = readAndExpand("objectWithDataProperties.json");
final User result = sut.deserialize(input, User.class);
assertTrue(result.getTypes().contains(Vocabulary.AGENT));
assertFalse(result.getTypes().contains(Vocabulary.USER)); // Type of the class should not be in @Types
}
@Test
void deserializationThrowsExceptionWhenTypesAttributeDoesNotContainTargetClassType() throws Exception {
final Object input = readAndExpand("objectWithDataProperties.json");
final TargetTypeException result = assertThrows(TargetTypeException.class,
() -> sut.deserialize(input, Employee.class));
assertThat(result.getMessage(), containsString(
"Neither " + Employee.class + " nor any of its subclasses matches the types "));
}
@Test
void deserializationPopulatesPropertiesFieldWithUnmappedPropertiesFoundInInput() throws Exception {
final Object input = readAndExpand("objectWithUnmappedProperties.json");
final Person result = sut.deserialize(input, Person.class);
final User model = USERS.get(HALSEY_URI);
assertEquals(model.getUri(), result.getUri());
assertEquals(model.getFirstName(), result.getFirstName());
assertEquals(model.getLastName(), result.getLastName());
assertFalse(result.getProperties().isEmpty());
assertTrue(result.getProperties().containsKey(Vocabulary.USERNAME));
assertEquals(1, result.getProperties().get(Vocabulary.USERNAME).size());
assertEquals(model.getUsername(), result.getProperties().get(Vocabulary.USERNAME).iterator().next());
assertTrue(result.getProperties().containsKey(Vocabulary.IS_ADMIN));
assertEquals(1, result.getProperties().get(Vocabulary.IS_ADMIN).size());
assertEquals(Boolean.TRUE.toString(), result.getProperties().get(Vocabulary.IS_ADMIN).iterator().next());
}
@Test
void deserializationPopulatesTypedProperties() throws Exception {
final Object input = readAndExpand("objectWithUnmappedProperties.json");
final ClassWithProperties result = sut.deserialize(input, ClassWithProperties.class);
assertNotNull(result);
final User model = USERS.get(HALSEY_URI);
assertEquals(model.getUri(), result.uri);
assertTrue(result.properties.containsKey(URI.create(Vocabulary.FIRST_NAME)));
assertEquals(model.getFirstName(), result.properties.get(URI.create(Vocabulary.FIRST_NAME)).iterator().next());
assertTrue((Boolean) result.properties.get(URI.create(Vocabulary.IS_ADMIN)).iterator().next());
}
@OWLClass(iri = Vocabulary.PERSON)
public static class ClassWithProperties {
@Id
private URI uri;
@Properties
private Map<URI, Set<?>> properties;
public ClassWithProperties() {
}
}
@Test
void deserializationThrowsExceptionWhenMultipleValuesForSingularFieldAreEncountered() throws Exception {
final Object input = readAndExpand("objectWithAttributeCardinalityViolation.json");
final JsonLdDeserializationException result = assertThrows(JsonLdDeserializationException.class,
() -> sut.deserialize(input, Person.class));
assertThat(result.getMessage(),
containsString("Encountered multiple values of property " + Vocabulary.FIRST_NAME));
}
@Test
void deserializationThrowsExceptionWhenMultipleValuesOfUnmappedPropertyForPropertiesWithSingularValuesAreEncountered()
throws Exception {
final Object input = readAndExpand("objectWithAttributeCardinalityViolation.json");
final JsonLdDeserializationException result = assertThrows(JsonLdDeserializationException.class,
() -> sut.deserialize(input, ClassWithSingularProperties.class));
assertThat(result.getMessage(),
containsString("Encountered multiple values of property " + Vocabulary.FIRST_NAME));
}
@OWLClass(iri = Vocabulary.PERSON)
public static class ClassWithSingularProperties {
@Id
private URI uri;
@Properties
private Map<String, String> properties;
public ClassWithSingularProperties() {
}
}
@Test
void deserializationSupportsPlainIdentifierObjectPropertyValues() throws Exception {
final Object input = readAndExpand("objectWithPlainIdentifierObjectPropertyValue.json");
final Organization result = sut.deserialize(input, Organization.class);
assertNotNull(result);
assertEquals(URI.create("http://dbpedia.org/resource/Czech_Republic"), result.getCountry());
}
@Test
void deserializationSupportsObjectsWithBlankNodeIds() throws Exception {
final Object input = readAndExpand("objectWithBlankNodeIdentifier.json");
final User result = sut.deserialize(input, User.class);
assertNotNull(result);
assertNull(result.getUri());
}
@Test
void deserializationReturnsSubclassInstanceWhenTypesMatch() throws Exception {
final Configuration config = new Configuration();
config.set(ConfigParam.SCAN_PACKAGE, "cz.cvut.kbss.jsonld.environment.model");
this.sut = JsonLdDeserializer.createExpandedDeserializer(config);
final Object input = readAndExpand("objectWithDataProperties.json");
final Person result = sut.deserialize(input, Person.class);
assertTrue(result instanceof User);
}
@Test
void deserializationSupportsPolymorphismForCollections() throws Exception {
final Configuration config = new Configuration();
config.set(ConfigParam.SCAN_PACKAGE, "cz.cvut.kbss.jsonld.environment.model");
config.set(ConfigParam.IGNORE_UNKNOWN_PROPERTIES, Boolean.toString(true));
this.sut = JsonLdDeserializer.createExpandedDeserializer(config);
final Object input = readAndExpand("objectWithPluralReference.json");
final PolymorphicOrganization result = sut.deserialize(input, PolymorphicOrganization.class);
assertNotNull(result.employees);
assertEquals(3, result.employees.size());
result.employees.forEach(e -> assertTrue(e instanceof Employee));
}
@OWLClass(iri = Vocabulary.ORGANIZATION)
public static class PolymorphicOrganization {
@Id
private URI id;
@OWLObjectProperty(iri = Vocabulary.HAS_MEMBER)
private Set<Person> employees;
public PolymorphicOrganization() {
}
}
@Test
void deserializationSupportsPolymorphismForAttributes() throws Exception {
final Configuration config = new Configuration();
config.set(ConfigParam.SCAN_PACKAGE, "cz.cvut.kbss.jsonld.environment.model");
config.set(ConfigParam.IGNORE_UNKNOWN_PROPERTIES, Boolean.toString(true));
this.sut = JsonLdDeserializer.createExpandedDeserializer(config);
final Object input = readAndExpand("objectWithSingularPolymorphicReference.json");
final PolymorphicPerson result = sut.deserialize(input, PolymorphicPerson.class);
assertTrue(result.friend instanceof Employee);
}
@OWLClass(iri = Vocabulary.PERSON)
public static class PolymorphicPerson {
@Id
private URI id;
@OWLObjectProperty(iri = Vocabulary.KNOWS)
private Person friend;
public PolymorphicPerson() {
}
}
@Test
void deserializationSupportsDeserializingObjectAsPlainIdentifier() throws Exception {
final Configuration config = new Configuration();
config.set(ConfigParam.IGNORE_UNKNOWN_PROPERTIES, Boolean.toString(true));
this.sut = JsonLdDeserializer.createExpandedDeserializer(config);
final Object input = readAndExpand("objectWithSingularPolymorphicReference.json");
final PersonWithPlainIdentifierAttribute result =
sut.deserialize(input, PersonWithPlainIdentifierAttribute.class);
assertEquals(URI.create("http://krizik.felk.cvut.cz/ontologies/jb4jsonld#Sarah+Palmer"), result.friend);
}
@OWLClass(iri = Vocabulary.PERSON)
public static class PersonWithPlainIdentifierAttribute {
@Id
private URI id;
@OWLObjectProperty(iri = Vocabulary.KNOWS)
private URI friend;
public PersonWithPlainIdentifierAttribute() {
}
}
@Test
void deserializationSupportsDeserializingObjectsAsPluralPlainIdentifiers() throws Exception {
final Configuration config = new Configuration();
config.set(ConfigParam.IGNORE_UNKNOWN_PROPERTIES, Boolean.toString(true));
this.sut = JsonLdDeserializer.createExpandedDeserializer(config);
final Object input = readAndExpand("objectWithPluralReference.json");
final OrganizationWithPlainIdentifiers result =
sut.deserialize(input, OrganizationWithPlainIdentifiers.class);
assertNotNull(result.members);
assertTrue(result.members
.contains(URI.create("http://krizik.felk.cvut.cz/ontologies/jb4jsonld#Catherine+Halsey")));
assertTrue(result.members.contains(URI.create("http://krizik.felk.cvut.cz/ontologies/jb4jsonld#Thomas+Lasky")));
assertTrue(result.members.contains(URI.create("http://krizik.felk.cvut.cz/ontologies/jb4jsonld#Sarah+Palmer")));
}
@OWLClass(iri = Vocabulary.ORGANIZATION)
public static class OrganizationWithPlainIdentifiers {
@Id
private URI id;
@OWLObjectProperty(iri = Vocabulary.HAS_MEMBER)
private Set<URI> members;
public OrganizationWithPlainIdentifiers() {
}
}
@Test
void deserializationHandlesJsonLdListOfValues() throws Exception {
final Configuration config = new Configuration();
config.set(ConfigParam.IGNORE_UNKNOWN_PROPERTIES, Boolean.toString(true));
this.sut = JsonLdDeserializer.createExpandedDeserializer(config);
final Object input = readAndExpand("objectWithList.json");
final OrganizationWithListOfMembers result =
sut.deserialize(input, OrganizationWithListOfMembers.class);
assertEquals(3, result.members.size());
assertEquals(HALSEY_URI, result.members.get(0).getUri());
assertEquals(LASKY_URI, result.members.get(1).getUri());
assertEquals(PALMER_URI, result.members.get(2).getUri());
}
@OWLClass(iri = Vocabulary.ORGANIZATION)
public static class OrganizationWithListOfMembers {
@Id
private URI id;
@OWLObjectProperty(iri = Vocabulary.HAS_MEMBER)
private List<Employee> members;
public OrganizationWithListOfMembers() {
}
}
@Test
void deserializationReconstructsObjectFromMultiplePlaces() throws Exception {
final Object input = readAndExpand("objectWithDefinitionSpreadOverMultipleReferences.json");
final Employee result = sut.deserialize(input, Employee.class);
verifyUserAttributes(USERS.get(HALSEY_URI), result);
}
@Test
void deserializationPutsUnmappedObjectReferencesIntoProperties() throws Exception {
final Object input = readAndExpand("objectWithSingularReference.json");
final PersonWithoutSubclass result = sut.deserialize(input, PersonWithoutSubclass.class);
assertTrue(result.properties.containsKey(Vocabulary.IS_MEMBER_OF));
assertTrue(result.properties.get(Vocabulary.IS_MEMBER_OF).contains(TestUtil.UNSC_URI.toString()));
assertTrue(result.properties.containsKey(Vocabulary.USERNAME));
assertTrue(result.properties.get(Vocabulary.USERNAME).contains("halsey@unsc.org"));
}
@OWLClass(iri = Vocabulary.PERSON)
public static class PersonWithoutSubclass {
@Id
public URI uri;
@OWLDataProperty(iri = Vocabulary.FIRST_NAME)
private String firstName;
@OWLDataProperty(iri = Vocabulary.LAST_NAME)
private String lastName;
@Properties
private Map<String, Set<String>> properties;
}
@Test
void deserializationPutsUnmappedObjectReferencesIntoTypedProperties() throws Exception {
final Object input = readAndExpand("objectWithSingularReference.json");
final ClassWithProperties result = sut.deserialize(input, ClassWithProperties.class);
assertTrue(result.properties.containsKey(URI.create(Vocabulary.IS_MEMBER_OF)));
assertTrue(result.properties.get(URI.create(Vocabulary.IS_MEMBER_OF)).contains(TestUtil.UNSC_URI));
}
@Test
void deserializationParsesNumericTimestampForDateField() throws Exception {
final Object input = readAndExpand("objectWithPluralReference.json");
final Organization result = sut.deserialize(input, Organization.class);
assertNotNull(result.getDateCreated());
assertTrue(result.getDateCreated().before(new Date()));
}
@Test
void deserializationIgnoresPropertyWithReadOnlyAccess() throws Exception {
final Object input = readAndExpand("objectWithReadOnlyPropertyValue.json");
final Study result = sut.deserialize(input, Study.class);
assertNull(result.getNoOfPeopleInvolved());
}
@Test
void deserializationHandlesAnnotationPropertyValuesWhichMixLiteralAndReferenceValues() throws Exception {
final Object input = readAndExpand("objectWithAnnotationPropertyReferenceValues.json");
final ObjectWithAnnotationProperties result = sut.deserialize(input, ObjectWithAnnotationProperties.class);
assertNotNull(result);
assertThat(result.getOrigins(), hasItems(URI.create("http://dbpedia.org/resource/Czech_Republic"), "TermIt"));
}
@Test
void deserializationHandlesSingularAnnotationPropertyWithReferenceValue() throws Exception {
final Object jsonLd = JsonUtils.fromString("{" +
" \"@id\": \"http://krizik.felk.cvut.cz/ontologies/jb4jsonld#ChangeRecord01\"," +
" \"@type\": \"http://krizik.felk.cvut.cz/ontologies/jb4jsonld/ObjectWithAnnotations\"," +
" \"http://krizik.felk.cvut.cz/ontologies/jb4jsonld/origin\": {" +
" \"@id\": \"http://dbpedia.org/resource/Czech_Republic\"" +
" }}");
final Object expanded = JsonLdProcessor.expand(jsonLd);
final ObjectWithAnnotationProperty result = sut.deserialize(expanded, ObjectWithAnnotationProperty.class);
assertNotNull(result);
assertEquals(URI.create("http://dbpedia.org/resource/Czech_Republic"), result.origin);
}
@OWLClass(iri = Vocabulary.OBJECT_WITH_ANNOTATIONS)
public static class ObjectWithAnnotationProperty {
@Id
private URI uri;
@OWLAnnotationProperty(iri = Vocabulary.ORIGIN)
private Object origin;
}
}