/
JacksonDeserializationOfBigNumbersToDurationTest.java
100 lines (79 loc) · 3.39 KB
/
JacksonDeserializationOfBigNumbersToDurationTest.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
package io.dropwizard.jackson;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import javax.annotation.Nullable;
import java.time.Duration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
public class JacksonDeserializationOfBigNumbersToDurationTest {
private final ObjectMapper objectMapper = Jackson.newObjectMapper();
@Test(timeout = 5000)
public void testDoesNotAttemptToDeserializeExtremelyBigNumbers() throws Exception {
Task task = objectMapper.readValue("{\"id\": 42, \"duration\": 1e1000000000}", Task.class);
assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(0));
}
@Test
public void testCanDeserializeZero() throws Exception {
Task task = objectMapper.readValue("{\"id\": 42, \"duration\": 0}", Task.class);
assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(0));
}
@Test
public void testCanDeserializeNormalTimestamp() throws Exception {
Task task = objectMapper.readValue("{\"id\": 42, \"duration\": 30}", Task.class);
assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(30));
}
@Test
public void testCanDeserializeNormalTimestampWithNanoseconds() throws Exception {
Task task = objectMapper.readValue("{\"id\": 42, \"duration\": 30.314400507}", Task.class);
assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(30, 314400507L));
}
@Test
public void testCanDeserializeFromString() throws Exception {
Task task = objectMapper.readValue("{\"id\": 42, \"duration\": \"PT30S\"}", Task.class);
assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(30));
}
@Test
public void testCanDeserializeMinDuration() throws Exception {
Task task = objectMapper.readValue("{\"id\": 42, \"duration\": -9223372036854775808}", Task.class);
assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(Long.MIN_VALUE));
}
@Test
public void testCanDeserializeMaxDuration() throws Exception {
Task task = objectMapper.readValue("{\"id\": 42, \"duration\": 9223372036854775807}", Task.class);
assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(Long.MAX_VALUE));
}
@Test
public void testCanNotDeserializeValueMoreThanMaxDuration() {
assertThatExceptionOfType(JsonMappingException.class).isThrownBy(
() -> objectMapper.readValue("{\"id\": 42, \"duration\": 9223372036854775808}", Task.class));
}
@Test
public void testCanNotDeserializeValueLessThanMinDuration() {
assertThatExceptionOfType(JsonMappingException.class).isThrownBy(
() -> objectMapper.readValue("{\"id\": 42, \"duration\": -9223372036854775809}", Task.class));
}
static class Task {
private int id;
@Nullable
private Duration duration;
@JsonProperty
int getId() {
return id;
}
@JsonProperty
void setId(int id) {
this.id = id;
}
@JsonProperty
@Nullable
Duration getDuration() {
return duration;
}
@JsonProperty
void setDuration(Duration duration) {
this.duration = duration;
}
}
}