-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathJacksonModuleDatabindingTest.java
More file actions
43 lines (33 loc) · 1.58 KB
/
JacksonModuleDatabindingTest.java
File metadata and controls
43 lines (33 loc) · 1.58 KB
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
package ua_parser;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import java.io.IOException;
import java.nio.charset.Charset;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class JacksonModuleDatabindingTest {
@Test
public void testJacksonModuleSpiRegistration() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
Class<?> clientMixin = objectMapper.findMixInClassFor(Client.class);
assertTrue(clientMixin.isAssignableFrom(ClientModule.ClientMixin.class));
}
@Test
public void testJacksonSerializationAndDeserialization() throws IOException, JSONException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
Client testClient = new Client(new UserAgent("Firefox", "3", "5", "5"),
new OS("Mac OS X", "10", "4", null, null),
new Device("Other"));
String expectedSerialization = IOUtils.resourceToString(
"ua_parser/testClient.json", Charset.defaultCharset(), getClass().getClassLoader());
String actualSerialization = objectMapper.writeValueAsString(testClient);
JSONAssert.assertEquals(expectedSerialization, actualSerialization, true);
Client deserializedClient = objectMapper.readValue(actualSerialization, Client.class);
assertEquals(testClient, deserializedClient);
}
}