-
Notifications
You must be signed in to change notification settings - Fork 333
Migrate JsonMapperTest to Java #10716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gh-worker-dd-mergequeue-cf854d
merged 7 commits into
master
from
sarahchen6/g2j-components
Mar 4, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
445b137
Migrate JsonMapperTest to Java
sarahchen6 1a7a50a
Update claude skill
sarahchen6 6336c01
Simplify casting
sarahchen6 ffcb57f
Improve readability
sarahchen6 24c2cc1
Rename var
sarahchen6 ecf9f02
Improve readability and address review comments
sarahchen6 5661257
Merge branch 'master' into sarahchen6/g2j-components
sarahchen6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 0 additions & 170 deletions
170
components/json/src/test/groovy/datadog/json/JsonMapperTest.groovy
This file was deleted.
Oops, something went wrong.
194 changes: 194 additions & 0 deletions
194
components/json/src/test/java/datadog/json/JsonMapperTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| package datadog.json; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
| import static java.util.Collections.emptyMap; | ||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| class JsonMapperTest { | ||
|
|
||
| @ParameterizedTest(name = "test mapping to JSON object: {0}") | ||
| @MethodSource("testMappingToJsonObjectArguments") | ||
| void testMappingToJsonObject(String testCase, Map<String, Object> input, String expected) | ||
| throws IOException { | ||
| String json = JsonMapper.toJson(input); | ||
| assertEquals(expected, json); | ||
|
|
||
| Map<String, Object> parsed = JsonMapper.fromJsonToMap(json); | ||
| if (input == null) { | ||
| assertEquals(emptyMap(), parsed); | ||
| } else { | ||
| assertEquals(input.size(), parsed.size()); | ||
| for (Map.Entry<String, Object> entry : input.entrySet()) { | ||
| String expectedKey = entry.getKey(); | ||
| Object expectedValue = entry.getValue(); | ||
| assertTrue(parsed.containsKey(expectedKey)); | ||
| Object parsedValue = parsed.get(expectedKey); | ||
| if (expectedValue instanceof UnsupportedType) { | ||
| assertEquals(expectedValue.toString(), parsedValue); | ||
| } else if (expectedValue instanceof Float) { | ||
| assertTrue(parsedValue instanceof Double); | ||
| assertEquals((Float) expectedValue, (Double) parsedValue, 0.001); | ||
| } else { | ||
| assertEquals(expectedValue, parsedValue); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static Stream<Arguments> testMappingToJsonObjectArguments() { | ||
| Map<String, Object> singleEntry = new LinkedHashMap<>(); | ||
| singleEntry.put("key1", "value1"); | ||
|
|
||
| Map<String, Object> twoEntries = new LinkedHashMap<>(); | ||
| twoEntries.put("key1", "value1"); | ||
| twoEntries.put("key2", "value2"); | ||
|
|
||
| Map<String, Object> quotedEntries = new LinkedHashMap<>(); | ||
| quotedEntries.put("key1", "va\"lu\"e1"); | ||
| quotedEntries.put("ke\"y2", "value2"); | ||
|
|
||
| Map<String, Object> complexMap = new LinkedHashMap<>(); | ||
| complexMap.put("key1", null); | ||
| complexMap.put("key2", "bar"); | ||
| complexMap.put("key3", 3); | ||
| complexMap.put("key4", 3456789123L); | ||
| complexMap.put("key5", 3.142f); | ||
| complexMap.put("key6", Math.PI); | ||
| complexMap.put("key7", true); | ||
| complexMap.put("key8", new UnsupportedType()); | ||
|
|
||
| return Stream.of( | ||
| Arguments.of("null input", null, "{}"), | ||
| Arguments.of("empty map", new HashMap<>(), "{}"), | ||
| Arguments.of("single entry", singleEntry, "{\"key1\":\"value1\"}"), | ||
| Arguments.of("two entries", twoEntries, "{\"key1\":\"value1\",\"key2\":\"value2\"}"), | ||
| Arguments.of( | ||
| "quoted entries", | ||
| quotedEntries, | ||
| "{\"key1\":\"va\\\"lu\\\"e1\",\"ke\\\"y2\":\"value2\"}"), | ||
| Arguments.of( | ||
| "complex map", | ||
| complexMap, | ||
| "{\"key1\":null,\"key2\":\"bar\",\"key3\":3,\"key4\":3456789123,\"key5\":3.142,\"key6\":3.141592653589793,\"key7\":true,\"key8\":\"toString\"}")); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "test mapping to Map from empty JSON object: {0}") | ||
| @MethodSource("testMappingToMapFromEmptyJsonObjectArguments") | ||
| void testMappingToMapFromEmptyJsonObject(String json) throws IOException { | ||
| Map<String, Object> parsed = JsonMapper.fromJsonToMap(json); | ||
| assertEquals(emptyMap(), parsed); | ||
| } | ||
|
|
||
| static Stream<Arguments> testMappingToMapFromEmptyJsonObjectArguments() { | ||
| return Stream.of( | ||
| Arguments.of((Object) null), Arguments.of("null"), Arguments.of(""), Arguments.of("{}")); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "test mapping to Map from non-object JSON: {0}") | ||
| @ValueSource(strings = {"1", "[1, 2]"}) | ||
| void testMappingToMapFromNonObjectJson(String json) { | ||
| assertThrows(IOException.class, () -> JsonMapper.fromJsonToMap(json)); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "test mapping iterable to JSON array: {0}") | ||
| @MethodSource("testMappingIterableToJsonArrayArguments") | ||
| void testMappingIterableToJsonArray(List<String> input, String expected) throws IOException { | ||
| String json = JsonMapper.toJson(input); | ||
| assertEquals(expected, json); | ||
|
|
||
| List<String> parsed = JsonMapper.fromJsonToList(json); | ||
| assertEquals(input != null ? input : emptyList(), parsed); | ||
| } | ||
|
|
||
| static Stream<Arguments> testMappingIterableToJsonArrayArguments() { | ||
| return Stream.of( | ||
| Arguments.of(null, "[]"), | ||
| Arguments.of(new ArrayList<>(), "[]"), | ||
| Arguments.of(Arrays.asList("value1"), "[\"value1\"]"), | ||
| Arguments.of(Arrays.asList("value1", "value2"), "[\"value1\",\"value2\"]"), | ||
| Arguments.of(Arrays.asList("va\"lu\"e1", "value2"), "[\"va\\\"lu\\\"e1\",\"value2\"]")); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "test mapping array to JSON array: {0}") | ||
| @MethodSource("testMappingArrayToJsonArrayArguments") | ||
| void testMappingArrayToJsonArray(String testCase, String[] input, String expected) | ||
| throws IOException { | ||
| String json = JsonMapper.toJson(input); | ||
| assertEquals(expected, json); | ||
|
|
||
| String[] parsed = JsonMapper.fromJsonToList(json).toArray(new String[] {}); | ||
| assertArrayEquals(input != null ? input : new String[] {}, parsed); | ||
| } | ||
|
|
||
| static Stream<Arguments> testMappingArrayToJsonArrayArguments() { | ||
| return Stream.of( | ||
| Arguments.of("null input", (Object) null, "[]"), | ||
| Arguments.of("empty array", new String[] {}, "[]"), | ||
| Arguments.of("single element", new String[] {"value1"}, "[\"value1\"]"), | ||
| Arguments.of("two elements", new String[] {"value1", "value2"}, "[\"value1\",\"value2\"]"), | ||
| Arguments.of( | ||
| "escaped quotes", | ||
| new String[] {"va\"lu\"e1", "value2"}, | ||
| "[\"va\\\"lu\\\"e1\",\"value2\"]")); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "test mapping to List from empty JSON object: {0}") | ||
| @MethodSource("testMappingToListFromEmptyJsonObjectArguments") | ||
| void testMappingToListFromEmptyJsonObject(String json) throws IOException { | ||
| List<String> parsed = JsonMapper.fromJsonToList(json); | ||
| assertEquals(emptyList(), parsed); | ||
| } | ||
|
|
||
| static Stream<Arguments> testMappingToListFromEmptyJsonObjectArguments() { | ||
| return Stream.of( | ||
| Arguments.of((Object) null), Arguments.of("null"), Arguments.of(""), Arguments.of("[]")); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "test mapping to JSON string: {0}") | ||
| @MethodSource("testMappingToJsonStringArguments") | ||
| void testMappingToJsonString(String input, String expected) { | ||
| String json = JsonMapper.toJson(input); | ||
| assertEquals(expected, json); | ||
| } | ||
|
|
||
| static Stream<Arguments> testMappingToJsonStringArguments() { | ||
| return Stream.of( | ||
| Arguments.of((Object) null, ""), | ||
| Arguments.of("", ""), | ||
| Arguments.of(String.valueOf((char) 4096), "\"\\u1000\""), | ||
| Arguments.of(String.valueOf((char) 256), "\"\\u0100\""), | ||
| Arguments.of(String.valueOf((char) 128), "\"\\u0080\""), | ||
| Arguments.of("\b", "\"\\b\""), | ||
| Arguments.of("\t", "\"\\t\""), | ||
| Arguments.of("\n", "\"\\n\""), | ||
| Arguments.of("\f", "\"\\f\""), | ||
| Arguments.of("\r", "\"\\r\""), | ||
| Arguments.of("\"", "\"\\\"\""), | ||
| Arguments.of("/", "\"\\/\""), | ||
| Arguments.of("\\", "\"\\\\\""), | ||
| Arguments.of("a", "\"a\"")); | ||
| } | ||
|
|
||
| private static class UnsupportedType { | ||
| @Override | ||
| public String toString() { | ||
| return "toString"; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.