1 /*
2 * Copyright (c) 2024 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 "event_export_config_test.h"
17
18 #include <vector>
19
20 #include "export_config_manager.h"
21 #include "export_event_list_parser.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace {
26 // all value is from resource file: test_event_export_config.json and test_events.json
27 constexpr char TEST_CONFIG_DIR[] = "/data/test/test_data/";
28 constexpr char TEST_CONFIG_FILE[] = "/data/test/test_data/test_event_export_config.json";
29 constexpr char TEST_MODULE_NAME[] = "test";
30 constexpr char TEST_SETTING_DB_PARAM_NAME[] = "test_param_key";
31 constexpr char TEST_SETTING_DB_PARAM_ENABLED_VAL[] = "1";
32 constexpr size_t TEST_MODULE_CNT = 1;
33 constexpr int64_t TEST_CAPACITY = 100;
34 constexpr int64_t TEST_SIZE = 2;
35 constexpr int64_t TEST_CYCLE = 3600;
36 constexpr int64_t TEST_FILE_STORE_DAY_CNT = 7;
37 constexpr size_t TEST_EXPORT_NAME_CNT = 3;
38 constexpr size_t TEST_EXPORT_CFG_FILE_CNT = 2;
39 constexpr int64_t TEST_EXPORT_CFG_VERSION = 202404061011;
40 constexpr char TEST_EXPORT_CFG_FILE[] = "/data/test/test_data/test_events_v2.json";
41 constexpr char INVALID_TEST_EXPORT_CFG_FILE1[] = "/data/test/test_data/invalid_test_events1.json";
42 constexpr char INVALID_TEST_EXPORT_CFG_FILE2[] = "/data/test/test_data/invalid_test_events2.json";
43 constexpr char INVALID_TEST_EXPORT_CFG_FILE3[] = "/data/test/test_data/invalid_test_events3.json";
44 constexpr int64_t DEFAULT_VERSION = 0;
45 }
SetUpTestCase()46 void EventExportConfigParseTest::SetUpTestCase()
47 {
48 }
49
TearDownTestCase()50 void EventExportConfigParseTest::TearDownTestCase()
51 {
52 }
53
SetUp()54 void EventExportConfigParseTest::SetUp()
55 {
56 }
57
TearDown()58 void EventExportConfigParseTest::TearDown()
59 {
60 }
61
62 /**
63 * @tc.name: EventExportConfigParseTest001
64 * @tc.desc: EventConfigManager test
65 * @tc.type: FUNC
66 * @tc.require: issueI9E8HA
67 */
68 HWTEST_F(EventExportConfigParseTest, EventExportConfigParseTest001, testing::ext::TestSize.Level3)
69 {
70 ExportConfigManager manager(TEST_CONFIG_DIR);
71 std::vector<std::string> moduleNames;
72 manager.GetModuleNames(moduleNames);
73 ASSERT_EQ(moduleNames.size(), TEST_MODULE_CNT);
74 auto testModuleName = moduleNames.at(0);
75 ASSERT_EQ(testModuleName, TEST_MODULE_NAME);
76 auto exportConfig = manager.GetExportConfig(testModuleName);
77 ASSERT_NE(exportConfig, nullptr);
78 std::vector<std::shared_ptr<ExportConfig>> configs;
79 manager.GetExportConfigs(configs);
80 ASSERT_EQ(configs.size(), TEST_MODULE_CNT);
81 }
82
83 /**
84 * @tc.name: EventExportConfigParseTest002
85 * @tc.desc: EventConfigParser test
86 * @tc.type: FUNC
87 * @tc.require: issueI9E8HA
88 */
89 HWTEST_F(EventExportConfigParseTest, EventExportConfigParseTest002, testing::ext::TestSize.Level3)
90 {
91 ExportConfigParser parser(TEST_CONFIG_FILE);
92 auto exportConfig = parser.Parse();
93 ASSERT_NE(exportConfig, nullptr);
94 ASSERT_EQ(exportConfig->moduleName, ""); // parser not set module name to config
95 ASSERT_EQ(exportConfig->exportDir, "/data/test/test_data");
96 ASSERT_EQ(exportConfig->maxCapcity, TEST_CAPACITY);
97 ASSERT_EQ(exportConfig->maxSize, TEST_SIZE);
98 ASSERT_EQ(exportConfig->taskCycle, TEST_CYCLE);
99 ASSERT_EQ(exportConfig->dayCnt, TEST_FILE_STORE_DAY_CNT);
100 }
101
102 /**
103 * @tc.name: EventExportConfigParseTest003
104 * @tc.desc: EventExportList test
105 * @tc.type: FUNC
106 * @tc.require: issueI9E8HA
107 */
108 HWTEST_F(EventExportConfigParseTest, EventExportConfigParseTest003, testing::ext::TestSize.Level3)
109 {
110 ExportConfigParser parser(TEST_CONFIG_FILE);
111 auto exportConfig = parser.Parse();
112 ASSERT_NE(exportConfig, nullptr);
113 ASSERT_EQ(exportConfig->eventsConfigFiles.size(), TEST_EXPORT_CFG_FILE_CNT);
114 }
115
116 /**
117 * @tc.name: EventExportConfigParseTest004
118 * @tc.desc: SettingDbParam test
119 * @tc.type: FUNC
120 * @tc.require: issueI9E8HA
121 */
122 HWTEST_F(EventExportConfigParseTest, EventExportConfigParseTest004, testing::ext::TestSize.Level3)
123 {
124 ExportConfigParser parser(TEST_CONFIG_FILE);
125 auto exportConfig = parser.Parse();
126 ASSERT_NE(exportConfig, nullptr);
127 ASSERT_EQ(exportConfig->exportSwitchParam.name, TEST_SETTING_DB_PARAM_NAME);
128 ASSERT_EQ(exportConfig->exportSwitchParam.enabledVal, TEST_SETTING_DB_PARAM_ENABLED_VAL);
129 ASSERT_EQ(exportConfig->sysUpgradeParam.name, TEST_SETTING_DB_PARAM_NAME);
130 ASSERT_EQ(exportConfig->sysUpgradeParam.enabledVal, TEST_SETTING_DB_PARAM_ENABLED_VAL);
131 }
132
133 /**
134 * @tc.name: ExportEventListParserTest005
135 * @tc.desc: ExportEventListParser test
136 * @tc.type: FUNC
137 * @tc.require: issueIAC4BC
138 */
139 HWTEST_F(EventExportConfigParseTest, ExportEventListParserTest005, testing::ext::TestSize.Level3)
140 {
141 ExportEventListParser parser(TEST_EXPORT_CFG_FILE);
142 ExportEventList list;
143 parser.GetExportEventList(list);
144 ASSERT_GT(list.size(), 0);
145 auto iter = list.find("DOMAIN1");
146 ASSERT_NE(iter, list.end());
147 iter = list.find("DOMAIN2");
148 ASSERT_NE(iter, list.end());
149 ASSERT_EQ(iter->second.size(), TEST_EXPORT_NAME_CNT);
150 ASSERT_EQ(parser.GetConfigurationVersion(), TEST_EXPORT_CFG_VERSION);
151 }
152
153 /**
154 * @tc.name: ExportEventListParserTest006
155 * @tc.desc: ExportEventListParser test
156 * @tc.type: FUNC
157 * @tc.require: issueIAC4BC
158 */
159 HWTEST_F(EventExportConfigParseTest, ExportEventListParserTest006, testing::ext::TestSize.Level3)
160 {
161 ExportEventListParser parser1(INVALID_TEST_EXPORT_CFG_FILE1);
162 ExportEventList list;
163 parser1.GetExportEventList(list);
164 ASSERT_EQ(list.size(), 0);
165 ASSERT_EQ(parser1.GetConfigurationVersion(), DEFAULT_VERSION);
166 ExportEventListParser parser2(INVALID_TEST_EXPORT_CFG_FILE2);
167 parser2.GetExportEventList(list);
168 ASSERT_EQ(list.size(), 0);
169 ASSERT_EQ(parser2.GetConfigurationVersion(), TEST_EXPORT_CFG_VERSION);
170 ExportEventListParser parser3(INVALID_TEST_EXPORT_CFG_FILE3);
171 parser3.GetExportEventList(list);
172 ASSERT_EQ(list.size(), 0);
173 }
174 } // namespace HiviewDFX
175 } // namespace OHOS