1 /*
2  * Copyright (c) 2022 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 "config_reader_test.h"
17 #include "plugin_mgr.h"
18 
19 using namespace std;
20 using namespace testing::ext;
21 
22 namespace OHOS {
23 namespace ResourceSchedule {
24 namespace {
25     const string PLUGIN_NAME = "demo";
26     const string CONFIG_NAME = "sample";
27     const string TEST_PREFIX_RES_PATH = "/data/test/resource/resschedfwk/parseconfig/";
28 }
29 
SetUpTestCase()30 void ConfigReaderTest::SetUpTestCase() {}
31 
TearDownTestCase()32 void ConfigReaderTest::TearDownTestCase() {}
33 
SetUp()34 void ConfigReaderTest::SetUp()
35 {
36     /**
37      * @tc.setup: initialize the member variable configReader_
38      */
39     configReader_ = make_shared<ConfigReader>();
40 }
41 
TearDown()42 void ConfigReaderTest::TearDown()
43 {
44     /**
45      * @tc.teardown: clear configReader_
46      */
47     configReader_ = nullptr;
48 }
49 
ParseConfigFile(const string & fileName)50 bool ConfigReaderTest::ParseConfigFile(const string& fileName)
51 {
52     std::vector<std::string> contents;
53     PluginMgr::GetInstance().GetConfigContent(-1, TEST_PREFIX_RES_PATH + fileName, contents);
54     bool ret = false;
55     for (auto content : contents) {
56         if (configReader_->LoadFromConfigContent(content)) {
57             ret = true;
58         }
59     }
60     return ret;
61 }
62 
63 /**
64  * @tc.name: Load Config File 001
65  * @tc.desc: Verify if can load not exist file.
66  * @tc.type: FUNC
67  * @tc.require: issueI798UT
68  * @tc.author:xukuan
69  */
70 HWTEST_F(ConfigReaderTest, LoadConfigFile001, TestSize.Level1)
71 {
72     std::vector<std::string> contents;
73     PluginMgr::GetInstance().GetConfigContent(-1, "fileNotExist", contents);
74 
75     /**
76      * @tc.steps: step1. load not exist config file
77      * @tc.expected: step1. return false when load not exist file
78      */
79     bool ret = false;
80     for (auto content : contents) {
81         if (configReader_->LoadFromConfigContent(content)) {
82             ret = true;
83         }
84     }
85     EXPECT_TRUE(!ret);
86 }
87 
88 /**
89  * @tc.name: Load Config File 002
90  * @tc.desc: Verify if can load invalid format config file.
91  * @tc.type: FUNC
92  * @tc.require: issueI798UT
93  * @tc.author:xukuan
94  */
95 HWTEST_F(ConfigReaderTest, LoadConfigFile002, TestSize.Level1)
96 {
97     bool ret = ParseConfigFile("invalid_format.xml");
98     EXPECT_TRUE(!ret);
99 }
100 
101 /**
102  * @tc.name: Load Config File 003
103  * @tc.desc: Verify if can load wrong root element config file.
104  * @tc.type: FUNC
105  * @tc.require: issueI798UT
106  * @tc.author:xukuan
107  */
108 HWTEST_F(ConfigReaderTest, LoadConfigFile003, TestSize.Level1)
109 {
110     bool ret = ParseConfigFile("root_element_wrong.xml");
111     EXPECT_TRUE(!ret);
112 }
113 
114 /**
115  * @tc.name: Load Config File 004
116  * @tc.desc: Verify if can load wrong plugin tag config file.
117  * @tc.type: FUNC
118  * @tc.require: issueI798UT
119  * @tc.author:xukuan
120  */
121 HWTEST_F(ConfigReaderTest, LoadConfigFile004, TestSize.Level1)
122 {
123     bool ret = ParseConfigFile("plugin_tag_wrong.xml");
124     EXPECT_TRUE(!ret);
125 }
126 
127 /**
128  * @tc.name: Load Config File 005
129  * @tc.desc: Verify if can load config file with comment.
130  * @tc.type: FUNC
131  * @tc.require: issueI798UT
132  * @tc.author:xukuan
133  */
134 HWTEST_F(ConfigReaderTest, LoadConfigFile005, TestSize.Level1)
135 {
136     bool ret = ParseConfigFile("res_sched_config_comments.xml");
137     EXPECT_TRUE(ret);
138 }
139 
140 /**
141  * @tc.name: Get Config 001
142  * @tc.desc: Verify if can load config with wrong pluginName or configName.
143  * @tc.type: FUNC
144  * @tc.require: issueI5WWV3
145  * @tc.author:lice
146  */
147 HWTEST_F(ConfigReaderTest, GetConfig001, TestSize.Level1)
148 {
149     PluginConfig config = configReader_->GetConfig("error.xml", "");
150     EXPECT_TRUE(config.itemList.empty());
151 
152     config = configReader_->GetConfig("res_sched_config_comments.xml", "error.xml");
153     EXPECT_TRUE(config.itemList.empty());
154 }
155 } // namespace ResourceSchedule
156 } // namespace OHOS
157