From 30ef45089c9d959c3d667519f56197b01690d38b Mon Sep 17 00:00:00 2001 From: George Tay Date: Tue, 6 Feb 2024 22:58:42 +0800 Subject: [PATCH] [#1878] Update `SystemTestUtil::assertJson` to compare Json objects instead of line-by-line analysis (#2087) `SystemTestUtil::assertJson` currently checks if two JSON files are equal by comparing the file contents line by line rather than by converting the files directly into JSON objects, which can then be directly compared using the built-in `equals` methods for `JsonElement`. This process also does not automatically check if the file is indeed a JSON file, and implicitly assumes that the input paths are paths to JSON files. This may cause expected behaviours if a user accidentally or intentionally includes a path to a non-JSON file within the method calls. Let's move to refactor the code, and utilise the built-in methods for `JsonElement` objects to check for the equality of JSON objects. --- .../java/reposense/util/SystemTestUtil.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/systemtest/java/reposense/util/SystemTestUtil.java b/src/systemtest/java/reposense/util/SystemTestUtil.java index 6d1ac7a506..722d767758 100644 --- a/src/systemtest/java/reposense/util/SystemTestUtil.java +++ b/src/systemtest/java/reposense/util/SystemTestUtil.java @@ -9,6 +9,7 @@ import org.junit.jupiter.api.Assertions; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -63,10 +64,16 @@ public static void assertSummaryJson(Path expectedSummaryJsonPath, Path actualSu */ public static void assertJson(Path expectedJsonPath, Path actualJsonPath) { Assertions.assertTrue(Files.exists(actualJsonPath)); - try { - Assertions.assertTrue(TestUtil.compareFileContents(expectedJsonPath, actualJsonPath)); - } catch (Exception e) { - Assertions.fail(e.getMessage()); + + try (FileReader fileReaderExpected = new FileReader(expectedJsonPath.toFile()); + FileReader fileReaderActual = new FileReader(actualJsonPath.toFile())) { + + JsonElement jsonExpected = JsonParser.parseReader(fileReaderExpected); + JsonElement jsonActual = JsonParser.parseReader(fileReaderActual); + + Assertions.assertEquals(jsonExpected, jsonActual); + } catch (IOException ex) { + Assertions.fail(ex.getMessage()); } } }