-
Notifications
You must be signed in to change notification settings - Fork 3
/
JsonLdDeserializationTest.java
153 lines (134 loc) · 6.48 KB
/
JsonLdDeserializationTest.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
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* 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.
*
* 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.deserialization;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.cvut.kbss.jsonld.ConfigParam;
import cz.cvut.kbss.jsonld.jackson.JsonLdModule;
import cz.cvut.kbss.jsonld.jackson.environment.Environment;
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.Person;
import cz.cvut.kbss.jsonld.jackson.environment.model.User;
import cz.cvut.kbss.jsonld.jackson.serialization.JsonLdSerializationTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
class JsonLdDeserializationTest {
private static final URI HALSEY_URI = URI
.create("http://krizik.felk.cvut.cz/ontologies/jb4jsonld#Catherine+Halsey");
private static final URI LASKY_URI = URI
.create("http://krizik.felk.cvut.cz/ontologies/jb4jsonld#Thomas+Lasky");
private static final URI PALMER_URI = URI
.create("http://krizik.felk.cvut.cz/ontologies/jb4jsonld#Sarah+Palmer");
private static final Map<URI, User> USERS = initUsers();
private static final URI ORG_URI = URI.create("http://krizik.felk.cvut.cz/ontologies/jb4jsonld#UNSC");
private static final String ORG_NAME = "UNSC";
private static final String[] ORG_BRANDS = {"Spartan-II", "Mjolnir IV"};
private ObjectMapper objectMapper;
private JsonLdModule jsonLdModule;
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.objectMapper = new ObjectMapper();
this.jsonLdModule = new JsonLdModule();
objectMapper.registerModule(jsonLdModule);
}
@Test
void testDeserializeInstanceWithDataProperties() throws Exception {
final String input = Environment.readData("objectWithDataProperties.json");
final User result = objectMapper.readValue(input, User.class);
assertNotNull(result);
final User expected = USERS.get(HALSEY_URI);
verifyUserAttributes(expected, result);
}
private void verifyUserAttributes(User expected, User actual) {
assertEquals(expected.getUri(), actual.getUri());
assertEquals(expected.getFirstName(), actual.getFirstName());
assertEquals(expected.getLastName(), actual.getLastName());
assertEquals(expected.getUsername(), actual.getUsername());
assertEquals(expected.getAdmin(), actual.getAdmin());
}
@Test
void testDeserializeInstanceWithSingularObjectProperty() throws Exception {
final String input = Environment.readData("objectWithSingularReference.json");
final Employee result = objectMapper.readValue(input, Employee.class);
verifyUserAttributes(USERS.get(HALSEY_URI), result);
assertNotNull(result.getEmployer());
verifyOrganizationAttributes(result.getEmployer());
}
private void verifyOrganizationAttributes(Organization actual) {
assertEquals(ORG_URI, actual.getUri());
assertEquals(ORG_NAME, actual.getName());
assertNotNull(actual.getDateCreated());
for (String b : ORG_BRANDS) {
assertTrue(actual.getBrands().contains(b));
}
}
@Test
void testDeserializeCollectionOfInstances() throws Exception {
final String input = Environment.readData("collectionOfInstances.json");
final List<Employee> result = objectMapper.readValue(input, new TypeReference<List<Employee>>() {
});
assertNotNull(result);
assertFalse(result.isEmpty());
result.forEach(e -> {
final User expected = USERS.get(e.getUri());
verifyUserAttributes(expected, e);
});
}
@Test
void testSupportForIgnoringUnknownProperties() throws Exception {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final String input = Environment.readData("objectWithUnknownProperty.json");
final User result = objectMapper.readValue(input, User.class);
assertNotNull(result);
verifyUserAttributes(USERS.get(HALSEY_URI), result);
}
/**
* @see JsonLdSerializationTest#serializationIgnoresJsonTypeInfoConfiguration()
*/
@Test
void deserializationIgnoresJsonTypeInfo() throws Exception {
final String input = Environment.readData("objectWithSingularReference.json");
final Employee result = objectMapper.readValue(input, Employee.class);
assertNotNull(result);
}
@Test
void deserializationSupportsPolymorphism() throws Exception {
jsonLdModule.configure(ConfigParam.SCAN_PACKAGE, "cz.cvut.kbss.jsonld.jackson.environment.model");
final String input = Environment.readData("objectWithSingularReference.json");
final Person result = objectMapper.readValue(input, Person.class);
assertTrue(result instanceof Employee);
}
@Test
void deserializationSkipsPropertiesMappedToFieldsWithReadOnlyAccess() throws Exception {
final String input = Environment.readData("objectWithReadOnlyProperty.json");
final Organization result = objectMapper.readValue(input, Organization.class);
assertNotNull(result);
assertNull(result.getEmployeeCount());
}
}