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