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 
16 #define LOG_TAG "ObjectSnapshotTest"
17 
18 #include "object_snapshot.h"
19 #include <gtest/gtest.h>
20 #include "snapshot/machine_status.h"
21 #include "executor_pool.h"
22 
23 using namespace testing::ext;
24 using namespace OHOS::DistributedObject;
25 using namespace OHOS::DistributedData;
26 namespace OHOS::Test {
27 
28 class ObjectSnapshotTest : public testing::Test {
29 public:
30     void SetUp();
31     void TearDown();
32 
33 protected:
34     AssetBindInfo AssetBindInfo_;
35     Asset asset_;
36     std::string uri_;
37     StoreInfo storeInfo_;
38 };
39 
SetUp()40 void ObjectSnapshotTest::SetUp()
41 {
42     uri_ = "file:://com.example.hmos.notepad/data/storage/el2/distributedfiles/dir/asset1.jpg";
43     Asset asset{
44         .name = "test_name",
45         .uri = uri_,
46         .modifyTime = "modifyTime",
47         .size = "size",
48         .hash = "modifyTime_size",
49     };
50     asset_ = asset;
51 
52     VBucket vBucket{ { "11", 111 } };
53     AssetBindInfo AssetBindInfo{
54         .storeName = "store_test",
55         .tableName = "table_test",
56         .primaryKey = vBucket,
57         .field = "attachment",
58         .assetName = "asset1.jpg",
59     };
60     AssetBindInfo_ = AssetBindInfo;
61     StoreInfo storeInfo {
62         .tokenId = 0,
63         .bundleName = "bundleName_test",
64         .storeName = "store_test",
65         .instanceId = 1,
66         .user = 100,
67     };
68     storeInfo_ = storeInfo;
69     ChangedAssetInfo changedAssetInfo(asset, AssetBindInfo, storeInfo);
70 }
71 
TearDown()72 void ObjectSnapshotTest::TearDown() {}
73 
74 /**
75 * @tc.name: UploadTest001
76 * @tc.desc: Upload test.
77 * @tc.type: FUNC
78 * @tc.require:
79 * @tc.author: wangbin
80 */
81 HWTEST_F(ObjectSnapshotTest, UploadTest001, TestSize.Level0)
82 {
83     auto snapshot = std::make_shared<ObjectSnapshot>();
84     Asset asset{
85         .name = "test_name",
86         .uri = "file:://com.example.hmos.notepad/data/storage/el2/distributedfiles/dir/asset2.jpg",
87         .modifyTime = "modifyTime1",
88         .size = "size1",
89         .hash = "modifyTime1_size1",
90     };
91     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
92     ASSERT_EQ(snapshot->IsBoundAsset(asset), false);
93     auto upload = snapshot->Upload(asset);
94     ASSERT_EQ(upload, 0);
95 }
96 
97 /**
98 * @tc.name: UploadTest002
99 * @tc.desc: Upload test.
100 * @tc.type: FUNC
101 * @tc.require:
102 * @tc.author: wangbin
103 */
104 HWTEST_F(ObjectSnapshotTest, UploadTest002, TestSize.Level0)
105 {
106     auto snapshot = std::make_shared<ObjectSnapshot>();
107     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
108     ASSERT_EQ(snapshot->IsBoundAsset(asset_), true);
109     auto upload = snapshot->Upload(asset_);
110     ASSERT_EQ(upload, 0);
111 }
112 
113 /**
114 * @tc.name: DownloadTest001
115 * @tc.desc: Download test.
116 * @tc.type: FUNC
117 * @tc.require:
118 * @tc.author: wangbin
119 */
120 HWTEST_F(ObjectSnapshotTest, DownloadTest001, TestSize.Level0)
121 {
122     auto snapshot = std::make_shared<ObjectSnapshot>();
123     Asset asset{
124         .name = "test_name",
125         .uri = "file:://com.example.hmos.notepad/data/storage/el2/distributedfiles/dir/asset2.jpg",
126         .modifyTime = "modifyTime1",
127         .size = "size1",
128         .hash = "modifyTime1_size1",
129     };
130     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
131     ASSERT_EQ(snapshot->IsBoundAsset(asset), false);
132     auto upload = snapshot->Download(asset);
133     ASSERT_EQ(upload, 0);
134 }
135 
136 /**
137 * @tc.name: DownloadTest002
138 * @tc.desc: Download test.
139 * @tc.type: FUNC
140 * @tc.require:
141 * @tc.author: wangbin
142 */
143 HWTEST_F(ObjectSnapshotTest, DownloadTest002, TestSize.Level0)
144 {
145     auto snapshot = std::make_shared<ObjectSnapshot>();
146     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
147     ASSERT_EQ(snapshot->IsBoundAsset(asset_), true);
148     auto upload = snapshot->Download(asset_);
149     ASSERT_EQ(upload, 0);
150 }
151 
152 /**
153 * @tc.name: GetAssetStatusTest001
154 * @tc.desc: GetAssetStatus test.
155 * @tc.type: FUNC
156 * @tc.require:
157 * @tc.author: wangbin
158 */
159 HWTEST_F(ObjectSnapshotTest, GetAssetStatusTest001, TestSize.Level0)
160 {
161     auto snapshot = std::make_shared<ObjectSnapshot>();
162     Asset asset{
163         .name = "test_name",
164         .uri = "file:://com.example.hmos.notepad/data/storage/el2/distributedfiles/dir/asset2.jpg",
165         .modifyTime = "modifyTime1",
166         .size = "size1",
167         .hash = "modifyTime1_size1",
168     };
169     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
170     ASSERT_EQ(snapshot->IsBoundAsset(asset), false);
171     auto upload = snapshot->GetAssetStatus(asset);
172     ASSERT_EQ(upload, STATUS_BUTT);
173 }
174 
175 /**
176 * @tc.name: GetAssetStatusTest002
177 * @tc.desc: GetAssetStatus test.
178 * @tc.type: FUNC
179 * @tc.require:
180 * @tc.author: wangbin
181 */
182 HWTEST_F(ObjectSnapshotTest, GetAssetStatusTest002, TestSize.Level0)
183 {
184     auto snapshot = std::make_shared<ObjectSnapshot>();
185     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
186     ASSERT_EQ(snapshot->IsBoundAsset(asset_), true);
187     auto upload = snapshot->GetAssetStatus(asset_);
188     ASSERT_EQ(upload, STATUS_STABLE);
189 }
190 
191 /**
192 * @tc.name: UploadedTest001
193 * @tc.desc: Uploaded test.
194 * @tc.type: FUNC
195 * @tc.require:
196 * @tc.author: wangbin
197 */
198 HWTEST_F(ObjectSnapshotTest, UploadedTest001, TestSize.Level0)
199 {
200     auto snapshot = std::make_shared<ObjectSnapshot>();
201     Asset asset{
202         .name = "test_name",
203         .uri = "file:://com.example.hmos.notepad/data/storage/el2/distributedfiles/dir/asset2.jpg",
204         .modifyTime = "modifyTime1",
205         .size = "size1",
206         .hash = "modifyTime1_size1",
207     };
208     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
209     ASSERT_EQ(snapshot->IsBoundAsset(asset), false);
210     auto upload = snapshot->Uploaded(asset);
211     ASSERT_EQ(upload, E_OK);
212 }
213 
214 /**
215 * @tc.name: UploadedTest002
216 * @tc.desc: Uploaded test.
217 * @tc.type: FUNC
218 * @tc.require:
219 * @tc.author: wangbin
220 */
221 HWTEST_F(ObjectSnapshotTest, UploadedTest002, TestSize.Level0)
222 {
223     auto snapshot = std::make_shared<ObjectSnapshot>();
224     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
225     ASSERT_EQ(snapshot->IsBoundAsset(asset_), true);
226     auto upload = snapshot->Uploaded(asset_);
227     ASSERT_EQ(upload, E_OK);
228 }
229 
230 /**
231 * @tc.name: DownloadedTest001
232 * @tc.desc: Downloaded test.
233 * @tc.type: FUNC
234 * @tc.require:
235 * @tc.author: wangbin
236 */
237 HWTEST_F(ObjectSnapshotTest, DownloadedTest001, TestSize.Level0)
238 {
239     auto snapshot = std::make_shared<ObjectSnapshot>();
240     Asset asset{
241         .name = "test_name",
242         .uri = "file:://com.example.hmos.notepad/data/storage/el2/distributedfiles/dir/asset2.jpg",
243         .modifyTime = "modifyTime1",
244         .size = "size1",
245         .hash = "modifyTime1_size1",
246     };
247     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
248     ASSERT_EQ(snapshot->IsBoundAsset(asset), false);
249     auto upload = snapshot->Downloaded(asset);
250     ASSERT_EQ(upload, E_OK);
251 }
252 
253 /**
254 * @tc.name: DownloadedTest002
255 * @tc.desc: Downloaded test.
256 * @tc.type: FUNC
257 * @tc.require:
258 * @tc.author: wangbin
259 */
260 HWTEST_F(ObjectSnapshotTest, DownloadedTest002, TestSize.Level0)
261 {
262     auto snapshot = std::make_shared<ObjectSnapshot>();
263     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
264     ASSERT_EQ(snapshot->IsBoundAsset(asset_), true);
265     auto upload = snapshot->Downloaded(asset_);
266     ASSERT_EQ(upload, E_OK);
267 }
268 
269 /**
270 * @tc.name: TransferredTest001
271 * @tc.desc: Transferred test.
272 * @tc.type: FUNC
273 * @tc.require:
274 * @tc.author: wangbin
275 */
276 HWTEST_F(ObjectSnapshotTest, TransferredTest001, TestSize.Level0)
277 {
278     auto snapshot = std::make_shared<ObjectSnapshot>();
279     Asset asset{
280         .name = "test_name",
281         .uri = "file:://com.example.hmos.notepad/data/storage/el2/distributedfiles/dir/asset2.jpg",
282         .modifyTime = "modifyTime1",
283         .size = "size1",
284         .hash = "modifyTime1_size1",
285     };
286     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
287     ASSERT_EQ(snapshot->IsBoundAsset(asset), false);
288     auto upload = snapshot->Transferred(asset);
289     ASSERT_EQ(upload, E_OK);
290 }
291 
292 /**
293 * @tc.name: TransferredTest002
294 * @tc.desc: Transferred test.
295 * @tc.type: FUNC
296 * @tc.require:
297 * @tc.author: wangbin
298 */
299 HWTEST_F(ObjectSnapshotTest, TransferredTest002, TestSize.Level0)
300 {
301     auto snapshot = std::make_shared<ObjectSnapshot>();
302     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
303     ASSERT_EQ(snapshot->IsBoundAsset(asset_), true);
304     auto upload = snapshot->Transferred(asset_);
305     ASSERT_EQ(upload, E_OK);
306 }
307 
308 /**
309 * @tc.name: OnDataChangedTest001
310 * @tc.desc: OnDataChanged test.
311 * @tc.type: FUNC
312 * @tc.require:
313 * @tc.author: wangbin
314 */
315 HWTEST_F(ObjectSnapshotTest, OnDataChangedTest001, TestSize.Level0)
316 {
317     auto snapshot = std::make_shared<ObjectSnapshot>();
318     std::string deviceId = "object_snapshot_test_1";
319     Asset asset{
320         .name = "test_name",
321         .uri = "file:://com.example.hmos.notepad/data/storage/el2/distributedfiles/dir/asset2.jpg",
322         .modifyTime = "modifyTime1",
323         .size = "size1",
324         .hash = "modifyTime1_size1",
325     };
326     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
327     ASSERT_EQ(snapshot->IsBoundAsset(asset), false);
328     auto upload = snapshot->OnDataChanged(asset, deviceId);
329     ASSERT_EQ(upload, E_OK);
330 }
331 
332 /**
333 * @tc.name: OnDataChangedTest002
334 * @tc.desc: OnDataChanged test.
335 * @tc.type: FUNC
336 * @tc.require:
337 * @tc.author: wangbin
338 */
339 HWTEST_F(ObjectSnapshotTest, OnDataChangedTest002, TestSize.Level0)
340 {
341     auto snapshot = std::make_shared<ObjectSnapshot>();
342     std::string deviceId = "object_snapshot_test_1";
343     snapshot->BindAsset(asset_, AssetBindInfo_, storeInfo_);
344     ASSERT_EQ(snapshot->IsBoundAsset(asset_), true);
345     auto upload = snapshot->OnDataChanged(asset_, deviceId);
346     ASSERT_EQ(upload, E_OK);
347 }
348 
349 /**
350 * @tc.name: BindAsset001
351 * @tc.desc: BindAsset test.
352 * @tc.type: FUNC
353 * @tc.require:
354 * @tc.author: wangbin
355 */
356 HWTEST_F(ObjectSnapshotTest, BindAsset001, TestSize.Level0)
357 {
358     auto snapshot = std::make_shared<ObjectSnapshot>();
359     Asset asset{
360         .name = "test_name",
361         .uri = "file:://com.example.hmos.notepad/data/storage/el2/distributedfiles/dir/asset2.jpg",
362         .modifyTime = "modifyTime1",
363         .size = "size1",
364         .hash = "modifyTime1_size1",
365     };
366     ASSERT_EQ(snapshot->IsBoundAsset(asset), false);
367     snapshot->BindAsset(asset, AssetBindInfo_, storeInfo_);
368     ASSERT_EQ(snapshot->IsBoundAsset(asset), true);
369     snapshot->BindAsset(asset, AssetBindInfo_, storeInfo_);
370 }
371 } // namespace OHOS::Test