From 4214e8103c454c4b3589f10faa561afe5156eb03 Mon Sep 17 00:00:00 2001 From: Scott Wierschem Date: Thu, 11 Jul 2024 16:18:15 -0500 Subject: [PATCH] - F verifyJson() accepts GsonBuilder Fixes #479 --- ...persTest.listAllVerifyFunctions.approved.md | 9 +++++---- .../org/approvaltests/JsonFormattingTest.java | 7 +++++++ ...estJsonFieldOrderingWithNulls.approved.json | 18 ++++++++++++++++++ .../src/main/java/com/spun/util/JsonUtils.java | 6 +++++- .../java/org/approvaltests/JsonApprovals.java | 11 ++++++++++- 5 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 approvaltests-tests/src/test/java/org/approvaltests/JsonFormattingTest.testJsonFieldOrderingWithNulls.approved.json diff --git a/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.listAllVerifyFunctions.approved.md b/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.listAllVerifyFunctions.approved.md index e6f4cfb4..33233805 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.listAllVerifyFunctions.approved.md +++ b/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.listAllVerifyFunctions.approved.md @@ -51,10 +51,11 @@ * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L291-L299) (Function7, Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$) * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L309-L317) (Function8, Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$) * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L326-L333) (Function9, Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$) - * JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L44-L47) (Object, $\color{#AAA}{\textsf{Options}}$) - * JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L39-L42) (Object, Function1, $\color{#AAA}{\textsf{Options}}$) - * JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L18-L21) (String, $\color{#AAA}{\textsf{Options}}$) - * JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L26-L30) (String, boolean, $\color{#AAA}{\textsf{Options}}$) + * JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L53-L56) (Object, $\color{#AAA}{\textsf{Options}}$) + * JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L48-L51) (Object, Function1, $\color{#AAA}{\textsf{Options}}$) + * JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L17-L20) (String, $\color{#AAA}{\textsf{Options}}$) + * JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L35-L39) (String, boolean, $\color{#AAA}{\textsf{Options}}$) + * JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L29-L34) (String, boolean, Function1, $\color{#AAA}{\textsf{Options}}$) * JsonJacksonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonJacksonApprovals.java#L14-L17) (Object, $\color{#AAA}{\textsf{Options}}$) * JsonXstreamApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonXstreamApprovals.java#L18-L21) (Object, $\color{#AAA}{\textsf{Options}}$) * VelocityApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/velocity/VelocityApprovals.java#L16-L24) (ContextAware, $\color{#AAA}{\textsf{Options}}$) diff --git a/approvaltests-tests/src/test/java/org/approvaltests/JsonFormattingTest.java b/approvaltests-tests/src/test/java/org/approvaltests/JsonFormattingTest.java index 04f93ae5..993f2bdc 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/JsonFormattingTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/JsonFormattingTest.java @@ -69,4 +69,11 @@ public void testJsonFieldOrdering() JsonApprovals.verifyJson(JsonUtils.reorderFields(json1)); JsonApprovals.verifyJson(JsonUtils.reorderFields(json2)); } + @Test + public void testJsonFieldOrderingWithNulls() + { + Approvals.settings().allowMultipleVerifyCallsForThisMethod(); + String json1 = "{\"infos\":{\"address\":null,\"phone\":\"my phone\"},\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]}}"; + JsonApprovals.verifyJson(json1, true, g -> g.serializeNulls()); + } } diff --git a/approvaltests-tests/src/test/java/org/approvaltests/JsonFormattingTest.testJsonFieldOrderingWithNulls.approved.json b/approvaltests-tests/src/test/java/org/approvaltests/JsonFormattingTest.testJsonFieldOrderingWithNulls.approved.json new file mode 100644 index 00000000..47bfd23f --- /dev/null +++ b/approvaltests-tests/src/test/java/org/approvaltests/JsonFormattingTest.testJsonFieldOrderingWithNulls.approved.json @@ -0,0 +1,18 @@ +{ + "infos": { + "address": null, + "phone": "my phone" + }, + "insurance": { + "forks": [ + 14, + 53, + 123 + ], + "prices": [ + 5, + 8, + "3%" + ] + } +} \ No newline at end of file diff --git a/approvaltests-util/src/main/java/com/spun/util/JsonUtils.java b/approvaltests-util/src/main/java/com/spun/util/JsonUtils.java index b278c0da..feee66f2 100644 --- a/approvaltests-util/src/main/java/com/spun/util/JsonUtils.java +++ b/approvaltests-util/src/main/java/com/spun/util/JsonUtils.java @@ -66,9 +66,13 @@ private static GsonBuilder addHandlingForDateObjects(GsonBuilder builder) return builder; } public static String reorderFields(String json) + { + return reorderFields(json, g -> g); + } + public static String reorderFields(String json, Function1 gsonBuilder) { JsonObject sortedJsonObject = sortJsonObject(json); - return asJson(sortedJsonObject); + return asJson(sortedJsonObject, gsonBuilder); } public static JsonObject sortJsonObject(String json) { diff --git a/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java b/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java index 4857da89..8afc4ca8 100644 --- a/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java +++ b/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java @@ -1,7 +1,6 @@ package org.approvaltests; import com.google.gson.GsonBuilder; -import com.spun.util.FormattedException; import com.spun.util.JsonUtils; import org.approvaltests.core.Options; import org.lambda.functions.Function1; @@ -23,6 +22,16 @@ public static void verifyJson(String json, boolean reorderJson) { verifyJson(json, reorderJson, new Options()); } + public static void verifyJson(String json, boolean reorderJson, Function1 gsonBuilder) + { + verifyJson(json, reorderJson, gsonBuilder, new Options()); + } + public static void verifyJson(String json, boolean reorderJson, Function1 gsonBuilder, + Options options) + { + String formattedJson = reorderJson ? JsonUtils.reorderFields(json, gsonBuilder) : JsonUtils.prettyPrint(json); + Approvals.verify(formattedJson, options.forFile().withExtension(".json")); + } public static void verifyJson(String json, boolean reorderJson, Options options) { String formattedJson = reorderJson ? JsonUtils.reorderFields(json) : JsonUtils.prettyPrint(json);