1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#include "utils_json_test.h"
17fb299fa2Sopenharmony_ci#include <fstream>
18fb299fa2Sopenharmony_ci#include "cJSON.h"
19fb299fa2Sopenharmony_ci#include "json_node.h"
20fb299fa2Sopenharmony_ci#include "utils.h"
21fb299fa2Sopenharmony_ci
22fb299fa2Sopenharmony_ciusing namespace std::literals;
23fb299fa2Sopenharmony_ciusing namespace std;
24fb299fa2Sopenharmony_ciusing namespace testing::ext;
25fb299fa2Sopenharmony_ciusing namespace Updater;
26fb299fa2Sopenharmony_ciusing namespace Utils;
27fb299fa2Sopenharmony_ci
28fb299fa2Sopenharmony_cinamespace UpdaterUt {
29fb299fa2Sopenharmony_ci// do something at the each function begining
30fb299fa2Sopenharmony_civoid UtilsJsonNodeUnitTest::SetUp(void)
31fb299fa2Sopenharmony_ci{
32fb299fa2Sopenharmony_ci    cout << "Updater Unit UtilsJsonNodeUnitTest Begin!" << endl;
33fb299fa2Sopenharmony_ci}
34fb299fa2Sopenharmony_ci
35fb299fa2Sopenharmony_ci// do something at the each function end
36fb299fa2Sopenharmony_civoid UtilsJsonNodeUnitTest::TearDown(void)
37fb299fa2Sopenharmony_ci{
38fb299fa2Sopenharmony_ci    cout << "Updater Unit UtilsJsonNodeUnitTest End!" << endl;
39fb299fa2Sopenharmony_ci}
40fb299fa2Sopenharmony_ci
41fb299fa2Sopenharmony_ci// init
42fb299fa2Sopenharmony_civoid UtilsJsonNodeUnitTest::SetUpTestCase(void)
43fb299fa2Sopenharmony_ci{
44fb299fa2Sopenharmony_ci    cout << "SetUpTestCase" << endl;
45fb299fa2Sopenharmony_ci}
46fb299fa2Sopenharmony_ci
47fb299fa2Sopenharmony_ci// end
48fb299fa2Sopenharmony_civoid UtilsJsonNodeUnitTest::TearDownTestCase(void)
49fb299fa2Sopenharmony_ci{
50fb299fa2Sopenharmony_ci    cout << "TearDownTestCase" << endl;
51fb299fa2Sopenharmony_ci}
52fb299fa2Sopenharmony_ci
53fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestGetKey, TestSize.Level0)
54fb299fa2Sopenharmony_ci{
55fb299fa2Sopenharmony_ci    {
56fb299fa2Sopenharmony_ci        std::string str = R"({"key": "value1"})";
57fb299fa2Sopenharmony_ci        JsonNode node(str);
58fb299fa2Sopenharmony_ci        EXPECT_EQ(node.Key(), std::nullopt);
59fb299fa2Sopenharmony_ci        EXPECT_EQ(node["key"].Key(), "key");
60fb299fa2Sopenharmony_ci    }
61fb299fa2Sopenharmony_ci    {
62fb299fa2Sopenharmony_ci        std::string str = R"({"key"})";
63fb299fa2Sopenharmony_ci        JsonNode node(str);
64fb299fa2Sopenharmony_ci        EXPECT_EQ(node.Key(), std::nullopt);
65fb299fa2Sopenharmony_ci    }
66fb299fa2Sopenharmony_ci}
67fb299fa2Sopenharmony_ci
68fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestEqualTypeNotMatched, TestSize.Level0)
69fb299fa2Sopenharmony_ci{
70fb299fa2Sopenharmony_ci    std::string str = R"({"key": "value1"})";
71fb299fa2Sopenharmony_ci    JsonNode node(str);
72fb299fa2Sopenharmony_ci    EXPECT_EQ((node["key"] == 1), false);
73fb299fa2Sopenharmony_ci}
74fb299fa2Sopenharmony_ci
75fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestStrNode, TestSize.Level0)
76fb299fa2Sopenharmony_ci{
77fb299fa2Sopenharmony_ci    std::string str = R"({"key": "value1"})";
78fb299fa2Sopenharmony_ci    JsonNode node(str);
79fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"].Type(), NodeType::STRING);
80fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"], "value1");
81fb299fa2Sopenharmony_ci}
82fb299fa2Sopenharmony_ci
83fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestIntNode, TestSize.Level0)
84fb299fa2Sopenharmony_ci{
85fb299fa2Sopenharmony_ci    std::string str = R"({"key": 1})";
86fb299fa2Sopenharmony_ci    JsonNode node(str);
87fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"].Type(), NodeType::INT);
88fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"], 1);
89fb299fa2Sopenharmony_ci}
90fb299fa2Sopenharmony_ci
91fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestBoolNode, TestSize.Level0)
92fb299fa2Sopenharmony_ci{
93fb299fa2Sopenharmony_ci    std::string str = R"({"key": true})";
94fb299fa2Sopenharmony_ci    JsonNode node(str);
95fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"].Type(), NodeType::BOOL);
96fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"], true);
97fb299fa2Sopenharmony_ci}
98fb299fa2Sopenharmony_ci
99fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestArrNode, TestSize.Level0)
100fb299fa2Sopenharmony_ci{
101fb299fa2Sopenharmony_ci    std::string str = R"({"key": [1, true, "value"]})";
102fb299fa2Sopenharmony_ci    JsonNode node(str);
103fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"].Type(), NodeType::ARRAY);
104fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"][0], 1);
105fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"][1], true);
106fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"][2], "value");
107fb299fa2Sopenharmony_ci}
108fb299fa2Sopenharmony_ci
109fb299fa2Sopenharmony_ci
110fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestObjNode, TestSize.Level0)
111fb299fa2Sopenharmony_ci{
112fb299fa2Sopenharmony_ci    std::string str = R"({"key": {"key" : "value"}}})";
113fb299fa2Sopenharmony_ci    JsonNode node(str);
114fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"].Type(), NodeType::OBJECT);
115fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"]["key"], "value");
116fb299fa2Sopenharmony_ci}
117fb299fa2Sopenharmony_ci
118fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestNULNode, TestSize.Level0)
119fb299fa2Sopenharmony_ci{
120fb299fa2Sopenharmony_ci    std::string str = R"({"key1": null})";
121fb299fa2Sopenharmony_ci    JsonNode node(str);
122fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key1"].Type(), NodeType::NUL);
123fb299fa2Sopenharmony_ci}
124fb299fa2Sopenharmony_ci
125fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestInvalidNode, TestSize.Level0)
126fb299fa2Sopenharmony_ci{
127fb299fa2Sopenharmony_ci    std::string str = R"({"key":})";
128fb299fa2Sopenharmony_ci    JsonNode node(str);
129fb299fa2Sopenharmony_ci    EXPECT_EQ(node.Type(), NodeType::UNKNOWN);
130fb299fa2Sopenharmony_ci}
131fb299fa2Sopenharmony_ci
132fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestAll, TestSize.Level0)
133fb299fa2Sopenharmony_ci{
134fb299fa2Sopenharmony_ci    static const std::string str = R"(
135fb299fa2Sopenharmony_ci    {
136fb299fa2Sopenharmony_ci        "A": "B",
137fb299fa2Sopenharmony_ci        "C": {
138fb299fa2Sopenharmony_ci            "D": "E",
139fb299fa2Sopenharmony_ci            "F": {
140fb299fa2Sopenharmony_ci                "G": {
141fb299fa2Sopenharmony_ci                    "H": "I",
142fb299fa2Sopenharmony_ci                    "J": 8879,
143fb299fa2Sopenharmony_ci                    "K": {
144fb299fa2Sopenharmony_ci                        "L": "M",
145fb299fa2Sopenharmony_ci                        "N": ["O", "P"]
146fb299fa2Sopenharmony_ci                    },
147fb299fa2Sopenharmony_ci                    "L": true
148fb299fa2Sopenharmony_ci                }
149fb299fa2Sopenharmony_ci            }
150fb299fa2Sopenharmony_ci        }
151fb299fa2Sopenharmony_ci    })";
152fb299fa2Sopenharmony_ci    JsonNode node(str);
153fb299fa2Sopenharmony_ci    const JsonNode &nodeC = node["C"];
154fb299fa2Sopenharmony_ci    const JsonNode &nodeG = nodeC["F"]["G"];
155fb299fa2Sopenharmony_ci    EXPECT_EQ(node.Type(), NodeType::OBJECT);
156fb299fa2Sopenharmony_ci    EXPECT_EQ(node["A"], "B");
157fb299fa2Sopenharmony_ci    EXPECT_EQ(nodeC["D"], "E");
158fb299fa2Sopenharmony_ci    EXPECT_EQ(nodeG["H"], "I");
159fb299fa2Sopenharmony_ci    EXPECT_EQ(nodeG["J"], 8879);
160fb299fa2Sopenharmony_ci    EXPECT_EQ(nodeG["K"]["L"], "M");
161fb299fa2Sopenharmony_ci    EXPECT_EQ(nodeG["K"]["N"][0], "O");
162fb299fa2Sopenharmony_ci    EXPECT_EQ(nodeG["K"]["N"][1], "P");
163fb299fa2Sopenharmony_ci    EXPECT_EQ(nodeG["L"], true);
164fb299fa2Sopenharmony_ci}
165fb299fa2Sopenharmony_ci
166fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestInvalidKey, TestSize.Level0)
167fb299fa2Sopenharmony_ci{
168fb299fa2Sopenharmony_ci    std::string str = R"({"key": "value1"})";
169fb299fa2Sopenharmony_ci    JsonNode node(str);
170fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key1"].Type(), NodeType::UNKNOWN);
171fb299fa2Sopenharmony_ci}
172fb299fa2Sopenharmony_ci
173fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestInvalidIndex, TestSize.Level0)
174fb299fa2Sopenharmony_ci{
175fb299fa2Sopenharmony_ci    {
176fb299fa2Sopenharmony_ci        std::string str = R"({"key": "value1"})";
177fb299fa2Sopenharmony_ci        JsonNode node(str);
178fb299fa2Sopenharmony_ci        EXPECT_EQ(node["key"].Type(), NodeType::STRING);
179fb299fa2Sopenharmony_ci        EXPECT_EQ(node["key1"].Type(), NodeType::UNKNOWN);
180fb299fa2Sopenharmony_ci        EXPECT_EQ(node["key"].Type(), NodeType::STRING);
181fb299fa2Sopenharmony_ci    }
182fb299fa2Sopenharmony_ci    {
183fb299fa2Sopenharmony_ci        std::string str = R"({"key": [1]})";
184fb299fa2Sopenharmony_ci        JsonNode node(str);
185fb299fa2Sopenharmony_ci        EXPECT_EQ(node["key"].Type(), NodeType::ARRAY);
186fb299fa2Sopenharmony_ci        EXPECT_EQ(node["key"][0].Type(), NodeType::INT);
187fb299fa2Sopenharmony_ci        EXPECT_EQ(node["key"][1].Type(), NodeType::UNKNOWN);
188fb299fa2Sopenharmony_ci    }
189fb299fa2Sopenharmony_ci}
190fb299fa2Sopenharmony_ci
191fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestInvalidPath0, TestSize.Level0)
192fb299fa2Sopenharmony_ci{
193fb299fa2Sopenharmony_ci    JsonNode node(Fs::path {R"(\invalid)"});
194fb299fa2Sopenharmony_ci    EXPECT_EQ(node.Type(), NodeType::UNKNOWN);
195fb299fa2Sopenharmony_ci}
196fb299fa2Sopenharmony_ci
197fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestInvalidPath1, TestSize.Level0)
198fb299fa2Sopenharmony_ci{
199fb299fa2Sopenharmony_ci    JsonNode node(Fs::path {"/data/noexist"});
200fb299fa2Sopenharmony_ci    EXPECT_EQ(node.Type(), NodeType::UNKNOWN);
201fb299fa2Sopenharmony_ci}
202fb299fa2Sopenharmony_ci
203fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestInvalidPath2, TestSize.Level0)
204fb299fa2Sopenharmony_ci{
205fb299fa2Sopenharmony_ci    constexpr auto invalidContent = R"({ "key" : "value")";
206fb299fa2Sopenharmony_ci    constexpr auto invalidJsonPath = "/tmp/tmp.json";
207fb299fa2Sopenharmony_ci    {
208fb299fa2Sopenharmony_ci        std::ofstream ofs(Fs::path {invalidJsonPath});
209fb299fa2Sopenharmony_ci        ofs << invalidContent;
210fb299fa2Sopenharmony_ci        ofs.flush();
211fb299fa2Sopenharmony_ci    }
212fb299fa2Sopenharmony_ci    JsonNode node(Fs::path {invalidJsonPath});
213fb299fa2Sopenharmony_ci    EXPECT_EQ(node.Type(), NodeType::UNKNOWN);
214fb299fa2Sopenharmony_ci    DeleteFile(invalidJsonPath);
215fb299fa2Sopenharmony_ci}
216fb299fa2Sopenharmony_ci
217fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestVectorAssign, TestSize.Level0)
218fb299fa2Sopenharmony_ci{
219fb299fa2Sopenharmony_ci    std::string str = R"({"key":[1, true, "value"]})";
220fb299fa2Sopenharmony_ci    JsonNode node {str};
221fb299fa2Sopenharmony_ci    constexpr int intVal = 2;
222fb299fa2Sopenharmony_ci    constexpr bool boolVal = false;
223fb299fa2Sopenharmony_ci    const char *strVal = "newValue";
224fb299fa2Sopenharmony_ci    int idx = 0;
225fb299fa2Sopenharmony_ci    node["key"][idx++] = intVal;
226fb299fa2Sopenharmony_ci    node["key"][idx++] = boolVal;
227fb299fa2Sopenharmony_ci    node["key"][idx++] = strVal;
228fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"][--idx], strVal);
229fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"][--idx], boolVal);
230fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"][--idx], intVal);
231fb299fa2Sopenharmony_ci}
232fb299fa2Sopenharmony_ci
233fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestIntAssign, TestSize.Level0)
234fb299fa2Sopenharmony_ci{
235fb299fa2Sopenharmony_ci    std::string str = R"({"key":1})";
236fb299fa2Sopenharmony_ci    JsonNode node {str};
237fb299fa2Sopenharmony_ci    constexpr int intVal = 2;
238fb299fa2Sopenharmony_ci    node["key"] = intVal;
239fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"], intVal);
240fb299fa2Sopenharmony_ci}
241fb299fa2Sopenharmony_ci
242fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestBoolAssign, TestSize.Level0)
243fb299fa2Sopenharmony_ci{
244fb299fa2Sopenharmony_ci    std::string str = R"({"key":true})";
245fb299fa2Sopenharmony_ci    JsonNode node {str};
246fb299fa2Sopenharmony_ci    constexpr bool boolVal = false;
247fb299fa2Sopenharmony_ci    node["key"] = boolVal;
248fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"], boolVal);
249fb299fa2Sopenharmony_ci}
250fb299fa2Sopenharmony_ci
251fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestStrAssign, TestSize.Level0)
252fb299fa2Sopenharmony_ci{
253fb299fa2Sopenharmony_ci    std::string str = R"({"key":"value"})";
254fb299fa2Sopenharmony_ci    JsonNode node {str};
255fb299fa2Sopenharmony_ci    const char *strVal = "newValue";
256fb299fa2Sopenharmony_ci    node["key"] = strVal;
257fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"], strVal);
258fb299fa2Sopenharmony_ci}
259fb299fa2Sopenharmony_ci
260fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestMultiLevelAssign, TestSize.Level0)
261fb299fa2Sopenharmony_ci{
262fb299fa2Sopenharmony_ci    std::string str = R"({"key1":{
263fb299fa2Sopenharmony_ci        "key2":1,
264fb299fa2Sopenharmony_ci        "key3":"value"
265fb299fa2Sopenharmony_ci    }})";
266fb299fa2Sopenharmony_ci    JsonNode node {str};
267fb299fa2Sopenharmony_ci    constexpr int newValue = 2;
268fb299fa2Sopenharmony_ci    node["key1"]["key2"] = newValue;
269fb299fa2Sopenharmony_ci    node["key1"]["key3"] = "value2";
270fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key1"]["key2"], newValue);
271fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key1"]["key3"], "value2");
272fb299fa2Sopenharmony_ci}
273fb299fa2Sopenharmony_ci
274fb299fa2Sopenharmony_ciHWTEST_F(UtilsJsonNodeUnitTest, TestInvalidAssign, TestSize.Level0)
275fb299fa2Sopenharmony_ci{
276fb299fa2Sopenharmony_ci    std::string str = R"({"key" : 1})";
277fb299fa2Sopenharmony_ci    JsonNode node {str};
278fb299fa2Sopenharmony_ci    constexpr int value = 1;
279fb299fa2Sopenharmony_ci    node["key"] = false;
280fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"], value);
281fb299fa2Sopenharmony_ci    node["key"] = "newValue";
282fb299fa2Sopenharmony_ci    EXPECT_EQ(node["key"], value);
283fb299fa2Sopenharmony_ci}
284fb299fa2Sopenharmony_ci} // namespace UpdaterUt
285