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 #define LOG_TAG "SerializableTest"
16 #include "serializable.h"
17 
18 #include <type_traits>
19 
20 #include "gtest/gtest.h"
21 #include "logger.h"
22 
23 using namespace testing::ext;
24 namespace OHOS::Test {
25 class SerializableTest : public testing::Test {
26 public:
27     struct Normal final : public Serializable {
28     public:
29         std::string name = "Test";
30         int32_t count = 0;
31         uint32_t status = 1;
32         int64_t value = 2;
33         bool isClear = false;
34         std::vector<std::string> cols{ "123", "345", "789" };
35         std::vector<std::vector<int32_t>> colRow{ { 123, 345, 789 }, { 123, 345, 789 } };
36 
MarshalOHOS::Test::SerializableTest::Normal37         bool Marshal(json &node) const override
38         {
39             SetValue(node[GET_NAME(name)], name);
40             SetValue(node[GET_NAME(count)], count);
41             SetValue(node[GET_NAME(status)], status);
42             SetValue(node[GET_NAME(value)], value);
43             SetValue(node[GET_NAME(isClear)], isClear);
44             SetValue(node[GET_NAME(cols)], cols);
45             SetValue(node[GET_NAME(colRow)], colRow);
46             return true;
47         }
UnmarshalOHOS::Test::SerializableTest::Normal48         bool Unmarshal(const json &node) override
49         {
50             GetValue(node, GET_NAME(name), name);
51             GetValue(node, GET_NAME(count), count);
52             GetValue(node, GET_NAME(status), status);
53             GetValue(node, GET_NAME(value), value);
54             GetValue(node, GET_NAME(isClear), isClear);
55             GetValue(node, GET_NAME(cols), cols);
56             GetValue(node, GET_NAME(colRow), colRow);
57             return true;
58         }
operator ==OHOS::Test::SerializableTest::Normal59         bool operator==(const Normal &ref) const
60         {
61             return name == ref.name && count == ref.count && status == ref.status && value == ref.value &&
62                    isClear == ref.isClear && cols == ref.cols;
63         }
64     };
65 
66     struct NormalEx final : public Serializable {
67     public:
68         std::vector<Normal> normals{ Normal(), Normal() };
69         Normal normal;
70         int32_t count = 123;
71         std::string name = "wdt";
MarshalOHOS::Test::SerializableTest::NormalEx72         bool Marshal(json &node) const override
73         {
74             SetValue(node[GET_NAME(normals)], normals);
75             SetValue(node[GET_NAME(normal)], normal);
76             SetValue(node[GET_NAME(count)], count);
77             SetValue(node[GET_NAME(name)], name);
78             return true;
79         }
UnmarshalOHOS::Test::SerializableTest::NormalEx80         bool Unmarshal(const json &node) override
81         {
82             GetValue(node, GET_NAME(normals), normals);
83             GetValue(node, GET_NAME(normal), normal);
84             GetValue(node, GET_NAME(count), count);
85             GetValue(node, GET_NAME(name), name);
86             return true;
87         }
operator ==OHOS::Test::SerializableTest::NormalEx88         bool operator==(const NormalEx &normalEx) const
89         {
90             return normals == normalEx.normals && count == normalEx.count && name == normalEx.name;
91         }
92     };
SetUpTestCase(void)93     static void SetUpTestCase(void)
94     {
95     }
TearDownTestCase(void)96     static void TearDownTestCase(void)
97     {
98     }
SetUp()99     void SetUp()
100     {
101         Test::SetUp();
102     }
TearDown()103     void TearDown()
104     {
105         Test::TearDown();
106     }
107 
108     template<typename T>
EqualPtr(const T * src,const T * target)109     static inline bool EqualPtr(const T *src, const T *target)
110     {
111         return (((src) == (target)) || ((src) != nullptr && (target) != nullptr && *(src) == *(target)));
112     }
113 };
114 
115 /**
116 * @tc.name: SerializableSuiteGetVal
117 * @tc.desc: Get Value.
118 * @tc.type: FUNC
119 * @tc.require:
120 */
121 HWTEST_F(SerializableTest, GetNormalVal, TestSize.Level2)
122 {
123     Normal normal;
124     normal.name = "normal";
125     normal.count = -1;
126     normal.status = 12;
127     normal.value = -56;
128     normal.isClear = true;
129     normal.cols = { "adfasdfas" };
130     auto jstr = to_string(normal.Marshall());
131     Normal normal1;
132     normal1.Unmarshall(jstr);
133     ASSERT_TRUE(normal == normal1) << normal1.name;
134 }
135 
136 /**
137 * @tc.name: Delete Serializable
138 * @tc.desc: can delete child class, but not delete parent class point.
139 * @tc.type: FUNC
140 * @tc.require:
141 */
142 HWTEST_F(SerializableTest, DeleteSerializable, TestSize.Level2)
143 {
144     ASSERT_FALSE(std::is_destructible<Serializable>::value);
145     ASSERT_TRUE(std::is_destructible<NormalEx>::value);
146 }
147 
148 /**
149 * @tc.name: SerializableSuiteGetMutilVal
150 * @tc.desc: mutil value case.
151 * @tc.type: FUNC
152 * @tc.require:
153 */
154 HWTEST_F(SerializableTest, GetMutilVal, TestSize.Level2)
155 {
156     NormalEx normalEx;
157     normalEx.normals = { Normal() };
158     normalEx.name = "normalEx";
159     auto jstr = to_string(normalEx.Marshall());
160     NormalEx normal1;
161     normal1.Unmarshall(jstr);
162     ASSERT_TRUE(normalEx == normal1) << normal1.name;
163 }
164 
165 /**
166 * @tc.name: IsJson
167 * @tc.desc: is json.
168 * @tc.type: FUNC
169 */
170 HWTEST_F(SerializableTest, IsJson, TestSize.Level1)
171 {
172     std::string str = "test";
173     std::string jsonStr = "\"test\"";
174     ASSERT_FALSE(Serializable::IsJson(str));
175     ASSERT_TRUE(Serializable::IsJson(jsonStr));
176 }
177 
178 /**
179 * @tc.name: ToJson_01
180 * @tc.desc: to json.
181 * @tc.type: FUNC
182 */
183 HWTEST_F(SerializableTest, ToJson_01, TestSize.Level1)
184 {
185     std::string jsonStr = "{\"key\":\"value\"}";
186     Serializable::json result = Serializable::ToJson(jsonStr);
187     ASSERT_FALSE(result.is_discarded());
188 }
189 
190 /**
191 * @tc.name: ToJson_02
192 * @tc.desc: to json.
193 * @tc.type: FUNC
194 */
195 HWTEST_F(SerializableTest, ToJson_02, TestSize.Level1)
196 {
197     std::string jsonStr = "invalid_json";
198     Serializable::json result = Serializable::ToJson(jsonStr);
199     ASSERT_FALSE(result.is_discarded());
200 }
201 
202 /**
203 * @tc.name: ToJson_03
204 * @tc.desc: to json.
205 * @tc.type: FUNC
206 */
207 HWTEST_F(SerializableTest, ToJson_03, TestSize.Level1)
208 {
209     std::string jsonStr = "";
210     Serializable::json result = Serializable::ToJson(jsonStr);
211     ASSERT_TRUE(result.empty());
212 }
213 
214 /**
215 * @tc.name: ToJson_04
216 * @tc.desc: to json.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(SerializableTest, ToJson_04, TestSize.Level1)
220 {
221     std::string jsonStr = "{invalid_json}";
222     Serializable::json result = Serializable::ToJson(jsonStr);
223     ASSERT_FALSE(result.is_discarded());
224 }
225 } // namespace OHOS::Test