-
Notifications
You must be signed in to change notification settings - Fork 2
/
CompactedJsonLdSerializerTest.java
331 lines (299 loc) · 14.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
/**
* 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.serialization;
import com.github.jsonldjava.utils.JsonUtils;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
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.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.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 static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.*;
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));
}
}