Skip to content

Commit

Permalink
[#1878] Update SystemTestUtil::assertJson to compare Json objects i…
Browse files Browse the repository at this point in the history
…nstead 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.
  • Loading branch information
georgetayqy authored Feb 6, 2024
1 parent 7977382 commit 30ef450
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/systemtest/java/reposense/util/SystemTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit 30ef450

Please sign in to comment.