diff --git a/services/distributedhardwarefwkservice/test/unittest/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/BUILD.gn index 7204caa3fa8bf6e96fc5d61cbdd6cccd57ab2365..eb44ec1b2828ec989195af6e00761070263fc733 100644 --- a/services/distributedhardwarefwkservice/test/unittest/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/BUILD.gn @@ -18,6 +18,7 @@ group("test") { "common/accessmanager:AccessManagerTest", "common/componentloader:component_loader_test", "common/componentmanager:component_manager_test", + "common/dbadapter:db_adapter_test", "common/distributedhardwaremanager:distributed_hardware_manager_test", "common/distributedhardwareservice:distributed_hardware_service_test", "common/localhardwaremanager:local_hardware_manager_test", diff --git a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8418a17d321fe14c85a5874bbb08cd0a549ba3a4 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/BUILD.gn @@ -0,0 +1,64 @@ +# 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. + +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +module_out_path = "distributed_hardware_fwk/db_adapter_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "//base/notification/eventhandler/interfaces/inner_api", + "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", + "include", + "${utils_path}/include", + "${utils_path}/include/log", + "${utils_path}/include/eventbus", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${common_path}/utils/include", + "${common_path}/log/include", + "//commonlibrary/c_utils/base/include", + "//third_party/json/include", + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + ] +} + +## UnitTest db_adapter_test +ohos_unittest("DbAdapterTest") { + module_out_path = module_out_path + + sources = [ + "src/db_adapter_test.cpp", + "src/mock_db_change_listener.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ "c_utils:utils" ] +} + +group("db_adapter_test") { + testonly = true + deps = [ ":DbAdapterTest" ] +} diff --git a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/db_adapter_test.h b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/db_adapter_test.h new file mode 100644 index 0000000000000000000000000000000000000000..e344d5763878c885254fef333b61828d6e7ee272 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/db_adapter_test.h @@ -0,0 +1,32 @@ +/* + * 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. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_DB_ADAPTER_TEST_H +#define OHOS_DISTRIBUTED_HARDWARE_DB_ADAPTER_TEST_H + +#include + +namespace OHOS { +namespace DistributedHardware { +class DbAdapterTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/mock_db_change_listener.h b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/mock_db_change_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..d4e5628f6f18c11343e5803b1ef603bfb5410ecb --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/mock_db_change_listener.h @@ -0,0 +1,33 @@ +/* + * 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. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_MOCK_DB_CHANGE_LISTENER_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_DB_CHANGE_LISTENER_H + +#include "distributed_kv_data_manager.h" +#include "kvstore_observer.h" + +namespace OHOS { +namespace DistributedHardware { +class MockDBChangeListener : public DistributedKv::KvStoreObserver { +public: + MockDBChangeListener() = default; + virtual ~MockDBChangeListener() = default; + + void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/db_adapter_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/db_adapter_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17387888d32bd87cd7012827e3d0aa0c16e99a95 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/db_adapter_test.cpp @@ -0,0 +1,239 @@ +/* + * 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 "db_adapter_test.h" + +#include +#include +#include +#include + +#include "capability_info.h" +#include "constants.h" +#define private public +#include "db_adapter.h" +#undef private +#include "dh_context.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "mock_db_change_listener.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "DbAdapterTest" + +namespace { +const string DATABASE_DIR = "/data/service/el1/public/database/dtbhardware_manager_service/"; +const string TEST_DEV_ID_0 = "bb536a637105409e904d4da83791aa11"; +const string TEST_DEV_ID_1 = "bb536a637105409e904d4da83791bb22"; +const string TEST_DEV_ID_2 = "bb536a637105409e904d4da83791bb33"; +const string TEST_DEV_NAME = "Dev1"; +const string TEST_DH_ID_0 = "Camera_00"; +const string TEST_DH_ID_1 = "Mic_01"; +const string TEST_DH_ID_2 = "Gps_02"; +const string TEST_DH_ID_3 = "Display_03"; +const string TEST_DH_ID_4 = "Input_04"; +const string TEST_DH_ATTR_0 = "db_test_attr0"; +const string TEST_DH_ATTR_1 = "db_test_attr1"; +constexpr uint16_t TEST_DEV_TYPE = 0x11; + +const shared_ptr CAP_INFO_0 = + make_shared(TEST_DH_ID_0, TEST_DEV_ID_0, DEV_NAME, TEST_DEV_TYPE, DHType::CAMERA, TEST_DH_ATTR_0); +const shared_ptr CAP_INFO_1 = + make_shared(TEST_DH_ID_1, TEST_DEV_ID_0, DEV_NAME, TEST_DEV_TYPE, DHType::AUDIO, TEST_DH_ATTR_0); +const shared_ptr CAP_INFO_2 = + make_shared(TEST_DH_ID_2, TEST_DEV_ID_0, DEV_NAME, TEST_DEV_TYPE, DHType::GPS, TEST_DH_ATTR_0); +const shared_ptr CAP_INFO_3 = + make_shared(TEST_DH_ID_3, TEST_DEV_ID_0, DEV_NAME, TEST_DEV_TYPE, DHType::HFP, TEST_DH_ATTR_0); +const shared_ptr CAP_INFO_4 = + make_shared(TEST_DH_ID_4, TEST_DEV_ID_0, DEV_NAME, TEST_DEV_TYPE, DHType::INPUT, TEST_DH_ATTR_0); + +const shared_ptr CAP_INFO_5 = + make_shared(TEST_DH_ID_0, TEST_DEV_ID_1, DEV_NAME, TEST_DEV_TYPE, DHType::CAMERA, TEST_DH_ATTR_1); +const shared_ptr CAP_INFO_6 = + make_shared(TEST_DH_ID_1, TEST_DEV_ID_1, DEV_NAME, TEST_DEV_TYPE, DHType::AUDIO, TEST_DH_ATTR_1); +const shared_ptr CAP_INFO_7 = + make_shared(TEST_DH_ID_2, TEST_DEV_ID_1, DEV_NAME, TEST_DEV_TYPE, DHType::GPS, TEST_DH_ATTR_1); +const shared_ptr CAP_INFO_8 = + make_shared(TEST_DH_ID_3, TEST_DEV_ID_1, DEV_NAME, TEST_DEV_TYPE, DHType::HFP, TEST_DH_ATTR_1); +const shared_ptr CAP_INFO_9 = + make_shared(TEST_DH_ID_4, TEST_DEV_ID_1, DEV_NAME, TEST_DEV_TYPE, DHType::INPUT, TEST_DH_ATTR_1); + +std::shared_ptr g_dbAdapterPtr; +} + +void DbAdapterTest::SetUpTestCase(void) +{ + auto ret = mkdir(DATABASE_DIR.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (ret != 0) { + DHLOGE("mkdir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } + + std::shared_ptr changeListener = std::make_shared(); + g_dbAdapterPtr = std::make_shared(APP_ID, GLOBAL_CAPABILITY_ID, changeListener); + if (g_dbAdapterPtr != nullptr) { + g_dbAdapterPtr->Init(); + } +} + +void DbAdapterTest::TearDownTestCase(void) +{ + if (g_dbAdapterPtr != nullptr) { + g_dbAdapterPtr->UnInit(); + } + + g_dbAdapterPtr = nullptr; + + auto ret = remove(DATABASE_DIR.c_str()); + if (ret != 0) { + DHLOGE("remove dir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } +} + +void DbAdapterTest::SetUp() +{ +} + +void DbAdapterTest::TearDown() +{ +} + +/** + * @tc.name: db_adapter_test_000 + * @tc.desc: Verify the Init function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(DbAdapterTest, db_adapter_test_000, TestSize.Level0) +{ + EXPECT_EQ(g_dbAdapterPtr->Init(), DH_FWK_SUCCESS); +} + +/** + * @tc.name: db_adapter_test_001 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(DbAdapterTest, db_adapter_test_001, TestSize.Level0) +{ + vector> resInfos { CAP_INFO_0, CAP_INFO_1, CAP_INFO_2, CAP_INFO_3, CAP_INFO_4, + CAP_INFO_5, CAP_INFO_6, CAP_INFO_7, CAP_INFO_8, CAP_INFO_9 }; + + std::vector keys; + std::vector values; + std::string key; + for (auto &resInfo : resInfos) { + key = resInfo->GetKey(); + keys.push_back(key); + values.push_back(resInfo->ToJsonString()); + } + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keys, values), DH_FWK_SUCCESS); + for (auto &resInfo : resInfos) { + g_dbAdapterPtr->RemoveDataByKey(resInfo->GetKey()); + } +} + +/** + * @tc.name: db_adapter_test_002 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_002, TestSize.Level0) +{ + std::vector keys { std::string(TEST_DEV_ID_2 + TEST_DH_ID_0) }; + std::vector values { TEST_DH_ATTR_0 }; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keys, values), DH_FWK_SUCCESS); + g_dbAdapterPtr->RemoveDataByKey(keys[0]); +} + +/** + * @tc.name: db_adapter_test_003 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_003, TestSize.Level0) +{ + std::vector keys { std::string(TEST_DEV_ID_2 + TEST_DH_ID_0) }; + std::vector valuesEmpty; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keys, valuesEmpty), ERR_DH_FWK_PARA_INVALID); +} + +/** + * @tc.name: db_adapter_test_004 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_004, TestSize.Level0) +{ + std::vector keysEmpty; + std::vector values { TEST_DH_ATTR_0 }; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keysEmpty, values), ERR_DH_FWK_PARA_INVALID); +} + +/** + * @tc.name: db_adapter_test_005 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_005, TestSize.Level0) +{ + std::vector keysEmpty; + std::vector valuesEmpty; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keysEmpty, valuesEmpty), ERR_DH_FWK_PARA_INVALID); +} + +/** + * @tc.name: db_adapter_test_006 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_006, TestSize.Level0) +{ + std::vector keys { std::string(TEST_DEV_ID_2 + TEST_DH_ID_0) }; + std::vector values { TEST_DH_ATTR_0, TEST_DH_ATTR_1 }; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keys, values), ERR_DH_FWK_PARA_INVALID); +} + +/** + * @tc.name: db_adapter_test_007 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_007, TestSize.Level0) +{ + g_dbAdapterPtr->kvStoragePtr_ = nullptr; + std::vector keys { std::string(TEST_DEV_ID_2 + TEST_DH_ID_0) }; + std::vector values { TEST_DH_ATTR_0 }; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keys, values), ERR_DH_FWK_RESOURCE_KV_STORAGE_POINTER_NULL); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/mock_db_change_listener.cpp b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/mock_db_change_listener.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa4895f89ef16fa42cb26c431b9790954f74e40a --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/mock_db_change_listener.cpp @@ -0,0 +1,32 @@ +/* + * 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_db_change_listener.h" + +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +class DBAdapter; +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "MockDBChangeListener" + +void MockDBChangeListener::OnChange(const DistributedKv::ChangeNotification &changeNotification) +{ + (void)changeNotification; +} +} // namespace DistributedHardware +} // namespace OHOS