From 710b0d03ec309681a654b0128fbacc6eb30b606a Mon Sep 17 00:00:00 2001 From: sathishk Date: Sat, 16 Mar 2024 11:45:20 +0530 Subject: [PATCH] Refactoring 4 --- .../sjson/core/parser/ArrayParserTest.java | 57 +++++++++++++++++++ .../sjson/core/parser/ObjectParserTest.java | 10 ++-- 2 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/techatpark/sjson/core/parser/ArrayParserTest.java diff --git a/src/test/java/com/techatpark/sjson/core/parser/ArrayParserTest.java b/src/test/java/com/techatpark/sjson/core/parser/ArrayParserTest.java new file mode 100644 index 0000000..006f0db --- /dev/null +++ b/src/test/java/com/techatpark/sjson/core/parser/ArrayParserTest.java @@ -0,0 +1,57 @@ +package com.techatpark.sjson.core.parser; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.JsonParser; +import com.techatpark.sjson.core.Json; +import com.techatpark.sjson.core.util.TestUtil; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import java.io.FileReader; +import java.io.IOException; +import java.io.StringReader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class ArrayParserTest { + final ObjectMapper objectMapper = new ObjectMapper(); + + + /** + * Provides paths to JSON files for parameterized tests. + * + * @return Stream of paths to JSON files + * @throws IOException if there is an issue listing files + */ + private static List jsonFilePath() throws IOException { + List jsonFilePath = new ArrayList<>(); + + for (Path path : TestUtil.getJSONFiles()) { + String jsonText = Files.readString(path).trim(); + if (jsonText.startsWith("[")) { + jsonFilePath.add(path); + } + } + + return jsonFilePath; + + } + + @ParameterizedTest + @MethodSource("jsonFilePath") + void testValid(final Path path) throws IOException { + Assertions.assertEquals(JsonParser + .parseReader( + new StringReader(objectMapper.writeValueAsString( + new Json().read( + new FileReader(path.toFile()) + )))), + JsonParser.parseReader(new FileReader(path.toFile())), + "Reverse JSON Failed for " + path); + } +} \ No newline at end of file diff --git a/src/test/java/com/techatpark/sjson/core/parser/ObjectParserTest.java b/src/test/java/com/techatpark/sjson/core/parser/ObjectParserTest.java index e24108f..9afe159 100644 --- a/src/test/java/com/techatpark/sjson/core/parser/ObjectParserTest.java +++ b/src/test/java/com/techatpark/sjson/core/parser/ObjectParserTest.java @@ -34,22 +34,22 @@ class ObjectParserTest { * @return Stream of paths to JSON files * @throws IOException if there is an issue listing files */ - private static List jsonObjectStrings() throws IOException { - List jsonObjectStrings = new ArrayList<>(); + private static List jsonFilePath() throws IOException { + List jsonFilePath = new ArrayList<>(); for (Path path : TestUtil.getJSONFiles()) { String jsonText = Files.readString(path).trim(); if (jsonText.startsWith("{")) { - jsonObjectStrings.add(path); + jsonFilePath.add(path); } } - return jsonObjectStrings; + return jsonFilePath; } @ParameterizedTest - @MethodSource("jsonObjectStrings") + @MethodSource("jsonFilePath") void testValid(final Path path) throws IOException { Assertions.assertEquals(JsonParser .parseReader(