1 /*
2  * Copyright (c) 2023 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 #define LOG_TAG "ValueProxyServiceTest"
17 #include "value_proxy.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include "log_print.h"
22 namespace OHOS::Test {
23 using namespace testing::ext;
24 using namespace OHOS::DistributedData;
25 class ValueProxyServiceTest : public testing::Test {
26 };
27 
28 /**
29 * @tc.name: GetSchema
30 * @tc.desc: GetSchema from cloud when no schema in meta.
31 * @tc.type: FUNC
32 * @tc.require:
33 * @tc.author: ht
34 */
HWTEST_F(ValueProxyServiceTest, VBucketsNormal2GaussDB, TestSize.Level0)35 HWTEST_F(ValueProxyServiceTest, VBucketsNormal2GaussDB, TestSize.Level0)
36 {
37     std::vector<DistributedDB::VBucket> dbVBuckets;
38     OHOS::DistributedData::VBuckets extends = {
39         {{"#gid", {"0000000"}}, {"#flag", {true }}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}},
40         {{"#gid", {"0000001"}}}
41     };
42     dbVBuckets = ValueProxy::Convert(std::move(extends));
43     ASSERT_EQ(dbVBuckets.size(), 2);
44 }
45 
46 /**
47 * @tc.name: GetSchema
48 * @tc.desc: GetSchema from cloud when no schema in meta.
49 * @tc.type: FUNC
50 * @tc.require:
51 * @tc.author: ht
52 */
HWTEST_F(ValueProxyServiceTest, VBucketsGaussDB2Normal, TestSize.Level0)53 HWTEST_F(ValueProxyServiceTest, VBucketsGaussDB2Normal, TestSize.Level0)
54 {
55     std::vector<DistributedDB::VBucket> dbVBuckets = {
56         {{"#gid", {"0000000"}}, {"#flag", {true }}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}},
57         {{"#gid", {"0000001"}}}
58     };
59     OHOS::DistributedData::VBuckets extends;
60     extends = ValueProxy::Convert(std::move(dbVBuckets));
61     ASSERT_EQ(extends.size(), 2);
62 }
63 
64 /**
65 * @tc.name: GetSchema
66 * @tc.desc: GetSchema from cloud when no schema in meta.
67 * @tc.type: FUNC
68 * @tc.require:
69 * @tc.author: ht
70 */
HWTEST_F(ValueProxyServiceTest, VBucketsNormal2Rdb, TestSize.Level0)71 HWTEST_F(ValueProxyServiceTest, VBucketsNormal2Rdb, TestSize.Level0)
72 {
73     using RdbBucket = OHOS::NativeRdb::ValuesBucket;
74     std::vector<RdbBucket> rdbVBuckets;
75     OHOS::DistributedData::VBuckets extends = {
76         {{"#gid", {"0000000"}}, {"#flag", {true }}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}},
77         {{"#gid", {"0000001"}}}
78     };
79     rdbVBuckets = ValueProxy::Convert(std::move(extends));
80     ASSERT_EQ(rdbVBuckets.size(), 2);
81 }
82 
83 /**
84 * @tc.name: GetSchema
85 * @tc.desc: GetSchema from cloud when no schema in meta.
86 * @tc.type: FUNC
87 * @tc.require:
88 * @tc.author: ht
89 */
HWTEST_F(ValueProxyServiceTest, VBucketsRdb2Normal, TestSize.Level0)90 HWTEST_F(ValueProxyServiceTest, VBucketsRdb2Normal, TestSize.Level0)
91 {
92     using RdbBucket = OHOS::NativeRdb::ValuesBucket;
93     using RdbValue = OHOS::NativeRdb::ValueObject;
94     std::vector<RdbBucket> rdbVBuckets = {
95         RdbBucket(std::map<std::string, RdbValue> {
96             {"#gid", {"0000000"}},
97             {"#flag", {true }},
98             {"#value", {int64_t(100)}},
99             {"#float", {double(100)}}
100         }),
101         RdbBucket(std::map<std::string, RdbValue> {
102             {"#gid", {"0000001"}}
103         })
104     };
105     OHOS::DistributedData::VBuckets extends;
106     extends = ValueProxy::Convert(std::move(rdbVBuckets));
107     ASSERT_EQ(extends.size(), 2);
108 }
109 
110 /**
111 * @tc.name: GetSchema
112 * @tc.desc: GetSchema from cloud when no schema in meta.
113 * @tc.type: FUNC
114 * @tc.require:
115 * @tc.author: ht
116 */
HWTEST_F(ValueProxyServiceTest, ConvertIntMapTest, TestSize.Level0)117 HWTEST_F(ValueProxyServiceTest, ConvertIntMapTest, TestSize.Level0)
118 {
119     std::map<std::string, int64_t> testMap = { { "name", 1 }, { "school", 2 }, { "address", 3 } };
120     auto res = ValueProxy::Convert<int64_t>(testMap);
121     auto testMap2 = std::map<std::string, int64_t>(res);
122     ASSERT_EQ(testMap2.find("name")->second, 1);
123 
124     auto errorMap = std::map<std::string, double>(res);
125     ASSERT_EQ(errorMap.size(), 0);
126 }
127 
128 /**
129 * @tc.name: GetSchema
130 * @tc.desc: GetSchema from cloud when no schema in meta.
131 * @tc.type: FUNC
132 * @tc.require:
133 * @tc.author: ht
134 */
HWTEST_F(ValueProxyServiceTest, ConvertAssetMapGaussDB2NormalTest, TestSize.Level0)135 HWTEST_F(ValueProxyServiceTest, ConvertAssetMapGaussDB2NormalTest, TestSize.Level0)
136 {
137     DistributedDB::Asset dbAsset0 { .name = "dbname", .uri = "dburi" };
138     DistributedDB::Asset dbAsset1 { .name = "dbname", .uri = "dburi" };
139     std::map<std::string, DistributedDB::Asset> dbMap { { "asset0", dbAsset0 }, { "asset1", dbAsset1 } };
140     OHOS::DistributedData::VBucket transferredAsset = ValueProxy::Convert(dbMap);
141     ASSERT_EQ(transferredAsset.size(), 2);
142     auto asset = std::get<OHOS::DistributedData::Asset>(transferredAsset.find("asset0")->second);
143     ASSERT_EQ(asset.name, "dbname");
144 
145     DistributedDB::Assets dbAssets { dbAsset0, dbAsset1 };
146     std::map<std::string, DistributedDB::Assets> dbAssetsMap { {"dbAssets", dbAssets} };
147     OHOS::DistributedData::VBucket transferredAssets = ValueProxy::Convert(dbAssetsMap);
148     ASSERT_EQ(transferredAssets.size(), 1);
149     auto assets = std::get<OHOS::DistributedData::Assets>(transferredAssets.find("dbAssets")->second);
150     ASSERT_EQ(assets.size(), 2);
151     auto dataAsset = assets.begin();
152     ASSERT_EQ(dataAsset->name, "dbname");
153 }
154 
155 /**
156 * @tc.name: GetSchema
157 * @tc.desc: GetSchema from cloud when no schema in meta.
158 * @tc.type: FUNC
159 * @tc.require:
160 * @tc.author: ht
161 */
HWTEST_F(ValueProxyServiceTest, ConvertAssetMapNormal2GaussDBTest, TestSize.Level0)162 HWTEST_F(ValueProxyServiceTest, ConvertAssetMapNormal2GaussDBTest, TestSize.Level0)
163 {
164     using NormalAsset = OHOS::DistributedData::Asset;
165     using NormalAssets = OHOS::DistributedData::Assets;
166     NormalAsset nAsset0 { .name = "name", .uri = "uri" };
167     NormalAsset nAsset1 { .name = "name", .uri = "uri" };
168     std::map<std::string, NormalAsset> nMap { { "asset0", nAsset0 }, { "asset1", nAsset1 } };
169     DistributedDB::VBucket transferredAsset = ValueProxy::Convert(nMap);
170     ASSERT_EQ(transferredAsset.size(), 2);
171     auto asset = std::get<DistributedDB::Asset>(transferredAsset.find("asset0")->second);
172     ASSERT_EQ(asset.name, "name");
173 
174     NormalAssets nAssets { nAsset0, nAsset1 };
175     std::map<std::string, NormalAssets> nAssetsMap { { "Assets", nAssets } };
176     DistributedDB::VBucket transferredAssets = ValueProxy::Convert(nAssetsMap);
177     ASSERT_EQ(transferredAssets.size(), 1);
178     auto assets = std::get<DistributedDB::Assets>(transferredAssets.find("Assets")->second);
179     ASSERT_EQ(assets.size(), 2);
180     auto dataAsset = assets.begin();
181     ASSERT_EQ(dataAsset->name, "name");
182 }
183 
184 /**
185 * @tc.name: GetSchema
186 * @tc.desc: GetSchema from cloud when no schema in meta.
187 * @tc.type: FUNC
188 * @tc.require:
189 * @tc.author: ht
190 */
HWTEST_F(ValueProxyServiceTest, ConvertAssetMapRdb2NormalTest, TestSize.Level0)191 HWTEST_F(ValueProxyServiceTest, ConvertAssetMapRdb2NormalTest, TestSize.Level0)
192 {
193     using RdbAsset = OHOS::NativeRdb::AssetValue;
194     using RdbAssets = std::vector<RdbAsset>;
195     RdbAsset dbAsset0 { .name = "dbname", .uri = "dburi" };
196     RdbAsset dbAsset1 { .name = "dbname", .uri = "dburi" };
197     std::map<std::string, RdbAsset> dbMap { { "asset0", dbAsset0 }, { "asset1", dbAsset1 } };
198     OHOS::DistributedData::VBucket transferredAsset = ValueProxy::Convert(dbMap);
199     ASSERT_EQ(transferredAsset.size(), 2);
200     auto asset = std::get<OHOS::DistributedData::Asset>(transferredAsset.find("asset0")->second);
201     ASSERT_EQ(asset.name, "dbname");
202 
203     RdbAssets dbAssets { dbAsset0, dbAsset1 };
204     std::map<std::string, RdbAssets> dbAssetsMap { {"dbAssets", dbAssets} };
205     OHOS::DistributedData::VBucket transferredAssets = ValueProxy::Convert(dbAssetsMap);
206     ASSERT_EQ(transferredAssets.size(), 1);
207     auto assets = std::get<OHOS::DistributedData::Assets>(transferredAssets.find("dbAssets")->second);
208     ASSERT_EQ(assets.size(), 2);
209     auto dataAsset = assets.begin();
210     ASSERT_EQ(dataAsset->name, "dbname");
211 }
212 
213 /**
214 * @tc.name: GetSchema
215 * @tc.desc: GetSchema from cloud when no schema in meta.
216 * @tc.type: FUNC
217 * @tc.require:
218 * @tc.author: ht
219 */
HWTEST_F(ValueProxyServiceTest, ConvertAssetMapNormal2RdbTest, TestSize.Level0)220 HWTEST_F(ValueProxyServiceTest, ConvertAssetMapNormal2RdbTest, TestSize.Level0)
221 {
222     using RdbAsset = OHOS::NativeRdb::AssetValue;
223     using RdbAssets = std::vector<RdbAsset>;
224     using NormalAsset = OHOS::DistributedData::Asset;
225     using NormalAssets = OHOS::DistributedData::Assets;
226     NormalAsset nAsset0 { .name = "name", .uri = "uri" };
227     NormalAsset nAsset1 { .name = "name", .uri = "uri" };
228     std::map<std::string, NormalAsset> nMap { { "asset0", nAsset0 }, { "asset1", nAsset1 } };
229     OHOS::NativeRdb::ValuesBucket transferredAsset = ValueProxy::Convert(nMap);
230     ASSERT_EQ(transferredAsset.Size(), 2);
231     OHOS::NativeRdb::ValueObject rdbObject;
232     transferredAsset.GetObject("asset0", rdbObject);
233     RdbAsset rdbAsset;
234     rdbObject.GetAsset(rdbAsset);
235     ASSERT_EQ(rdbAsset.name, "name");
236 
237     NormalAssets nAssets { nAsset0, nAsset1 };
238     std::map<std::string, NormalAssets> nAssetsMap { { "Assets", nAssets } };
239     OHOS::NativeRdb::ValuesBucket transferredAssets = ValueProxy::Convert(nAssetsMap);
240     ASSERT_EQ(transferredAssets.Size(), 1);
241     OHOS::NativeRdb::ValueObject rdbObject2;
242     transferredAssets.GetObject("Assets", rdbObject2);
243     RdbAssets rdbAssets;
244     rdbObject2.GetAssets(rdbAssets);
245     ASSERT_EQ(rdbAssets.size(), 2);
246     auto dataAsset = rdbAssets.begin();
247     ASSERT_EQ(dataAsset->name, "name");
248 }
249 
250 /**
251 * @tc.name: AssetConvertToDataStatus
252 * @tc.desc: Asset::ConvertToDataStatus function test.
253 * @tc.type: FUNC
254 * @tc.require:
255 * @tc.author: SQL
256 */
HWTEST_F(ValueProxyServiceTest, AssetConvertToDataStatus, TestSize.Level0)257 HWTEST_F(ValueProxyServiceTest, AssetConvertToDataStatus, TestSize.Level0)
258 {
259     DistributedDB::Asset asset;
260     asset.status = static_cast<uint32_t>(DistributedDB::AssetStatus::DOWNLOADING);
261     auto result = ValueProxy::Asset::ConvertToDataStatus(asset);
262     EXPECT_EQ(result, DistributedData::Asset::STATUS_DOWNLOADING);
263 
264     asset.status = static_cast<uint32_t>(DistributedDB::AssetStatus::ABNORMAL);
265     result = ValueProxy::Asset::ConvertToDataStatus(asset);
266     EXPECT_EQ(result, DistributedData::Asset::STATUS_ABNORMAL);
267 
268     asset.status = static_cast<uint32_t>(DistributedDB::AssetStatus::NORMAL);
269     result = ValueProxy::Asset::ConvertToDataStatus(asset);
270     EXPECT_EQ(result, DistributedData::Asset::STATUS_NORMAL);
271 
272     asset.status = static_cast<uint32_t>(DistributedDB::AssetStatus::UPDATE);
273     result = ValueProxy::Asset::ConvertToDataStatus(asset);
274     EXPECT_EQ(result, DistributedData::Asset::STATUS_UPDATE);
275 
276     asset.status = static_cast<uint32_t>(DistributedDB::AssetStatus::DELETE);
277     result = ValueProxy::Asset::ConvertToDataStatus(asset);
278     EXPECT_EQ(result, DistributedData::Asset::STATUS_DELETE);
279 
280     asset.status = static_cast<uint32_t>(DistributedDB::AssetStatus::INSERT);
281     result = ValueProxy::Asset::ConvertToDataStatus(asset);
282     EXPECT_EQ(result, DistributedData::Asset::STATUS_INSERT);
283 
284     asset.status = static_cast<uint32_t>(DistributedDB::AssetStatus::UPDATE) +
285         static_cast<uint32_t>(DistributedDB::AssetStatus::ABNORMAL);
286     result = ValueProxy::Asset::ConvertToDataStatus(asset);
287     EXPECT_EQ(result, DistributedData::Asset::STATUS_NORMAL);
288 }
289 
290 /**
291 * @tc.name: AssetConvertToDBStatus
292 * @tc.desc: Asset::ConvertToDBStatus function test.
293 * @tc.type: FUNC
294 * @tc.require:
295 * @tc.author: SQL
296 */
HWTEST_F(ValueProxyServiceTest, AssetConvertToDBStatus, TestSize.Level0)297 HWTEST_F(ValueProxyServiceTest, AssetConvertToDBStatus, TestSize.Level0)
298 {
299     uint32_t status = static_cast<uint32_t>(DistributedData::Asset::STATUS_NORMAL);
300     auto result = ValueProxy::Asset::ConvertToDBStatus(status);
301     EXPECT_EQ(result, DistributedDB::AssetStatus::NORMAL);
302 
303     status = static_cast<uint32_t>(DistributedData::Asset::STATUS_ABNORMAL);
304     result = ValueProxy::Asset::ConvertToDBStatus(status);
305     EXPECT_EQ(result, DistributedDB::AssetStatus::ABNORMAL);
306 
307     status = static_cast<uint32_t>(DistributedData::Asset::STATUS_INSERT);
308     result = ValueProxy::Asset::ConvertToDBStatus(status);
309     EXPECT_EQ(result, DistributedDB::AssetStatus::INSERT);
310 
311     status = static_cast<uint32_t>(DistributedData::Asset::STATUS_UPDATE);
312     result = ValueProxy::Asset::ConvertToDBStatus(status);
313     EXPECT_EQ(result, DistributedDB::AssetStatus::UPDATE);
314 
315     status = static_cast<uint32_t>(DistributedData::Asset::STATUS_DELETE);
316     result = ValueProxy::Asset::ConvertToDBStatus(status);
317     EXPECT_EQ(result, DistributedDB::AssetStatus::DELETE);
318 
319     status = static_cast<uint32_t>(DistributedData::Asset::STATUS_DOWNLOADING);
320     result = ValueProxy::Asset::ConvertToDBStatus(status);
321     EXPECT_EQ(result, DistributedDB::AssetStatus::DOWNLOADING);
322 
323     status = static_cast<uint32_t>(DistributedData::Asset::STATUS_UNKNOWN);
324     result = ValueProxy::Asset::ConvertToDBStatus(status);
325     EXPECT_EQ(result, DistributedDB::AssetStatus::NORMAL);
326 }
327 
328 /**
329 * @tc.name: TempAssetConvertToDataStatus
330 * @tc.desc: TempAsset::ConvertToDataStatus function test.
331 * @tc.type: FUNC
332 * @tc.require:
333 * @tc.author: SQL
334 */
HWTEST_F(ValueProxyServiceTest, TempAssetConvertToDataStatus, TestSize.Level0)335 HWTEST_F(ValueProxyServiceTest, TempAssetConvertToDataStatus, TestSize.Level0)
336 {
337     uint32_t status = static_cast<uint32_t>(DistributedDB::AssetStatus::NORMAL);
338     auto result = ValueProxy::TempAsset::ConvertToDataStatus(status);
339     EXPECT_EQ(result, DistributedData::Asset::STATUS_NORMAL);
340 
341     status = static_cast<uint32_t>(DistributedDB::AssetStatus::ABNORMAL);
342     result = ValueProxy::TempAsset::ConvertToDataStatus(status);
343     EXPECT_EQ(result, DistributedData::Asset::STATUS_ABNORMAL);
344 
345     status = static_cast<uint32_t>(DistributedDB::AssetStatus::INSERT);
346     result = ValueProxy::TempAsset::ConvertToDataStatus(status);
347     EXPECT_EQ(result, DistributedData::Asset::STATUS_INSERT);
348 
349     status = static_cast<uint32_t>(DistributedDB::AssetStatus::UPDATE);
350     result = ValueProxy::TempAsset::ConvertToDataStatus(status);
351     EXPECT_EQ(result, DistributedData::Asset::STATUS_UPDATE);
352 
353     status = static_cast<uint32_t>(DistributedDB::AssetStatus::DELETE);
354     result = ValueProxy::TempAsset::ConvertToDataStatus(status);
355     EXPECT_EQ(result, DistributedData::Asset::STATUS_DELETE);
356 
357     status = static_cast<uint32_t>(DistributedDB::AssetStatus::DOWNLOADING);
358     result = ValueProxy::TempAsset::ConvertToDataStatus(status);
359     EXPECT_EQ(result, DistributedData::Asset::STATUS_DOWNLOADING);
360 
361     status = static_cast<uint32_t>(DistributedDB::AssetStatus::DOWNLOAD_WITH_NULL);
362     result = ValueProxy::TempAsset::ConvertToDataStatus(status);
363     EXPECT_NE(result, DistributedData::Asset::STATUS_NORMAL);
364 }
365 } // namespace OHOS::Test