1+ #include < gtest/gtest.h>
2+
3+ #include < vector>
4+ #include < string>
5+ #include < memory>
6+
7+ #include " JsonHandler.h"
8+
9+ namespace JsonParsing
10+ {
11+ class JsonHandlerTest : public ::testing::Test
12+ {
13+ protected:
14+ JsonHandler handler {{}};
15+
16+ protected:
17+ void SetUp () override {}
18+ void TearDown () override {}
19+
20+ void setParseOptions (const ParseOptions& opt)
21+ {
22+ handler = JsonHandler (opt);
23+ }
24+ };
25+
26+ TEST_F (JsonHandlerTest, TestGetCompressedJson_Success)
27+ {
28+ std::string inputJson = R"( {"key": "value"})" ;
29+ auto result = handler.GetCompressedJson (inputJson);
30+
31+ ASSERT_TRUE (result.success );
32+ ASSERT_EQ (result.response , R"( {"key":"value"})" );
33+ }
34+
35+ TEST_F (JsonHandlerTest, TestGetCompressedJson_InvalidJson)
36+ {
37+ std::string inputJson = R"( {"key": "value")" ; // Missing closing brace
38+ auto result = handler.GetCompressedJson (inputJson);
39+
40+ ASSERT_FALSE (result.success );
41+ ASSERT_TRUE (result.response .empty ());
42+ }
43+
44+ TEST_F (JsonHandlerTest, TestFormatJson_Success)
45+ {
46+ std::string inputJson = R"( {"key": "value"})" ;
47+ auto result = handler.FormatJson (inputJson, {}, {}, ' ' , 4 );
48+
49+ ASSERT_TRUE (result.success );
50+ ASSERT_EQ (result.response , " {\n \" key\" : \" value\"\n }" );
51+ }
52+
53+ TEST_F (JsonHandlerTest, TestFormatJson_InvalidJson)
54+ {
55+ std::string inputJson = R"( {"key": "value")" ; // Invalid JSON
56+ auto result = handler.FormatJson (inputJson, {}, {}, ' ' , 4 );
57+
58+ ASSERT_FALSE (result.success );
59+ }
60+
61+ // Test ValidateJson
62+ TEST_F (JsonHandlerTest, TestValidateJson_Success)
63+ {
64+ std::string inputJson = R"( {"key": "value"})" ;
65+ auto result = handler.ValidateJson (inputJson);
66+
67+ ASSERT_TRUE (result.success );
68+ }
69+
70+ TEST_F (JsonHandlerTest, TestValidateJson_InvalidJson)
71+ {
72+ std::string inputJson = R"( {"key": "value")" ; // Invalid JSON
73+ auto result = handler.ValidateJson (inputJson);
74+
75+ ASSERT_FALSE (result.success );
76+ }
77+
78+ // Test SortJsonByKey
79+ TEST_F (JsonHandlerTest, TestSortJsonByKey_Success)
80+ {
81+ std::string inputJson = R"( {"b": "valueB", "a": "valueA"})" ;
82+ auto result = handler.SortJsonByKey (inputJson, {}, {}, ' ' , 4 );
83+
84+ ASSERT_TRUE (result.success );
85+ ASSERT_EQ (result.response , " {\n \" a\" : \" valueA\" ,\n \" b\" : \" valueB\"\n }" );
86+ }
87+
88+ TEST_F (JsonHandlerTest, TestSortJsonByKey_InvalidJson)
89+ {
90+ std::string inputJson = R"( {"b": "valueB", "a": "valueA")" ; // Invalid JSON
91+ auto result = handler.SortJsonByKey (inputJson, {}, {}, ' ' , 4 );
92+
93+ ASSERT_FALSE (result.success );
94+ }
95+ } // namespace JsonParsing
0 commit comments