• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using namespace testing::ext;
21 using namespace OHOS::DistributedData;
22 
23 class SerializableTest : public testing::Test {
24 public:
25     struct Normal final : public Serializable {
26     public:
27         std::string name = "Test";
28         int32_t count = 0;
29         uint32_t status = 1;
30         int64_t value = 2;
31         bool isClear = false;
32         std::vector<std::string> cols{ "123", "345", "789" };
33         std::vector<std::vector<int32_t>> colRow{ { 123, 345, 789 }, { 123, 345, 789 } };
34 
MarshalSerializableTest::Normal35         bool Marshal(json &node) const override
36         {
37             SetValue(node[GET_NAME(name)], name);
38             SetValue(node[GET_NAME(count)], count);
39             SetValue(node[GET_NAME(status)], status);
40             SetValue(node[GET_NAME(value)], value);
41             SetValue(node[GET_NAME(isClear)], isClear);
42             SetValue(node[GET_NAME(cols)], cols);
43             SetValue(node[GET_NAME(colRow)], colRow);
44             return true;
45         }
UnmarshalSerializableTest::Normal46         bool Unmarshal(const json &node) override
47         {
48             GetValue(node, GET_NAME(name), name);
49             GetValue(node, GET_NAME(count), count);
50             GetValue(node, GET_NAME(status), status);
51             GetValue(node, GET_NAME(value), value);
52             GetValue(node, GET_NAME(isClear), isClear);
53             GetValue(node, GET_NAME(cols), cols);
54             GetValue(node, GET_NAME(colRow), colRow);
55             return true;
56         }
operator ==SerializableTest::Normal57         bool operator == (const Normal &ref) const
58         {
59             return name == ref.name && count == ref.count && status == ref.status && value == ref.value
60                 && isClear == ref.isClear && cols == ref.cols;
61         }
62     };
63 
64     struct NormalEx final : public Serializable {
65     public:
66         std::vector<Normal> normals {Normal(), Normal()};
67         Normal normal;
68         int32_t count = 123;
69         std::string name = "wdt";
MarshalSerializableTest::NormalEx70         bool Marshal(json &node) const override
71         {
72             SetValue(node[GET_NAME(normals)], normals);
73             SetValue(node[GET_NAME(normal)], normal);
74             SetValue(node[GET_NAME(count)], count);
75             SetValue(node[GET_NAME(name)], name);
76             return true;
77         }
UnmarshalSerializableTest::NormalEx78         bool Unmarshal(const json &node) override
79         {
80             GetValue(node, GET_NAME(normals), normals);
81             GetValue(node, GET_NAME(normal), normal);
82             GetValue(node, GET_NAME(count), count);
83             GetValue(node, GET_NAME(name), name);
84             return true;
85         }
operator ==SerializableTest::NormalEx86         bool operator==(const NormalEx &normalEx) const
87         {
88             return normals == normalEx.normals && count == normalEx.count && name == normalEx.name;
89         }
90     };
SetUpTestCase(void)91     static void SetUpTestCase(void)
92     {
93     }
TearDownTestCase(void)94     static void TearDownTestCase(void)
95     {
96     }
SetUp()97     void SetUp()
98     {
99         Test::SetUp();
100     }
TearDown()101     void TearDown()
102     {
103         Test::TearDown();
104     }
105 };
106 
107 /**
108 * @tc.name: SerializableSuiteGetVal
109 * @tc.desc: Get Value.
110 * @tc.type: FUNC
111 * @tc.require:
112 * @tc.author: Sven Wang
113 */
114 HWTEST_F(SerializableTest, GetNormalVal, TestSize.Level2)
115 {
116     ZLOGI("SerializableSuite GetNormalVal begin.");
117     Normal normal;
118     normal.name = "normal";
119     normal.count = -1;
120     normal.status = 12;
121     normal.value = -56;
122     normal.isClear = true;
123     normal.cols = {"adfasdfas"};
124     auto jstr = to_string(normal.Marshall());
125     Normal normal1;
126     normal1.Unmarshall(jstr);
127     ASSERT_TRUE(normal == normal1) << normal1.name;
128 }
129 
130 /**
131 * @tc.name: Delete Serializable
132 * @tc.desc: can delete child class, but not delete parent class point.
133 * @tc.type: FUNC
134 * @tc.require:
135 * @tc.author: Sven Wang
136 */
137 HWTEST_F(SerializableTest, DeleteSerializable, TestSize.Level2)
138 {
139     ZLOGI("SerializableSuite DeleteSerializable begin.");
140     ASSERT_FALSE(std::is_destructible<Serializable>::value);
141     ASSERT_TRUE(std::is_destructible<NormalEx>::value);
142 }
143 
144 /**
145 * @tc.name: SerializableSuiteGetMutilVal
146 * @tc.desc: mutil value case.
147 * @tc.type: FUNC
148 * @tc.require:
149 * @tc.author: Sven Wang
150 */
151 HWTEST_F(SerializableTest, GetMutilVal, TestSize.Level2)
152 {
153     ZLOGI("SerializableSuite GetMutilVal begin.");
154     NormalEx normalEx;
155     normalEx.normals = {Normal()};
156     normalEx.name = "normalEx";
157     auto jstr = to_string(normalEx.Marshall());
158     NormalEx normal1;
159     normal1.Unmarshall(jstr);
160     ASSERT_TRUE(normalEx == normal1) << normal1.name;
161 }
162 
163 /**
164 * @tc.name: GetMap
165 * @tc.desc: mutil value case.
166 * @tc.type: FUNC
167 * @tc.require:
168 * @tc.author: Sven Wang
169 */
170 HWTEST_F(SerializableTest, GetMap, TestSize.Level2)
171 {
172     ZLOGI("SerializableSuite GetMapVals begin.");
173     std::map<std::string, NormalEx> marshData;
174     NormalEx normalEx;
175     normalEx.normals = { Normal() };
176     normalEx.name = "normalEx";
177     marshData.insert(std::pair{ "test1", normalEx });
178     auto jsonData = NormalEx::Marshall(marshData);
179 
180     std::map<std::string, NormalEx> unmarshData;
181     NormalEx::Unmarshall(jsonData, unmarshData);
182     ASSERT_TRUE((marshData["test1"] == unmarshData["test1"])) << jsonData;
183 }
184 
185 /**
186 * @tc.name: GetMapInStruct
187 * @tc.desc: mutil value case.
188 * @tc.type: FUNC
189 * @tc.require:
190 * @tc.author: Sven Wang
191 */
192 HWTEST_F(SerializableTest, GetMapInStruct, TestSize.Level2)
193 {
194     struct TestMeta : public Serializable {
195         std::map<std::string, NormalEx> data;
196         std::map<std::string, bool> *index = nullptr;
197         std::vector<std::map<std::string, NormalEx>> others;
~TestMetaTestMeta198         ~TestMeta()
199         {
200             delete index;
201             index = nullptr;
202         }
MarshalTestMeta203         bool Marshal(json &node) const
204         {
205             SetValue(node[GET_NAME(data)], data);
206             SetValue(node[GET_NAME(index)], index);
207             SetValue(node[GET_NAME(others)], others);
208             return true;
209         }
210 
UnmarshalTestMeta211         bool Unmarshal(const json &node)
212         {
213             GetValue(node, GET_NAME(data), data);
214             GetValue(node, GET_NAME(index), index);
215             GetValue(node, GET_NAME(others), others);
216             return true;
217         }
218     };
219     ZLOGI("SerializableSuite GetMapVals begin.");
220     TestMeta marData;
221     NormalEx normalEx;
222     normalEx.normals = { Normal() };
223     normalEx.name = "normalEx";
224     marData.data.insert(std::pair{ "test1", normalEx });
225     marData.others.push_back({ std::pair{ "test2", normalEx } });
226     marData.index = new (std::nothrow) std::map<std::string, bool>;
227     ASSERT_NE(marData.index, nullptr);
228     marData.index->insert(std::pair{ "test1", true });
229     marData.index->insert(std::pair{ "test2", true });
230     auto jsonData = NormalEx::Marshall(marData);
231     TestMeta unmarData;
232     NormalEx::Unmarshall(jsonData, unmarData);
233     ASSERT_TRUE((marData.data == unmarData.data)) << jsonData;
234     ASSERT_TRUE((marData.others == unmarData.others)) << jsonData;
235     ASSERT_NE(unmarData.index, nullptr);
236     ASSERT_TRUE((*marData.index == *unmarData.index)) << jsonData;
237 }