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