unittest_brake_test_executor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <functional>
19 #include <memory>
20 
21 #include <gtest/gtest.h>
22 #include <gmock/gmock.h>
23 
24 #include <std_srvs/Trigger.h>
25 
27 #include <prbt_hardware_support/BrakeTestErrorCodes.h>
29 
30 
32 {
33 
34 using namespace prbt_hardware_support;
35 using namespace testing;
36 
37 using std::placeholders::_1;
38 
40 {
41 public:
42  MOCK_METHOD0(detectMotion, bool());
43  MOCK_METHOD0(holdController, void());
44  MOCK_METHOD0(executeBrakeTest, BrakeTest::Response());
45  MOCK_METHOD0(unholdController, void());
46  MOCK_METHOD1(sendBrakeTestResult, bool(const bool brake_test_result));
47 
48 };
49 
64 TEST(BrakeTestExecutorTest, testBrakeTestTriggeringRobotNotMoving)
65 {
66  SystemMock mock;
67  BrakeTestExecutor brake_test_executor(std::bind(&SystemMock::detectMotion, &mock),
68  std::bind(&SystemMock::holdController, &mock),
69  std::bind(&SystemMock::executeBrakeTest, &mock),
70  std::bind(&SystemMock::unholdController, &mock),
71  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1));
72 
73  {
74  InSequence dummy;
75 
76  EXPECT_CALL(mock, detectMotion()).Times(1).WillOnce(Return(false));
77  EXPECT_CALL(mock, holdController()).Times(1);
78  EXPECT_CALL(mock, executeBrakeTest())
79  .Times(1)
80  .WillOnce(testing::Invoke(
81  []() {
82  BrakeTest::Response res;
83  res.success = true;
84  return res;
85  }));
86  EXPECT_CALL(mock, unholdController()).Times(1);
87  EXPECT_CALL(mock, sendBrakeTestResult(_)).Times(1).WillOnce(Return(true));
88  }
89 
90  BrakeTest brake_test_srv;
91  EXPECT_TRUE(brake_test_executor.executeBrakeTest(brake_test_srv.request, brake_test_srv.response)) << "Failed to call brake test service.";
92  EXPECT_TRUE(brake_test_srv.response.success) << "Brake tests failed unexpectedly. Message: " << brake_test_srv.response.error_msg;
93 }
94 
107 TEST(BrakeTestExecutorTest, testBrakeTestServiceWithRobotMotion)
108 {
109  SystemMock mock;
110  BrakeTestExecutor brake_test_executor(std::bind(&SystemMock::detectMotion, &mock),
111  std::bind(&SystemMock::holdController, &mock),
112  std::bind(&SystemMock::executeBrakeTest, &mock),
113  std::bind(&SystemMock::unholdController, &mock),
114  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1));
115 
116  EXPECT_CALL(mock, detectMotion()).Times(1).WillOnce(Return(true));
117  EXPECT_CALL(mock, holdController()).Times(0);
118  EXPECT_CALL(mock, executeBrakeTest()).Times(0);
119  EXPECT_CALL(mock, unholdController()).Times(0);
120  EXPECT_CALL(mock, sendBrakeTestResult(_)).Times(0);
121 
122 
123  BrakeTest brake_test_srv;
124  EXPECT_TRUE(brake_test_executor.executeBrakeTest(brake_test_srv.request, brake_test_srv.response));
125  EXPECT_FALSE(brake_test_srv.response.success) << "Brake tests was unexpectedly successful";
126  EXPECT_EQ(BrakeTestErrorCodes::ROBOT_MOTION_DETECTED, brake_test_srv.response.error_code.value);
127 }
128 
144 TEST(BrakeTestExecutorTest, testBrakeTestServiceTriggerFails)
145 {
146  SystemMock mock;
147  BrakeTestExecutor brake_test_executor(std::bind(&SystemMock::detectMotion, &mock),
148  std::bind(&SystemMock::holdController, &mock),
149  std::bind(&SystemMock::executeBrakeTest, &mock),
150  std::bind(&SystemMock::unholdController, &mock),
151  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1));
152 
153  {
154  InSequence dummy;
155 
156  EXPECT_CALL(mock, detectMotion()).Times(1).WillOnce(Return(false));
157  EXPECT_CALL(mock, holdController()).Times(1);
158  EXPECT_CALL(mock, executeBrakeTest())
159  .Times(1)
160  .WillOnce(testing::Invoke([]()
161  {
162  BrakeTest::Response res;
163  res.success = false;
164  res.error_msg = "Test error message";
165  res.error_code.value = BrakeTestErrorCodes::FAILURE;
166  return res;
167  }));
168  EXPECT_CALL(mock, unholdController()).Times(1);
169  EXPECT_CALL(mock, sendBrakeTestResult(_)).Times(1).WillOnce(Return(true));
170  }
171 
172  BrakeTest brake_test_srv;
173  EXPECT_TRUE(brake_test_executor.executeBrakeTest(brake_test_srv.request, brake_test_srv.response));
174  EXPECT_FALSE(brake_test_srv.response.success);
175 }
176 
188 TEST(BrakeTestExecutorTest, testBrakeTestResultServiceFails)
189 {
190  SystemMock mock;
191  BrakeTestExecutor brake_test_executor(std::bind(&SystemMock::detectMotion, &mock),
192  std::bind(&SystemMock::holdController, &mock),
193  std::bind(&SystemMock::executeBrakeTest, &mock),
194  std::bind(&SystemMock::unholdController, &mock),
195  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1));
196 
197  {
198  InSequence dummy;
199 
200  EXPECT_CALL(mock, detectMotion()).Times(1).WillOnce(Return(false));
201  EXPECT_CALL(mock, holdController()).Times(1);
202  EXPECT_CALL(mock, executeBrakeTest())
203  .Times(1)
204  .WillOnce(testing::Invoke([]()
205  {
206  BrakeTest::Response res;
207  res.success = true;
208  return res;
209  }));
210  EXPECT_CALL(mock, unholdController()).Times(1);
211  EXPECT_CALL(mock, sendBrakeTestResult(_)).Times(1).WillOnce(Return(false));
212  }
213 
214  BrakeTest brake_test_srv;
215  EXPECT_TRUE(brake_test_executor.executeBrakeTest(brake_test_srv.request, brake_test_srv.response));
216  EXPECT_FALSE(brake_test_srv.response.success);
217  EXPECT_EQ(brake_test_srv.response.error_code.value, BrakeTestErrorCodes::FAILURE);
218 }
219 
224 TEST(BrakeTestExecutorTest, testMissingHoldFunc)
225 {
226  SystemMock mock;
227  EXPECT_THROW(BrakeTestExecutor(std::bind(&SystemMock::detectMotion, &mock),
228  nullptr,
229  std::bind(&SystemMock::executeBrakeTest, &mock),
230  std::bind(&SystemMock::unholdController, &mock),
231  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1)),
233 }
234 
239 TEST(BrakeTestExecutorTest, testMissingUnholdFunc)
240 {
241  SystemMock mock;
242  EXPECT_THROW(BrakeTestExecutor(std::bind(&SystemMock::detectMotion, &mock),
243  std::bind(&SystemMock::holdController, &mock),
244  std::bind(&SystemMock::executeBrakeTest, &mock),
245  nullptr,
246  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1)),
248 }
249 
254 TEST(BrakeTestExecutorTest, testMissingDetectMotionFunc)
255 {
256  SystemMock mock;
257  EXPECT_THROW(BrakeTestExecutor(nullptr,
258  std::bind(&SystemMock::holdController, &mock),
259  std::bind(&SystemMock::executeBrakeTest, &mock),
260  std::bind(&SystemMock::unholdController, &mock),
261  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1)),
263 }
264 
269 TEST(BrakeTestExecutorTest, testMissingExecuteBrakeTestFunc)
270 {
271  SystemMock mock;
272  EXPECT_THROW(BrakeTestExecutor(std::bind(&SystemMock::detectMotion, &mock),
273  std::bind(&SystemMock::holdController, &mock),
274  nullptr,
275  std::bind(&SystemMock::unholdController, &mock),
276  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1)),
278 }
279 
284 TEST(BrakeTestExecutorTest, testMissingSendBrakeTestResultFunc)
285 {
286  SystemMock mock;
287  EXPECT_THROW(BrakeTestExecutor(std::bind(&SystemMock::detectMotion, &mock),
288  std::bind(&SystemMock::holdController, &mock),
289  std::bind(&SystemMock::executeBrakeTest, &mock),
290  std::bind(&SystemMock::unholdController, &mock),
291  nullptr),
293 }
294 
299 TEST(BrakeTestExecutorTest, testDtorBrakeTestExecutorException)
300 {
301  std::unique_ptr<BrakeTestExecutorException> ex {new BrakeTestExecutorException("TestException")};
302 }
303 
305 {
306 public:
307  MOCK_METHOD1(call, bool(std_srvs::Trigger& srv));
308  MOCK_METHOD0(getService, std::string());
309 };
310 
314 TEST(BrakeTestExecutorTest, testTriggerServiceCallFailure)
315 {
316  TriggerServiceMock mock;
317  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
318  EXPECT_CALL(mock, call(_)).Times(1).WillOnce(Return(false));
319 
320  triggerServiceCall<TriggerServiceMock>(mock);
321 }
322 
327 TEST(BrakeTestExecutorTest, testTriggerServiceCallResponseFalse)
328 {
329  TriggerServiceMock mock;
330  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
331  std_srvs::Trigger exp_srv;
332  exp_srv.response.success = false;
333  EXPECT_CALL(mock, call(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(exp_srv), Return(true)));
334 
335  triggerServiceCall<TriggerServiceMock>(mock);
336 }
337 
339 {
340 public:
341  MOCK_METHOD1(call, bool(BrakeTest& srv));
342  MOCK_METHOD0(getService, std::string());
343 };
344 
350 TEST(BrakeTestExecutorTest, testExecuteBrakeTestCallFailure)
351 {
353  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
354  EXPECT_CALL(mock, call(_)).Times(1).WillOnce(Return(false));
355 
356  BrakeTest::Response res {executeBrakeTestCall<BrakeTestServiceMock>(mock)};
357  EXPECT_FALSE(res.success);
358  EXPECT_EQ(res.error_code.value, BrakeTestErrorCodes::TRIGGER_BRAKETEST_SERVICE_FAILURE);
359 }
360 
362 {
363 public:
364  MOCK_METHOD1(call, bool(SendBrakeTestResult& srv));
365  MOCK_METHOD0(getService, std::string());
366 };
367 
371 TEST(BrakeTestExecutorTest, testSendBrakeTestResultCallFailure)
372 {
374  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
375  EXPECT_CALL(mock, call(_)).Times(1).WillOnce(Return(false));
376 
377  EXPECT_FALSE(sendBrakeTestResultCall<SendBrakeTestResltServiceMock>(mock, true));
378 }
379 
380 MATCHER(IsRequestResultFalse, "") { return !arg.request.result; }
381 
385 TEST(BrakeTestExecutorTest, testSendBrakeTestResultCallSuccess)
386 {
388  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
389  EXPECT_CALL(mock, call(IsRequestResultFalse())).Times(1).WillOnce(Return(true));
390 
391  EXPECT_TRUE(sendBrakeTestResultCall<SendBrakeTestResltServiceMock>(mock, false));
392 }
393 
394 } // namespace brake_test_executor_test
395 
396 int main(int argc, char *argv[])
397 {
398  testing::InitGoogleMock(&argc, argv);
399  return RUN_ALL_TESTS();
400 }
bool call(const std::string &service_name, MReq &req, MRes &res)
bool executeBrakeTest(BrakeTest::Request &req, BrakeTest::Response &response)
Triggers execution of brake tests only if the controller is not executing a trajectory.
TEST(BrakeTestExecutorTest, testBrakeTestTriggeringRobotNotMoving)
int main(int argc, char *argv[])


prbt_hardware_support
Author(s):
autogenerated on Tue Feb 2 2021 03:50:17