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 "gtest/gtest.h"
18 #include "log_print.h"
19 #include "rdb_asset_loader.h"
20 #include "rdb_notifier_proxy.h"
21 #include "rdb_watcher.h"
22 #include "store/cursor.h"
23 #include "store/general_value.h"
24 #include "store/general_watcher.h"
25 using namespace OHOS;
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace OHOS::DistributedRdb;
29 using namespace OHOS::DistributedData;
30 using Type = DistributedDB::Type;
31 const DistributedDB::Asset g_rdbAsset = {
32     .version = 1,
33     .name = "Phone",
34     .assetId = "0",
35     .subpath = "/local/sync",
36     .uri = "file://rdbtest/local/sync",
37     .modifyTime = "123456",
38     .createTime = "createTime",
39     .size = "256",
40     .hash = "ASE"
41 };
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 };
66 
67 class RdbWatcherTest : public testing::Test {
68 public:
SetUpTestCase(void)69     static void SetUpTestCase(void){};
TearDownTestCase(void)70     static void TearDownTestCase(void){};
SetUp()71     void SetUp(){};
TearDown()72     void TearDown(){};
73 };
74 
75 /**
76 * @tc.name: Download
77 * @tc.desc: RdbAssetLoader download test.
78 * @tc.type: FUNC
79 * @tc.require:
80 * @tc.author: SQL
81 */
82 HWTEST_F(RdbAssetLoaderTest, Download, TestSize.Level0)
83 {
84     BindAssets bindAssets;
85     std::shared_ptr<MockAssetLoader> cloudAssetLoader = std::make_shared<MockAssetLoader>();
86     DistributedRdb::RdbAssetLoader rdbAssetLoader(cloudAssetLoader, &bindAssets);
87     std::string tableName = "testTable";
88     std::string groupId = "testGroup";
89     Type prefix;
90     std::map<std::string, DistributedDB::Assets> assets;
91     assets["asset1"].push_back(g_rdbAsset);
92     auto result = rdbAssetLoader.Download(tableName, groupId, prefix, assets);
93     EXPECT_EQ(result, DistributedDB::DBStatus::OK);
94 }
95 
96 /**
97 * @tc.name: RemoveLocalAssets
98 * @tc.desc: RdbAssetLoader RemoveLocalAssets test.
99 * @tc.type: FUNC
100 * @tc.require:
101 * @tc.author: SQL
102 */
103 HWTEST_F(RdbAssetLoaderTest, RemoveLocalAssets, TestSize.Level0)
104 {
105     BindAssets bindAssets;
106     std::shared_ptr<MockAssetLoader> cloudAssetLoader = std::make_shared<MockAssetLoader>();
107     DistributedRdb::RdbAssetLoader rdbAssetLoader(cloudAssetLoader, &bindAssets);
108     std::vector<DistributedDB::Asset> assets;
109     assets.push_back(g_rdbAsset);
110     auto result = rdbAssetLoader.RemoveLocalAssets(assets);
111     EXPECT_EQ(result, DistributedDB::DBStatus::OK);
112 }
113 
114 /**
115 * @tc.name: PostEvent
116 * @tc.desc: RdbAssetLoader PostEvent abnormal
117 * @tc.type: FUNC
118 * @tc.require:
119 * @tc.author: SQL
120 */
121 HWTEST_F(RdbAssetLoaderTest, PostEvent, TestSize.Level0)
122 {
123     BindAssets bindAssets;
124     bindAssets.bindAssets = nullptr;
125     std::shared_ptr<AssetLoader> assetLoader = std::make_shared<AssetLoader>();
126     DistributedRdb::RdbAssetLoader rdbAssetLoader(assetLoader, &bindAssets);
127     std::string tableName = "testTable";
128     std::string groupId = "testGroup";
129     Type prefix;
130     std::map<std::string, DistributedDB::Assets> assets;
131     assets["asset1"].push_back(g_rdbAsset);
132     auto result = rdbAssetLoader.Download(tableName, groupId, prefix, assets);
133     EXPECT_EQ(result, DistributedDB::DBStatus::CLOUD_ERROR);
134 }
135 
136 /**
137 * @tc.name: RdbWatcher
138 * @tc.desc: RdbWatcher function test.
139 * @tc.type: FUNC
140 * @tc.require:
141 * @tc.author: SQL
142 */
143 HWTEST_F(RdbWatcherTest, RdbWatcher, TestSize.Level0)
144 {
145     GeneralWatcher::Origin origin;
146     GeneralWatcher::PRIFields primaryFields = {{"primaryFields1", "primaryFields2"}};
147     GeneralWatcher::ChangeInfo values;
148     std::shared_ptr<RdbWatcher> watcher = std::make_shared<RdbWatcher>();
149     sptr<RdbNotifierProxy> notifier;
150     watcher->SetNotifier(notifier);
151     EXPECT_EQ(watcher->notifier_, nullptr);
152     auto result = watcher->OnChange(origin, primaryFields, std::move(values));
153     EXPECT_EQ(result, GeneralError::E_NOT_INIT);
154 }
155 } // namespace DistributedRDBTest
156 } // namespace OHOS::Test