From 445b137f0dc145bf32e8728dfe6b8b413448e760 Mon Sep 17 00:00:00 2001 From: Sarah Chen Date: Mon, 2 Mar 2026 17:15:01 -0500 Subject: [PATCH 1/6] Migrate JsonMapperTest to Java --- .../groovy/datadog/json/JsonMapperTest.groovy | 170 ---------------- .../java/datadog/json/JsonMapperTest.java | 185 ++++++++++++++++++ 2 files changed, 185 insertions(+), 170 deletions(-) delete mode 100644 components/json/src/test/groovy/datadog/json/JsonMapperTest.groovy create mode 100644 components/json/src/test/java/datadog/json/JsonMapperTest.java diff --git a/components/json/src/test/groovy/datadog/json/JsonMapperTest.groovy b/components/json/src/test/groovy/datadog/json/JsonMapperTest.groovy deleted file mode 100644 index 43dd4dcb0aa..00000000000 --- a/components/json/src/test/groovy/datadog/json/JsonMapperTest.groovy +++ /dev/null @@ -1,170 +0,0 @@ -package datadog.json - -import spock.lang.Specification - -import static java.lang.Math.PI -import static java.util.Collections.emptyMap - -class JsonMapperTest extends Specification { - - def "test mapping to JSON object: #input"() { - setup: - def parsedExpected = input == null ? emptyMap() : input.clone() - parsedExpected.collect { - it -> { - if (it.value instanceof UnsupportedType) { - it.value = it.value.toString() - } else if (it.value instanceof Float) { - it.value = new Double(it.value) - } - - it - } - } - - when: - String json = JsonMapper.toJson((Map) input) - - then: - json == expected - - when: - def parsed = JsonMapper.fromJsonToMap(json) - - then: - if (input == null) { - parsed == [:] - } else { - parsed.size() == input.size() - input.each { - assert parsed.containsKey(it.key) - if (it.value instanceof UnsupportedType) { - assert parsed.get(it.key) == it.value.toString() - } else if (it.value instanceof Float) { - assert parsed.get(it.key) instanceof Double - assert (parsed.get(it.key) - it.value) < 0.001 - } else { - assert parsed.get(it.key) == it.value - } - } - } - - where: - input | expected - null | '{}' - new HashMap<>() | '{}' - ['key1': 'value1'] | '{"key1":"value1"}' - ['key1': 'value1', 'key2': 'value2'] | '{"key1":"value1","key2":"value2"}' - ['key1': 'va"lu"e1', 'ke"y2': 'value2'] | '{"key1":"va\\"lu\\"e1","ke\\"y2":"value2"}' - ['key1': null, 'key2': 'bar', 'key3': 3, 'key4': 3456789123L, 'key5': 3.142f, 'key6': PI, 'key7': true, 'key8': new UnsupportedType()] | '{"key1":null,"key2":"bar","key3":3,"key4":3456789123,"key5":3.142,"key6":3.141592653589793,"key7":true,"key8":"toString"}' - } - - private class UnsupportedType { - @Override - String toString() { - 'toString' - } - } - - def "test mapping to Map from empty JSON object"() { - when: - def parsed = JsonMapper.fromJsonToMap(json) - - then: - parsed == [:] - - where: - json << [null, 'null', '', '{}'] - } - - def "test mapping to Map from non-object JSON"() { - when: - JsonMapper.fromJsonToMap(json) - - then: - thrown(IOException) - - where: - json << ['1', '[1, 2]'] - } - - def "test mapping iterable to JSON array: #input"() { - when: - String json = JsonMapper.toJson(input as Collection) - - then: - json == expected - - when: - def parsed = JsonMapper.fromJsonToList(json) - - then: - parsed == (input?:[]) - - where: - input | expected - null | "[]" - new ArrayList<>() | "[]" - ['value1'] | "[\"value1\"]" - ['value1', 'value2'] | "[\"value1\",\"value2\"]" - ['va"lu"e1', 'value2'] | "[\"va\\\"lu\\\"e1\",\"value2\"]" - } - - def "test mapping array to JSON array: #input"() { - when: - String json = JsonMapper.toJson((String[]) input) - - then: - json == expected - - when: - def parsed = JsonMapper.fromJsonToList(json).toArray(new String[0]) - - then: - parsed == (String[]) (input?:[]) - - where: - input | expected - null | "[]" - [] | "[]" - ['value1'] | "[\"value1\"]" - ['value1', 'value2'] | "[\"value1\",\"value2\"]" - ['va"lu"e1', 'value2'] | "[\"va\\\"lu\\\"e1\",\"value2\"]" - } - - def "test mapping to List from empty JSON object"() { - when: - def parsed = JsonMapper.fromJsonToList(json) - - then: - parsed == [] - - where: - json << [null, 'null', '', '[]'] - } - - def "test mapping to JSON string: input"() { - when: - String escaped = JsonMapper.toJson((String) string) - - then: - escaped == expected - - where: - string | expected - null | "" - "" | "" - ((char) 4096).toString() | '"\\u1000"' - ((char) 256).toString() | '"\\u0100"' - ((char) 128).toString() | '"\\u0080"' - "\b" | '"\\b"' - "\t" | '"\\t"' - "\n" | '"\\n"' - "\f" | '"\\f"' - "\r" | '"\\r"' - '"' | '"\\\""' - '/' | '"\\/"' - '\\' | '"\\\\"' - "a" | '"a"' - } -} diff --git a/components/json/src/test/java/datadog/json/JsonMapperTest.java b/components/json/src/test/java/datadog/json/JsonMapperTest.java new file mode 100644 index 00000000000..162e3775ab6 --- /dev/null +++ b/components/json/src/test/java/datadog/json/JsonMapperTest.java @@ -0,0 +1,185 @@ +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("testMappingToJsonObject_arguments") + void testMappingToJsonObject(Map input, String expected) throws IOException { + String json = JsonMapper.toJson(input); + assertEquals(expected, json); + + Map parsed = JsonMapper.fromJsonToMap(json); + if (input == null) { + assertEquals(emptyMap(), parsed); + } else { + assertEquals(input.size(), parsed.size()); + for (Map.Entry entry : input.entrySet()) { + assertTrue(parsed.containsKey(entry.getKey())); + if (entry.getValue() instanceof UnsupportedType) { + assertEquals(entry.getValue().toString(), parsed.get(entry.getKey())); + } else if (entry.getValue() instanceof Float) { + assertTrue(parsed.get(entry.getKey()) instanceof Double); + assertEquals( + (double) (float) (Float) entry.getValue(), + (Double) parsed.get(entry.getKey()), + 0.001); + } else { + assertEquals(entry.getValue(), parsed.get(entry.getKey())); + } + } + } + } + + static Stream testMappingToJsonObject_arguments() { + Map singleEntry = new LinkedHashMap<>(); + singleEntry.put("key1", "value1"); + + Map twoEntries = new LinkedHashMap<>(); + twoEntries.put("key1", "value1"); + twoEntries.put("key2", "value2"); + + Map quotedEntries = new LinkedHashMap<>(); + quotedEntries.put("key1", "va\"lu\"e1"); + quotedEntries.put("ke\"y2", "value2"); + + Map 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, "{}"), + Arguments.of(new HashMap<>(), "{}"), + Arguments.of(singleEntry, "{\"key1\":\"value1\"}"), + Arguments.of(twoEntries, "{\"key1\":\"value1\",\"key2\":\"value2\"}"), + Arguments.of(quotedEntries, "{\"key1\":\"va\\\"lu\\\"e1\",\"ke\\\"y2\":\"value2\"}"), + Arguments.of( + 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("testMappingToMapFromEmptyJsonObject_arguments") + void testMappingToMapFromEmptyJsonObject(String json) throws IOException { + Map parsed = JsonMapper.fromJsonToMap(json); + assertEquals(emptyMap(), parsed); + } + + static Stream testMappingToMapFromEmptyJsonObject_arguments() { + 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("testMappingIterableToJsonArray_arguments") + void testMappingIterableToJsonArray(List input, String expected) throws IOException { + String json = JsonMapper.toJson(input); + assertEquals(expected, json); + + List parsed = JsonMapper.fromJsonToList(json); + assertEquals(input != null ? input : emptyList(), parsed); + } + + static Stream testMappingIterableToJsonArray_arguments() { + 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("testMappingArrayToJsonArray_arguments") + void testMappingArrayToJsonArray(String[] input, String expected) throws IOException { + String json = JsonMapper.toJson(input); + assertEquals(expected, json); + + String[] parsed = JsonMapper.fromJsonToList(json).toArray(new String[0]); + assertArrayEquals(input != null ? input : new String[0], parsed); + } + + static Stream testMappingArrayToJsonArray_arguments() { + return Stream.of( + Arguments.of((Object) null, "[]"), + Arguments.of(new String[0], "[]"), + Arguments.of(new String[] {"value1"}, "[\"value1\"]"), + Arguments.of(new String[] {"value1", "value2"}, "[\"value1\",\"value2\"]"), + Arguments.of(new String[] {"va\"lu\"e1", "value2"}, "[\"va\\\"lu\\\"e1\",\"value2\"]")); + } + + @ParameterizedTest(name = "test mapping to List from empty JSON object: {0}") + @MethodSource("testMappingToListFromEmptyJsonObject_arguments") + void testMappingToListFromEmptyJsonObject(String json) throws IOException { + List parsed = JsonMapper.fromJsonToList(json); + assertEquals(emptyList(), parsed); + } + + static Stream testMappingToListFromEmptyJsonObject_arguments() { + return Stream.of( + Arguments.of((Object) null), Arguments.of("null"), Arguments.of(""), Arguments.of("[]")); + } + + @ParameterizedTest(name = "test mapping to JSON string: {0}") + @MethodSource("testMappingToJsonString_arguments") + void testMappingToJsonString(String input, String expected) { + String escaped = JsonMapper.toJson(input); + assertEquals(expected, escaped); + } + + static Stream testMappingToJsonString_arguments() { + 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"; + } + } +} From 1a7a50afbf8b79ebc5ccc33367909315e6a0e16c Mon Sep 17 00:00:00 2001 From: Sarah Chen Date: Tue, 3 Mar 2026 13:48:17 -0500 Subject: [PATCH 2/6] Update claude skill --- .../skills/migrate-groovy-to-java/SKILL.md | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.claude/skills/migrate-groovy-to-java/SKILL.md b/.claude/skills/migrate-groovy-to-java/SKILL.md index dd6556e53f9..a86c56d6711 100644 --- a/.claude/skills/migrate-groovy-to-java/SKILL.md +++ b/.claude/skills/migrate-groovy-to-java/SKILL.md @@ -5,16 +5,16 @@ description: migrate test groovy files to java Migrate test Groovy files to Java using JUnit 5 -1. List all groovy files of the current gradle module -2. convert groovy files to Java using Junit 5 -3. make sure the tests are still passing after migration -4. remove groovy files +1. List all Groovy files of the current Gradle module +2. Convert Groovy files to Java using JUnit 5 +3. Make sure the tests are still passing after migration and that the test count has not changed +4. Remove Groovy files -When converting groovy code to java code make sure that: -- the Java code generated is compatible with JDK 8 -- when translating Spock test, favor using `@CsvSource` with `|` delimiters -- when using a `@MethodSource`, use the test method name, and suffix it with `_arguments` -- when converting tuples, create light dedicated structure instead to keep the typing system +When converting Groovy code to Java code make sure that: +- The Java code generated is compatible with JDK 8 +- When translating Spock tests, favor using `@CsvSource` with `|` delimiters +- When using `@MethodSource`, use the test method name and suffix it with `_arguments` +- When converting tuples, create light dedicated structure instead to keep the typing system - Instead of checking a state and throwing an exception, use JUnit asserts -- Do not wrap checked exception and throwing a Runtime exception, prefer adding a throws clause at method declaration +- Do not wrap checked exception and throw a Runtime exception; prefer adding a throws clause at method declaration - Do not mark local variables `final` From 6336c01085d0f467fbaa180bca89b7519a8b65aa Mon Sep 17 00:00:00 2001 From: Sarah Chen Date: Tue, 3 Mar 2026 14:15:25 -0500 Subject: [PATCH 3/6] Simplify casting --- .../json/src/test/java/datadog/json/JsonMapperTest.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/components/json/src/test/java/datadog/json/JsonMapperTest.java b/components/json/src/test/java/datadog/json/JsonMapperTest.java index 162e3775ab6..f6a5967770b 100644 --- a/components/json/src/test/java/datadog/json/JsonMapperTest.java +++ b/components/json/src/test/java/datadog/json/JsonMapperTest.java @@ -39,10 +39,7 @@ void testMappingToJsonObject(Map input, String expected) throws assertEquals(entry.getValue().toString(), parsed.get(entry.getKey())); } else if (entry.getValue() instanceof Float) { assertTrue(parsed.get(entry.getKey()) instanceof Double); - assertEquals( - (double) (float) (Float) entry.getValue(), - (Double) parsed.get(entry.getKey()), - 0.001); + assertEquals((Float) entry.getValue(), (Double) parsed.get(entry.getKey()), 0.001); } else { assertEquals(entry.getValue(), parsed.get(entry.getKey())); } From ffcb57f6b706820dc905e7b6f111cc096814c060 Mon Sep 17 00:00:00 2001 From: Sarah Chen Date: Tue, 3 Mar 2026 16:07:55 -0500 Subject: [PATCH 4/6] Improve readability --- .../java/datadog/json/JsonMapperTest.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/components/json/src/test/java/datadog/json/JsonMapperTest.java b/components/json/src/test/java/datadog/json/JsonMapperTest.java index f6a5967770b..cb64c7b3f12 100644 --- a/components/json/src/test/java/datadog/json/JsonMapperTest.java +++ b/components/json/src/test/java/datadog/json/JsonMapperTest.java @@ -34,14 +34,17 @@ void testMappingToJsonObject(Map input, String expected) throws } else { assertEquals(input.size(), parsed.size()); for (Map.Entry entry : input.entrySet()) { - assertTrue(parsed.containsKey(entry.getKey())); - if (entry.getValue() instanceof UnsupportedType) { - assertEquals(entry.getValue().toString(), parsed.get(entry.getKey())); - } else if (entry.getValue() instanceof Float) { - assertTrue(parsed.get(entry.getKey()) instanceof Double); - assertEquals((Float) entry.getValue(), (Double) parsed.get(entry.getKey()), 0.001); + String k = entry.getKey(); + Object v = entry.getValue(); + assertTrue(parsed.containsKey(k)); + Object parsed_k = parsed.get(k); + if (v instanceof UnsupportedType) { + assertEquals(v.toString(), parsed_k); + } else if (v instanceof Float) { + assertTrue(parsed_k instanceof Double); + assertEquals((Float) v, (Double) parsed_k, 0.001); } else { - assertEquals(entry.getValue(), parsed.get(entry.getKey())); + assertEquals(v, parsed_k); } } } @@ -151,8 +154,8 @@ static Stream testMappingToListFromEmptyJsonObject_arguments() { @ParameterizedTest(name = "test mapping to JSON string: {0}") @MethodSource("testMappingToJsonString_arguments") void testMappingToJsonString(String input, String expected) { - String escaped = JsonMapper.toJson(input); - assertEquals(expected, escaped); + String json = JsonMapper.toJson(input); + assertEquals(expected, json); } static Stream testMappingToJsonString_arguments() { From 24c2cc1feadb9a5192733dd2f260332ac4b7f3d2 Mon Sep 17 00:00:00 2001 From: Sarah Chen Date: Tue, 3 Mar 2026 16:24:51 -0500 Subject: [PATCH 5/6] Rename var --- .../src/test/java/datadog/json/JsonMapperTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/json/src/test/java/datadog/json/JsonMapperTest.java b/components/json/src/test/java/datadog/json/JsonMapperTest.java index cb64c7b3f12..6df89222dd5 100644 --- a/components/json/src/test/java/datadog/json/JsonMapperTest.java +++ b/components/json/src/test/java/datadog/json/JsonMapperTest.java @@ -37,14 +37,14 @@ void testMappingToJsonObject(Map input, String expected) throws String k = entry.getKey(); Object v = entry.getValue(); assertTrue(parsed.containsKey(k)); - Object parsed_k = parsed.get(k); + Object parsed_v = parsed.get(k); if (v instanceof UnsupportedType) { - assertEquals(v.toString(), parsed_k); + assertEquals(v.toString(), parsed_v); } else if (v instanceof Float) { - assertTrue(parsed_k instanceof Double); - assertEquals((Float) v, (Double) parsed_k, 0.001); + assertTrue(parsed_v instanceof Double); + assertEquals((Float) v, (Double) parsed_v, 0.001); } else { - assertEquals(v, parsed_k); + assertEquals(v, parsed_v); } } } From ecf9f02931b912e4b3289b8074f51f3225de076c Mon Sep 17 00:00:00 2001 From: Sarah Chen Date: Wed, 4 Mar 2026 11:03:16 -0500 Subject: [PATCH 6/6] Improve readability and address review comments --- .../skills/migrate-groovy-to-java/SKILL.md | 10 ++- .../java/datadog/json/JsonMapperTest.java | 81 ++++++++++--------- 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/.claude/skills/migrate-groovy-to-java/SKILL.md b/.claude/skills/migrate-groovy-to-java/SKILL.md index a86c56d6711..8d74a118ecb 100644 --- a/.claude/skills/migrate-groovy-to-java/SKILL.md +++ b/.claude/skills/migrate-groovy-to-java/SKILL.md @@ -10,11 +10,13 @@ Migrate test Groovy files to Java using JUnit 5 3. Make sure the tests are still passing after migration and that the test count has not changed 4. Remove Groovy files -When converting Groovy code to Java code make sure that: +When converting Groovy code to Java code, make sure that: - The Java code generated is compatible with JDK 8 - When translating Spock tests, favor using `@CsvSource` with `|` delimiters -- When using `@MethodSource`, use the test method name and suffix it with `_arguments` -- When converting tuples, create light dedicated structure instead to keep the typing system +- When using `@MethodSource`, name the arguments method by appending `Arguments` using camelCase to the test method name (e.g. `testMethodArguments`) +- Ensure parameterized test names are human-readable (i.e. no hashcodes); instead add a description string as the first `Arguments.of(...)` value or index the test case +- When converting tuples, create a light dedicated structure instead to keep the typing system - Instead of checking a state and throwing an exception, use JUnit asserts -- Do not wrap checked exception and throw a Runtime exception; prefer adding a throws clause at method declaration +- Do not wrap checked exceptions and throw a Runtime exception; prefer adding a throws clause at method declaration - Do not mark local variables `final` +- Ensure variables are human-readable; avoid single-letter names and pre-define variables that are referenced multiple times diff --git a/components/json/src/test/java/datadog/json/JsonMapperTest.java b/components/json/src/test/java/datadog/json/JsonMapperTest.java index 6df89222dd5..6f9660d07b5 100644 --- a/components/json/src/test/java/datadog/json/JsonMapperTest.java +++ b/components/json/src/test/java/datadog/json/JsonMapperTest.java @@ -23,8 +23,9 @@ class JsonMapperTest { @ParameterizedTest(name = "test mapping to JSON object: {0}") - @MethodSource("testMappingToJsonObject_arguments") - void testMappingToJsonObject(Map input, String expected) throws IOException { + @MethodSource("testMappingToJsonObjectArguments") + void testMappingToJsonObject(String testCase, Map input, String expected) + throws IOException { String json = JsonMapper.toJson(input); assertEquals(expected, json); @@ -34,23 +35,23 @@ void testMappingToJsonObject(Map input, String expected) throws } else { assertEquals(input.size(), parsed.size()); for (Map.Entry entry : input.entrySet()) { - String k = entry.getKey(); - Object v = entry.getValue(); - assertTrue(parsed.containsKey(k)); - Object parsed_v = parsed.get(k); - if (v instanceof UnsupportedType) { - assertEquals(v.toString(), parsed_v); - } else if (v instanceof Float) { - assertTrue(parsed_v instanceof Double); - assertEquals((Float) v, (Double) parsed_v, 0.001); + 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(v, parsed_v); + assertEquals(expectedValue, parsedValue); } } } } - static Stream testMappingToJsonObject_arguments() { + static Stream testMappingToJsonObjectArguments() { Map singleEntry = new LinkedHashMap<>(); singleEntry.put("key1", "value1"); @@ -73,24 +74,28 @@ static Stream testMappingToJsonObject_arguments() { complexMap.put("key8", new UnsupportedType()); return Stream.of( - Arguments.of(null, "{}"), - Arguments.of(new HashMap<>(), "{}"), - Arguments.of(singleEntry, "{\"key1\":\"value1\"}"), - Arguments.of(twoEntries, "{\"key1\":\"value1\",\"key2\":\"value2\"}"), - Arguments.of(quotedEntries, "{\"key1\":\"va\\\"lu\\\"e1\",\"ke\\\"y2\":\"value2\"}"), + 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("testMappingToMapFromEmptyJsonObject_arguments") + @MethodSource("testMappingToMapFromEmptyJsonObjectArguments") void testMappingToMapFromEmptyJsonObject(String json) throws IOException { Map parsed = JsonMapper.fromJsonToMap(json); assertEquals(emptyMap(), parsed); } - static Stream testMappingToMapFromEmptyJsonObject_arguments() { + static Stream testMappingToMapFromEmptyJsonObjectArguments() { return Stream.of( Arguments.of((Object) null), Arguments.of("null"), Arguments.of(""), Arguments.of("{}")); } @@ -102,7 +107,7 @@ void testMappingToMapFromNonObjectJson(String json) { } @ParameterizedTest(name = "test mapping iterable to JSON array: {0}") - @MethodSource("testMappingIterableToJsonArray_arguments") + @MethodSource("testMappingIterableToJsonArrayArguments") void testMappingIterableToJsonArray(List input, String expected) throws IOException { String json = JsonMapper.toJson(input); assertEquals(expected, json); @@ -111,7 +116,7 @@ void testMappingIterableToJsonArray(List input, String expected) throws assertEquals(input != null ? input : emptyList(), parsed); } - static Stream testMappingIterableToJsonArray_arguments() { + static Stream testMappingIterableToJsonArrayArguments() { return Stream.of( Arguments.of(null, "[]"), Arguments.of(new ArrayList<>(), "[]"), @@ -121,44 +126,48 @@ static Stream testMappingIterableToJsonArray_arguments() { } @ParameterizedTest(name = "test mapping array to JSON array: {0}") - @MethodSource("testMappingArrayToJsonArray_arguments") - void testMappingArrayToJsonArray(String[] input, String expected) throws IOException { + @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[0]); - assertArrayEquals(input != null ? input : new String[0], parsed); + String[] parsed = JsonMapper.fromJsonToList(json).toArray(new String[] {}); + assertArrayEquals(input != null ? input : new String[] {}, parsed); } - static Stream testMappingArrayToJsonArray_arguments() { + static Stream testMappingArrayToJsonArrayArguments() { return Stream.of( - Arguments.of((Object) null, "[]"), - Arguments.of(new String[0], "[]"), - Arguments.of(new String[] {"value1"}, "[\"value1\"]"), - Arguments.of(new String[] {"value1", "value2"}, "[\"value1\",\"value2\"]"), - Arguments.of(new String[] {"va\"lu\"e1", "value2"}, "[\"va\\\"lu\\\"e1\",\"value2\"]")); + 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("testMappingToListFromEmptyJsonObject_arguments") + @MethodSource("testMappingToListFromEmptyJsonObjectArguments") void testMappingToListFromEmptyJsonObject(String json) throws IOException { List parsed = JsonMapper.fromJsonToList(json); assertEquals(emptyList(), parsed); } - static Stream testMappingToListFromEmptyJsonObject_arguments() { + static Stream testMappingToListFromEmptyJsonObjectArguments() { return Stream.of( Arguments.of((Object) null), Arguments.of("null"), Arguments.of(""), Arguments.of("[]")); } @ParameterizedTest(name = "test mapping to JSON string: {0}") - @MethodSource("testMappingToJsonString_arguments") + @MethodSource("testMappingToJsonStringArguments") void testMappingToJsonString(String input, String expected) { String json = JsonMapper.toJson(input); assertEquals(expected, json); } - static Stream testMappingToJsonString_arguments() { + static Stream testMappingToJsonStringArguments() { return Stream.of( Arguments.of((Object) null, ""), Arguments.of("", ""),