1 /*
2  * Copyright (c) 2021 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 <type_traits>
17 #include "log_print.h"
18 #include "serializable/serializable.h"
19 #include "gtest/gtest.h"
20 
21 using namespace testing::ext;
22 using namespace OHOS::DistributedData;
23 namespace OHOS::Test {
24 class SerializableTest : public testing::Test {
25 public:
26     struct Normal final : public Serializable {
27     public:
28         std::string name = "Test";
29         int32_t count = 0;
30         uint32_t status = 1;
31         int64_t value = 2;
32         bool isClear = false;
33         std::vector<std::string> cols{ "123", "345", "789" };
34         std::vector<std::vector<int32_t>> colRow{ { 123, 345, 789 }, { 123, 345, 789 } };
35 
MarshalOHOS::Test::SerializableTest::Normal36         bool Marshal(json &node) const override
37         {
38             SetValue(node[GET_NAME(name)], name);
39             SetValue(node[GET_NAME(count)], count);
40             SetValue(node[GET_NAME(status)], status);
41             SetValue(node[GET_NAME(value)], value);
42             SetValue(node[GET_NAME(isClear)], isClear);
43             SetValue(node[GET_NAME(cols)], cols);
44             SetValue(node[GET_NAME(colRow)], colRow);
45             return true;
46         }
UnmarshalOHOS::Test::SerializableTest::Normal47         bool Unmarshal(const json &node) override
48         {
49             GetValue(node, GET_NAME(name), name);
50             GetValue(node, GET_NAME(count), count);
51             GetValue(node, GET_NAME(status), status);
52             GetValue(node, GET_NAME(value), value);
53             GetValue(node, GET_NAME(isClear), isClear);
54             GetValue(node, GET_NAME(cols), cols);
55             GetValue(node, GET_NAME(colRow), colRow);
56             return true;
57         }
operator ==OHOS::Test::SerializableTest::Normal58         bool operator == (const Normal &ref) const
59         {
60             return name == ref.name && count == ref.count && status == ref.status && value == ref.value
61                 && isClear == ref.isClear && cols == ref.cols;
62         }
63     };
64 
65     struct NormalEx final : public Serializable {
66     public:
67         std::vector<Normal> normals {Normal(), Normal()};
68         Normal normal;
69         int32_t count = 123;
70         std::string name = "wdt";
MarshalOHOS::Test::SerializableTest::NormalEx71         bool Marshal(json &node) const override
72         {
73             SetValue(node[GET_NAME(normals)], normals);
74             SetValue(node[GET_NAME(normal)], normal);
75             SetValue(node[GET_NAME(count)], count);
76             SetValue(node[GET_NAME(name)], name);
77             return true;
78         }
UnmarshalOHOS::Test::SerializableTest::NormalEx79         bool Unmarshal(const json &node) override
80         {
81             GetValue(node, GET_NAME(normals), normals);
82             GetValue(node, GET_NAME(normal), normal);
83             GetValue(node, GET_NAME(count), count);
84             GetValue(node, GET_NAME(name), name);
85             return true;
86         }
operator ==OHOS::Test::SerializableTest::NormalEx87         bool operator==(const NormalEx &normalEx) const
88         {
89             return normals == normalEx.normals && count == normalEx.count && name == normalEx.name;
90         }
91     };
SetUpTestCase(void)92     static void SetUpTestCase(void)
93     {
94     }
TearDownTestCase(void)95     static void TearDownTestCase(void)
96     {
97     }
SetUp()98     void SetUp()
99     {
100         Test::SetUp();
101     }
TearDown()102     void TearDown()
103     {
104         Test::TearDown();
105     }
106 
107     template<typename T>
EqualPtr(const T * src,const T * target)108     static inline bool EqualPtr(const T *src, const T *target)
109     {
110         return (((src) == (target)) || ((src) != nullptr && (target) != nullptr && *(src) == *(target)));
111     }
112 };
113 
114 /**
115 * @tc.name: SerializableSuiteGetVal
116 * @tc.desc: Get Value.
117 * @tc.type: FUNC
118 * @tc.require:
119 * @tc.author: Sven Wang
120 */
121 HWTEST_F(SerializableTest, GetNormalVal, TestSize.Level2)
122 {
123     ZLOGI("SerializableSuite GetNormalVal begin.");
124     Normal normal;
125     normal.name = "normal";
126     normal.count = -1;
127     normal.status = 12;
128     normal.value = -56;
129     normal.isClear = true;
130     normal.cols = {"adfasdfas"};
131     auto jstr = to_string(normal.Marshall());
132     Normal normal1;
133     normal1.Unmarshall(jstr);
134     ASSERT_TRUE(normal == normal1) << normal1.name;
135 }
136 
137 /**
138 * @tc.name: Delete Serializable
139 * @tc.desc: can delete child class, but not delete parent class point.
140 * @tc.type: FUNC
141 * @tc.require:
142 * @tc.author: Sven Wang
143 */
144 HWTEST_F(SerializableTest, DeleteSerializable, TestSize.Level2)
145 {
146     ZLOGI("SerializableSuite DeleteSerializable begin.");
147     ASSERT_FALSE(std::is_destructible<Serializable>::value);
148     ASSERT_TRUE(std::is_destructible<NormalEx>::value);
149 }
150 
151 /**
152 * @tc.name: SerializableSuiteGetMutilVal
153 * @tc.desc: mutil value case.
154 * @tc.type: FUNC
155 * @tc.require:
156 * @tc.author: Sven Wang
157 */
158 HWTEST_F(SerializableTest, GetMutilVal, TestSize.Level2)
159 {
160     ZLOGI("SerializableSuite GetMutilVal begin.");
161     NormalEx normalEx;
162     normalEx.normals = {Normal()};
163     normalEx.name = "normalEx";
164     auto jstr = to_string(normalEx.Marshall());
165     NormalEx normal1;
166     normal1.Unmarshall(jstr);
167     ASSERT_TRUE(normalEx == normal1) << normal1.name;
168 }
169 
170 /**
171 * @tc.name: GetMap
172 * @tc.desc: mutil value case.
173 * @tc.type: FUNC
174 * @tc.require:
175 * @tc.author: Sven Wang
176 */
177 HWTEST_F(SerializableTest, GetMap, TestSize.Level2)
178 {
179     ZLOGI("SerializableSuite GetMapVals begin.");
180     std::map<std::string, NormalEx> marshData;
181     NormalEx normalEx;
182     normalEx.normals = { Normal() };
183     normalEx.name = "normalEx";
184     marshData.insert(std::pair{ "test1", normalEx });
185     auto jsonData = NormalEx::Marshall(marshData);
186 
187     std::map<std::string, NormalEx> unmarshData;
188     NormalEx::Unmarshall(jsonData, unmarshData);
189     ASSERT_TRUE((marshData["test1"] == unmarshData["test1"])) << jsonData;
190 }
191 
192 /**
193 * @tc.name: GetMapInStruct
194 * @tc.desc: mutil value case.
195 * @tc.type: FUNC
196 * @tc.require:
197 * @tc.author: Sven Wang
198 */
199 HWTEST_F(SerializableTest, GetMapInStruct, TestSize.Level2)
200 {
201     struct TestMeta : public Serializable {
202         std::map<std::string, NormalEx> data;
203         std::map<std::string, bool> *index = nullptr;
204         std::vector<std::map<std::string, NormalEx>> others;
~TestMetaOHOS::Test::TestMeta205         ~TestMeta()
206         {
207             delete index;
208             index = nullptr;
209         }
MarshalOHOS::Test::TestMeta210         bool Marshal(json &node) const
211         {
212             SetValue(node[GET_NAME(data)], data);
213             SetValue(node[GET_NAME(index)], index);
214             SetValue(node[GET_NAME(others)], others);
215             return true;
216         }
217 
UnmarshalOHOS::Test::TestMeta218         bool Unmarshal(const json &node)
219         {
220             GetValue(node, GET_NAME(data), data);
221             GetValue(node, GET_NAME(index), index);
222             GetValue(node, GET_NAME(others), others);
223             return true;
224         }
225     };
226     ZLOGI("SerializableSuite GetMapVals begin.");
227     TestMeta marData;
228     NormalEx normalEx;
229     normalEx.normals = { Normal() };
230     normalEx.name = "normalEx";
231     marData.data.insert(std::pair{ "test1", normalEx });
232     marData.others.push_back({ std::pair{ "test2", normalEx } });
233     marData.index = new (std::nothrow) std::map<std::string, bool>;
234     ASSERT_NE(marData.index, nullptr);
235     marData.index->insert(std::pair{ "test1", true });
236     marData.index->insert(std::pair{ "test2", true });
237     auto jsonData = NormalEx::Marshall(marData);
238     TestMeta unmarData;
239     NormalEx::Unmarshall(jsonData, unmarData);
240     ASSERT_TRUE((marData.data == unmarData.data)) << jsonData;
241     ASSERT_TRUE((marData.others == unmarData.others)) << jsonData;
242     ASSERT_NE(unmarData.index, nullptr);
243     ASSERT_TRUE((*marData.index == *unmarData.index)) << jsonData;
244 }
245 
246 /**
247 * @tc.name: GetTestValue
248 * @tc.desc: set value with point param.
249 * @tc.type: FUNC
250 * @tc.require:
251 * @tc.author: Sven Wang
252 */
253 HWTEST_F(SerializableTest, SetPointerValue, TestSize.Level2)
254 {
255     struct Test final : public Serializable {
256     public:
257         int32_t *count = nullptr;
258         int64_t *value = nullptr;
259         uint32_t *status = nullptr;
260         bool *isClear = nullptr;
~TestOHOS::Test::Test261         ~Test()
262         {
263             delete count;
264             count = nullptr;
265             delete value;
266             value = nullptr;
267             delete status;
268             status = nullptr;
269             delete isClear;
270             isClear = nullptr;
271         }
MarshalOHOS::Test::Test272         bool Marshal(json &node) const override
273         {
274             SetValue(node[GET_NAME(count)], count);
275             SetValue(node[GET_NAME(status)], status);
276             SetValue(node[GET_NAME(value)], value);
277             SetValue(node[GET_NAME(isClear)], isClear);
278             return true;
279         }
UnmarshalOHOS::Test::Test280         bool Unmarshal(const json &node) override
281         {
282             GetValue(node, GET_NAME(count), count);
283             GetValue(node, GET_NAME(status), status);
284             GetValue(node, GET_NAME(value), value);
285             GetValue(node, GET_NAME(isClear), isClear);
286             return true;
287         }
operator ==OHOS::Test::Test288         bool operator==(const Test &test) const
289         {
290             return (EqualPtr(count, test.count)) && (EqualPtr(status, test.status)) &&
291                 (EqualPtr(value, test.value)) && (EqualPtr(isClear, test.isClear));
292         }
293     };
294     Test in;
295     in.count = new int32_t(100);
296     in.value = new int64_t(-100);
297     in.status = new uint32_t(110);
298     in.isClear = new bool(true);
299     auto json = to_string(in.Marshall());
300     Test out;
301     out.Unmarshall(json);
302     ASSERT_TRUE(in == out) << in.count;
303 }
304 
305 /**
306 * @tc.name: IsJson
307 * @tc.desc: is json.
308 * @tc.type: FUNC
309 */
310 HWTEST_F(SerializableTest, IsJson, TestSize.Level1)
311 {
312     std::string str = "test";
313     std::string jsonStr = "\"test\"";
314     ASSERT_FALSE(Serializable::IsJson(str));
315     ASSERT_TRUE(Serializable::IsJson(jsonStr));
316 }
317 } // namespace OHOS::Test