From 2910c5a254dda9fef779295565787440d31d2bb9 Mon Sep 17 00:00:00 2001 From: Vaibhav Ramchandani Date: Sat, 2 Mar 2024 13:06:46 -0400 Subject: [PATCH] feat: Add test cases for JsonFormatter class This class is in com.jayway.jsonpath.internal package.Add cases to cover the following: -Simple Json Object, Json Object Array -Json With Escaped Characters,Json Object with Escaped Characters --- .../jsonpath/internal/JsonFormatterTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 json-path/src/test/java/com/jayway/jsonpath/internal/JsonFormatterTest.java diff --git a/json-path/src/test/java/com/jayway/jsonpath/internal/JsonFormatterTest.java b/json-path/src/test/java/com/jayway/jsonpath/internal/JsonFormatterTest.java new file mode 100644 index 00000000..b0172cfc --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/internal/JsonFormatterTest.java @@ -0,0 +1,61 @@ +package com.jayway.jsonpath.internal; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +public class JsonFormatterTest { + + // Test case for pretty printing a simple JSON object + // The input is a simple JSON object + // The expected output is the same JSON object with proper indentation + @Test + public void testPrettyPrint_SimpleJson() { + JsonFormatter jsonFormatter = new JsonFormatter(); + String input = "{\"name\":\"Vaibhav Ramchandani\",\"age\":23,\"city\":\"Halifax\"}"; + String actualOutput = jsonFormatter.prettyPrint(input); + String expectedOutput = "{\n \"name\" : \"Vaibhav Ramchandani\",\n \"age\" : 23,\n \"city\" : \"Halifax\"\n}"; + assertEquals(expectedOutput.replaceAll("\\s", ""), actualOutput.replaceAll("\\s", "")); + } + + // Test case for pretty printing a JSON object with escaped characters + // The input is a JSON object with escaped characters + // The expected output is the same JSON object with proper indentation + + @Test + public void testPrettyPrint_JsonWithEscapedCharacters() { + JsonFormatter jsonFormatter = new JsonFormatter(); + String input = "{\"message\":\"This is ,\\nJsonPath Repo!\"}"; + String actualOutput = jsonFormatter.prettyPrint(input); + String expectedOutput = "{\n \"message\" : \"This is ,\\nJsonPath Repo!\"\n}"; + assertEquals(expectedOutput.replaceAll("\\s", ""), actualOutput.replaceAll("\\s", "")); + } + + // Test case for pretty printing a JSON array + // The input is a JSON array + // The expected output is the same JSON array with proper indentation + @Test + public void testPrettyPrint_JsonWithArray() { + + JsonFormatter jsonFormatter = new JsonFormatter(); + String input = "[{\"name\":\"Vaibhav R\",\"age\":23},{\"name\":\"Sanskar K\",\"age\":24}]"; + String actualOutput=jsonFormatter.prettyPrint(input); + String expectedOutput = "[\n {\n \"name\" : \"Vaibhav R\",\n \"age\" : 23\n },\n {\n \"name\" : \"Sanskar K\",\n \"age\" : 24\n }\n]"; + assertEquals(expectedOutput.replaceAll("\\s", ""), actualOutput.replaceAll("\\s", "")); + } + + // Test case for pretty printing a JSON object with single quotes + // The input is a JSON object with single quotes + // The expected output is the same JSON object with proper indentation + @Test + public void testPrettyPrint_JsonWithSingleQuotes() { + + JsonFormatter jsonFormatter = new JsonFormatter(); + String input = "{'name':'Vaibhav Ramchandani','age':23,'city':'Halifax'}"; + String actualOutput = jsonFormatter.prettyPrint(input); + String expectedOutput = "{\n 'name' : 'Vaibhav Ramchandani',\n 'age' : 23,\n 'city' : 'Halifax'\n}"; + assertEquals(expectedOutput.replaceAll("\\s", ""), actualOutput.replaceAll("\\s", "")); + } +} +