-
Notifications
You must be signed in to change notification settings - Fork 3
/
JsonLdSerializationTest.java
264 lines (234 loc) · 10.8 KB
/
JsonLdSerializationTest.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
/**
* 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.jackson.serialization;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.cvut.kbss.jopa.model.annotations.Id;
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
import cz.cvut.kbss.jopa.model.annotations.Types;
import cz.cvut.kbss.jopa.vocabulary.RDF;
import cz.cvut.kbss.jopa.vocabulary.RDFS;
import cz.cvut.kbss.jsonld.exception.MissingTypeInfoException;
import cz.cvut.kbss.jsonld.jackson.JsonLdModule;
import cz.cvut.kbss.jsonld.jackson.environment.Generator;
import cz.cvut.kbss.jsonld.jackson.environment.StatementCopyingHandler;
import cz.cvut.kbss.jsonld.jackson.environment.Vocabulary;
import cz.cvut.kbss.jsonld.jackson.environment.model.Employee;
import cz.cvut.kbss.jsonld.jackson.environment.model.Organization;
import cz.cvut.kbss.jsonld.jackson.environment.model.User;
import cz.cvut.kbss.jsonld.serialization.JsonNodeFactory;
import cz.cvut.kbss.jsonld.serialization.ValueSerializer;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.rio.*;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
public class JsonLdSerializationTest {
private Repository repository;
private RepositoryConnection connection;
private RDFParser parser;
private JsonLdModule module;
private ObjectMapper objectMapper;
@BeforeEach
void setUp() {
initRepository();
this.parser = Rio.createParser(RDFFormat.JSONLD);
parser.setRDFHandler(new StatementCopyingHandler(connection));
this.module = new JsonLdModule();
this.objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
}
private void initRepository() {
this.repository = new SailRepository(new MemoryStore());
repository.init();
this.connection = repository.getConnection();
}
@AfterEach
void tearDown() {
connection.close();
repository.shutDown();
}
@Test
void testSerializeInstanceWithDataProperties() throws Exception {
final User user = Generator.generateUser();
serializeAndStore(user);
verifyUserAttributes(user);
}
private void serializeAndStore(Object instance)
throws RepositoryException, IOException, RDFParseException, RDFHandlerException {
final String result = objectMapper.writeValueAsString(instance);
connection.begin();
try (final ByteArrayInputStream bin = new ByteArrayInputStream(result.getBytes())) {
parser.parse(bin, null);
}
connection.commit();
}
private boolean contains(URI subject, String property, Object value) {
final ValueFactory vf = connection.getValueFactory();
Value rdfVal = null;
if (value instanceof URI) {
rdfVal = vf.createIRI(value.toString());
} else if (value instanceof Integer) {
rdfVal = vf.createLiteral((Integer) value);
} else if (value instanceof Long) {
rdfVal = vf.createLiteral((Long) value);
} else if (value instanceof Double) {
rdfVal = vf.createLiteral((Double) value);
} else if (value instanceof Boolean) {
rdfVal = vf.createLiteral((Boolean) value);
} else if (value instanceof Date) {
rdfVal = vf.createLiteral((Date) value);
} else if (value instanceof String) {
rdfVal = vf.createLiteral(value.toString());
}
return connection.getStatements(vf.createIRI(subject.toString()), vf.createIRI(property), rdfVal, false)
.hasNext();
}
private void verifyUserAttributes(User user) {
assertTrue(contains(user.getUri(), RDF.TYPE, URI.create(Vocabulary.PERSON)));
assertTrue(contains(user.getUri(), RDF.TYPE, URI.create(Vocabulary.USER)));
assertTrue(contains(user.getUri(), Vocabulary.FIRST_NAME, user.getFirstName()));
assertTrue(contains(user.getUri(), Vocabulary.LAST_NAME, user.getLastName()));
assertTrue(contains(user.getUri(), Vocabulary.USERNAME, user.getUsername()));
assertTrue(contains(user.getUri(), Vocabulary.IS_ADMIN, user.getAdmin()));
}
@Test
void testSerializeInstanceWithSingularReference() throws Exception {
final Employee employee = Generator.generateEmployee();
final Organization org = employee.getEmployer();
serializeAndStore(employee);
verifyUserAttributes(employee);
assertTrue(contains(employee.getUri(), Vocabulary.IS_MEMBER_OF, org.getUri()));
verifyOrganizationAttributes(org);
}
private void verifyOrganizationAttributes(Organization org) {
assertTrue(contains(org.getUri(), RDFS.LABEL, org.getName()));
// There is currently a grey zone of representing dates - we're using long timestamp, but RDF4J parses it XML integer
assertTrue(contains(org.getUri(), Vocabulary.DATE_CREATED, null));
for (String brand : org.getBrands()) {
assertTrue(contains(org.getUri(), Vocabulary.BRAND, brand));
}
}
@Test
void testSerializeInstanceWithPluralReference() throws Exception {
final Organization org = Generator.generateOrganization();
generateEmployeesForOrganization(org, false);
serializeAndStore(org);
verifyOrganizationAttributes(org);
for (Employee emp : org.getEmployees()) {
assertTrue(contains(org.getUri(), Vocabulary.HAS_MEMBER, emp.getUri()));
verifyUserAttributes(emp);
}
}
private void generateEmployeesForOrganization(Organization org, boolean backwardReference) {
for (int i = 0; i < Generator.randomCount(10); i++) {
final Employee emp = Generator.generateEmployee();
emp.setEmployer(backwardReference ? org : null);
org.addEmployee(emp);
}
}
@Test
void testSerializeInstanceWithPluralReferenceAndBackwardReferences() throws Exception {
final Organization org = Generator.generateOrganization();
generateEmployeesForOrganization(org, true);
serializeAndStore(org);
verifyOrganizationAttributes(org);
for (Employee emp : org.getEmployees()) {
assertTrue(contains(org.getUri(), Vocabulary.HAS_MEMBER, emp.getUri()));
assertTrue(contains(emp.getUri(), Vocabulary.IS_MEMBER_OF, org.getUri()));
verifyUserAttributes(emp);
}
}
@Test
void testSerializeCollectionOfInstances() throws Exception {
final Set<User> users = new HashSet<>();
for (int i = 0; i < Generator.randomCount(10); i++) {
users.add(Generator.generateUser());
}
serializeAndStore(users);
for (User u : users) {
verifyUserAttributes(u);
}
}
/**
* Jackson's {@link com.fasterxml.jackson.annotation.JsonTypeInfo} can be ignored, because the
* serialized/deserialized objects contain type information by virtue of the JSON-LD {@code @type} attribute.
*/
@Test
public void serializationIgnoresJsonTypeInfoConfiguration() throws Exception {
final User emp = Generator.generateEmployee();
serializeAndStore(emp);
assertTrue(contains(emp.getUri(), RDF.TYPE, URI.create(Vocabulary.PERSON)));
assertTrue(contains(emp.getUri(), RDF.TYPE, URI.create(Vocabulary.USER)));
assertTrue(contains(emp.getUri(), RDF.TYPE, URI.create(Vocabulary.EMPLOYEE)));
}
@Test
void serializationSupportsClassesWithoutOWLClassAnnotationButWithTypes() throws Exception {
final PersonNoOWLClass person = new PersonNoOWLClass();
person.uri = Generator.generateUri();
person.label = "test";
person.types = Collections.singleton(Vocabulary.PERSON);
serializeAndStore(person);
assertTrue(contains(person.uri, RDF.TYPE, URI.create(Vocabulary.PERSON)));
assertTrue(contains(person.uri, cz.cvut.kbss.jopa.vocabulary.RDFS.LABEL, person.label));
}
private static class PersonNoOWLClass {
@Id
private URI uri;
@OWLDataProperty(iri = cz.cvut.kbss.jopa.vocabulary.RDFS.LABEL)
private String label;
@Types
private Set<String> types;
}
@Test
void serializationFailsForInstanceWithoutTypeInfo() {
final PersonNoOWLClass person = new PersonNoOWLClass();
person.uri = Generator.generateUri();
person.label = "test";
final JsonMappingException result = assertThrows(JsonMappingException.class, () -> serializeAndStore(person));
assertThat(result.getCause(), is(instanceOf(MissingTypeInfoException.class)));
}
@Test
void serializationSkipsFieldsWithWriteOnlyAccess() throws Exception {
final User user = Generator.generateUser();
user.setPassword("test-117");
serializeAndStore(user);
assertFalse(contains(user.getUri(), Vocabulary.PASSWORD, null));
}
@Test
void serializationSupportsCustomSerializers() throws Exception {
final ValueSerializer<Boolean> custom = (value, ctx) -> JsonNodeFactory.createLiteralNode(ctx.getAttributeId(), value.toString());
module.registerSerializer(Boolean.class, custom);
final User user = Generator.generateUser();
serializeAndStore(user);
assertFalse(contains(user.getUri(), Vocabulary.IS_ADMIN, user.getAdmin()));
assertTrue(contains(user.getUri(), Vocabulary.IS_ADMIN, user.getAdmin().toString()));
}
}