-
Notifications
You must be signed in to change notification settings - Fork 2
/
CompactedJsonLdSerializerTest.java
525 lines (475 loc) · 24.7 KB
/
CompactedJsonLdSerializerTest.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
525
/**
* 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.jsonld.serialization;
import com.github.jsonldjava.core.JsonLdProcessor;
import com.github.jsonldjava.core.JsonLdUtils;
import com.github.jsonldjava.utils.JsonUtils;
import cz.cvut.kbss.jopa.model.MultilingualString;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
import cz.cvut.kbss.jopa.vocabulary.RDFS;
import cz.cvut.kbss.jsonld.ConfigParam;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.common.IdentifierUtil;
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.*;
import cz.cvut.kbss.jsonld.exception.MissingIdentifierException;
import cz.cvut.kbss.jsonld.serialization.util.BufferedJsonGenerator;
import org.hamcrest.core.StringStartsWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
@SuppressWarnings("unchecked")
class CompactedJsonLdSerializerTest {
private BufferedJsonGenerator jsonWriter;
private JsonLdSerializer sut;
@BeforeEach
void setUp() {
this.jsonWriter = new BufferedJsonGenerator();
this.sut = new CompactedJsonLdSerializer(jsonWriter);
}
// The following tests only verify validity of the output JSON-LD, no structure checks are performed
@Test
void testSerializeObjectWithDataProperties() throws Exception {
final User user = Generator.generateUser();
sut.serialize(user);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
}
@Test
void testSerializeCollectionOfObjects() throws Exception {
final Set<User> users = Generator.generateUsers();
sut.serialize(users);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
}
@Test
void testSerializeObjectWithSingularReference() throws Exception {
final Employee employee = Generator.generateEmployee();
sut.serialize(employee);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
final Map<String, ?> map = (Map<String, ?>) jsonObject;
verifyEmployee(employee, map);
}
@Test
void testSerializeObjectWithPluralReference() throws Exception {
final Organization org = Generator.generateOrganization();
generateEmployees(org, false); // No backward references for this test
sut.serialize(org);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
final Map<String, ?> map = (Map<String, ?>) jsonObject;
verifyOrganization(org, map);
}
private void generateEmployees(Organization org, boolean withBackwardReference) {
for (int i = 0; i < Generator.randomCount(5, 10); i++) {
final Employee emp = Generator.generateEmployee();
emp.setEmployer(withBackwardReference ? org : null);
org.addEmployee(emp);
}
}
@Test
void testSerializeObjectWithBackwardReferences() throws Exception {
final Organization org = Generator.generateOrganization();
generateEmployees(org, true);
sut.serialize(org);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
final Map<String, ?> map = (Map<String, ?>) jsonObject;
verifyOrganization(org, map);
}
@Test
void testSerializeObjectWithPluralReferences() throws Exception {
final Organization org = Generator.generateOrganization();
generateEmployees(org, true);
org.getEmployees().stream().filter(emp -> Generator.randomBoolean()).forEach(org::addAdmin);
if (org.getAdmins() == null || org.getAdmins().isEmpty()) {
org.setAdmins(new HashSet<>(Collections.singletonList(org.getEmployees().iterator().next())));
}
sut.serialize(org);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
final Map<String, ?> map = (Map<String, ?>) jsonObject;
verifyOrganization(org, map);
}
private void verifyOrganization(Organization org, Map<String, ?> json) {
assertEquals(org.getUri().toString(), json.get(JsonLd.ID));
assertEquals(org.getDateCreated().getTime(), json.get(Vocabulary.DATE_CREATED));
assertEquals(org.getBrands().size(), ((Collection<?>) json.get(Vocabulary.BRAND)).size());
assertTrue(org.getBrands().containsAll((Collection<?>) json.get(Vocabulary.BRAND)));
if (org.getAdmins() != null && !org.getAdmins().isEmpty()) {
assertTrue(json.get(Vocabulary.HAS_ADMIN) instanceof List);
final List<?> admins = (List<?>) json.get(Vocabulary.HAS_ADMIN);
verifyOrganizationMembers(admins, org.getAdmins());
}
if (org.getEmployees() != null && !org.getEmployees().isEmpty()) {
assertTrue(json.get(Vocabulary.HAS_MEMBER) instanceof List);
final List<?> members = (List<?>) json.get(Vocabulary.HAS_MEMBER);
verifyOrganizationMembers(members, org.getEmployees());
}
final List<?> types = (List<?>) json.get(JsonLd.TYPE);
assertTrue(types.contains(Vocabulary.ORGANIZATION));
}
private void verifyOrganizationMembers(List<?> json, Set<Employee> members) {
final Set<URI> memberUris = members.stream().map(Person::getUri).collect(Collectors.toSet());
for (Object member : json) {
assertTrue(member instanceof Map);
final Map<?, ?> memberMap = (Map<?, ?>) member;
final URI memberUri = URI.create(memberMap.get(JsonLd.ID).toString());
assertTrue(memberUris.contains(memberUri));
final Optional<Employee> e = members.stream().filter(emp -> emp.getUri().equals(memberUri))
.findFirst();
assert e.isPresent();
verifyEmployee(e.get(), memberMap);
}
}
private void verifyEmployee(Employee employee, Map<?, ?> json) {
assertEquals(employee.getUri().toString(), json.get(JsonLd.ID));
if (json.size() > 1) {
final List<?> types = (List<?>) json.get(JsonLd.TYPE);
assertTrue(types.contains(Vocabulary.EMPLOYEE));
assertTrue(types.contains(Vocabulary.USER));
assertTrue(types.contains(Vocabulary.PERSON));
assertEquals(employee.getFirstName(), json.get(Vocabulary.FIRST_NAME));
assertEquals(employee.getLastName(), json.get(Vocabulary.LAST_NAME));
assertEquals(employee.getUsername(), json.get(Vocabulary.USERNAME));
}
}
@Test
void serializationOfCollectionOfInstancesReferencingSameInstanceUsesReferenceNode() throws Exception {
final Organization org = Generator.generateOrganization();
generateEmployees(org, true);
final Set<Employee> employees = org.getEmployees();
org.setEmployees(null);
sut.serialize(employees);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
final List<?> jsonList = (List<?>) jsonObject;
for (int i = 1; i < jsonList.size(); i++) { // Start from 1, the first one contains the full object
final Map<?, ?> item = (Map<?, ?>) jsonList.get(i);
assertTrue(item.get(Vocabulary.IS_MEMBER_OF) instanceof Map);
final Map<?, ?> map = (Map<?, ?>) item.get(Vocabulary.IS_MEMBER_OF);
assertEquals(1, map.size());
assertEquals(org.getUri().toString(), map.get(JsonLd.ID));
}
}
@Test
void testSerializationOfObjectWithStringBasedUnmappedProperties() throws Exception {
final Person person = Generator.generatePerson();
sut.serialize(person);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertEquals(person.getUri().toString(), json.get(JsonLd.ID));
final List<?> types = (List<?>) json.get(JsonLd.TYPE);
assertEquals(1, types.size());
assertEquals(Vocabulary.PERSON, types.get(0));
assertEquals(person.getFirstName(), json.get(Vocabulary.FIRST_NAME));
assertEquals(person.getLastName(), json.get(Vocabulary.LAST_NAME));
for (Map.Entry<String, Set<String>> entry : person.getProperties().entrySet()) {
assertTrue(json.containsKey(entry.getKey()));
final List<?> values = (List<?>) json.get(entry.getKey());
assertNotNull(values);
assertEquals(entry.getValue().size(), values.size());
values.forEach(v -> assertTrue(entry.getValue().contains(v)));
}
}
@Test
void serializationPutsOwlClassAndTypesContentIntoOneTypeProperty() throws Exception {
final User user = Generator.generateUser();
final String type = Generator.URI_BASE + "TypeOne";
user.setTypes(Collections.singleton(type));
sut.serialize(user);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
final List<?> types = (List<?>) json.get(JsonLd.TYPE);
assertTrue(types.contains(Vocabulary.USER));
assertTrue(types.contains(type));
}
@Test
void serializationSkipsNullDataPropertyValues() throws Exception {
final User user = Generator.generateUser();
user.setAdmin(null);
sut.serialize(user);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertFalse(json.containsKey(Vocabulary.IS_ADMIN));
}
@Test
void serializationSkipsNullObjectPropertyValues() throws Exception {
final Employee employee = Generator.generateEmployee();
employee.setEmployer(null);
sut.serialize(employee);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertFalse(json.containsKey(Vocabulary.IS_MEMBER_OF));
}
@Test
void serializationSerializesPlainIdentifierObjectPropertyValue() throws Exception {
final Organization company = Generator.generateOrganization();
company.setCountry(URI.create("http://dbpedia.org/resource/Czech_Republic"));
sut.serialize(company);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
final Object value = json.get(Vocabulary.ORIGIN);
assertTrue(value instanceof Map);
final Map<String, ?> country = (Map<String, ?>) value;
assertEquals(1, country.size());
assertEquals(company.getCountry().toString(), country.get(JsonLd.ID));
}
@Test
void serializationGeneratesBlankNodeIfInstancesDoesNotHaveIdentifierValue() throws Exception {
final Organization company = Generator.generateOrganization();
company.setUri(null);
sut.serialize(company);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertTrue(json.containsKey(JsonLd.ID));
assertThat(json.get(JsonLd.ID).toString(), StringStartsWith.startsWith("_:"));
}
@Test
void serializationUsesGeneratedBlankNodeForObjectReference() throws Exception {
final Organization company = Generator.generateOrganization();
company.setUri(null);
final Employee employee = Generator.generateEmployee();
employee.setEmployer(company);
company.addEmployee(employee);
sut.serialize(company);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
final String id = (String) json.get(JsonLd.ID);
final List<?> employees = (List<?>) json.get(Vocabulary.HAS_MEMBER);
for (Object e : employees) {
final Map<?, ?> eMap = (Map<?, ?>) e;
final Map<?, ?> employer = (Map<?, ?>) eMap.get(Vocabulary.IS_MEMBER_OF);
assertEquals(id, employer.get(JsonLd.ID));
}
}
@Test
void serializationGeneratesBlankNodeIdentifierForInstanceOfClassWithoutIdentifierField() throws Exception {
final PersonWithoutIdentifier person = new PersonWithoutIdentifier();
person.firstName = "Thomas";
person.lastName = "Lasky";
sut.serialize(person);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
final String id = (String) json.get(JsonLd.ID);
assertNotNull(id);
assertThat(id, startsWith(IdentifierUtil.B_NODE_PREFIX));
}
@SuppressWarnings("unused")
@OWLClass(iri = Vocabulary.PERSON)
private static class PersonWithoutIdentifier {
@OWLDataProperty(iri = Vocabulary.FIRST_NAME)
private String firstName;
@OWLDataProperty(iri = Vocabulary.LAST_NAME)
private String lastName;
}
@Test
void serializationThrowsMissingIdentifierExceptionWhenNoIdentifierFieldIsFoundAndRequiredIdIsConfigured() {
sut.configuration().set(ConfigParam.REQUIRE_ID, Boolean.TRUE.toString());
final PersonWithoutIdentifier person = new PersonWithoutIdentifier();
person.firstName = "Thomas";
person.lastName = "Lasky";
assertThrows(MissingIdentifierException.class, () -> sut.serialize(person));
}
@Test
void serializationSkipsPropertiesWithWriteOnlyAccess() throws Exception {
final User user = Generator.generateUser();
user.setPassword("test-117");
sut.serialize(user);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertFalse(json.containsKey(Vocabulary.PASSWORD));
}
@Test
void serializationSerializesPropertyWithReadOnlyAccess() throws Exception {
final Study study = new Study();
study.setUri(Generator.generateUri());
study.setName("Test study");
study.setParticipants(Collections.singleton(Generator.generateEmployee()));
study.setMembers(Collections.singleton(Generator.generateEmployee()));
sut.serialize(study);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertTrue(json.containsKey(Vocabulary.NUMBER_OF_PEOPLE_INVOLVED));
assertEquals(study.getNoOfPeopleInvolved(), json.get(Vocabulary.NUMBER_OF_PEOPLE_INVOLVED));
}
@Test
void serializationSerializesAnnotationPropertyStringValueAsString() throws Exception {
final ObjectWithAnnotationProperties toSerialize = new ObjectWithAnnotationProperties(Generator.generateUri());
toSerialize.setChangedValue(Generator.generateUri().toString());
sut.serialize(toSerialize);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertTrue(json.containsKey(Vocabulary.CHANGED_VALUE));
assertEquals(toSerialize.getChangedValue(), json.get(Vocabulary.CHANGED_VALUE));
}
@Test
void serializationSerializesUrisOfAnnotationPropertyAttributeAsObjectsWithId() throws Exception {
final ObjectWithAnnotationProperties toSerialize = new ObjectWithAnnotationProperties(Generator.generateUri());
toSerialize
.setOrigins(IntStream.range(0, 5).mapToObj(i -> Generator.generateUri()).collect(Collectors.toSet()));
sut.serialize(toSerialize);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertTrue(json.containsKey(Vocabulary.ORIGIN));
final Object origins = json.get(Vocabulary.ORIGIN);
assertThat(origins, instanceOf(List.class));
final List<?> originList = (List<?>) origins;
assertEquals(toSerialize.getOrigins().size(), originList.size());
final Set<?> result = originList.stream().map(item -> {
assertThat(item, instanceOf(Map.class));
assertTrue(((Map<?, ?>) item).containsKey(JsonLd.ID));
return ((Map<?, ?>) item).get(JsonLd.ID);
}).collect(Collectors.toSet());
assertEquals(toSerialize.getOrigins().stream().map(Object::toString).collect(Collectors.toSet()), result);
}
@Test
void serializationSerializesEnumDataPropertyAsStringValueOfEnumConstant() throws Exception {
final User user = Generator.generateUser();
user.setRole(Role.ADMIN);
sut.serialize(user);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertTrue(json.containsKey(Vocabulary.ROLE));
assertEquals(Role.ADMIN.toString(), json.get(Vocabulary.ROLE));
}
@Test
void serializationSerializesConcreteValueOfFieldOfTypeObject() throws Exception {
final GenericMember instance = new GenericMember();
instance.setUri(Generator.generateUri());
instance.setMemberOf(Generator.generateOrganization());
sut.serialize(instance);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertTrue(json.containsKey(Vocabulary.IS_MEMBER_OF));
final Map<?, ?> org = (Map<?, ?>) json.get(Vocabulary.IS_MEMBER_OF);
assertFalse(org.isEmpty());
final List<String> types = (List<String>) org.get(JsonLd.TYPE);
assertThat(types, hasItem(Vocabulary.ORGANIZATION));
}
@Test
void serializationSerializesMultilingualStringWithValues() throws Exception {
final ObjectWithMultilingualString instance = new ObjectWithMultilingualString(Generator.generateUri());
final MultilingualString name = new MultilingualString();
name.set("en", "Leveraging Semantic Web Technologies in Domain-specific Information Systems");
name.set("cs", "Využití technologií sémantického webu v doménových informačních systémech");
instance.setLabel(name);
sut.serialize(instance);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertTrue(json.containsKey(RDFS.LABEL));
final List<?> label = (List<?>) json.get(RDFS.LABEL);
assertEquals(name.getValue().size(), label.size());
for (Object item : label) {
assertThat(item, instanceOf(Map.class));
final Map<?, ?> m = (Map<?, ?>) item;
assertTrue(m.containsKey(JsonLd.LANGUAGE));
assertTrue(m.containsKey(JsonLd.VALUE));
assertTrue(name.contains(m.get(JsonLd.LANGUAGE).toString()));
assertEquals(name.get(m.get(JsonLd.LANGUAGE).toString()), m.get(JsonLd.VALUE));
}
}
@Test
void serializationSerializesPluralMultilingualString() throws Exception {
final ObjectWithPluralMultilingualString instance = new ObjectWithPluralMultilingualString(
URI.create("http://onto.fel.cvut.cz/ontologies/jb4json-ld/concept#instance-117711"));
final MultilingualString one = new MultilingualString();
one.set("en", "Building");
one.set("cs", "Budova");
final MultilingualString two = new MultilingualString();
two.set("en", "Construction");
two.set("cs", "Stavba");
instance.setAltLabel(new HashSet<>(Arrays.asList(one, two)));
sut.serialize(instance);
final Object resultExpanded = JsonLdProcessor.expand(JsonUtils.fromString(jsonWriter.getResult()));
final Object expectedExpanded = TestUtil.readAndExpand("objectWithPluralMultilingualString.json");
assertTrue(JsonLdUtils.deepCompare(expectedExpanded, resultExpanded));
}
@Test
void serializationSerializesIndividualsInTypedUnmappedPropertiesAsObjects() throws Exception {
final PersonWithTypedProperties instance = new PersonWithTypedProperties();
instance.setUri(Generator.generateUri());
instance.setFirstName("Sarah");
instance.setLastName("Palmer");
instance.setProperties(new HashMap<>());
final URI someProperty = Generator.generateUri();
final String simpleValue = "Simple string value";
instance.getProperties().put(someProperty, Collections.singleton(simpleValue));
final Person friend = Generator.generatePerson();
instance.getProperties().put(URI.create(Vocabulary.KNOWS), Collections.singleton(friend));
sut.serialize(instance);
final Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertEquals(simpleValue, json.get(someProperty.toString()));
final Object friendObject = json.get(Vocabulary.KNOWS);
assertThat(friendObject, instanceOf(Map.class));
final Map<String, ?> friendJson = (Map<String, ?>) friendObject;
assertEquals(friend.getUri().toString(), friendJson.get(JsonLd.ID));
assertEquals(friend.getFirstName(), friendJson.get(Vocabulary.FIRST_NAME));
assertEquals(friend.getLastName(), friendJson.get(Vocabulary.LAST_NAME));
}
@Test
void serializationSerializesIdentifierInTypedUnmappedPropertiesAsObjectsWithId() throws Exception {
final PersonWithTypedProperties instance = new PersonWithTypedProperties();
instance.setUri(Generator.generateUri());
instance.setFirstName("Sarah");
instance.setLastName("Palmer");
instance.setProperties(new HashMap<>());
final URI someProperty = Generator.generateUri();
final Integer simpleValue = 4;
instance.getProperties().put(someProperty, Collections.singleton(simpleValue));
final URI friendId = Generator.generateUri();
instance.getProperties().put(URI.create(Vocabulary.KNOWS), Collections.singleton(friendId));
sut.serialize(instance);
final Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertEquals(simpleValue, json.get(someProperty.toString()));
final Object friendObject = json.get(Vocabulary.KNOWS);
assertThat(friendObject, instanceOf(Map.class));
final Map<String, ?> friendJson = (Map<String, ?>) friendObject;
assertEquals(friendId.toString(), friendJson.get(JsonLd.ID));
}
@Test
void serializationSerializesMultilingualStringInTypedUnmappedProperties() throws Exception {
final PersonWithTypedProperties instance = new PersonWithTypedProperties();
instance.setUri(Generator.generateUri());
final MultilingualString ms = MultilingualString.create("en", "Falcon");
ms.set("cs", "Sokol");
final URI property = URI.create("http://xmlns.com/foaf/0.1/nick");
instance.setProperties(Collections.singletonMap(property, Collections.singleton(ms)));
sut.serialize(instance);
final Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
final Map<String, ?> json = (Map<String, ?>) jsonObject;
assertTrue(json.containsKey(property.toString()));
final List<?> nick = (List<?>) json.get(property.toString());
assertEquals(ms.getValue().size(), nick.size());
for (Object item : nick) {
assertThat(item, instanceOf(Map.class));
final Map<?, ?> m = (Map<?, ?>) item;
assertTrue(m.containsKey(JsonLd.LANGUAGE));
assertTrue(m.containsKey(JsonLd.VALUE));
assertTrue(ms.contains(m.get(JsonLd.LANGUAGE).toString()));
assertEquals(ms.get(m.get(JsonLd.LANGUAGE).toString()), m.get(JsonLd.VALUE));
}
}
}