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 "ObjectAssetMachineTest"
17 
18 #include "object_asset_machine.h"
19 
20 #include <gtest/gtest.h>
21 
22 #include "snapshot/machine_status.h"
23 #include "object_asset_loader.h"
24 #include "executor_pool.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::DistributedObject;
28 using namespace OHOS::DistributedData;
29 namespace OHOS::Test {
30 
31 class ObjectAssetMachineTest : public testing::Test {
32 public:
33     void SetUp();
34     void TearDown();
35 
36 protected:
37     AssetBindInfo AssetBindInfo_;
38     Asset asset_;
39     std::string uri_;
40     std::string bundleName_ = "test_bundleName";
41     std::map<std::string, ChangedAssetInfo> changedAssets_;
42     std::string sessionId = "123";
43     StoreInfo storeInfo_;
44     std::shared_ptr<ObjectAssetMachine> machine;
45 };
46 
SetUp()47 void ObjectAssetMachineTest::SetUp()
48 {
49     uri_ = "file:://com.huawei.hmos.notepad/data/storage/el2/distributedfiles/dir/asset1.jpg";
50     Asset asset{
51         .name = "test_name",
52         .uri = uri_,
53         .modifyTime = "modifyTime",
54         .size = "size",
55         .hash = "modifyTime_size",
56     };
57     asset_ = asset;
58 
59     VBucket vBucket{ { "11", 111 } };
60     AssetBindInfo AssetBindInfo{
61         .storeName = "store_test",
62         .tableName = "table_test",
63         .primaryKey = vBucket,
64         .field = "attachment",
65         .assetName = "asset1.jpg",
66     };
67     AssetBindInfo_ = AssetBindInfo;
68     StoreInfo storeInfo {
69         .tokenId = 0,
70         .bundleName = "bundleName_test",
71         .storeName = "store_test",
72         .instanceId = 1,
73         .user = 100,
74     };
75     storeInfo_ = storeInfo;
76     ChangedAssetInfo changedAssetInfo(asset, AssetBindInfo, storeInfo);
77     changedAssets_[uri_] = changedAssetInfo;
78     if (machine == nullptr) {
79         machine = std::make_shared<ObjectAssetMachine>();
80         auto executors = std::make_shared<ExecutorPool>(2, 1);
81         ObjectAssetLoader::GetInstance()->SetThreadPool(executors);
82     }
83 }
84 
TearDown()85 void ObjectAssetMachineTest::TearDown() {}
86 
87 /**
88 * @tc.name: StatusTransfer001
89 * @tc.desc: Remote changed when transferring.
90 * @tc.type: FUNC
91 * @tc.require:
92 * @tc.author: whj
93 */
94 HWTEST_F(ObjectAssetMachineTest, StatusTransfer001, TestSize.Level0)
95 {
96     Asset asset{
97         .name = "test_name",
98         .uri = uri_,
99         .modifyTime = "modifyTime2",
100         .size = "size2",
101         .hash = "modifyTime2_size2",
102     };
103     std::pair<std::string, Asset> changedAsset{ "device_2", asset };
104     changedAssets_[uri_].status = STATUS_TRANSFERRING;
105     machine->DFAPostEvent(REMOTE_CHANGED, changedAssets_[uri_], asset, changedAsset);
106     ASSERT_EQ(changedAssets_[uri_].status, STATUS_WAIT_TRANSFER);
107     ASSERT_EQ(changedAssets_[uri_].deviceId, changedAsset.first);
108     ASSERT_EQ(changedAssets_[uri_].asset.hash, asset.hash);
109 }
110 
111 /**
112 * @tc.name: StatusTransfer002
113 * @tc.desc: Transfer finished.
114 * @tc.type: FUNC
115 * @tc.require:
116 * @tc.author: whj
117 */
118 HWTEST_F(ObjectAssetMachineTest, StatusTransfer002, TestSize.Level0)
119 {
120     Asset asset{
121         .name = "test_name",
122         .uri = uri_,
123         .modifyTime = "modifyTime2",
124         .size = "size2",
125         .hash = "modifyTime2_size2",
126     };
127     std::pair<std::string, Asset> changedAsset{ "device_2", asset };
128     changedAssets_[uri_].status = STATUS_TRANSFERRING;
129     machine->DFAPostEvent(TRANSFER_FINISHED, changedAssets_[uri_], asset, changedAsset);
130     ASSERT_EQ(changedAssets_[uri_].status, STATUS_STABLE);
131 }
132 
133 /**
134 * @tc.name: StatusTransfer003
135 * @tc.desc: Transfer event
136 * @tc.type: FUNC
137 * @tc.require:
138 * @tc.author: nhj
139 */
140 HWTEST_F(ObjectAssetMachineTest, StatusTransfer003, TestSize.Level0)
141 {
142     Asset asset{
143         .name = "test_name",
144         .uri = uri_,
145         .modifyTime = "modifyTime1",
146         .size = "size1",
147         .hash = "modifyTime1_size1",
148     };
149     std::pair<std::string, Asset> changedAsset{ "device_2", asset };
150     changedAssets_[uri_].status = STATUS_UPLOADING;
151     machine->DFAPostEvent(REMOTE_CHANGED, changedAssets_[uri_], asset, changedAsset);
152     ASSERT_EQ(changedAssets_[uri_].status, STATUS_WAIT_TRANSFER);
153     ASSERT_EQ(changedAssets_[uri_].deviceId, changedAsset.first);
154     ASSERT_EQ(changedAssets_[uri_].asset.hash, asset.hash);
155 }
156 
157 /**
158 * @tc.name: StatusUpload001
159 * @tc.desc: No conflict scenarios: normal cloud sync.
160 * @tc.type: FUNC
161 * @tc.require:
162 * @tc.author: whj
163 */
164 HWTEST_F(ObjectAssetMachineTest, StatusUpload001, TestSize.Level0)
165 {
166     Asset asset{
167         .name = "test_name",
168         .uri = uri_,
169         .modifyTime = "modifyTime1",
170         .size = "size1",
171         .hash = "modifyTime1_size1",
172     };
173     std::pair<std::string, Asset> changedAsset{ "device_1", asset };
174     machine->DFAPostEvent(UPLOAD, changedAssets_[uri_], asset, changedAsset);
175     ASSERT_EQ(changedAssets_[uri_].status, STATUS_UPLOADING);
176 
177     machine->DFAPostEvent(UPLOAD_FINISHED, changedAssets_[uri_], asset);
178     ASSERT_EQ(changedAssets_[uri_].status, STATUS_STABLE);
179 }
180 
181 /**
182 * @tc.name: StatusUpload002
183 * @tc.desc: Conflict scenario: Upload before transfer.
184 * @tc.type: FUNC
185 * @tc.require:
186 * @tc.author: whj
187 */
188 HWTEST_F(ObjectAssetMachineTest, StatusUpload002, TestSize.Level0)
189 {
190     auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
191     auto timestamp = std::to_string(time);
192     Asset asset{
193         .name = "name_" + timestamp,
194         .uri = "uri_" + timestamp,
195         .modifyTime = "modifyTime_" + timestamp,
196         .size = "size_" + timestamp,
197         .hash = "modifyTime_size_" + timestamp,
198     };
199     AssetBindInfo bindInfo{
200         .storeName = "store_" + timestamp,
201         .tableName = "table_" + timestamp,
202         .primaryKey = {{ "key_" + timestamp, "value_" + timestamp }},
203         .field = "attachment_" + timestamp,
204         .assetName = "asset_" + timestamp + ".jpg",
205     };
206     StoreInfo storeInfo {
207         .tokenId = time,
208         .bundleName = "bundleName_" + timestamp,
209         .storeName = "store_" + timestamp,
210         .instanceId = time,
211         .user = time,
212     };
213     ChangedAssetInfo changedAssetInfo(asset, bindInfo, storeInfo);
214     std::pair<std::string, Asset> changedAsset{ "device_" + timestamp, asset };
215 
216     machine->DFAPostEvent(UPLOAD, changedAssetInfo, asset);
217     ASSERT_EQ(changedAssetInfo.status, STATUS_UPLOADING);
218 
219     machine->DFAPostEvent(REMOTE_CHANGED, changedAssetInfo, asset, changedAsset);
220     ASSERT_EQ(changedAssetInfo.status, STATUS_WAIT_TRANSFER);
221     ASSERT_EQ(changedAssetInfo.asset.hash, asset.hash);
222 
223     machine->DFAPostEvent(UPLOAD_FINISHED, changedAssetInfo, asset);
224     ASSERT_EQ(changedAssetInfo.status, STATUS_TRANSFERRING);
225 }
226 
227 /**
228 * @tc.name: StatusDownload001
229 * @tc.desc: No conflict scenarios: normal cloud sync.
230 * @tc.type: FUNC
231 * @tc.require:
232 * @tc.author: nhj
233 */
234 HWTEST_F(ObjectAssetMachineTest, StatusDownload001, TestSize.Level0)
235 {
236     Asset asset{
237         .name = "name_006",
238         .uri = "uri_006",
239         .modifyTime = "modifyTime_006",
240         .size = "size_006",
241         .hash = "modifyTime_006_size_006",
242     };
243     AssetBindInfo AssetBindInfo{
244         .storeName = "store_006",
245         .tableName = "table_006",
246         .primaryKey = {{ "006", "006" }},
247         .field = "attachment_006",
248         .assetName = "asset_006.jpg",
249     };
250     StoreInfo storeInfo {
251         .tokenId = 600,
252         .bundleName = "bundleName_006",
253         .storeName = "store_006",
254         .instanceId = 600,
255         .user = 600,
256     };
257     ChangedAssetInfo changedAssetInfo(asset, AssetBindInfo, storeInfo);
258     std::pair<std::string, Asset> changedAsset{ "device_006", asset };
259 
260     machine->DFAPostEvent(DOWNLOAD, changedAssetInfo, asset, changedAsset);
261     ASSERT_EQ(changedAssetInfo.status, STATUS_DOWNLOADING);
262 
263     machine->DFAPostEvent(DOWNLOAD_FINISHED, changedAssetInfo, asset);
264     ASSERT_EQ(changedAssetInfo.status, STATUS_STABLE);
265 }
266 } // namespace OHOS::Test