/
JsonIncludeTest.java
137 lines (114 loc) · 5.86 KB
/
JsonIncludeTest.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
package com.ventulus95.testtechproject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ventulus95.testtechproject.dto.PojoDefault;
import com.ventulus95.testtechproject.dto.PojoDefaultAbsent;
import com.ventulus95.testtechproject.dto.PojoDefaultEmpty;
import com.ventulus95.testtechproject.dto.PojoDefaultNon;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
public class JsonIncludeTest {
private ObjectMapper objectMapper = new ObjectMapper();
@Test
void 기본옵션_JsonInclude테스트() throws JSONException, JsonProcessingException {
PojoDefault pojoDefault = PojoDefault.builder().build();
prettyPrint(pojoDefault);
JSONAssert.assertEquals("""
{"string": null,"_long": null,"aboolean": null,"integer": null,"localDateTime":null}
""", objectMapper.writeValueAsString(PojoDefault.builder().build()), true);
}
@Test
void Non_Null_JsonInclude테스트() throws JSONException, JsonProcessingException {
PojoDefault pojoDefault = PojoDefault.builder().build();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
prettyPrint(pojoDefault);
JSONAssert.assertEquals("""
{}
""", objectMapper.writeValueAsString(pojoDefault), true);
}
@Test
void Non_Absent_JsonInclude테스트() throws JSONException, JsonProcessingException {
PojoDefaultAbsent pojoDefault = PojoDefaultAbsent.builder().build();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
prettyPrint(pojoDefault);
JSONAssert.assertEquals("""
{}
""", objectMapper.writeValueAsString(pojoDefault), true);
}
@Test
void Non_Absent_JsonInclude테스트_optional이_null이_아닌경우() throws JSONException, JsonProcessingException {
PojoDefaultAbsent pojoDefault = PojoDefaultAbsent.builder().stringOptional(Optional.ofNullable(null)).build();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
prettyPrint(pojoDefault);
JSONAssert.assertEquals("""
{stringOptional: {} }
""", objectMapper.writeValueAsString(pojoDefault), JSONCompareMode.LENIENT);
}
@Test
void Non_Absent_JsonInclude테스트_atomic이_null이_아닌경우() throws JSONException, JsonProcessingException {
PojoDefaultAbsent pojoDefault = PojoDefaultAbsent.builder().atomicInteger(new AtomicInteger()).build();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
prettyPrint(pojoDefault);
JSONAssert.assertEquals("""
{atomicInteger: 0}
""", objectMapper.writeValueAsString(pojoDefault), JSONCompareMode.LENIENT);
}
@Test
void Non_Empty_JsonInclude테스트() throws JSONException, JsonProcessingException {
PojoDefaultEmpty pojoDefaultEmpty = PojoDefaultEmpty.builder().build();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
prettyPrint(pojoDefaultEmpty);
JSONAssert.assertEquals("""
{}
""", objectMapper.writeValueAsString(pojoDefaultEmpty), JSONCompareMode.LENIENT);
}
@Test
void Non_Empty_JsonInclude테스트_array안에_들어있는경우() throws JSONException, JsonProcessingException {
PojoDefaultEmpty pojoDefaultEmpty = PojoDefaultEmpty.builder().array(new Integer[1]).build();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
prettyPrint(pojoDefaultEmpty);
JSONAssert.assertEquals("""
{}
""", objectMapper.writeValueAsString(pojoDefaultEmpty), true);
}
@Test
void Non_Default_JsonInclude테스트() throws JSONException, JsonProcessingException {
PojoDefaultNon pojoDefaultEmpty = PojoDefaultNon.builder().string("").timestamp(new Timestamp(0L)).build();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
prettyPrint(pojoDefaultEmpty);
System.out.println(pojoDefaultEmpty);
JSONAssert.assertEquals("""
{}
""", objectMapper.writeValueAsString(pojoDefaultEmpty), JSONCompareMode.STRICT);
}
@Test
void Non_Default_JsonInclude테스트_살짝_틀어보기() throws JSONException, JsonProcessingException {
PojoDefaultNon pojoDefaultEmpty = PojoDefaultNon.builder().string(" ").timestamp(new Timestamp(1L)).build();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
prettyPrint(pojoDefaultEmpty);
System.out.println(pojoDefaultEmpty);
JSONAssert.assertEquals("""
{}
""", objectMapper.writeValueAsString(pojoDefaultEmpty), JSONCompareMode.STRICT);
}
@Test
void Use_Default_JsonInclude테스트() throws JSONException, JsonProcessingException {
PojoDefaultEmpty pojoDefaultEmpty = PojoDefaultEmpty.builder().list(new ArrayList<>()).build();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
prettyPrint(pojoDefaultEmpty);
System.out.println(pojoDefaultEmpty);
JSONAssert.assertEquals("""
{ "integer" : 0, "trueorFalse" : false, "list" : [ ]}
""", objectMapper.writeValueAsString(pojoDefaultEmpty), JSONCompareMode.STRICT);
}
private void prettyPrint(Object o) throws JsonProcessingException {
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(o));
}
}