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 const std::string TEST_STRING = "Ace Unittest"; 29 const std::string TEST_KEY = "JsonObjectTypeTest"; 30 const std::string TEST_FALSE_KEY = "FalseKey"; 31 } // namespace 32 33 class JsonUtilTest : public testing::Test {}; 34 35 /** 36 * @tc.name: JsonUtilTest001 37 * @tc.desc: Check json util function for bool type value 38 * @tc.type: FUNC 39 */ 40 HWTEST_F(JsonUtilTest, JsonUtilTest001, TestSize.Level1) 41 { 42 /** 43 * @tc.steps: step1. construct the test string with bool value. 44 */ 45 std::string testJson = "true"; 46 47 /** 48 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsBool and GetBool. 49 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 50 */ 51 std::unique_ptr<JsonValue> boolValue = JsonUtil::ParseJsonString(testJson); 52 ASSERT_TRUE(boolValue); 53 EXPECT_TRUE(boolValue->IsValid()); 54 EXPECT_FALSE(boolValue->IsNull()); 55 EXPECT_TRUE(boolValue->IsBool()); 56 EXPECT_TRUE(boolValue->GetBool()); 57 } 58 59 /** 60 * @tc.name: JsonUtilTest002 61 * @tc.desc: Check json util function for bool type value 62 * @tc.type: FUNC 63 */ 64 HWTEST_F(JsonUtilTest, JsonUtilTest002, TestSize.Level1) 65 { 66 /** 67 * @tc.steps: step1. construct the test string with bool value. 68 */ 69 std::string testJson = "false"; 70 71 /** 72 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsBool and GetBool. 73 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 74 */ 75 std::unique_ptr<JsonValue> boolValue = JsonUtil::ParseJsonString(testJson); 76 ASSERT_TRUE(boolValue); 77 EXPECT_TRUE(boolValue->IsValid()); 78 EXPECT_FALSE(boolValue->IsNull()); 79 EXPECT_TRUE(boolValue->IsBool()); 80 EXPECT_FALSE(boolValue->GetBool()); 81 } 82 83 /** 84 * @tc.name: JsonUtilTest003 85 * @tc.desc: Check json util function for signed integer 86 * @tc.type: FUNC 87 */ 88 HWTEST_F(JsonUtilTest, JsonUtilTest003, TestSize.Level1) 89 { 90 /** 91 * @tc.steps: step1. construct the test string with signed integer. 92 */ 93 std::string testJson = "-1"; 94 int32_t intNum = -1; 95 uint32_t uintNum = 0; 96 double doubleNum = -1; 97 98 /** 99 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsNumber, GetInt, GetUInt and GetDouble. 100 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 101 */ 102 std::unique_ptr<JsonValue> intValue = JsonUtil::ParseJsonString(testJson); 103 ASSERT_TRUE(intValue); 104 EXPECT_TRUE(intValue->IsValid()); 105 EXPECT_FALSE(intValue->IsNull()); 106 EXPECT_TRUE(intValue->IsNumber()); 107 EXPECT_TRUE(intValue->GetInt() == intNum); 108 EXPECT_TRUE(intValue->GetUInt() == uintNum); 109 EXPECT_TRUE(NearEqual(intValue->GetDouble(), doubleNum)); 110 } 111 112 /** 113 * @tc.name: JsonUtilTest004 114 * @tc.desc: Check json util function for unsigned integer 115 * @tc.type: FUNC 116 */ 117 HWTEST_F(JsonUtilTest, JsonUtilTest004, TestSize.Level1) 118 { 119 /** 120 * @tc.steps: step1. construct the test string with unsigned integer. 121 */ 122 std::string testJson = "1"; 123 int32_t intNum = 1; 124 uint32_t uintNum = 1; 125 double doubleNum = 1; 126 127 /** 128 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsNumber, GetInt, GetUInt and GetDouble. 129 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 130 */ 131 std::unique_ptr<JsonValue> uintValue = JsonUtil::ParseJsonString(testJson); 132 ASSERT_TRUE(uintValue); 133 EXPECT_TRUE(uintValue->IsValid()); 134 EXPECT_FALSE(uintValue->IsNull()); 135 EXPECT_TRUE(uintValue->IsNumber()); 136 EXPECT_TRUE(uintValue->GetInt() == intNum); 137 EXPECT_TRUE(uintValue->GetUInt() == uintNum); 138 EXPECT_TRUE(NearEqual(uintValue->GetDouble(), doubleNum)); 139 } 140 141 /** 142 * @tc.name: JsonUtilTest005 143 * @tc.desc: Check json util function for decimal number 144 * @tc.type: FUNC 145 */ 146 HWTEST_F(JsonUtilTest, JsonUtilTest005, TestSize.Level1) 147 { 148 /** 149 * @tc.steps: step1. construct the test string with decimal number. 150 */ 151 std::string testJson = "6.66"; 152 int32_t intNum = 6; 153 uint32_t uintNum = 6; 154 double doubleNum = 6.66; 155 156 /** 157 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsNumber, GetInt, GetUInt and GetDouble. 158 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 159 */ 160 std::unique_ptr<JsonValue> doubleValue = JsonUtil::ParseJsonString(testJson); 161 ASSERT_TRUE(doubleValue); 162 EXPECT_TRUE(doubleValue->IsValid()); 163 EXPECT_FALSE(doubleValue->IsNull()); 164 EXPECT_TRUE(doubleValue->IsNumber()); 165 EXPECT_TRUE(doubleValue->GetInt() == intNum); 166 EXPECT_TRUE(doubleValue->GetUInt() == uintNum); 167 EXPECT_TRUE(NearEqual(doubleValue->GetDouble(), doubleNum)); 168 } 169 170 /** 171 * @tc.name: JsonUtilTest006 172 * @tc.desc: Check json util function for string 173 * @tc.type: FUNC 174 */ 175 HWTEST_F(JsonUtilTest, JsonUtilTest006, TestSize.Level1) 176 { 177 /** 178 * @tc.steps: step1. construct the test string with string. 179 */ 180 std::string testJson = "\"Ace Unittest\""; 181 182 /** 183 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsString and GetString. 184 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 185 */ 186 std::unique_ptr<JsonValue> stringValue = JsonUtil::ParseJsonString(testJson); 187 ASSERT_TRUE(stringValue); 188 EXPECT_TRUE(stringValue->IsValid()); 189 EXPECT_FALSE(stringValue->IsNull()); 190 EXPECT_TRUE(stringValue->IsString()); 191 EXPECT_TRUE(stringValue->GetString() == TEST_STRING); 192 } 193 194 /** 195 * @tc.name: JsonUtilTest007 196 * @tc.desc: Check json util function for empty string 197 * @tc.type: FUNC 198 */ 199 HWTEST_F(JsonUtilTest, JsonUtilTest007, TestSize.Level1) 200 { 201 /** 202 * @tc.steps: step1. construct the test string with empty string. 203 */ 204 std::string testJson = "\"\""; 205 206 /** 207 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsString and GetString. 208 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 209 */ 210 std::unique_ptr<JsonValue> emptyStringValue = JsonUtil::ParseJsonString(testJson); 211 ASSERT_TRUE(emptyStringValue); 212 EXPECT_TRUE(emptyStringValue->IsValid()); 213 EXPECT_FALSE(emptyStringValue->IsNull()); 214 EXPECT_TRUE(emptyStringValue->IsString()); 215 EXPECT_TRUE(emptyStringValue->GetString().empty()); 216 } 217 218 /** 219 * @tc.name: JsonUtilTest008 220 * @tc.desc: Check json util function for JsonObject 221 * @tc.type: FUNC 222 */ 223 HWTEST_F(JsonUtilTest, JsonUtilTest008, TestSize.Level1) 224 { 225 /** 226 * @tc.steps: step1. construct the test string with JsonObject. 227 */ 228 std::string testJson = R"({"JsonObjectTypeTest": "Ace Unittest"})"; 229 230 /** 231 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsObject, Contains and GetValue. 232 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 233 */ 234 std::unique_ptr<JsonValue> objectValue = JsonUtil::ParseJsonString(testJson); 235 ASSERT_TRUE(objectValue); 236 EXPECT_TRUE(objectValue->IsValid()); 237 EXPECT_FALSE(objectValue->IsNull()); 238 EXPECT_TRUE(objectValue->IsObject()); 239 EXPECT_TRUE(objectValue->Contains(TEST_KEY)); 240 EXPECT_FALSE(objectValue->Contains(TEST_FALSE_KEY)); 241 EXPECT_TRUE(objectValue->GetValue(TEST_KEY)->GetString() == TEST_STRING); 242 EXPECT_TRUE(objectValue->GetValue(TEST_FALSE_KEY)->GetString().empty()); 243 } 244 245 /** 246 * @tc.name: JsonUtilTest009 247 * @tc.desc: Check json util function for incorrect JsonObject 248 * @tc.type: FUNC 249 */ 250 HWTEST_F(JsonUtilTest, JsonUtilTest009, TestSize.Level1) 251 { 252 /** 253 * @tc.steps: step1. construct the test string with incorrect JsonObject. 254 */ 255 std::string testJson = R"({"JsonObjectTypeTest": ""})"; 256 257 /** 258 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsObject, Contains and GetValue. 259 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 260 */ 261 std::unique_ptr<JsonValue> objectValue = JsonUtil::ParseJsonString(testJson); 262 ASSERT_TRUE(objectValue); 263 EXPECT_TRUE(objectValue->IsValid()); 264 EXPECT_FALSE(objectValue->IsNull()); 265 EXPECT_TRUE(objectValue->IsObject()); 266 EXPECT_TRUE(objectValue->Contains(TEST_KEY)); 267 EXPECT_TRUE(objectValue->GetValue(TEST_KEY)->GetString().empty()); 268 } 269 270 /** 271 * @tc.name: JsonUtilTest010 272 * @tc.desc: Check json util function for array 273 * @tc.type: FUNC 274 */ 275 HWTEST_F(JsonUtilTest, JsonUtilTest010, TestSize.Level1) 276 { 277 /** 278 * @tc.steps: step1. construct the test string with array. 279 */ 280 std::string testJson = "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"; 281 int32_t testArraySize = 10; 282 int32_t testArrayIndex = 5; 283 int32_t testArrayValue = 5; 284 285 /** 286 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsArray, GetArraySize and GetArrayItem. 287 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 288 */ 289 std::unique_ptr<JsonValue> arrayValue = JsonUtil::ParseJsonString(testJson); 290 ASSERT_TRUE(arrayValue); 291 EXPECT_TRUE(arrayValue->IsValid()); 292 EXPECT_FALSE(arrayValue->IsNull()); 293 EXPECT_TRUE(arrayValue->IsArray()); 294 EXPECT_TRUE(arrayValue->GetArraySize() == testArraySize); 295 EXPECT_TRUE(arrayValue->GetArrayItem(testArrayIndex)->GetInt() == testArrayValue); 296 } 297 298 /** 299 * @tc.name: JsonUtilTest011 300 * @tc.desc: Check json util function for empty array 301 * @tc.type: FUNC 302 */ 303 HWTEST_F(JsonUtilTest, JsonUtilTest011, TestSize.Level1) 304 { 305 /** 306 * @tc.steps: step1. construct the test string with empty array. 307 */ 308 std::string testJson = "[]"; 309 310 /** 311 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull, IsArray, GetArraySize and GetArrayItem. 312 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 313 */ 314 std::unique_ptr<JsonValue> arrayValue = JsonUtil::ParseJsonString(testJson); 315 ASSERT_TRUE(arrayValue); 316 EXPECT_TRUE(arrayValue->IsValid()); 317 EXPECT_FALSE(arrayValue->IsNull()); 318 EXPECT_TRUE(arrayValue->IsArray()); 319 EXPECT_TRUE(arrayValue->GetArraySize() == 0); 320 } 321 322 /** 323 * @tc.name: JsonUtilTest012 324 * @tc.desc: Check json util function for empty test string 325 * @tc.type: FUNC 326 */ 327 HWTEST_F(JsonUtilTest, JsonUtilTest012, TestSize.Level1) 328 { 329 /** 330 * @tc.steps: step1. construct the empty test string. 331 */ 332 std::string testJson; 333 334 /** 335 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull. 336 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 337 */ 338 std::unique_ptr<JsonValue> emptyValue = JsonUtil::ParseJsonString(testJson); 339 ASSERT_TRUE(emptyValue); 340 EXPECT_FALSE(emptyValue->IsValid()); 341 EXPECT_TRUE(emptyValue->IsNull()); 342 } 343 344 /** 345 * @tc.name: JsonUtilTest013 346 * @tc.desc: Check json util function for illegal type value 347 * @tc.type: FUNC 348 */ 349 HWTEST_F(JsonUtilTest, JsonUtilTest013, TestSize.Level1) 350 { 351 /** 352 * @tc.steps: step1. construct the test string with illegal type value. 353 */ 354 std::string testJson = "{Ace Unittest}"; 355 356 /** 357 * @tc.steps: step2. get JsonValue and check results of IsValid, IsNull. 358 * @tc.expected: step2. get the JsonValue successfully and the results are correct. 359 */ 360 std::unique_ptr<JsonValue> illegalValue = JsonUtil::ParseJsonString(testJson); 361 ASSERT_TRUE(illegalValue); 362 EXPECT_FALSE(illegalValue->IsValid()); 363 EXPECT_TRUE(illegalValue->IsNull()); 364 } 365 } // namespace OHOS::Ace