From b93659d78f6723e53a7423077ecac4fbdd59bed9 Mon Sep 17 00:00:00 2001 From: xuwenqi Date: Tue, 6 May 2025 21:23:49 +0800 Subject: [PATCH] add test Signed-off-by: xuwenqi Change-Id: I08c1949637d7f18e1ff6c69b50ba0712ac2d705c --- .../service/test/BUILD.gn | 5 + .../service/test/data_share_data_test.cpp | 112 ++++++++++++++++++ .../test/data_share_strategies_test.cpp | 56 +++++++++ 3 files changed, 173 insertions(+) create mode 100644 services/distributeddataservice/service/test/data_share_data_test.cpp create mode 100644 services/distributeddataservice/service/test/data_share_strategies_test.cpp diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 17eb73404..6a249545b 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1049,11 +1049,13 @@ ohos_unittest("DataShareServiceImplTest") { "${datashare_path}/frameworks/native/common/include", "${datashare_path}/interfaces/inner_api/common/include", "${datashare_path}/interfaces/inner_api/consumer/include", + "${data_service_path}/service/data_share/strategies/data_share", "//third_party/json/single_include", ] sources = [ "${data_service_path}/service/common/xcollie.cpp", + "${data_service_path}/service/common/value_proxy.cpp", "${data_service_path}/service/crypto/src/crypto_manager.cpp", "${data_service_path}/service/data_share/common/app_connect_manager.cpp", "${data_service_path}/service/data_share/common/bundle_mgr_proxy.cpp", @@ -1101,7 +1103,9 @@ ohos_unittest("DataShareServiceImplTest") { "${data_service_path}/service/kvdb/user_delegate.cpp", "${data_service_path}/service/permission/src/permission_validator.cpp", "${data_service_path}/service/permission/src/permit_delegate.cpp", + "${data_service_path}/service/rdb/rdb_result_set_impl.cpp", "data_share_common_test.cpp", + "data_share_data_test.cpp", "data_share_obs_proxy_test.cpp", "data_share_profile_config_test.cpp", "data_share_service_impl_test.cpp", @@ -1109,6 +1113,7 @@ ohos_unittest("DataShareServiceImplTest") { "data_share_subscriber_managers_test.cpp", "data_share_types_util_test.cpp", "kv_dalegate_test.cpp", + "data_share_strategies_test.cpp", ] configs = [ ":module_private_config" ] diff --git a/services/distributeddataservice/service/test/data_share_data_test.cpp b/services/distributeddataservice/service/test/data_share_data_test.cpp new file mode 100644 index 000000000..9973ccde2 --- /dev/null +++ b/services/distributeddataservice/service/test/data_share_data_test.cpp @@ -0,0 +1,112 @@ +/* +* Copyright (c) 2025 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. +*/ +#define LOG_TAG "DataShareDataTest" + +#include +#include +#include "template_data.h" +#include "data_share_profile_config.h" +#include "executor_pool.h" +#include "grd_error.h" +#include "kv_delegate.h" +#include "log_print.h" +#include "resultset_json_formatter.h" +#include "rdb_result_set_impl.h" +#include "published_data.h" +#include "template_data.h" + +using namespace testing::ext; +using namespace OHOS::DataShare; +using namespace OHOS::DistributedRdb; +namespace OHOS::Test { +using OHOS::DataShare::LogLabel; +class DataShareDataTest : public testing::Test { +public: + static void SetUpTestCase(void){}; + static void TearDownTestCase(void){}; + void SetUp(){}; + void TearDown(){}; +}; + +/** +* @tc.name: PublishedDataQuery001 +* @tc.desc: test Query function when delegate is nullptr +* @tc.type: FUNC +* @tc.require:issueIC5TL8 +* @tc.precon: None +* @tc.step: + 1.Creat a publishedData object and delegate is null + 2.call Query function +* @tc.experct: Query failed and result is empty +*/ +HWTEST_F(DataShareDataTest, PublishedDataQuery001, TestSize.Level1) +{ + ZLOGI("DataShareDataTest PublishedDataQuery001 start"); + PublishedDataNode node; + PublishedData publishedData(node); + std::string bundleName = "bundlename"; + int32_t userId = 1; + auto result = publishedData.Query(bundleName, userId); + EXPECT_TRUE(result.empty()); + ZLOGI("DataShareDataTest PublishedDataQuery001 end"); +} + +/** +* @tc.name: Unmarshal001 +* @tc.desc: test Unmarshal function when node is nullptr +* @tc.type: FUNC +* @tc.require:issueIC5TL8 +* @tc.precon: None +* @tc.step: + 1.Creat a templateRootNode object and node is null + 2.call Unmarshal function +* @tc.experct: Unmarshal failed and return false +*/ +HWTEST_F(DataShareDataTest, Unmarshal001, TestSize.Level1) +{ + ZLOGI("DataShareDataTest Unmarshal001 start"); + DistributedData::Serializable::json node; + TemplateRootNode templateRootNode; + auto result = templateRootNode.Unmarshal(node); + EXPECT_EQ(result, false); + ZLOGI("DataShareDataTest Unmarshal001 end"); +} + +/** +* @tc.name: TemplateDataQuery001 +* @tc.desc: test Query function when delegate is null +* @tc.type: FUNC +* @tc.require:issueIC5TL8 +* @tc.precon: None +* @tc.step: + 1.Creat a templateRootNode object and delegate is null + 2.call Query function +* @tc.experct: Query failed and return E_ERROR +*/ +HWTEST_F(DataShareDataTest, TemplateDataQuery001, TestSize.Level1) +{ + ZLOGI("DataShareDataTest TemplateDataQuery001 start"); + std::string uri = "uri"; + std::string bundleName = "bundlename"; + int64_t subscriberId = 1; + int32_t userId = 1; + Template templateData; + TemplateData templateDataNode(uri, bundleName, subscriberId, userId, templateData); + std::string filter = "filter"; + auto result = templateDataNode.Query(filter, templateData); + EXPECT_EQ(result, E_ERROR); + ZLOGI("DataShareDataTest TemplateDataQuery001 end"); +} +} // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/service/test/data_share_strategies_test.cpp b/services/distributeddataservice/service/test/data_share_strategies_test.cpp new file mode 100644 index 000000000..5ab21e477 --- /dev/null +++ b/services/distributeddataservice/service/test/data_share_strategies_test.cpp @@ -0,0 +1,56 @@ +/* +* Copyright (c) 2025 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. +*/ +#define LOG_TAG "DataShareStrategiesTest.cpp" + +#include +#include +#include "data_share_profile_config.h" +#include "data_share_service_impl.h" +#include "log_print.h" +#include "load_config_from_data_share_bundle_info_strategy.h" + +using namespace testing::ext; +using namespace OHOS::DataShare; +namespace OHOS::Test { +class DataShareStrategiesTest : public testing::Test { +public: + static void SetUpTestCase(void){}; + static void TearDownTestCase(void){}; + void SetUp(){}; + void TearDown(){}; +}; + +/** +* @tc.name: LoadConfigFromUri001 +* @tc.desc: test LoadConfigFromUri function when delegate is null +* @tc.type: FUNC +* @tc.require:issueIC5TL8 +* @tc.precon: None +* @tc.step: + 1.Creat a LoadConfigFromUri object and delegate is null + 2.call LoadConfigFromUri function +* @tc.experct: LoadConfigFromUri failed and return false +*/ +HWTEST_F(DataShareStrategiesTest, LoadConfigFromUri001, TestSize.Level1) +{ + ZLOGI("DataShareStrategiesTest LoadConfigFromUri001 start"); + std::shared_ptr context = std::make_shared(); + context->uri = "error_uri"; + LoadConfigFromDataShareBundleInfoStrategy strategy; + bool result = strategy.LoadConfigFromUri(context); + EXPECT_EQ(result, false); + ZLOGI("DataShareStrategiesTest LoadConfigFromUri001 end"); +} +} // namespace OHOS::Test \ No newline at end of file -- Gitee