1 /*
2 * Copyright (c) 2024 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 "gtest/gtest.h"
17
18 #include "base/json/json_util.h"
19 #include "base/json/node_object.h"
20 #include "base/json/uobject.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS::Ace {
26 namespace {} // namespace
27
28 class NodeObjectTest : public testing::Test {};
29
30 /**
31 * @tc.name: NodeObjectTest001
32 * @tc.desc: AddItemToObject()
33 * @tc.type: FUNC
34 */
HWTEST_F(NodeObjectTest, NodeObjectTest001, TestSize.Level1)35 HWTEST_F(NodeObjectTest, NodeObjectTest001, TestSize.Level1)
36 {
37 /**
38 * @tc.steps: step1. Create a NodeObject.
39 */
40 auto nodeObject = NodeObject::Create();
41
42 /**
43 * @tc.steps: step2. Save value through AdditemToObject.
44 * @tc.expected: step2. Asserting values through the get method.
45 */
46 std::string stringValue = "test";
47 double doubleValue = 1.0;
48 size_t sizeValue = 10;
49 int32_t int32Value = 5;
50 int64_t int64Value = 1;
51 bool boolValue = false;
52
53 nodeObject->Put(nullptr, const_cast<char*>(stringValue.c_str()));
54 nodeObject->Put(nullptr, nullptr);
55 nodeObject->Put(nullptr, doubleValue);
56 nodeObject->Put(nullptr, sizeValue);
57 nodeObject->Put(nullptr, int32Value);
58 nodeObject->Put(nullptr, int64Value);
59 nodeObject->Put(nullptr, boolValue);
60
61 std::string str = "string";
62 nodeObject->Put(const_cast<char*>(str.c_str()), const_cast<char*>(stringValue.c_str()));
63 EXPECT_EQ(nodeObject->GetString("string", ""), "test");
64 EXPECT_EQ(nodeObject->GetString("", "test"), "test");
65 str = "double";
66 nodeObject->Put(const_cast<char*>(str.c_str()), doubleValue);
67 EXPECT_EQ(nodeObject->GetDouble("double", 0), 1.0);
68 EXPECT_EQ(nodeObject->GetDouble("", 0), 0);
69 str = "int32_t";
70 nodeObject->Put(const_cast<char*>(str.c_str()), int32Value);
71 EXPECT_EQ(nodeObject->GetInt("int32_t", 0), 5);
72 EXPECT_EQ(nodeObject->GetInt("", 0), 0);
73 str = "int64_t";
74 nodeObject->Put(const_cast<char*>(str.c_str()), int64Value);
75 EXPECT_EQ(nodeObject->GetInt64("int64_t", 0), 1);
76 EXPECT_EQ(nodeObject->GetInt64("", 0), 0);
77 str = "bool";
78 nodeObject->Put(const_cast<char*>(str.c_str()), boolValue);
79 EXPECT_EQ(nodeObject->GetBool("bool", true), false);
80 EXPECT_EQ(nodeObject->GetBool("", true), true);
81 }
82
83 /**
84 * @tc.name: NodeObjectTest002
85 * @tc.desc: Put(const char* key, size_t value)/GetUInt(const std::string& key, uint32_t defaultVal)
86 * @tc.type: FUNC
87 */
HWTEST_F(NodeObjectTest, NodeObjectTest002, TestSize.Level1)88 HWTEST_F(NodeObjectTest, NodeObjectTest002, TestSize.Level1)
89 {
90 /**
91 * @tc.steps: step1. Create a NodeObject.
92 */
93 auto nodeObject = NodeObject::Create();
94
95 /**
96 * @tc.steps: step2. Put(const char* key, size_t value)
97 * @tc.expected: step2. Asserting put success.
98 */
99 const char* key = "key-string";
100 size_t value = 5;
101 bool ret = nodeObject->Put(key, value);
102 EXPECT_EQ(ret, true);
103
104 /**
105 * @tc.steps: step3. GetUInt(const std::string& key, uint32_t defaultVal)
106 * @tc.expected: step3. Asserting success.
107 */
108 const std::string key2 = "aaaa";
109 uint32_t defaultVal = 0;
110 // key not exist
111 uint32_t ret2 = nodeObject->GetUInt(key2, defaultVal);
112 EXPECT_EQ(ret2, defaultVal);
113 // key exist
114 const char* key3 = "key-temp";
115 nodeObject->Put(key3, 11);
116 uint32_t ret3 = nodeObject->GetUInt(key3, defaultVal);
117 EXPECT_EQ(ret3, 11);
118 }
119
120 /**
121 * @tc.name: NodeObjectTest003
122 * @tc.desc: Put(const char* key, const std::unique_ptr<NodeObject>& value)
123 * @tc.type: FUNC
124 */
HWTEST_F(NodeObjectTest, NodeObjectTest003, TestSize.Level1)125 HWTEST_F(NodeObjectTest, NodeObjectTest003, TestSize.Level1)
126 {
127 /**
128 * @tc.steps: step1. Create a NodeObject.
129 */
130 auto nodeObject = NodeObject::Create();
131
132 // value is nullptr
133 const std::unique_ptr<NodeObject> value = nullptr;
134 const char* key = nullptr;
135 bool ret = nodeObject->Put(key, value);
136 EXPECT_EQ(ret, false);
137 // value is not nullptr,key is nullptr
138 auto value2 = NodeObject::Create();
139 bool ret2 = nodeObject->Put(key, value2);
140 EXPECT_EQ(ret2, false);
141 const std::unique_ptr<JsonValue> value3 = std::make_unique<JsonValue>();
142 bool ret3 = nodeObject->Put(key, value3);
143 EXPECT_EQ(ret3, false);
144 }
145 } // namespace OHOS::Ace