Skip to content

Commit 264bb80

Browse files
Merge remote-tracking branch 'origin/bbujon/json-tests' into alexeyk/table-test-example
2 parents 97e35b9 + 60ec4bf commit 264bb80

File tree

1 file changed

+61
-87
lines changed

1 file changed

+61
-87
lines changed

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

Lines changed: 61 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,29 @@
1010
import static org.junit.jupiter.params.provider.Arguments.arguments;
1111

1212
import java.io.IOException;
13-
import java.util.Arrays;
1413
import java.util.LinkedHashMap;
1514
import java.util.List;
1615
import java.util.Map;
1716
import java.util.stream.Stream;
18-
import org.junit.jupiter.api.Test;
1917
import org.junit.jupiter.params.ParameterizedTest;
2018
import org.junit.jupiter.params.provider.Arguments;
2119
import org.junit.jupiter.params.provider.MethodSource;
20+
import org.tabletest.junit.Scenario;
2221
import org.tabletest.junit.TableTest;
2322

2423
class JsonMapperTest {
2524
@TableTest({
26-
"scenario | input | expected ",
27-
"null input | | '{}' ",
28-
"empty map | [:] | '{}' ",
29-
"single entry | [key1: value1] | '{\"key1\":\"value1\"}' ",
30-
"two entries | [key1: value1, key2: value2] | '{\"key1\":\"value1\",\"key2\":\"value2\"}' "
25+
"Scenario | Input | Expected ",
26+
"null input | | '{}' ",
27+
"empty map | [:] | '{}' ",
28+
"single entry | [key1: value1] | '{\"key1\":\"value1\"}' ",
29+
"two entries | [key1: value1, key2: value2] | '{\"key1\":\"value1\",\"key2\":\"value2\"}' ",
30+
"quoted entries | [key1: va\"lu\"e1, ke\"y2: value2] | '{\"key1\":\"va\\\"lu\\\"e1\",\"ke\\\"y2\":\"value2\"}'"
3131
})
32-
void testMappingToJsonObject(Map<String, Object> input, String expected) throws IOException {
33-
assertMapToJsonRoundTrip(input, expected);
34-
}
35-
36-
@Test
37-
void testMappingToJsonObjectWithComplexMap() throws IOException {
38-
Map<String, Object> input = new LinkedHashMap<>();
39-
input.put("key1", null);
40-
input.put("key2", "bar");
41-
input.put("key3", 3);
42-
input.put("key4", 3456789123L);
43-
input.put("key5", 3.142f);
44-
input.put("key6", Math.PI);
45-
input.put("key7", true);
46-
47-
assertMapToJsonRoundTrip(
48-
input,
49-
"{\"key1\":null,\"key2\":\"bar\",\"key3\":3,\"key4\":3456789123,\"key5\":3.142,\"key6\":3.141592653589793,\"key7\":true}");
50-
}
51-
52-
@Test
53-
void testMappingToJsonObjectWithQuotedEntries() throws IOException {
54-
Map<String, Object> input = new LinkedHashMap<>();
55-
input.put("key1", "va\"lu\"e1");
56-
input.put("ke\"y2", "value2");
57-
58-
assertMapToJsonRoundTrip(input, "{\"key1\":\"va\\\"lu\\\"e1\",\"ke\\\"y2\":\"value2\"}");
59-
}
60-
61-
@Test
62-
void testMappingToJsonObjectWithUnsupportedType() throws IOException {
63-
Map<String, Object> input = new LinkedHashMap<>();
64-
input.put("key1", new UnsupportedType());
65-
66-
assertMapToJsonRoundTrip(input, "{\"key1\":\"toString\"}");
67-
}
68-
69-
private void assertMapToJsonRoundTrip(Map<String, Object> input, String expected)
32+
@ParameterizedTest(name = "test mapping to JSON object: {0}")
33+
@MethodSource("testMappingToJsonObjectArguments")
34+
void testMappingToJsonObject(
35+
@Scenario String ignoredScenario, Map<String, Object> input, String expected)
7036
throws IOException {
7137
String json = JsonMapper.toJson(input);
7238
assertEquals(expected, json);
@@ -93,13 +59,32 @@ private void assertMapToJsonRoundTrip(Map<String, Object> input, String expected
9359
}
9460
}
9561

62+
static Stream<Arguments> testMappingToJsonObjectArguments() {
63+
Map<String, Object> complexMap = new LinkedHashMap<>();
64+
complexMap.put("key1", null);
65+
complexMap.put("key2", "bar");
66+
complexMap.put("key3", 3);
67+
complexMap.put("key4", 3456789123L);
68+
complexMap.put("key5", 3.142f);
69+
complexMap.put("key6", Math.PI);
70+
complexMap.put("key7", true);
71+
complexMap.put("key8", new UnsupportedType());
72+
73+
return Stream.of(
74+
arguments(
75+
"complex map",
76+
complexMap,
77+
"{\"key1\":null,\"key2\":\"bar\",\"key3\":3,\"key4\":3456789123,\"key5\":3.142,\"key6\":3.141592653589793,\"key7\":true,\"key8\":\"toString\"}"));
78+
}
79+
9680
@TableTest({
97-
"scenario | json ",
98-
"null | ",
99-
"null string | 'null' ",
100-
"empty string | '' ",
101-
"empty object | '{}' "
81+
"Scenario | Json ",
82+
"null | ",
83+
"null string | 'null'",
84+
"empty string | '' ",
85+
"empty object | '{}' "
10286
})
87+
@ParameterizedTest(name = "test mapping to Map from empty JSON object: {0}")
10388
void testMappingToMapFromEmptyJsonObject(String json) throws IOException {
10489
Map<String, Object> parsed = JsonMapper.fromJsonToMap(json);
10590
assertEquals(emptyMap(), parsed);
@@ -108,22 +93,25 @@ void testMappingToMapFromEmptyJsonObject(String json) throws IOException {
10893
// temporary disable spotless, will open issue to fix this.
10994
// spotless:off
11095
@TableTest({
111-
"scenario | json ",
112-
"integer | 1 ",
113-
"array | '[1, 2]' "
96+
"Scenario | Json ",
97+
"integer | 1 ",
98+
"array | [1, 2]"
11499
})
115100
// spotless:on
101+
@ParameterizedTest(name = "test mapping to Map from non-object JSON: {0}")
116102
void testMappingToMapFromNonObjectJson(String json) {
117103
assertThrows(IOException.class, () -> JsonMapper.fromJsonToMap(json));
118104
}
119105

120106
@TableTest({
121-
"scenario | input | expected ",
122-
"null input | | '[]' ",
123-
"empty list | [] | '[]' ",
124-
"single value | [value1] | '[\"value1\"]' ",
125-
"two values | [value1, value2] | '[\"value1\",\"value2\"]' "
107+
"Scenario | Input | Expected ",
108+
"null input | | '[]' ",
109+
"empty list | [] | '[]' ",
110+
"single value | [value1] | '[\"value1\"]' ",
111+
"two values | [value1, value2] | '[\"value1\",\"value2\"]' ",
112+
"quoted values | [va\"lu\"e1, value2] | '[\"va\\\"lu\\\"e1\",\"value2\"]'"
126113
})
114+
@ParameterizedTest(name = "test mapping iterable to JSON array: {0}")
127115
void testMappingIterableToJsonArray(List<String> input, String expected) throws IOException {
128116
String json = JsonMapper.toJson(input);
129117
assertEquals(expected, json);
@@ -132,19 +120,16 @@ void testMappingIterableToJsonArray(List<String> input, String expected) throws
132120
assertEquals(input != null ? input : emptyList(), parsed);
133121
}
134122

135-
@Test
136-
void testMappingIterableToJsonArrayWithQuotedValue() throws IOException {
137-
List<String> input = Arrays.asList("va\"lu\"e1", "value2");
138-
String json = JsonMapper.toJson(input);
139-
assertEquals("[\"va\\\"lu\\\"e1\",\"value2\"]", json);
140-
141-
List<String> parsed = JsonMapper.fromJsonToList(json);
142-
assertEquals(input, parsed);
143-
}
144-
123+
@TableTest({
124+
"Scenario | Input | Expected ",
125+
"null input | | '[]' ",
126+
"empty array | [] | '[]' ",
127+
"single element | [value1] | '[\"value1\"]' ",
128+
"two elements | [value1, value2] | '[\"value1\",\"value2\"]' ",
129+
"escaped quotes | [va\"lu\"e1, value2] | '[\"va\\\"lu\\\"e1\",\"value2\"]'"
130+
})
145131
@ParameterizedTest(name = "test mapping array to JSON array: {0}")
146-
@MethodSource("testMappingArrayToJsonArrayArguments")
147-
void testMappingArrayToJsonArray(String testCase, String[] input, String expected)
132+
void testMappingArrayToJsonArray(String ignoredScenario, String[] input, String expected)
148133
throws IOException {
149134
String json = JsonMapper.toJson(input);
150135
assertEquals(expected, json);
@@ -153,25 +138,14 @@ void testMappingArrayToJsonArray(String testCase, String[] input, String expecte
153138
assertArrayEquals(input != null ? input : new String[] {}, parsed);
154139
}
155140

156-
static Stream<Arguments> testMappingArrayToJsonArrayArguments() {
157-
return Stream.of(
158-
arguments("null input", (Object) null, "[]"),
159-
arguments("empty array", new String[] {}, "[]"),
160-
arguments("single element", new String[] {"value1"}, "[\"value1\"]"),
161-
arguments("two elements", new String[] {"value1", "value2"}, "[\"value1\",\"value2\"]"),
162-
arguments(
163-
"escaped quotes",
164-
new String[] {"va\"lu\"e1", "value2"},
165-
"[\"va\\\"lu\\\"e1\",\"value2\"]"));
166-
}
167-
168141
@TableTest({
169-
"scenario | json ",
170-
"null | ",
171-
"null string | 'null' ",
172-
"empty string | '' ",
173-
"empty array | '[]' "
142+
"Scenario | Json ",
143+
"null | ",
144+
"null string | 'null'",
145+
"empty string | '' ",
146+
"empty array | '[]' "
174147
})
148+
@ParameterizedTest(name = "test mapping to List from empty JSON object: {0}")
175149
void testMappingToListFromEmptyJsonObject(String json) throws IOException {
176150
List<String> parsed = JsonMapper.fromJsonToList(json);
177151
assertEquals(emptyList(), parsed);

0 commit comments

Comments
 (0)