Skip to content

Commit 445b137

Browse files
committed
Migrate JsonMapperTest to Java
1 parent 6ece63b commit 445b137

2 files changed

Lines changed: 185 additions & 170 deletions

File tree

components/json/src/test/groovy/datadog/json/JsonMapperTest.groovy

Lines changed: 0 additions & 170 deletions
This file was deleted.
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package datadog.json;
2+
3+
import static java.util.Collections.emptyList;
4+
import static java.util.Collections.emptyMap;
5+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
import java.io.IOException;
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.HashMap;
14+
import java.util.LinkedHashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.stream.Stream;
18+
import org.junit.jupiter.params.ParameterizedTest;
19+
import org.junit.jupiter.params.provider.Arguments;
20+
import org.junit.jupiter.params.provider.MethodSource;
21+
import org.junit.jupiter.params.provider.ValueSource;
22+
23+
class JsonMapperTest {
24+
25+
@ParameterizedTest(name = "test mapping to JSON object: {0}")
26+
@MethodSource("testMappingToJsonObject_arguments")
27+
void testMappingToJsonObject(Map<String, Object> input, String expected) throws IOException {
28+
String json = JsonMapper.toJson(input);
29+
assertEquals(expected, json);
30+
31+
Map<String, Object> parsed = JsonMapper.fromJsonToMap(json);
32+
if (input == null) {
33+
assertEquals(emptyMap(), parsed);
34+
} else {
35+
assertEquals(input.size(), parsed.size());
36+
for (Map.Entry<String, Object> entry : input.entrySet()) {
37+
assertTrue(parsed.containsKey(entry.getKey()));
38+
if (entry.getValue() instanceof UnsupportedType) {
39+
assertEquals(entry.getValue().toString(), parsed.get(entry.getKey()));
40+
} else if (entry.getValue() instanceof Float) {
41+
assertTrue(parsed.get(entry.getKey()) instanceof Double);
42+
assertEquals(
43+
(double) (float) (Float) entry.getValue(),
44+
(Double) parsed.get(entry.getKey()),
45+
0.001);
46+
} else {
47+
assertEquals(entry.getValue(), parsed.get(entry.getKey()));
48+
}
49+
}
50+
}
51+
}
52+
53+
static Stream<Arguments> testMappingToJsonObject_arguments() {
54+
Map<String, Object> singleEntry = new LinkedHashMap<>();
55+
singleEntry.put("key1", "value1");
56+
57+
Map<String, Object> twoEntries = new LinkedHashMap<>();
58+
twoEntries.put("key1", "value1");
59+
twoEntries.put("key2", "value2");
60+
61+
Map<String, Object> quotedEntries = new LinkedHashMap<>();
62+
quotedEntries.put("key1", "va\"lu\"e1");
63+
quotedEntries.put("ke\"y2", "value2");
64+
65+
Map<String, Object> complexMap = new LinkedHashMap<>();
66+
complexMap.put("key1", null);
67+
complexMap.put("key2", "bar");
68+
complexMap.put("key3", 3);
69+
complexMap.put("key4", 3456789123L);
70+
complexMap.put("key5", 3.142f);
71+
complexMap.put("key6", Math.PI);
72+
complexMap.put("key7", true);
73+
complexMap.put("key8", new UnsupportedType());
74+
75+
return Stream.of(
76+
Arguments.of(null, "{}"),
77+
Arguments.of(new HashMap<>(), "{}"),
78+
Arguments.of(singleEntry, "{\"key1\":\"value1\"}"),
79+
Arguments.of(twoEntries, "{\"key1\":\"value1\",\"key2\":\"value2\"}"),
80+
Arguments.of(quotedEntries, "{\"key1\":\"va\\\"lu\\\"e1\",\"ke\\\"y2\":\"value2\"}"),
81+
Arguments.of(
82+
complexMap,
83+
"{\"key1\":null,\"key2\":\"bar\",\"key3\":3,\"key4\":3456789123,\"key5\":3.142,\"key6\":3.141592653589793,\"key7\":true,\"key8\":\"toString\"}"));
84+
}
85+
86+
@ParameterizedTest(name = "test mapping to Map from empty JSON object: {0}")
87+
@MethodSource("testMappingToMapFromEmptyJsonObject_arguments")
88+
void testMappingToMapFromEmptyJsonObject(String json) throws IOException {
89+
Map<String, Object> parsed = JsonMapper.fromJsonToMap(json);
90+
assertEquals(emptyMap(), parsed);
91+
}
92+
93+
static Stream<Arguments> testMappingToMapFromEmptyJsonObject_arguments() {
94+
return Stream.of(
95+
Arguments.of((Object) null), Arguments.of("null"), Arguments.of(""), Arguments.of("{}"));
96+
}
97+
98+
@ParameterizedTest(name = "test mapping to Map from non-object JSON: {0}")
99+
@ValueSource(strings = {"1", "[1, 2]"})
100+
void testMappingToMapFromNonObjectJson(String json) {
101+
assertThrows(IOException.class, () -> JsonMapper.fromJsonToMap(json));
102+
}
103+
104+
@ParameterizedTest(name = "test mapping iterable to JSON array: {0}")
105+
@MethodSource("testMappingIterableToJsonArray_arguments")
106+
void testMappingIterableToJsonArray(List<String> input, String expected) throws IOException {
107+
String json = JsonMapper.toJson(input);
108+
assertEquals(expected, json);
109+
110+
List<String> parsed = JsonMapper.fromJsonToList(json);
111+
assertEquals(input != null ? input : emptyList(), parsed);
112+
}
113+
114+
static Stream<Arguments> testMappingIterableToJsonArray_arguments() {
115+
return Stream.of(
116+
Arguments.of(null, "[]"),
117+
Arguments.of(new ArrayList<>(), "[]"),
118+
Arguments.of(Arrays.asList("value1"), "[\"value1\"]"),
119+
Arguments.of(Arrays.asList("value1", "value2"), "[\"value1\",\"value2\"]"),
120+
Arguments.of(Arrays.asList("va\"lu\"e1", "value2"), "[\"va\\\"lu\\\"e1\",\"value2\"]"));
121+
}
122+
123+
@ParameterizedTest(name = "test mapping array to JSON array: {0}")
124+
@MethodSource("testMappingArrayToJsonArray_arguments")
125+
void testMappingArrayToJsonArray(String[] input, String expected) throws IOException {
126+
String json = JsonMapper.toJson(input);
127+
assertEquals(expected, json);
128+
129+
String[] parsed = JsonMapper.fromJsonToList(json).toArray(new String[0]);
130+
assertArrayEquals(input != null ? input : new String[0], parsed);
131+
}
132+
133+
static Stream<Arguments> testMappingArrayToJsonArray_arguments() {
134+
return Stream.of(
135+
Arguments.of((Object) null, "[]"),
136+
Arguments.of(new String[0], "[]"),
137+
Arguments.of(new String[] {"value1"}, "[\"value1\"]"),
138+
Arguments.of(new String[] {"value1", "value2"}, "[\"value1\",\"value2\"]"),
139+
Arguments.of(new String[] {"va\"lu\"e1", "value2"}, "[\"va\\\"lu\\\"e1\",\"value2\"]"));
140+
}
141+
142+
@ParameterizedTest(name = "test mapping to List from empty JSON object: {0}")
143+
@MethodSource("testMappingToListFromEmptyJsonObject_arguments")
144+
void testMappingToListFromEmptyJsonObject(String json) throws IOException {
145+
List<String> parsed = JsonMapper.fromJsonToList(json);
146+
assertEquals(emptyList(), parsed);
147+
}
148+
149+
static Stream<Arguments> testMappingToListFromEmptyJsonObject_arguments() {
150+
return Stream.of(
151+
Arguments.of((Object) null), Arguments.of("null"), Arguments.of(""), Arguments.of("[]"));
152+
}
153+
154+
@ParameterizedTest(name = "test mapping to JSON string: {0}")
155+
@MethodSource("testMappingToJsonString_arguments")
156+
void testMappingToJsonString(String input, String expected) {
157+
String escaped = JsonMapper.toJson(input);
158+
assertEquals(expected, escaped);
159+
}
160+
161+
static Stream<Arguments> testMappingToJsonString_arguments() {
162+
return Stream.of(
163+
Arguments.of((Object) null, ""),
164+
Arguments.of("", ""),
165+
Arguments.of(String.valueOf((char) 4096), "\"\\u1000\""),
166+
Arguments.of(String.valueOf((char) 256), "\"\\u0100\""),
167+
Arguments.of(String.valueOf((char) 128), "\"\\u0080\""),
168+
Arguments.of("\b", "\"\\b\""),
169+
Arguments.of("\t", "\"\\t\""),
170+
Arguments.of("\n", "\"\\n\""),
171+
Arguments.of("\f", "\"\\f\""),
172+
Arguments.of("\r", "\"\\r\""),
173+
Arguments.of("\"", "\"\\\"\""),
174+
Arguments.of("/", "\"\\/\""),
175+
Arguments.of("\\", "\"\\\\\""),
176+
Arguments.of("a", "\"a\""));
177+
}
178+
179+
private static class UnsupportedType {
180+
@Override
181+
public String toString() {
182+
return "toString";
183+
}
184+
}
185+
}

0 commit comments

Comments
 (0)