diff --git a/services/distributed/test/unittest/BUILD.gn b/services/distributed/test/unittest/BUILD.gn index 6e816c21b59a977b02bd619e3a7b425a9a96239c..d7286306ed3044fca746ccd8408f82d7ef5fdc34 100644 --- a/services/distributed/test/unittest/BUILD.gn +++ b/services/distributed/test/unittest/BUILD.gn @@ -22,6 +22,7 @@ group("ans_unit_test") { if (distributed_notification_supported) { deps += [ ":ans_distributed_unit_test", + ":distributed_database_branch_test", ":distributed_preferences_database_test", ":distributed_screen_status_manager_branch_test", ] @@ -177,3 +178,49 @@ ohos_unittest("distributed_screen_status_manager_branch_test") { subsystem_name = "${subsystem_name}" part_name = "${component_name}" } + +ohos_unittest("distributed_database_branch_test") { + module_out_path = "${component_name}/unittest" + include_dirs = [ + "/${services_path}/distributed/include", + "//commonlibrary/c_utils/base/include", + "${services_path}/distributed/test/unittest/mock", + ] + + sources = [ + "distributed_database_branch_test/distributed_database_branch_test.cpp", + "distributed_database_branch_test/mock_distributed_flow_control.cpp", + "distributed_database_branch_test/mock_distributed_kv_data_manager.cpp", + "distributed_database_branch_test/mock_single_kv_store.cpp", + ] + + configs = [ + "${services_path}/distributed/:ans_distributed_config", + "${core_path}:public_ans_core_config", + "${frameworks_module_ans_path}:ans_innerkits_public_config", + ] + + deps = [ + "${core_path}:ans_core", + "${services_path}/distributed:libans_distributed", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "c_utils:utils", + "eventhandler:libeventhandler", + "hitrace_native:hitrace_meter", + "hitrace_native:libhitracechain", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "multimedia_image_framework:image_native", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} diff --git a/services/distributed/test/unittest/distributed_database_branch_test/distributed_database_branch_test.cpp b/services/distributed/test/unittest/distributed_database_branch_test/distributed_database_branch_test.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d64f59b21614e73044a149b3357085ee2a667b52 --- /dev/null +++ b/services/distributed/test/unittest/distributed_database_branch_test/distributed_database_branch_test.cpp @@ -0,0 +1,530 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "gtest/gtest.h" +#define private public +#include "distributed_database.h" + +#include "ans_inner_errors.h" +#include "mock_single_kv_store.h" + +extern void mockStartWatchDeviceChange(bool mockRet); +extern void mockGetSingleKvStore(bool mockRet); +extern void mockSubscribeKvStore(bool mockRet); +extern void mockKvStoreFlowControl(bool mockRet); +extern void mockRemoveDeviceData(bool mockRet); +extern void mockKvManagerFlowControl(bool mockRet); +extern void mockGetLocalDevice(bool mockRet); +extern void mockGetDeviceList(bool mockRet); + +using namespace testing::ext; +using namespace OHOS::DistributedKv; +namespace OHOS { +namespace Notification { +class DistributedDatabaseBranchTest : public testing::Test { +public: + void SetUp() override; + void TearDown() override; + +public: + virtual void OnInsert(const std::string &deviceId, const std::string &key, const std::string &value); + virtual void OnUpdate(const std::string &deviceId, const std::string &key, const std::string &value); + virtual void OnDelete(const std::string &deviceId, const std::string &key, const std::string &value); + virtual void OnConnected(const std::string &deviceId); + virtual void OnDisconnected(const std::string &deviceId); + +protected: + std::shared_ptr database_; + std::shared_ptr databaseCallback_; + std::shared_ptr deviceCallback_; +}; + +void DistributedDatabaseBranchTest::SetUp() +{ + DistributedDatabaseCallback::IDatabaseChange databaseCallback = { + .OnInsert = std::bind(&DistributedDatabaseBranchTest::OnInsert, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + .OnUpdate = std::bind(&DistributedDatabaseBranchTest::OnUpdate, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + .OnDelete = std::bind(&DistributedDatabaseBranchTest::OnDelete, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + }; + DistributedDeviceCallback::IDeviceChange deviceCallback = { + .OnConnected = std::bind(&DistributedDatabaseBranchTest::OnConnected, this, std::placeholders::_1), + .OnDisconnected = std::bind(&DistributedDatabaseBranchTest::OnDisconnected, this, std::placeholders::_1), + }; + + databaseCallback_ = std::make_shared(databaseCallback); + deviceCallback_ = std::make_shared(deviceCallback); + database_ = std::make_shared(databaseCallback_, deviceCallback_); + database_->OnDeviceConnected(); +} + +void DistributedDatabaseBranchTest::TearDown() +{ + database_ = nullptr; + databaseCallback_ = nullptr; + deviceCallback_ = nullptr; +} + +void DistributedDatabaseBranchTest::OnInsert(const std::string &deviceId, const std::string &key, const std::string &value) +{} + +void DistributedDatabaseBranchTest::OnUpdate(const std::string &deviceId, const std::string &key, const std::string &value) +{} + +void DistributedDatabaseBranchTest::OnDelete(const std::string &deviceId, const std::string &key, const std::string &value) +{} + +void DistributedDatabaseBranchTest::OnConnected(const std::string &deviceId) +{} + +void DistributedDatabaseBranchTest::OnDisconnected(const std::string &deviceId) +{} + +/** + * @tc.name : DistributedDatabaseBranchTest_0100 + * @tc.number : DistributedDatabaseBranchTest_0100 + * @tc.desc : Test CheckKvDataManager function and kvDataManager_ == nullptr. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_0100, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = nullptr; + mockStartWatchDeviceChange(false); + EXPECT_EQ(false, database_->CheckKvDataManager()); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_0200 + * @tc.number : DistributedDatabaseBranchTest_0200 + * @tc.desc : Test GetKvStore function and CheckKvDataManager is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_0200, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = nullptr; + mockStartWatchDeviceChange(false); + database_->GetKvStore(); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_0300 + * @tc.number : DistributedDatabaseBranchTest_0300 + * @tc.desc : Test GetKvStore function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_0300, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = std::make_unique(); + mockGetSingleKvStore(false); + database_->GetKvStore(); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_0400 + * @tc.number : DistributedDatabaseBranchTest_0400 + * @tc.desc : Test GetKvStore function and kvStore_ == nullptr. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_0400, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = std::make_unique(); + mockGetSingleKvStore(true); + database_->kvStore_ = nullptr; + database_->GetKvStore(); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_0500 + * @tc.number : DistributedDatabaseBranchTest_0500 + * @tc.desc : Test GetKvStore function and SubscribeKvStore != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_0500, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = std::make_unique(); + // set GetSingleKvStore is Status::SUCCESS + mockGetSingleKvStore(true); + // set kvStore_ is not nullptr + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + // set SubscribeKvStore != Status::SUCCESS + mockSubscribeKvStore(false); + database_->GetKvStore(); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_0600 + * @tc.number : DistributedDatabaseBranchTest_0600 + * @tc.desc : Test CheckKvStore function and kvStore_ != nullptr. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_0600, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + EXPECT_EQ(true, database_->CheckKvStore()); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_0700 + * @tc.number : DistributedDatabaseBranchTest_0700 + * @tc.desc : Test CheckKvStore function and kvStore_ == nullptr. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_0700, Function | SmallTest | Level1) +{ + database_->kvStore_ = nullptr; + EXPECT_EQ(false, database_->CheckKvStore()); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_0800 + * @tc.number : DistributedDatabaseBranchTest_0800 + * @tc.desc : Test PutToDistributedDB function and KvStoreFlowControl is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_0800, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(false); + std::string key = ""; + std::string value = ""; + EXPECT_EQ(false, database_->PutToDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_0900 + * @tc.number : DistributedDatabaseBranchTest_0900 + * @tc.desc : Test PutToDistributedDB function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_0900, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(true); + std::string key = ""; + std::string value = ""; + EXPECT_EQ(false, database_->PutToDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_1000 + * @tc.number : DistributedDatabaseBranchTest_1000 + * @tc.desc : Test GetFromDistributedDB function and kvStore_ == nullptr. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_1000, Function | SmallTest | Level1) +{ + database_->kvStore_ = nullptr; + std::string key = ""; + std::string value = ""; + EXPECT_EQ(false, database_->GetFromDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_1100 + * @tc.number : DistributedDatabaseBranchTest_1100 + * @tc.desc : Test GetFromDistributedDB function and KvStoreFlowControl is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_1100, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(false); + std::string key = ""; + std::string value = ""; + EXPECT_EQ(false, database_->GetFromDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_1200 + * @tc.number : DistributedDatabaseBranchTest_1200 + * @tc.desc : Test GetFromDistributedDB function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_1200, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(true); + std::string key = ""; + std::string value = ""; + EXPECT_EQ(false, database_->GetFromDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_1300 + * @tc.number : DistributedDatabaseBranchTest_1300 + * @tc.desc : Test GetEntriesFromDistributedDB function and kvStore_ == nullptr. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_1300, Function | SmallTest | Level1) +{ + database_->kvStore_ = nullptr; + std::string prefixKey = ""; + Entry entry; + std::vector entries; + entries.emplace_back(entry); + EXPECT_EQ(false, database_->GetEntriesFromDistributedDB(prefixKey, entries)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_1400 + * @tc.number : DistributedDatabaseBranchTest_1400 + * @tc.desc : Test GetEntriesFromDistributedDB function and KvStoreFlowControl is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_1400, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(false); + std::string prefixKey = ""; + Entry entry; + std::vector entries; + entries.emplace_back(entry); + EXPECT_EQ(false, database_->GetEntriesFromDistributedDB(prefixKey, entries)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_1500 + * @tc.number : DistributedDatabaseBranchTest_1500 + * @tc.desc : Test GetEntriesFromDistributedDB function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_1500, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(true); + std::string prefixKey = ""; + Entry entry; + std::vector entries; + entries.emplace_back(entry); + EXPECT_EQ(false, database_->GetEntriesFromDistributedDB(prefixKey, entries)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_1600 + * @tc.number : DistributedDatabaseBranchTest_1600 + * @tc.desc : Test DeleteToDistributedDB function and KvStoreFlowControl is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_1600, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(false); + std::string key = ""; + EXPECT_EQ(false, database_->DeleteToDistributedDB(key)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_1700 + * @tc.number : DistributedDatabaseBranchTest_1700 + * @tc.desc : Test DeleteToDistributedDB function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_1700, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(true); + std::string key = ""; + EXPECT_EQ(false, database_->DeleteToDistributedDB(key)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_1800 + * @tc.number : DistributedDatabaseBranchTest_1800 + * @tc.desc : Test ClearDataByDevice function and kvStore_ == nullptr. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_1800, Function | SmallTest | Level1) +{ + database_->kvStore_ = nullptr; + std::string deviceId = ""; + EXPECT_EQ(false, database_->ClearDataByDevice(deviceId)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_1900 + * @tc.number : DistributedDatabaseBranchTest_1900 + * @tc.desc : Test ClearDataByDevice function and KvStoreFlowControl is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_1900, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(false); + std::string deviceId = ""; + EXPECT_EQ(false, database_->ClearDataByDevice(deviceId)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_2000 + * @tc.number : DistributedDatabaseBranchTest_2000 + * @tc.desc : Test ClearDataByDevice function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_2000, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(true); + mockRemoveDeviceData(false); + std::string deviceId = ""; + EXPECT_EQ(false, database_->ClearDataByDevice(deviceId)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_2100 + * @tc.number : DistributedDatabaseBranchTest_2100 + * @tc.desc : Test ClearDataByDevice function and status == DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_2100, Function | SmallTest | Level1) +{ + std::shared_ptr kvStore = std::make_shared(); + database_->kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(true); + mockRemoveDeviceData(true); + std::string deviceId = ""; + EXPECT_EQ(true, database_->ClearDataByDevice(deviceId)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_2200 + * @tc.number : DistributedDatabaseBranchTest_2200 + * @tc.desc : Test GetLocalDeviceId function and CheckKvDataManager is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_2200, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = nullptr; + std::string deviceId = ""; + EXPECT_EQ(false, database_->GetLocalDeviceId(deviceId)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_2300 + * @tc.number : DistributedDatabaseBranchTest_2300 + * @tc.desc : Test GetLocalDeviceId function and localDeviceId_ is empty. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_2300, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = std::make_unique(); + mockKvManagerFlowControl(false); + std::string deviceId = ""; + EXPECT_EQ(false, database_->GetLocalDeviceId(deviceId)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_2400 + * @tc.number : DistributedDatabaseBranchTest_2400 + * @tc.desc : Test GetLocalDeviceInfo function and CheckKvDataManager is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_2400, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = nullptr; + DeviceInfo localInfo; + EXPECT_EQ(false, database_->GetLocalDeviceInfo(localInfo)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_2500 + * @tc.number : DistributedDatabaseBranchTest_2500 + * @tc.desc : Test GetLocalDeviceInfo function and KvManagerFlowControl is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_2500, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = std::make_unique(); + mockKvManagerFlowControl(false); + DeviceInfo localInfo; + EXPECT_EQ(false, database_->GetLocalDeviceInfo(localInfo)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_2600 + * @tc.number : DistributedDatabaseBranchTest_2600 + * @tc.desc : Test GetLocalDeviceInfo function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_2600, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = std::make_unique(); + mockKvManagerFlowControl(true); + mockGetLocalDevice(false); + DeviceInfo localInfo; + EXPECT_EQ(false, database_->GetLocalDeviceInfo(localInfo)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_2700 + * @tc.number : DistributedDatabaseBranchTest_2700 + * @tc.desc : Test GetDeviceInfoList function and CheckKvDataManager is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_2700, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = nullptr; + std::vector deviceList; + EXPECT_EQ(false, database_->GetDeviceInfoList(deviceList)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_2800 + * @tc.number : DistributedDatabaseBranchTest_2800 + * @tc.desc : Test GetDeviceInfoList function and KvManagerFlowControl is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_2800, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = std::make_unique(); + mockKvManagerFlowControl(false); + std::vector deviceList; + EXPECT_EQ(false, database_->GetDeviceInfoList(deviceList)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_2900 + * @tc.number : DistributedDatabaseBranchTest_2900 + * @tc.desc : Test GetDeviceInfoList function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_2900, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = std::make_unique(); + mockKvManagerFlowControl(true); + mockGetDeviceList(false); + std::vector deviceList; + EXPECT_EQ(false, database_->GetDeviceInfoList(deviceList)); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_3000 + * @tc.number : DistributedDatabaseBranchTest_3000 + * @tc.desc : Test RecreateDistributedDB function and CheckKvDataManager is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_3000, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = nullptr; + EXPECT_EQ(false, database_->RecreateDistributedDB()); +} + +/** + * @tc.name : DistributedDatabaseBranchTest_3100 + * @tc.number : DistributedDatabaseBranchTest_3100 + * @tc.desc : Test RecreateDistributedDB function and KvManagerFlowControl is false. + */ +HWTEST_F(DistributedDatabaseBranchTest, DistributedDatabaseBranchTest_3100, Function | SmallTest | Level1) +{ + database_->kvDataManager_ = std::make_unique(); + mockKvManagerFlowControl(false); + EXPECT_EQ(false, database_->RecreateDistributedDB()); +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_database_branch_test/mock_distributed_flow_control.cpp b/services/distributed/test/unittest/distributed_database_branch_test/mock_distributed_flow_control.cpp new file mode 100755 index 0000000000000000000000000000000000000000..988bcd2492226026ffcad87568fc3a0015fb3b94 --- /dev/null +++ b/services/distributed/test/unittest/distributed_database_branch_test/mock_distributed_flow_control.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "distributed_flow_control.h" + +namespace { + bool g_mockKvStoreFlowControlRet = true; + bool g_mockKvManagerFlowControlRet = true; +} + +void mockKvStoreFlowControl(bool mockRet) +{ + g_mockKvStoreFlowControlRet = mockRet; +} + +void mockKvManagerFlowControl(bool mockRet) +{ + g_mockKvManagerFlowControlRet = mockRet; +} + +namespace OHOS { +namespace Notification { +DistributedFlowControl::DistributedFlowControl( + size_t kvManagerSecondMaxinum, size_t kvManagerMinuteMaxinum, size_t kvStoreSecondMaxinum, + size_t kvStoreMinuteMaxinum) + : kvManagerSecondMaxinum_(kvManagerSecondMaxinum), + kvManagerMinuteMaxinum_(kvManagerMinuteMaxinum), + kvStoreSecondMaxinum_(kvStoreSecondMaxinum), + kvStoreMinuteMaxinum_(kvStoreMinuteMaxinum) +{} + +bool DistributedFlowControl::KvManagerFlowControl(void) +{ + return g_mockKvManagerFlowControlRet; +} + +bool DistributedFlowControl::KvStoreFlowControl(void) +{ + return g_mockKvStoreFlowControlRet; +} +} // namespace Notification +} // namespace OHOS diff --git a/services/distributed/test/unittest/distributed_database_branch_test/mock_distributed_kv_data_manager.cpp b/services/distributed/test/unittest/distributed_database_branch_test/mock_distributed_kv_data_manager.cpp new file mode 100755 index 0000000000000000000000000000000000000000..cb6167078a6f5125a27e89ee4ceae7787c55d487 --- /dev/null +++ b/services/distributed/test/unittest/distributed_database_branch_test/mock_distributed_kv_data_manager.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "distributed_kv_data_manager.h" + +#include "mock_single_kv_store.h" + +namespace { + bool g_mockStartWatchDeviceChangeRet = true; + bool g_mockGetSingleKvStoreRet = true; + bool g_mockGetLocalDeviceRet = true; + bool g_mockGetDeviceListRet = true; +} + +void mockStartWatchDeviceChange(bool mockRet) +{ + g_mockStartWatchDeviceChangeRet = mockRet; +} + +void mockGetSingleKvStore(bool mockRet) +{ + g_mockGetSingleKvStoreRet = mockRet; +} + +void mockGetLocalDevice(bool mockRet) +{ + g_mockGetLocalDeviceRet = mockRet; +} + +void mockGetDeviceList(bool mockRet) +{ + g_mockGetDeviceListRet = mockRet; +} + +namespace OHOS { +namespace DistributedKv { +DistributedKvDataManager::DistributedKvDataManager() +{} + +DistributedKvDataManager::~DistributedKvDataManager() +{} + +Status DistributedKvDataManager::GetDeviceList(std::vector &deviceInfoList, DeviceFilterStrategy strategy) +{ + if (false == g_mockGetDeviceListRet) { + return Status::INVALID_ARGUMENT; + } + return Status::SUCCESS; +} + +Status DistributedKvDataManager::GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, + std::shared_ptr &singleKvStore) +{ + if (false == g_mockGetSingleKvStoreRet) { + return Status::INVALID_ARGUMENT; + } + return Status::SUCCESS; +} + +Status DistributedKvDataManager::StartWatchDeviceChange(std::shared_ptr observer) +{ + if (false == g_mockStartWatchDeviceChangeRet) { + return Status::INVALID_ARGUMENT; + } + return Status::SUCCESS; +} + +Status DistributedKvDataManager::GetLocalDevice(DeviceInfo &localDevice) +{ + if (false == g_mockGetLocalDeviceRet) { + return Status::INVALID_ARGUMENT; + } + return Status::SUCCESS; +} +} // namespace DistributedKv +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_database_branch_test/mock_single_kv_store.cpp b/services/distributed/test/unittest/distributed_database_branch_test/mock_single_kv_store.cpp new file mode 100755 index 0000000000000000000000000000000000000000..3185c841e1458ae2d2fdb5983937d6a5c7bb54bc --- /dev/null +++ b/services/distributed/test/unittest/distributed_database_branch_test/mock_single_kv_store.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mock_single_kv_store.h" +#include "types.h" + +namespace { + bool g_mockSubscribeKvStoreRet = true; + bool g_mockRemoveDeviceDataRet = true; +} + +void mockSubscribeKvStore(bool mockRet) +{ + g_mockSubscribeKvStoreRet = mockRet; +} + +void mockRemoveDeviceData(bool mockRet) +{ + g_mockRemoveDeviceDataRet = mockRet; +} + +namespace OHOS { +namespace DistributedKv { +Status MockSingleKvStore::GetEntries(const Key &prefixKey, std::vector &entries) const +{ + return Status::INVALID_ARGUMENT; +} + +Status MockSingleKvStore::GetEntries(const DataQuery &query, std::vector &entries) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetResultSet(const Key &prefixKey, std::shared_ptr &resultSet) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetResultSet( + const DataQuery &query, std::shared_ptr &resultSet) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::CloseResultSet(std::shared_ptr &resultSet) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetCount(const DataQuery &query, int &result) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::RemoveDeviceData(const std::string &device) +{ + if (false == g_mockRemoveDeviceDataRet) { + return Status::INVALID_ARGUMENT; + } + return Status::SUCCESS; +} + +StoreId MockSingleKvStore::GetStoreId() const +{ + StoreId storeId; + storeId.storeId = ""; + return storeId; +} + +Status MockSingleKvStore::Delete(const Key &key) +{ + return Status::INVALID_ARGUMENT; +} + +Status MockSingleKvStore::Put(const Key &key, const Value &value) +{ + return Status::INVALID_ARGUMENT; +} + +Status MockSingleKvStore::Get(const Key &key, Value &value) +{ + return Status::INVALID_ARGUMENT; +} + +Status MockSingleKvStore::SubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) +{ + if (false == g_mockSubscribeKvStoreRet) { + return Status::INVALID_ARGUMENT; + } + return Status::SUCCESS; +} + +Status MockSingleKvStore::UnSubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::RegisterSyncCallback(std::shared_ptr callback) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::UnRegisterSyncCallback() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::PutBatch(const std::vector &entries) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::DeleteBatch(const std::vector &keys) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::StartTransaction() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Commit() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Rollback() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SetSyncParam(const KvSyncParam &syncParam) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetSyncParam(KvSyncParam &syncParam) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SetCapabilityEnabled(bool enabled) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SetCapabilityRange( + const std::vector &localLabels, const std::vector &remoteSupportLabels) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetSecurityLevel(SecurityLevel &securityLevel) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Sync(const std::vector &deviceIds, SyncMode mode, + const DataQuery &query, std::shared_ptr syncCallback) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::UnsubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Backup(const std::string &file, const std::string &baseDir) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Restore(const std::string &file, const std::string &baseDir) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::DeleteBackup( + const std::vector &files, const std::string &baseDir, + std::map &status) +{ + return Status::SUCCESS; +} +} // namespace DistributedKv +} // namespace OHOS