• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "RdbAssetLoaderTest"
16 
17 #include "rdb_asset_loader.h"
18 
19 #include "gtest/gtest.h"
20 #include "log_print.h"
21 #include "rdb_notifier_proxy.h"
22 #include "rdb_watcher.h"
23 #include "store/cursor.h"
24 #include "store/general_value.h"
25 #include "store/general_watcher.h"
26 using namespace OHOS;
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace OHOS::DistributedRdb;
30 using namespace OHOS::DistributedData;
31 using Type = DistributedDB::Type;
32 using AssetsRecord = DistributedData::AssetRecord;
33 const DistributedDB::Asset g_rdbAsset = { .version = 1,
34     .name = "Phone",
35     .assetId = "0",
36     .subpath = "/local/sync",
37     .uri = "file://rdbtest/local/sync",
38     .modifyTime = "123456",
39     .createTime = "createTime",
40     .size = "256",
41     .hash = "ASE" };
42 namespace OHOS::Test {
43 namespace DistributedRDBTest {
44 class RdbAssetLoaderTest : public testing::Test {
45 public:
SetUpTestCase(void)46     static void SetUpTestCase(void){};
TearDownTestCase(void)47     static void TearDownTestCase(void){};
SetUp()48     void SetUp(){};
TearDown()49     void TearDown(){};
50 };
51 
52 class MockAssetLoader : public DistributedData::AssetLoader {
53 public:
Download(const std::string & tableName,const std::string & gid,const DistributedData::Value & prefix,VBucket & assets)54     int32_t Download(const std::string &tableName, const std::string &gid,
55         const DistributedData::Value &prefix, VBucket &assets) override
56     {
57         return GeneralError::E_OK;
58     }
59 
RemoveLocalAssets(const std::string & tableName,const std::string & gid,const DistributedData::Value & prefix,VBucket & assets)60     int32_t RemoveLocalAssets(const std::string &tableName, const std::string &gid,
61         const DistributedData::Value &prefix, VBucket &assets) override
62     {
63         return GeneralError::E_OK;
64     }
65 
CancelDownload()66     int32_t CancelDownload() override
67     {
68         return GeneralError::E_OK;
69     }
70 };
71 
72 class RdbWatcherTest : public testing::Test {
73 public:
SetUpTestCase(void)74     static void SetUpTestCase(void){};
TearDownTestCase(void)75     static void TearDownTestCase(void){};
SetUp()76     void SetUp(){};
TearDown()77     void TearDown(){};
78 };
79 
80 /**
81 * @tc.name: Download
82 * @tc.desc: RdbAssetLoader download test.
83 * @tc.type: FUNC
84 * @tc.require:
85 * @tc.author: SQL
86 */
87 HWTEST_F(RdbAssetLoaderTest, Download, TestSize.Level0)
88 {
89     BindAssets bindAssets;
90     std::shared_ptr<MockAssetLoader> cloudAssetLoader = std::make_shared<MockAssetLoader>();
91     DistributedRdb::RdbAssetLoader rdbAssetLoader(cloudAssetLoader, &bindAssets);
92     std::string tableName = "testTable";
93     std::string groupId = "testGroup";
94     Type prefix;
95     std::map<std::string, DistributedDB::Assets> assets;
96     assets["asset1"].push_back(g_rdbAsset);
97     auto result = rdbAssetLoader.Download(tableName, groupId, prefix, assets);
98     EXPECT_EQ(result, DistributedDB::DBStatus::OK);
99 }
100 
101 /**
102 * @tc.name: BatchDownload
103 * @tc.desc: RdbAssetLoader batch download test.
104 * @tc.type: FUNC
105 * @tc.require:
106 * @tc.author:
107 */
108 HWTEST_F(RdbAssetLoaderTest, BatchDownload, TestSize.Level0)
109 {
110     BindAssets bindAssets;
111     auto cloudAssetLoader = std::make_shared<AssetLoader>();
112     DistributedRdb::RdbAssetLoader rdbAssetLoader(cloudAssetLoader, &bindAssets);
113     std::string tableName = "testTable";
114     Type prefix;
115     std::map<std::string, DistributedDB::Assets> assets;
116     assets["asset1"].push_back(g_rdbAsset);
117     std::vector<DistributedDB::IAssetLoader::AssetRecord> assetRecords;
118     DistributedDB::IAssetLoader::AssetRecord assetRecord { .gid = "gid", .prefix = prefix, .assets = assets };
119     assetRecords.emplace_back(assetRecord);
120     rdbAssetLoader.BatchDownload(tableName, assetRecords);
121     ASSERT_TRUE(!assetRecords.empty());
122     EXPECT_EQ(assetRecords[0].status, DistributedDB::DBStatus::OK);
123 }
124 
125 /**
126 * @tc.name: Convert001
127 * @tc.desc: Convert test.
128 * @tc.type: FUNC
129 * @tc.require:
130 * @tc.author:
131 */
132 HWTEST_F(RdbAssetLoaderTest, Convert001, TestSize.Level0)
133 {
134     BindAssets bindAssets;
135     auto cloudAssetLoader = std::make_shared<AssetLoader>();
136     DistributedRdb::RdbAssetLoader rdbAssetLoader(cloudAssetLoader, &bindAssets);
137     Type prefix;
138     std::map<std::string, DistributedDB::Assets> assets;
139     assets["asset1"].push_back(g_rdbAsset);
140     std::vector<DistributedDB::IAssetLoader::AssetRecord> assetRecords;
141     DistributedDB::IAssetLoader::AssetRecord assetRecord { .gid = "gid", .prefix = prefix, .assets = assets };
142     assetRecords.emplace_back(assetRecord);
143     assetRecords.emplace_back(assetRecord);
144     auto assetsRecords = rdbAssetLoader.Convert(std::move(assetRecords));
145     EXPECT_TRUE(!assetsRecords.empty());
146 }
147 
148 /**
149 * @tc.name: Convert002
150 * @tc.desc: Convert test.
151 * @tc.type: FUNC
152 * @tc.require:
153 * @tc.author:
154 */
155 HWTEST_F(RdbAssetLoaderTest, Convert002, TestSize.Level0)
156 {
157     BindAssets bindAssets;
158     auto cloudAssetLoader = std::make_shared<AssetLoader>();
159     DistributedRdb::RdbAssetLoader rdbAssetLoader(cloudAssetLoader, &bindAssets);
160     Value prefix;
161     VBucket assets;
162     std::vector<AssetsRecord> assetsRecords;
163     AssetRecord assetsRecord { .gid = "gid", .prefix = prefix, .assets = assets };
164     assetsRecords.emplace_back(assetsRecord);
165     auto assetRecords = rdbAssetLoader.Convert(std::move(assetsRecords));
166     EXPECT_TRUE(!assetRecords.empty());
167 }
168 
169 /**
170 * @tc.name: RemoveLocalAssets
171 * @tc.desc: RdbAssetLoader RemoveLocalAssets test.
172 * @tc.type: FUNC
173 * @tc.require:
174 * @tc.author: SQL
175 */
176 HWTEST_F(RdbAssetLoaderTest, RemoveLocalAssets, TestSize.Level0)
177 {
178     BindAssets bindAssets;
179     std::shared_ptr<MockAssetLoader> cloudAssetLoader = std::make_shared<MockAssetLoader>();
180     DistributedRdb::RdbAssetLoader rdbAssetLoader(cloudAssetLoader, &bindAssets);
181     std::vector<DistributedDB::Asset> assets;
182     assets.push_back(g_rdbAsset);
183     auto result = rdbAssetLoader.RemoveLocalAssets(assets);
184     EXPECT_EQ(result, DistributedDB::DBStatus::OK);
185 }
186 
187 /**
188 * @tc.name: CancelDownloadTest
189 * @tc.desc: RdbAssetLoader CancelDownload test.
190 * @tc.type: FUNC
191 * @tc.require:
192 * @tc.author: Hollokin
193 */
194 HWTEST_F(RdbAssetLoaderTest, CancelDownloadTest, TestSize.Level0)
195 {
196     BindAssets bindAssets;
197     DistributedRdb::RdbAssetLoader rdbAssetLoader1(nullptr, &bindAssets);
198     auto result = rdbAssetLoader1.CancelDownload();
199     EXPECT_EQ(result, DistributedDB::DBStatus::DB_ERROR);
200 
201     std::shared_ptr<MockAssetLoader> cloudAssetLoader = std::make_shared<MockAssetLoader>();
202     DistributedRdb::RdbAssetLoader rdbAssetLoader2(cloudAssetLoader, &bindAssets);
203     result = rdbAssetLoader2.CancelDownload();
204     EXPECT_EQ(result, DistributedDB::DBStatus::OK);
205 }
206 
207 /**
208 * @tc.name: PostEvent001
209 * @tc.desc: RdbAssetLoader PostEvent001 test
210 * @tc.type: FUNC
211 * @tc.require:
212 * @tc.author: SQL
213 */
214 HWTEST_F(RdbAssetLoaderTest, PostEvent001, TestSize.Level0)
215 {
216     BindAssets bindAssets;
217     bindAssets.bindAssets = nullptr;
218     std::shared_ptr<AssetLoader> assetLoader = std::make_shared<AssetLoader>();
219     DistributedRdb::RdbAssetLoader rdbAssetLoader(assetLoader, &bindAssets);
220     std::string tableName = "testTable";
221     std::string groupId = "testGroup";
222     Type prefix;
223     std::map<std::string, DistributedDB::Assets> assets;
224     assets["asset1"].push_back(g_rdbAsset);
225     auto result = rdbAssetLoader.Download(tableName, groupId, prefix, assets);
226     EXPECT_EQ(result, DistributedDB::DBStatus::CLOUD_ERROR);
227 }
228 
229 /**
230 * @tc.name: PostEvent002
231 * @tc.desc: RdbAssetLoader PostEvent002 test
232 * @tc.type: FUNC
233 * @tc.require:
234 * @tc.author: SQL
235 */
236 HWTEST_F(RdbAssetLoaderTest, PostEvent002, TestSize.Level0)
237 {
238     DistributedData::Asset asset = {
239         .name = "",
240         .id = "",
241         .path = "",
242         .uri = "",
243         .modifyTime = "",
244         .createTime = "",
245         .size = "",
246         .hash = "",
247         .status = DistributedData::Asset::STATUS_DELETE,
248     };
249     DistributedData::Assets assets;
250     assets.push_back(asset);
251     BindAssets bindAssets;
252     bindAssets.bindAssets = nullptr;
253     std::shared_ptr<AssetLoader> assetLoader = std::make_shared<AssetLoader>();
254     DistributedRdb::RdbAssetLoader rdbAssetLoader(assetLoader, &bindAssets);
255     std::set<std::string> skipAssets;
256     std::set<std::string> deleteAssets;
257     rdbAssetLoader.PostEvent(DistributedData::AssetEvent::DOWNLOAD, assets, skipAssets, deleteAssets);
258     EXPECT_EQ(deleteAssets.size(), 1);
259 }
260 
261 /**
262 * @tc.name: ConvertStatus
263 * @tc.desc: RdbAssetLoader ConvertStatus abnormal
264 * @tc.type: FUNC
265 * @tc.require:
266 * @tc.author: SQL
267 */
268 HWTEST_F(RdbAssetLoaderTest, ConvertStatus, TestSize.Level0)
269 {
270     auto status = RdbAssetLoader::ConvertStatus(DistributedRdb::RdbAssetLoader::AssetStatus::STATUS_DOWNLOADING);
271     EXPECT_EQ(status, DistributedDB::DBStatus::OK);
272     status = RdbAssetLoader::ConvertStatus(DistributedRdb::RdbAssetLoader::AssetStatus::STATUS_BUTT);
273     EXPECT_EQ(status, DistributedDB::DBStatus::CLOUD_ERROR);
274     status = RdbAssetLoader::ConvertStatus(DistributedRdb::RdbAssetLoader::AssetStatus::STATUS_SKIP_ASSET);
275     EXPECT_EQ(status, DistributedDB::DBStatus::SKIP_ASSET);
276 }
277 
278 /**
279 * @tc.name: RdbWatcher
280 * @tc.desc: RdbWatcher function test.
281 * @tc.type: FUNC
282 * @tc.require:
283 * @tc.author: SQL
284 */
285 HWTEST_F(RdbWatcherTest, RdbWatcher, TestSize.Level0)
286 {
287     GeneralWatcher::Origin origin;
288     GeneralWatcher::PRIFields primaryFields = {{"primaryFields1", "primaryFields2"}};
289     GeneralWatcher::ChangeInfo values;
290     std::shared_ptr<RdbWatcher> watcher = std::make_shared<RdbWatcher>();
291     sptr<RdbNotifierProxy> notifier;
292     watcher->SetNotifier(notifier);
293     EXPECT_EQ(watcher->notifier_, nullptr);
294     auto result = watcher->OnChange(origin, primaryFields, std::move(values));
295     EXPECT_EQ(result, GeneralError::E_NOT_INIT);
296 }
297 } // namespace DistributedRDBTest
298 } // namespace OHOS::Test