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 
26 #include <pilz_msgs/BrakeTest.h>
27 
29 #include <prbt_hardware_support/BrakeTestErrorCodes.h>
31 
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 
63 TEST(BrakeTestExecutorTest, testBrakeTestTriggeringRobotNotMoving)
64 {
65  SystemMock mock;
66  BrakeTestExecutor brake_test_executor(
67  std::bind(&SystemMock::detectMotion, &mock), std::bind(&SystemMock::holdController, &mock),
68  std::bind(&SystemMock::executeBrakeTest, &mock), std::bind(&SystemMock::unholdController, &mock),
69  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1));
70 
71  {
72  InSequence dummy;
73 
74  EXPECT_CALL(mock, detectMotion()).Times(1).WillOnce(Return(false));
75  EXPECT_CALL(mock, holdController()).Times(1);
76  EXPECT_CALL(mock, executeBrakeTest()).Times(1).WillOnce(testing::Invoke([]() {
77  BrakeTest::Response res;
78  res.success = true;
79  return res;
80  }));
81  EXPECT_CALL(mock, unholdController()).Times(1);
82  EXPECT_CALL(mock, sendBrakeTestResult(_)).Times(1).WillOnce(Return(true));
83  }
84 
85  pilz_msgs::BrakeTest brake_test_srv;
86  EXPECT_TRUE(brake_test_executor.executeBrakeTest(brake_test_srv.request, brake_test_srv.response)) << "Failed to "
87  "call brake "
88  "test service.";
89  EXPECT_TRUE(brake_test_srv.response.success)
90  << "Brake tests failed unexpectedly. Message: " << brake_test_srv.response.error_msg;
91 }
92 
105 TEST(BrakeTestExecutorTest, testBrakeTestServiceWithRobotMotion)
106 {
107  SystemMock mock;
108  BrakeTestExecutor brake_test_executor(
109  std::bind(&SystemMock::detectMotion, &mock), std::bind(&SystemMock::holdController, &mock),
110  std::bind(&SystemMock::executeBrakeTest, &mock), std::bind(&SystemMock::unholdController, &mock),
111  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1));
112 
113  EXPECT_CALL(mock, detectMotion()).Times(1).WillOnce(Return(true));
114  EXPECT_CALL(mock, holdController()).Times(0);
115  EXPECT_CALL(mock, executeBrakeTest()).Times(0);
116  EXPECT_CALL(mock, unholdController()).Times(0);
117  EXPECT_CALL(mock, sendBrakeTestResult(_)).Times(0);
118 
119  pilz_msgs::BrakeTest brake_test_srv;
120  EXPECT_TRUE(brake_test_executor.executeBrakeTest(brake_test_srv.request, brake_test_srv.response));
121  EXPECT_FALSE(brake_test_srv.response.success) << "Brake tests was unexpectedly successful";
122 }
123 
139 TEST(BrakeTestExecutorTest, testBrakeTestServiceTriggerFails)
140 {
141  SystemMock mock;
142  BrakeTestExecutor brake_test_executor(
143  std::bind(&SystemMock::detectMotion, &mock), std::bind(&SystemMock::holdController, &mock),
144  std::bind(&SystemMock::executeBrakeTest, &mock), std::bind(&SystemMock::unholdController, &mock),
145  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1));
146 
147  {
148  InSequence dummy;
149 
150  EXPECT_CALL(mock, detectMotion()).Times(1).WillOnce(Return(false));
151  EXPECT_CALL(mock, holdController()).Times(1);
152  EXPECT_CALL(mock, executeBrakeTest()).Times(1).WillOnce(testing::Invoke([]() {
153  BrakeTest::Response res;
154  res.success = false;
155  res.error_msg = "Test error message";
156  res.error_code.value = BrakeTestErrorCodes::FAILURE;
157  return res;
158  }));
159  EXPECT_CALL(mock, unholdController()).Times(1);
160  EXPECT_CALL(mock, sendBrakeTestResult(_)).Times(1).WillOnce(Return(true));
161  }
162 
163  pilz_msgs::BrakeTest brake_test_srv;
164  EXPECT_TRUE(brake_test_executor.executeBrakeTest(brake_test_srv.request, brake_test_srv.response));
165  EXPECT_FALSE(brake_test_srv.response.success);
166 }
167 
179 TEST(BrakeTestExecutorTest, testBrakeTestResultServiceFails)
180 {
181  SystemMock mock;
182  BrakeTestExecutor brake_test_executor(
183  std::bind(&SystemMock::detectMotion, &mock), std::bind(&SystemMock::holdController, &mock),
184  std::bind(&SystemMock::executeBrakeTest, &mock), std::bind(&SystemMock::unholdController, &mock),
185  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1));
186 
187  {
188  InSequence dummy;
189 
190  EXPECT_CALL(mock, detectMotion()).Times(1).WillOnce(Return(false));
191  EXPECT_CALL(mock, holdController()).Times(1);
192  EXPECT_CALL(mock, executeBrakeTest()).Times(1).WillOnce(testing::Invoke([]() {
193  BrakeTest::Response res;
194  res.success = true;
195  return res;
196  }));
197  EXPECT_CALL(mock, unholdController()).Times(1);
198  EXPECT_CALL(mock, sendBrakeTestResult(_)).Times(1).WillOnce(Return(false));
199  }
200 
201  pilz_msgs::BrakeTest brake_test_srv;
202  EXPECT_TRUE(brake_test_executor.executeBrakeTest(brake_test_srv.request, brake_test_srv.response));
203  EXPECT_FALSE(brake_test_srv.response.success);
204 }
205 
210 TEST(BrakeTestExecutorTest, testMissingHoldFunc)
211 {
212  SystemMock mock;
213  EXPECT_THROW(BrakeTestExecutor(std::bind(&SystemMock::detectMotion, &mock), nullptr,
214  std::bind(&SystemMock::executeBrakeTest, &mock),
215  std::bind(&SystemMock::unholdController, &mock),
216  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1)),
218 }
219 
224 TEST(BrakeTestExecutorTest, testMissingUnholdFunc)
225 {
226  SystemMock mock;
227  EXPECT_THROW(BrakeTestExecutor(std::bind(&SystemMock::detectMotion, &mock),
228  std::bind(&SystemMock::holdController, &mock),
229  std::bind(&SystemMock::executeBrakeTest, &mock), nullptr,
230  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1)),
232 }
233 
238 TEST(BrakeTestExecutorTest, testMissingDetectMotionFunc)
239 {
240  SystemMock mock;
241  EXPECT_THROW(BrakeTestExecutor(nullptr, std::bind(&SystemMock::holdController, &mock),
242  std::bind(&SystemMock::executeBrakeTest, &mock),
243  std::bind(&SystemMock::unholdController, &mock),
244  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1)),
246 }
247 
252 TEST(BrakeTestExecutorTest, testMissingExecuteBrakeTestFunc)
253 {
254  SystemMock mock;
255  EXPECT_THROW(BrakeTestExecutor(std::bind(&SystemMock::detectMotion, &mock),
256  std::bind(&SystemMock::holdController, &mock), nullptr,
257  std::bind(&SystemMock::unholdController, &mock),
258  std::bind(&SystemMock::sendBrakeTestResult, &mock, _1)),
260 }
261 
266 TEST(BrakeTestExecutorTest, testMissingSendBrakeTestResultFunc)
267 {
268  SystemMock mock;
269  EXPECT_THROW(BrakeTestExecutor(std::bind(&SystemMock::detectMotion, &mock),
270  std::bind(&SystemMock::holdController, &mock),
271  std::bind(&SystemMock::executeBrakeTest, &mock),
272  std::bind(&SystemMock::unholdController, &mock), nullptr),
274 }
275 
280 TEST(BrakeTestExecutorTest, testDtorBrakeTestExecutorException)
281 {
282  std::unique_ptr<BrakeTestExecutorException> ex{ new BrakeTestExecutorException("TestException") };
283 }
284 
286 {
287 public:
288  MOCK_METHOD1(call, bool(std_srvs::Trigger& srv));
289  MOCK_METHOD0(getService, std::string());
290 };
291 
295 TEST(BrakeTestExecutorTest, testTriggerServiceCallFailure)
296 {
297  TriggerServiceMock mock;
298  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
299  EXPECT_CALL(mock, call(_)).Times(1).WillOnce(Return(false));
300 
301  triggerServiceCall<TriggerServiceMock>(mock);
302 }
303 
308 TEST(BrakeTestExecutorTest, testTriggerServiceCallResponseFalse)
309 {
310  TriggerServiceMock mock;
311  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
312  std_srvs::Trigger exp_srv;
313  exp_srv.response.success = false;
314  EXPECT_CALL(mock, call(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(exp_srv), Return(true)));
315 
316  triggerServiceCall<TriggerServiceMock>(mock);
317 }
318 
320 {
321 public:
322  MOCK_METHOD1(call, bool(BrakeTest& srv));
323  MOCK_METHOD0(getService, std::string());
324 };
325 
331 TEST(BrakeTestExecutorTest, testExecuteBrakeTestCallFailure)
332 {
334  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
335  EXPECT_CALL(mock, call(_)).Times(1).WillOnce(Return(false));
336 
337  BrakeTest::Response res{ executeBrakeTestCall<BrakeTestServiceMock>(mock) };
338  EXPECT_FALSE(res.success);
339  EXPECT_EQ(res.error_code.value, BrakeTestErrorCodes::TRIGGER_BRAKETEST_SERVICE_FAILURE);
340 }
341 
343 {
344 public:
345  MOCK_METHOD1(call, bool(SendBrakeTestResult& srv));
346  MOCK_METHOD0(getService, std::string());
347 };
348 
352 TEST(BrakeTestExecutorTest, testSendBrakeTestResultCallFailure)
353 {
355  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
356  EXPECT_CALL(mock, call(_)).Times(1).WillOnce(Return(false));
357 
358  EXPECT_FALSE(sendBrakeTestResultCall<SendBrakeTestResltServiceMock>(mock, true));
359 }
360 
361 MATCHER(IsRequestResultFalse, "")
362 {
363  return !arg.request.result;
364 }
365 
369 TEST(BrakeTestExecutorTest, testSendBrakeTestResultCallSuccess)
370 {
372  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
373  EXPECT_CALL(mock, call(IsRequestResultFalse())).Times(1).WillOnce(Return(true));
374 
375  EXPECT_TRUE(sendBrakeTestResultCall<SendBrakeTestResltServiceMock>(mock, false));
376 }
377 
378 } // namespace brake_test_executor_test
379 
380 int main(int argc, char* argv[])
381 {
382  testing::InitGoogleMock(&argc, argv);
383  return RUN_ALL_TESTS();
384 }
bool executeBrakeTest(pilz_msgs::BrakeTest::Request &req, pilz_msgs::BrakeTest::Response &response)
bool call(const std::string &service_name, MReq &req, MRes &res)
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 Mon Feb 28 2022 23:14:34