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