1 /*
2 * Copyright (c) 2021-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 #define private public
17 #define protected public
18 #include "matching_skills.h"
19 #undef private
20 #undef protected
21
22 #include <gtest/gtest.h>
23
24 using namespace testing::ext;
25 using namespace OHOS::EventFwk;
26 using OHOS::Parcel;
27
28 static const size_t SET_COUNT = 1;
29 static const size_t MAX_COUNT = 100;
30
31 class MatchingSkillsTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 };
38
SetUpTestCase(void)39 void MatchingSkillsTest::SetUpTestCase(void)
40 {}
41
TearDownTestCase(void)42 void MatchingSkillsTest::TearDownTestCase(void)
43 {}
44
SetUp(void)45 void MatchingSkillsTest::SetUp(void)
46 {}
47
TearDown(void)48 void MatchingSkillsTest::TearDown(void)
49 {}
50
51 /*
52 * Feature: MatchingSkills
53 * Function: AddEntity/GetEntity/CountEntities/HasEntity/RemoveEntity
54 * SubFunction: NA
55 * FunctionPoints: AddEntity/GetEntity
56 * EnvConditions: NA
57 * CaseDescription: Verify the function when after add entity can get entity
58 * and count entity is right,has entity and can success remove entity
59 */
60 HWTEST_F(MatchingSkillsTest, MatchingSkills_Entity_001, TestSize.Level1)
61 {
62 MatchingSkills matchSkills;
63 std::string empty;
64 std::string entity = "event.unit.test";
65 matchSkills.AddEntity(entity);
66 matchSkills.AddEntity(entity);
67 EXPECT_EQ(SET_COUNT, matchSkills.CountEntities());
68
69 EXPECT_EQ(true, matchSkills.HasEntity(entity));
70
71 EXPECT_EQ(entity, matchSkills.GetEntity(0));
72
73 EXPECT_EQ(false, entity == matchSkills.GetEntity(MAX_COUNT));
74
75 matchSkills.RemoveEntity(entity);
76
77 matchSkills.RemoveEntity(entity);
78
79 EXPECT_EQ(0, matchSkills.CountEntities());
80 }
81
82 /*
83 * Feature: MatchingSkills
84 * Function: event
85 * SubFunction: NA
86 * FunctionPoints: AddEvent/GetEvent/CountEvent/HasEvent/RemoveEvent
87 * EnvConditions: NA
88 * CaseDescription: Verify the function when after add event can get event
89 * and count event is right,has event and can success remove event
90 */
91 HWTEST_F(MatchingSkillsTest, MatchingSkills_Event_001, TestSize.Level1)
92 {
93 MatchingSkills matchSkills;
94 std::string empty;
95 std::string event = "event.unit.test";
96 matchSkills.AddEvent(event);
97 matchSkills.AddEvent(event);
98
99 EXPECT_EQ(true, matchSkills.HasEvent(event));
100
101 EXPECT_EQ(event, matchSkills.GetEvent(0));
102
103 EXPECT_EQ(false, event == matchSkills.GetEvent(SET_COUNT));
104 matchSkills.RemoveEvent(event);
105
106 matchSkills.RemoveEvent(event);
107
108 EXPECT_EQ(0, matchSkills.CountEvent());
109 }
110
111 /*
112 * Feature: MatchingSkills
113 * Function: scheme
114 * SubFunction: NA
115 * FunctionPoints: AddScheme/GetScheme/CountScheme/HasScheme/RemoveScheme
116 * EnvConditions: NA
117 * CaseDescription: Verify the function when after add scheme can get scheme
118 * and count scheme is right,has scheme and can success remove scheme
119 */
120 HWTEST_F(MatchingSkillsTest, MatchingSkills_Scheme_001, TestSize.Level1)
121 {
122 MatchingSkills matchSkills;
123 std::string empty;
124 std::string shceme = "event.unit.test";
125 matchSkills.AddScheme(shceme);
126 matchSkills.AddScheme(shceme);
127
128 EXPECT_EQ(true, matchSkills.HasScheme(shceme));
129
130 EXPECT_EQ(shceme, matchSkills.GetScheme(0));
131
132 EXPECT_EQ(false, shceme == matchSkills.GetScheme(SET_COUNT));
133
134 matchSkills.RemoveScheme(shceme);
135
136 matchSkills.RemoveScheme(shceme);
137
138 EXPECT_EQ(0, matchSkills.CountSchemes());
139 }
140
141 /*
142 * Feature: MatchingSkills
143 * Function: Match Event
144 * SubFunction: NA
145 * FunctionPoints: Match Event
146 * EnvConditions: NA
147 * CaseDescription: Verify match event
148 */
149 HWTEST_F(MatchingSkillsTest, MatchingSkills_MatchEvent_001, TestSize.Level1)
150 {
151 MatchingSkills matchSkills;
152 EXPECT_EQ(false, matchSkills.MatchEvent(""));
153
154 std::string event = "event.unit.test";
155 matchSkills.AddEvent(event);
156 EXPECT_EQ(true, matchSkills.MatchEvent(event));
157 }
158
159 /*
160 * Feature: MatchingSkills
161 * Function: Match entity
162 * SubFunction: NA
163 * FunctionPoints: Match entity
164 * EnvConditions: NA
165 * CaseDescription: Verify match entity
166 */
167
168 HWTEST_F(MatchingSkillsTest, MatchingSkills_MatchEntity_001, TestSize.Level1)
169 {
170 MatchingSkills matchSkills;
171 std::vector<std::string> entities;
172 EXPECT_EQ(true, matchSkills.MatchEntity(entities));
173
174 std::string entity = "event.unit.test";
175 matchSkills.AddEntity(entity);
176 entities.emplace_back(entity);
177 EXPECT_EQ(true, matchSkills.MatchEntity(entities));
178 entities.emplace_back("entitydiffer");
179 EXPECT_EQ(false, matchSkills.MatchEntity(entities));
180 }
181
182 /*
183 * Feature: MatchingSkills
184 * Function: Match scheme
185 * SubFunction: NA
186 * FunctionPoints: Match scheme
187 * EnvConditions: NA
188 * CaseDescription: Verify match scheme
189 */
190
191 HWTEST_F(MatchingSkillsTest, MatchingSkills_MatchScheme_001, TestSize.Level1)
192 {
193 MatchingSkills matchSkills;
194 EXPECT_EQ(true, matchSkills.MatchScheme(""));
195 std::string scheme = "action.system.event";
196 matchSkills.AddScheme(scheme);
197 EXPECT_EQ(true, matchSkills.MatchScheme(scheme));
198 EXPECT_EQ(false, matchSkills.MatchScheme("schemediffer"));
199 }
200
201 /*
202 * Feature: Match
203 * Function: Match
204 * SubFunction: NA
205 * FunctionPoints: Match
206 * EnvConditions: NA
207 * CaseDescription: Verify match
208 */
209
210 HWTEST_F(MatchingSkillsTest, MatchingSkills_Match_001, TestSize.Level1)
211 {
212 MatchingSkills matchSkills;
213 std::string event = "event.unit.test";
214 matchSkills.AddEvent(event);
215 std::string entity;
216 matchSkills.AddEntity(entity);
217 Want want;
218 want.AddEntity(entity);
219 want.SetAction(event);
220 EXPECT_EQ(true, matchSkills.Match(want));
221 }