• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <memory>
17 
18 #include "gtest/gtest.h"
19 
20 #include "base/json/json_util.h"
21 #include "base/utils/utils.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS::Ace {
27 namespace {
28 
29 const std::string TEST_STRING = "Ace Unittest";
30 const std::string TEST_KEY = "JsonObjectTypeTest";
31 const std::string TEST_FALSE_KEY = "FalseKey";
32 
33 } // namespace
34 
35 class JsonUtilsTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() const override;
40     void TearDown() const override;
41 };
42 
SetUpTestCase()43 void JsonUtilsTest::SetUpTestCase()
44 {
45     GTEST_LOG_(INFO) << "JsonUtilsTest SetUpTestCase";
46 }
47 
TearDownTestCase()48 void JsonUtilsTest::TearDownTestCase()
49 {
50     GTEST_LOG_(INFO) << "JsonUtilsTest TearDownTestCase";
51 }
52 
SetUp()53 void JsonUtilsTest::SetUp()
54 {
55     GTEST_LOG_(INFO) << "JsonUtilsTest SetUp";
56 }
57 
TearDown()58 void JsonUtilsTest::TearDown()
59 {
60     GTEST_LOG_(INFO) << "JsonUtilsTest TearDown";
61 }
62 
63 /**
64  * @tc.name: JsonUtilsTest001
65  * @tc.desc: Check json util function for bool type value
66  * @tc.type: FUNC
67  */
68 HWTEST_F(JsonUtilsTest, JsonUtilsTest001, TestSize.Level1)
69 {
70     /**
71      * @tc.steps: step1. construct the test string with bool value.
72      */
73     std::string testJson = "true";
74 
75     /**
76      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsBool and GetBool.
77      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
78      */
79     std::unique_ptr<JsonValue> boolValue = JsonUtil::ParseJsonString(testJson);
80     ASSERT_TRUE(boolValue);
81     EXPECT_TRUE(boolValue->IsValid());
82     EXPECT_FALSE(boolValue->IsNull());
83     EXPECT_TRUE(boolValue->IsBool());
84     EXPECT_TRUE(boolValue->GetBool());
85 }
86 
87 /**
88  * @tc.name: JsonUtilsTest002
89  * @tc.desc: Check json util function for bool type value
90  * @tc.type: FUNC
91  */
92 HWTEST_F(JsonUtilsTest, JsonUtilsTest002, TestSize.Level1)
93 {
94     /**
95      * @tc.steps: step1. construct the test string with bool value.
96      */
97     std::string testJson = "false";
98 
99     /**
100      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsBool and GetBool.
101      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
102      */
103     std::unique_ptr<JsonValue> boolValue = JsonUtil::ParseJsonString(testJson);
104     ASSERT_TRUE(boolValue);
105     EXPECT_TRUE(boolValue->IsValid());
106     EXPECT_FALSE(boolValue->IsNull());
107     EXPECT_TRUE(boolValue->IsBool());
108     EXPECT_FALSE(boolValue->GetBool());
109 }
110 
111 /**
112  * @tc.name: JsonUtilsTest003
113  * @tc.desc: Check json util function for signed integer
114  * @tc.type: FUNC
115  */
116 HWTEST_F(JsonUtilsTest, JsonUtilsTest003, TestSize.Level1)
117 {
118     /**
119      * @tc.steps: step1. construct the test string with signed integer.
120      */
121     std::string testJson = "-1";
122     int32_t intNum = -1;
123     uint32_t uintNum = 0;
124     double doubleNum = -1;
125 
126     /**
127      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsNumber, GetInt, GetUInt and GetDouble.
128      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
129      */
130     std::unique_ptr<JsonValue> intValue = JsonUtil::ParseJsonString(testJson);
131     ASSERT_TRUE(intValue);
132     EXPECT_TRUE(intValue->IsValid());
133     EXPECT_FALSE(intValue->IsNull());
134     EXPECT_TRUE(intValue->IsNumber());
135     EXPECT_TRUE(intValue->GetInt() == intNum);
136     EXPECT_TRUE(intValue->GetUInt() == uintNum);
137     EXPECT_TRUE(NearEqual(intValue->GetDouble(), doubleNum));
138 }
139 
140 /**
141  * @tc.name: JsonUtilsTest004
142  * @tc.desc: Check json util function for unsigned integer
143  * @tc.type: FUNC
144  */
145 HWTEST_F(JsonUtilsTest, JsonUtilsTest004, TestSize.Level1)
146 {
147     /**
148      * @tc.steps: step1. construct the test string with unsigned integer.
149      */
150     std::string testJson = "1";
151     int32_t intNum = 1;
152     uint32_t uintNum = 1;
153     double doubleNum = 1;
154 
155     /**
156      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsNumber, GetInt, GetUInt and GetDouble.
157      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
158      */
159     std::unique_ptr<JsonValue> uintValue = JsonUtil::ParseJsonString(testJson);
160     ASSERT_TRUE(uintValue);
161     EXPECT_TRUE(uintValue->IsValid());
162     EXPECT_FALSE(uintValue->IsNull());
163     EXPECT_TRUE(uintValue->IsNumber());
164     EXPECT_TRUE(uintValue->GetInt() == intNum);
165     EXPECT_TRUE(uintValue->GetUInt() == uintNum);
166     EXPECT_TRUE(NearEqual(uintValue->GetDouble(), doubleNum));
167 }
168 
169 /**
170  * @tc.name: JsonUtilsTest005
171  * @tc.desc: Check json util function for decimal number
172  * @tc.type: FUNC
173  */
174 HWTEST_F(JsonUtilsTest, JsonUtilsTest005, TestSize.Level1)
175 {
176     /**
177      * @tc.steps: step1. construct the test string with decimal number.
178      */
179     std::string testJson = "6.66";
180     int32_t intNum = 6;
181     uint32_t uintNum = 6;
182     double doubleNum = 6.66;
183 
184     /**
185      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsNumber, GetInt, GetUInt and GetDouble.
186      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
187      */
188     std::unique_ptr<JsonValue> doubleValue = JsonUtil::ParseJsonString(testJson);
189     ASSERT_TRUE(doubleValue);
190     EXPECT_TRUE(doubleValue->IsValid());
191     EXPECT_FALSE(doubleValue->IsNull());
192     EXPECT_TRUE(doubleValue->IsNumber());
193     EXPECT_TRUE(doubleValue->GetInt() == intNum);
194     EXPECT_TRUE(doubleValue->GetUInt() == uintNum);
195     EXPECT_TRUE(NearEqual(doubleValue->GetDouble(), doubleNum));
196 }
197 
198 /**
199  * @tc.name: JsonUtilsTest006
200  * @tc.desc: Check json util function for string
201  * @tc.type: FUNC
202  */
203 HWTEST_F(JsonUtilsTest, JsonUtilsTest006, TestSize.Level1)
204 {
205     /**
206      * @tc.steps: step1. construct the test string with string.
207      */
208     std::string testJson = "\"Ace Unittest\"";
209 
210     /**
211      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsString and GetString.
212      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
213      */
214     std::unique_ptr<JsonValue> stringValue = JsonUtil::ParseJsonString(testJson);
215     ASSERT_TRUE(stringValue);
216     EXPECT_TRUE(stringValue->IsValid());
217     EXPECT_FALSE(stringValue->IsNull());
218     EXPECT_TRUE(stringValue->IsString());
219     EXPECT_TRUE(stringValue->GetString() == TEST_STRING);
220 }
221 
222 /**
223  * @tc.name: JsonUtilsTest007
224  * @tc.desc: Check json util function for empty string
225  * @tc.type: FUNC
226  */
227 HWTEST_F(JsonUtilsTest, JsonUtilsTest007, TestSize.Level1)
228 {
229     /**
230      * @tc.steps: step1. construct the test string with empty string.
231      */
232     std::string testJson = "\"\"";
233 
234     /**
235      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsString and GetString.
236      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
237      */
238     std::unique_ptr<JsonValue> emptyStringValue = JsonUtil::ParseJsonString(testJson);
239     ASSERT_TRUE(emptyStringValue);
240     EXPECT_TRUE(emptyStringValue->IsValid());
241     EXPECT_FALSE(emptyStringValue->IsNull());
242     EXPECT_TRUE(emptyStringValue->IsString());
243     EXPECT_TRUE(emptyStringValue->GetString().empty());
244 }
245 
246 /**
247  * @tc.name: JsonUtilsTest008
248  * @tc.desc: Check json util function for JsonObject
249  * @tc.type: FUNC
250  */
251 HWTEST_F(JsonUtilsTest, JsonUtilsTest008, TestSize.Level1)
252 {
253     /**
254      * @tc.steps: step1. construct the test string with JsonObject.
255      */
256     std::string testJson = R"({"JsonObjectTypeTest": "Ace Unittest"})";
257 
258     /**
259      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsObject, Contains and GetValue.
260      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
261      */
262     std::unique_ptr<JsonValue> objectValue = JsonUtil::ParseJsonString(testJson);
263     ASSERT_TRUE(objectValue);
264     EXPECT_TRUE(objectValue->IsValid());
265     EXPECT_FALSE(objectValue->IsNull());
266     EXPECT_TRUE(objectValue->IsObject());
267     EXPECT_TRUE(objectValue->Contains(TEST_KEY));
268     EXPECT_FALSE(objectValue->Contains(TEST_FALSE_KEY));
269     EXPECT_TRUE(objectValue->GetValue(TEST_KEY)->GetString() == TEST_STRING);
270     EXPECT_TRUE(objectValue->GetValue(TEST_FALSE_KEY)->GetString().empty());
271 }
272 
273 /**
274  * @tc.name: JsonUtilsTest009
275  * @tc.desc: Check json util function for incorrect JsonObject
276  * @tc.type: FUNC
277  */
278 HWTEST_F(JsonUtilsTest, JsonUtilsTest009, TestSize.Level1)
279 {
280     /**
281      * @tc.steps: step1. construct the test string with incorrect JsonObject.
282      */
283     std::string testJson = R"({"JsonObjectTypeTest": ""})";
284 
285     /**
286      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsObject, Contains and GetValue.
287      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
288      */
289     std::unique_ptr<JsonValue> objectValue = JsonUtil::ParseJsonString(testJson);
290     ASSERT_TRUE(objectValue);
291     EXPECT_TRUE(objectValue->IsValid());
292     EXPECT_FALSE(objectValue->IsNull());
293     EXPECT_TRUE(objectValue->IsObject());
294     EXPECT_TRUE(objectValue->Contains(TEST_KEY));
295     EXPECT_TRUE(objectValue->GetValue(TEST_KEY)->GetString().empty());
296 }
297 
298 /**
299  * @tc.name: JsonUtilsTest010
300  * @tc.desc: Check json util function for array
301  * @tc.type: FUNC
302  */
303 HWTEST_F(JsonUtilsTest, JsonUtilsTest010, TestSize.Level1)
304 {
305     /**
306      * @tc.steps: step1. construct the test string with array.
307      */
308     std::string testJson = "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]";
309     int32_t testArraySize = 10;
310     int32_t testArrayIndex = 5;
311     int32_t testArrayValue = 5;
312 
313     /**
314      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsArray, GetArraySize and GetArrayItem.
315      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
316      */
317     std::unique_ptr<JsonValue> arrayValue = JsonUtil::ParseJsonString(testJson);
318     ASSERT_TRUE(arrayValue);
319     EXPECT_TRUE(arrayValue->IsValid());
320     EXPECT_FALSE(arrayValue->IsNull());
321     EXPECT_TRUE(arrayValue->IsArray());
322     EXPECT_TRUE(arrayValue->GetArraySize() == testArraySize);
323     EXPECT_TRUE(arrayValue->GetArrayItem(testArrayIndex)->GetInt() == testArrayValue);
324 }
325 
326 /**
327  * @tc.name: JsonUtilsTest011
328  * @tc.desc: Check json util function for empty array
329  * @tc.type: FUNC
330  */
331 HWTEST_F(JsonUtilsTest, JsonUtilsTest011, TestSize.Level1)
332 {
333     /**
334      * @tc.steps: step1. construct the test string with empty array.
335      */
336     std::string testJson = "[]";
337 
338     /**
339      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsArray, GetArraySize and GetArrayItem.
340      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
341      */
342     std::unique_ptr<JsonValue> arrayValue = JsonUtil::ParseJsonString(testJson);
343     ASSERT_TRUE(arrayValue);
344     EXPECT_TRUE(arrayValue->IsValid());
345     EXPECT_FALSE(arrayValue->IsNull());
346     EXPECT_TRUE(arrayValue->IsArray());
347     EXPECT_TRUE(arrayValue->GetArraySize() == 0);
348 }
349 
350 /**
351  * @tc.name: JsonUtilsTest012
352  * @tc.desc: Check json util function for empty test string
353  * @tc.type: FUNC
354  */
355 HWTEST_F(JsonUtilsTest, JsonUtilsTest012, TestSize.Level1)
356 {
357     /**
358      * @tc.steps: step1. construct the empty test string.
359      */
360     std::string testJson;
361 
362     /**
363      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull.
364      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
365      */
366     std::unique_ptr<JsonValue> emptyValue = JsonUtil::ParseJsonString(testJson);
367     ASSERT_TRUE(emptyValue);
368     EXPECT_FALSE(emptyValue->IsValid());
369     EXPECT_TRUE(emptyValue->IsNull());
370 }
371 
372 /**
373  * @tc.name: JsonUtilsTest013
374  * @tc.desc: Check json util function for illegal type value
375  * @tc.type: FUNC
376  */
377 HWTEST_F(JsonUtilsTest, JsonUtilsTest013, TestSize.Level1)
378 {
379     /**
380      * @tc.steps: step1. construct the test string with illegal type value.
381      */
382     std::string testJson = "{Ace Unittest}";
383 
384     /**
385      * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull.
386      * @tc.expected: step2. get the JsonValue successfully and the results are correct.
387      */
388     std::unique_ptr<JsonValue> illegalValue = JsonUtil::ParseJsonString(testJson);
389     ASSERT_TRUE(illegalValue);
390     EXPECT_FALSE(illegalValue->IsValid());
391     EXPECT_TRUE(illegalValue->IsNull());
392 }
393 
394 } // namespace OHOS::Ace