1 /*
2  * Copyright (c) 2022 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 #include <memory>
17 
18 #include "gtest/gtest.h"
19 
20 #define private public
21 #include "distributed_database.h"
22 #include "distributed_preferences.h"
23 
24 using namespace testing::ext;
25 namespace OHOS {
26 namespace Notification {
27 class DistributedDatabaseTest : public testing::Test {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 public:
33     virtual void OnInsert(const std::string &deviceId, const std::string &key, const std::string &value);
34     virtual void OnUpdate(const std::string &deviceId, const std::string &key, const std::string &value);
35     virtual void OnDelete(const std::string &deviceId, const std::string &key, const std::string &value);
36     virtual void OnConnected(const std::string &deviceId);
37     virtual void OnDisconnected(const std::string &deviceId);
38 
39 protected:
40     std::shared_ptr<DistributedDatabase> database_;
41     std::shared_ptr<DistributedDatabaseCallback> databaseCallback_;
42     std::shared_ptr<DistributedDeviceCallback> deviceCallback_;
43 };
44 
SetUp()45 void DistributedDatabaseTest::SetUp()
46 {
47     DistributedPreferences::GetInstance()->SetDistributedEnable(true);
48     DistributedDatabaseCallback::IDatabaseChange databaseCallback = {
49         .OnInsert = std::bind(&DistributedDatabaseTest::OnInsert,
50             this,
51             std::placeholders::_1,
52             std::placeholders::_2,
53             std::placeholders::_3),
54         .OnUpdate = std::bind(&DistributedDatabaseTest::OnUpdate,
55             this,
56             std::placeholders::_1,
57             std::placeholders::_2,
58             std::placeholders::_3),
59         .OnDelete = std::bind(&DistributedDatabaseTest::OnDelete,
60             this,
61             std::placeholders::_1,
62             std::placeholders::_2,
63             std::placeholders::_3),
64     };
65     DistributedDeviceCallback::IDeviceChange deviceCallback = {
66         .OnConnected = std::bind(&DistributedDatabaseTest::OnConnected, this, std::placeholders::_1),
67         .OnDisconnected = std::bind(&DistributedDatabaseTest::OnDisconnected, this, std::placeholders::_1),
68     };
69 
70     databaseCallback_ = std::make_shared<DistributedDatabaseCallback>(databaseCallback);
71     deviceCallback_ = std::make_shared<DistributedDeviceCallback>(deviceCallback);
72     database_ = std::make_shared<DistributedDatabase>(databaseCallback_, deviceCallback_);
73     database_->OnDeviceConnected();
74 }
75 
TearDown()76 void DistributedDatabaseTest::TearDown()
77 {
78     database_ = nullptr;
79     databaseCallback_ = nullptr;
80     deviceCallback_ = nullptr;
81 }
82 
OnInsert(const std::string & deviceId,const std::string & key,const std::string & value)83 void DistributedDatabaseTest::OnInsert(const std::string &deviceId, const std::string &key, const std::string &value)
84 {}
85 
OnUpdate(const std::string & deviceId,const std::string & key,const std::string & value)86 void DistributedDatabaseTest::OnUpdate(const std::string &deviceId, const std::string &key, const std::string &value)
87 {}
88 
OnDelete(const std::string & deviceId,const std::string & key,const std::string & value)89 void DistributedDatabaseTest::OnDelete(const std::string &deviceId, const std::string &key, const std::string &value)
90 {}
91 
OnConnected(const std::string & deviceId)92 void DistributedDatabaseTest::OnConnected(const std::string &deviceId)
93 {}
94 
OnDisconnected(const std::string & deviceId)95 void DistributedDatabaseTest::OnDisconnected(const std::string &deviceId)
96 {}
97 
98 /**
99  * @tc.name      : DistributedDatabase_PutToDistributedDB_00100
100  * @tc.number    : PutToDistributedDB_00100
101  * @tc.desc      : Put a key-value.
102  */
103 HWTEST_F(DistributedDatabaseTest, PutToDistributedDB_00100, Function | SmallTest | Level1)
104 {
105     // text OnChange function
106     std::vector<DistributedKv::Entry> insertEntries;
107     std::vector<DistributedKv::Entry> updateEntries;
108     std::vector<DistributedKv::Entry> deleteEntries;
109     DistributedKv::ChangeNotification change(
110             std::move(insertEntries), std::move(updateEntries), std::move(deleteEntries), "<remoteDeviceId>", false);
111     databaseCallback_->OnChange(change);
112 
113     // text OnDeviceOnline、OnDeviceOffline function
114     DistributedHardware::DmDeviceInfo deviceInfo;
115     deviceCallback_->OnDeviceOnline(deviceInfo);
116     deviceCallback_->OnDeviceOffline(deviceInfo);
117 
118     // text PutToDistributedDB function
119     std::string key("<key>");
120     std::string value("<value>");
121 
122     EXPECT_EQ(database_->PutToDistributedDB(key, value), true);
123 }
124 
125 /**
126  * @tc.name      : DistributedDatabase_GetFromDistributedDB_00100
127  * @tc.number    : GetFromDistributedDB_00100
128  * @tc.desc      : Get value by its key.
129  */
130 HWTEST_F(DistributedDatabaseTest, GetFromDistributedDB_00100, Function | SmallTest | Level1)
131 {
132     std::string key("<key>");
133     std::string value;
134 
135     EXPECT_EQ(database_->GetFromDistributedDB(key, value), true);
136 }
137 
138 /**
139  * @tc.name      : DistributedDatabase_GetFromDistributedDB_00200
140  * @tc.number    : GetFromDistributedDB_00200
141  * @tc.desc      : Get all entries which key start with prefixKey.
142  */
143 HWTEST_F(DistributedDatabaseTest, GetFromDistributedDB_00200, Function | SmallTest | Level1)
144 {
145     std::string prifixKey("<");
146     std::vector<DistributedDatabase::Entry> entries;
147 
148     EXPECT_EQ(database_->GetEntriesFromDistributedDB(prifixKey, entries), true);
149 }
150 
151 /**
152  * @tc.name      : DistributedDatabase_DeleteToDistributedDB_00100
153  * @tc.number    : DeleteToDistributedDB_00100
154  * @tc.desc      : Delete a key-value with its key.
155  */
156 HWTEST_F(DistributedDatabaseTest, DeleteToDistributedDB_00100, Function | SmallTest | Level1)
157 {
158     std::string key("<key>");
159 
160     EXPECT_EQ(database_->DeleteToDistributedDB(key), true);
161 }
162 
163 /**
164  * @tc.name      : DistributedDatabase_ClearDataByDevice_00100
165  * @tc.number    : ClearDataByDevice_00100
166  * @tc.desc      : Remove the device data from remote.
167  */
168 HWTEST_F(DistributedDatabaseTest, ClearDataByDevice_00100, Function | SmallTest | Level1)
169 {
170     std::string deviceId("<remoteDeviceId>");
171 
172     EXPECT_EQ(database_->ClearDataByDevice(deviceId), false);
173 }
174 
175 /**
176  * @tc.name      : DistributedDatabase_GetLocalDeviceId_00100
177  * @tc.number    : GetLocalDeviceId_00100
178  * @tc.desc      : Get local device id.
179  */
180 HWTEST_F(DistributedDatabaseTest, GetLocalDeviceId_00100, Function | SmallTest | Level1)
181 {
182     std::string deviceId;
183 
184     EXPECT_EQ(database_->GetLocalDeviceId(deviceId), true);
185 }
186 
187 /**
188  * @tc.name      : DistributedDatabase_GetLocalDeviceInfo_00100
189  * @tc.number    : GetLocalDeviceInfo_00100
190  * @tc.desc      : Get local device information.
191  */
192 HWTEST_F(DistributedDatabaseTest, GetLocalDeviceInfo_00100, Function | SmallTest | Level1)
193 {
194     DistributedDatabase::DeviceInfo deviceInfo;
195 
196     EXPECT_EQ(database_->GetLocalDeviceInfo(deviceInfo), true);
197 }
198 
199 /**
200  * @tc.name      : DistributedDatabase_GetDeviceInfoList_00100
201  * @tc.number    : GetDeviceInfoList_00100
202  * @tc.desc      : Get informations for all devices.
203  */
204 HWTEST_F(DistributedDatabaseTest, GetDeviceInfoList_00100, Function | SmallTest | Level1)
205 {
206     std::vector<DistributedDatabase::DeviceInfo> deviceInfos;
207 
208     EXPECT_EQ(database_->GetDeviceInfoList(deviceInfos), true);
209 }
210 }  // namespace Notification
211 }  // namespace OHOS
212