-
Notifications
You must be signed in to change notification settings - Fork 3
/
JsonLdDeserializationTest.java
122 lines (106 loc) · 5.13 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
/**
* Copyright (C) 2017 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.environment.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.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.User;
import org.junit.Before;
import org.junit.Test;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
public class JsonLdDeserializationTest {
private static final URI HALSEY_URI = URI
.create("http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#Catherine+Halsey");
private static final URI LASKY_URI = URI
.create("http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#Thomas+Lasky");
private static final URI PALMER_URI = URI
.create("http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#Sarah+Palmer");
private static final Map<URI, User> USERS = initUsers();
private static final URI ORG_URI = URI.create("http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#UNSC");
private static final String ORG_NAME = "UNSC";
private static final String[] ORG_BRANDS = {"Spartan-II", "Mjolnir IV"};
private ObjectMapper objectMapper;
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;
}
@Before
public void setUp() {
this.objectMapper = new ObjectMapper();
objectMapper.registerModule(new JsonLdModule());
}
@Test
public 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
public 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
public 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
public 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);
}
}