1 /*
2 * Copyright (c) 2025 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 "edm_errors.h"
17 #include "event_config.h"
18 #include "hilog_wrapper.h"
19 #include <fstream>
20 #include <gtest/gtest.h>
21 #include <string>
22 #include "file_ex.h"
23
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace ExternalDeviceManager {
28 static std::string g_localJsonStr = R"(
29 [
30 {
31 "domain": "USB",
32 "fault": [
33 {
34 "faultName": "TRANSFOR_FAULT",
35 "type": "FAULT",
36 "title": "usb_transmission_error_title",
37 "msg": "usb_troubleshoot_message",
38 "uri": "www.gitee.com"
39 }
40 ]
41 }
42 ]
43 )";
44
45 static std::string g_ccmJsonStr = R"(
46 [
47 {
48 "domain": "USB",
49 "fault": [
50 {
51 "faultName": "TRANSFOR_FAULT",
52 "type": "FAULT",
53 "title": "usb_transmission_error_title",
54 "msg": "usb_troubleshoot_message",
55 "uri": "www.gitee.com"
56 },
57 {
58 "faultName": "READ_FAULT",
59 "type": "FAULT",
60 "title": "usb_transmission_error_title",
61 "msg": "usb_troubleshoot_message",
62 "uri": "www.gitee.com"
63 }
64 ]
65 },
66 {
67 "domain": "MOUSE",
68 "fault": [
69 {
70 "faultName": "TRANSFOR_FAULT",
71 "type": "FAULT",
72 "title": "usb_transmission_error_title",
73 "msg": "usb_troubleshoot_message",
74 "uri": "www.gitee.com"
75 }
76 ]
77 }
78 ]
79 )";
80
81 const std::string LOCAL_J_SON_FILE_PATH = "./local_event_config.json";
82 const std::string CCM_J_SON_FILE_PATH = "./ccm_event_config.json";
83
84 class EventConfigTest : public testing::Test {
85 public:
86 static void SetUpTestCase(void);
87 static void TearDownTestCase(void);
88 void SetUp();
89 void TearDown();
90 static void DeleteFile(const std::string &filePath);
91 };
92
DeleteFile(const std::string & filePath)93 void EventConfigTest::DeleteFile(const std::string &filePath)
94 {
95 if (access(filePath.c_str(), F_OK) == 0) {
96 if (remove(filePath.c_str()) != 0) {
97 EDM_LOGE(MODULE_SERVICE, "Failed to remove file: %{public}s", filePath.c_str());
98 }
99 }
100 }
101
SetUpTestCase(void)102 void EventConfigTest::SetUpTestCase(void)
103 {
104 SaveStringToFile(LOCAL_J_SON_FILE_PATH, g_localJsonStr);
105 SaveStringToFile(CCM_J_SON_FILE_PATH, g_ccmJsonStr);
106 }
107
TearDownTestCase(void)108 void EventConfigTest::TearDownTestCase(void)
109 {
110 DeleteFile(LOCAL_J_SON_FILE_PATH);
111 DeleteFile(CCM_J_SON_FILE_PATH);
112 }
SetUp(void)113 void EventConfigTest::SetUp(void) {}
TearDown(void)114 void EventConfigTest::TearDown(void) {}
115
116 /**
117 * @tc.name: ParseJsonFile001
118 * @tc.desc: Test ParseJsonFile
119 * @tc.type: FUNC
120 */
121 HWTEST_F(EventConfigTest, ParseJsonFile001, TestSize.Level1)
122 {
123 EDM_LOGI(MODULE_SERVICE, "ParseJsonFile001 begin");
124 auto &eventConfig = EventConfig::GetInstance();
125 EXPECT_NE(&eventConfig, nullptr);
126 eventConfig.ParseJsonFile();
127 size_t size = eventConfig.peripheralFaultsMap_.size();
128 if (size > 0) {
129 std::vector<FaultInfo> faults = eventConfig.GetFaultsInfoByDomain("USB");
130 EXPECT_GT(faults.size(), 0);
131 FaultInfo faultInfo = eventConfig.GetFaultInfo("USB", "TRANSFOR_FAULT");
132 EXPECT_EQ(faultInfo.faultName, "TRANSFOR_FAULT");
133 EXPECT_EQ(faultInfo.type, "FAULT");
134 EXPECT_EQ(faultInfo.title, "usb_transmission_error_title");
135 EXPECT_EQ(faultInfo.msg, "usb_troubleshoot_message");
136 EDM_LOGI(MODULE_SERVICE, "ParseJsonFile001 end");
137 }
138 }
139
140 /**
141 * @tc.name: ParseJsonFile002
142 * @tc.desc: Test ParseJsonFile
143 * @tc.type: FUNC
144 */
145 HWTEST_F(EventConfigTest, ParseJsonFile002, TestSize.Level1)
146 {
147 EDM_LOGI(MODULE_SERVICE, "ParseJsonFile002 begin");
148 EventConfig &eventConfig = EventConfig::GetInstance();
149 EXPECT_NE(&eventConfig, nullptr);
150 DomainFaultsMap peripheralFaultsMap;
151 bool bRet = eventConfig.ParseJsonFile(LOCAL_J_SON_FILE_PATH, peripheralFaultsMap);
152 EXPECT_TRUE(bRet);
153 EXPECT_GT(peripheralFaultsMap.size(), 0);
154 EDM_LOGI(MODULE_SERVICE, "ParseJsonFile002 end");
155 }
156
157 /**
158 * @tc.name: FillFaultsMap001
159 * @tc.desc: Test FillFaultsMap
160 * @tc.type: FUNC
161 */
162 HWTEST_F(EventConfigTest, FillFaultsMap001, TestSize.Level1)
163 {
164 EDM_LOGI(MODULE_SERVICE, "FillFaultsMap001 begin");
165 EventConfig &eventConfig = EventConfig::GetInstance();
166 EXPECT_NE(&eventConfig, nullptr);
167 DomainFaultsMap ccmMap;
168 DomainFaultsMap localMap;
169 DomainFaultsMap comMap;
170 bool bRet = eventConfig.ParseJsonFile(LOCAL_J_SON_FILE_PATH, ccmMap);
171 EXPECT_TRUE(bRet);
172 EXPECT_EQ(ccmMap.size(), 1);
173 bRet = eventConfig.ParseJsonFile(CCM_J_SON_FILE_PATH, localMap);
174 EXPECT_TRUE(bRet);
175 EXPECT_EQ(localMap.size(), 2);
176 comMap = eventConfig.FillFaultsMap(ccmMap, localMap);
177 EXPECT_EQ(comMap.size(), 1);
178 EDM_LOGI(MODULE_SERVICE, "FillFaultsMap001 end");
179 }
180 } // namespace ExternalDeviceManager
181 } // namespace OHOS
182