unittest_canopen_braketest_adapter.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 <ros/ros.h>
25 #include <canopen_chain_node/GetObject.h>
26 #include <canopen_chain_node/SetObject.h>
27 
30 #include <prbt_hardware_support/BrakeTest.h>
32 
34 {
35 using namespace prbt_hardware_support;
36 using namespace testing;
37 
38 using canopen_chain_node::GetObjectRequest;
39 using canopen_chain_node::GetObjectResponse;
40 using canopen_chain_node::SetObjectRequest;
41 using canopen_chain_node::SetObjectResponse;
42 
43 static const std::string BRAKE_TEST_SERVICE_NAME{ "/prbt/braketest_adapter_node/trigger_braketest" };
44 
45 static const std::string BRAKE_TEST_DURATION_OBJECT_INDEX{ "2060sub1" };
46 static const std::string START_BRAKE_TEST_OBJECT_INDEX{ "2060sub2" };
47 static const std::string BRAKE_TEST_STATUS_OBJECT_INDEX{ "2060sub3" };
48 
49 static const std::string NODE_NAMES_PARAMETER_NAME{ "/prbt/driver/nodes" };
50 static const std::string BRAKETEST_REQUIRED_NAME{ "braketest_required" };
51 static const std::string NODE_NAMES_PREFIX{ "prbt_joint_" };
52 static const std::vector<size_t> NODE_TEST_SET{ { 0, 2, 5 } };
53 
54 #define DEFAULT_SETUP \
55  CANOpenChainNodeMock canopen_chain_node; \
56  ros::NodeHandle nh_adapter("/prbt/braketest_adapter_node"); \
57  prbt_hardware_support::CANOpenBrakeTestAdapter canopen_braketest_adapter(nh_adapter); \
58  ros::ServiceClient brake_test_srv_client = nh_.serviceClient<BrakeTest>(BRAKE_TEST_SERVICE_NAME); \
59  \
60  ASSERT_TRUE(brake_test_srv_client.exists()) << "Brake test service not available.";
61 
62 class CanOpenBraketestAdapterTest : public Test
63 {
64 protected:
66 };
67 
74 TEST_F(CanOpenBraketestAdapterTest, testCANOpenBrakeTestAdapterExceptionDtor)
75 {
76  {
77  std::shared_ptr<CANOpenBrakeTestAdapterException> ex{ new CANOpenBrakeTestAdapterException("Test msg") };
78  }
79 }
80 
94 TEST_F(CanOpenBraketestAdapterTest, testBrakeTestServiceWithoutCANGetService)
95 {
96  CANOpenChainNodeMock canopen_chain_node;
97  canopen_chain_node.shutdownGetService();
98 
99  EXPECT_THROW(prbt_hardware_support::CANOpenBrakeTestAdapter canopen_braketest_adapter(nh_),
101 }
102 
116 TEST_F(CanOpenBraketestAdapterTest, testBrakeTestServiceWithoutCANSetService)
117 {
118  CANOpenChainNodeMock canopen_chain_node;
119  canopen_chain_node.shutdownSetService();
120 
121  EXPECT_THROW(prbt_hardware_support::CANOpenBrakeTestAdapter canopen_braketest_adapter(nh_),
123 }
124 
140 TEST_F(CanOpenBraketestAdapterTest, testBrakeTestServiceWithoutNodeParameters)
141 {
143 
144  /**********
145  * Step 1 *
146  **********/
148  ASSERT_TRUE(nh_.getParam(NODE_NAMES_PARAMETER_NAME, rpc));
149  nh_.deleteParam(NODE_NAMES_PARAMETER_NAME);
150 
151  /**********
152  * Step 3 *
153  **********/
154  BrakeTest srv;
155  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
156  EXPECT_FALSE(srv.response.success);
157  EXPECT_EQ(BrakeTestErrorCodes::GET_NODE_NAMES_FAILURE, srv.response.error_code.value);
158 
159  nh_.setParam(NODE_NAMES_PARAMETER_NAME, rpc);
160 }
161 
178 TEST_F(CanOpenBraketestAdapterTest, testBrakeTestServiceWithNodeParametersTypeError)
179 {
181 
182  /**********
183  * Step 1 *
184  **********/
186  ASSERT_TRUE(nh_.getParam(NODE_NAMES_PARAMETER_NAME, rpc));
187  auto param_modified_name = NODE_NAMES_PARAMETER_NAME + "/" + NODE_NAMES_PREFIX + "1/" + BRAKETEST_REQUIRED_NAME;
188  nh_.setParam(param_modified_name, 99);
189 
190  /**********
191  * Step 3 *
192  **********/
193  BrakeTest srv;
194  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
195  EXPECT_FALSE(srv.response.success);
196  EXPECT_EQ(BrakeTestErrorCodes::GET_NODE_NAMES_FAILURE, srv.response.error_code.value);
197 
198  nh_.setParam(NODE_NAMES_PARAMETER_NAME, rpc);
199 }
200 
222 TEST_F(CanOpenBraketestAdapterTest, testGetBrakeTestDurationServiceCallFailure)
223 {
225 
226  for (size_t node_index : NODE_TEST_SET)
227  {
228  std::string node{ NODE_NAMES_PREFIX + std::to_string(node_index + 1) };
229 
230  /**********
231  * Step 1 *
232  **********/
233  // Avoid unmatched expectations
234  canopen_chain_node.expectAnything();
235 
236  EXPECT_CALL(canopen_chain_node, get_obj(AllOf(Field(&GetObjectRequest::node, node),
237  Field(&GetObjectRequest::object, BRAKE_TEST_DURATION_OBJECT_INDEX)),
238  _))
239  .Times(AtLeast(1))
240  .WillRepeatedly(Return(false));
241 
242  EXPECT_CALL(canopen_chain_node, set_obj(AllOf(Field(&SetObjectRequest::node, node),
243  Field(&SetObjectRequest::object, START_BRAKE_TEST_OBJECT_INDEX)),
244  _))
245  .Times(0);
246 
247  /**********
248  * Step 2 *
249  **********/
250  BrakeTest srv;
251  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
252  EXPECT_FALSE(srv.response.success);
253  EXPECT_EQ(BrakeTestErrorCodes::GET_DURATION_FAILURE, srv.response.error_code.value);
254 
255  /**********
256  * Step 3 *
257  **********/
258  ASSERT_TRUE(Mock::VerifyAndClearExpectations(&canopen_chain_node));
259  }
260 }
261 
283 TEST_F(CanOpenBraketestAdapterTest, testGetBrakeTestDurationServiceResponseFailure)
284 {
286 
287  GetObjectResponse duration_resp;
288  duration_resp.success = false;
289 
290  for (size_t node_index : NODE_TEST_SET)
291  {
292  std::string node{ NODE_NAMES_PREFIX + std::to_string(node_index + 1) };
293 
294  /**********
295  * Step 1 *
296  **********/
297  // Avoid unmatched expectations
298  canopen_chain_node.expectAnything();
299 
300  EXPECT_CALL(canopen_chain_node, get_obj(AllOf(Field(&GetObjectRequest::node, node),
301  Field(&GetObjectRequest::object, BRAKE_TEST_DURATION_OBJECT_INDEX)),
302  _))
303  .Times(AtLeast(1))
304  .WillRepeatedly(DoAll(SetArgReferee<1>(duration_resp), Return(true)));
305 
306  EXPECT_CALL(canopen_chain_node, set_obj(AllOf(Field(&SetObjectRequest::node, node),
307  Field(&SetObjectRequest::object, START_BRAKE_TEST_OBJECT_INDEX)),
308  _))
309  .Times(0);
310 
311  /**********
312  * Step 2 *
313  **********/
314  BrakeTest srv;
315  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
316  EXPECT_FALSE(srv.response.success);
317  EXPECT_EQ(BrakeTestErrorCodes::GET_DURATION_FAILURE, srv.response.error_code.value);
318 
319  /**********
320  * Step 3 *
321  **********/
322  ASSERT_TRUE(Mock::VerifyAndClearExpectations(&canopen_chain_node));
323  }
324 }
325 
345 TEST_F(CanOpenBraketestAdapterTest, testStartBrakeTestServiceCallSuccess)
346 {
348 
349  /**********
350  * Step 1 *
351  **********/
352  canopen_chain_node.setDefaultActions();
353 
354  /**********
355  * Step 2 *
356  **********/
357  BrakeTest srv;
358  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
359  EXPECT_TRUE(srv.response.success);
360 }
361 
383 TEST_F(CanOpenBraketestAdapterTest, testStartBrakeTestServiceCallFailure)
384 {
386 
387  for (size_t node_index : NODE_TEST_SET)
388  {
389  std::string node{ NODE_NAMES_PREFIX + std::to_string(node_index + 1) };
390 
391  /**********
392  * Step 1 *
393  **********/
394  // Avoid unmatched expectations
395  canopen_chain_node.expectAnything();
396 
397  EXPECT_CALL(canopen_chain_node, get_obj(AllOf(Field(&GetObjectRequest::node, node),
398  Field(&GetObjectRequest::object, BRAKE_TEST_DURATION_OBJECT_INDEX)),
399  _))
400  .Times(AtLeast(1));
401 
402  EXPECT_CALL(canopen_chain_node, set_obj(AllOf(Field(&SetObjectRequest::node, node),
403  Field(&SetObjectRequest::object, START_BRAKE_TEST_OBJECT_INDEX)),
404  _))
405  .WillOnce(Return(false));
406 
407  /**********
408  * Step 2 *
409  **********/
410  BrakeTest srv;
411  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
412  EXPECT_FALSE(srv.response.success);
413  EXPECT_EQ(BrakeTestErrorCodes::START_BRAKE_TEST_FAILURE, srv.response.error_code.value);
414 
415  /**********
416  * Step 3 *
417  **********/
418  Mock::VerifyAndClearExpectations(&canopen_chain_node);
419  }
420 }
421 
443 TEST_F(CanOpenBraketestAdapterTest, testStartBrakeTestServiceResponseFailure)
444 {
446 
447  SetObjectResponse start_resp;
448  start_resp.success = false;
449 
450  for (size_t node_index : NODE_TEST_SET)
451  {
452  std::string node{ NODE_NAMES_PREFIX + std::to_string(node_index + 1) };
453 
454  /**********
455  * Step 1 *
456  **********/
457  // Avoid unmatched expectations
458  canopen_chain_node.expectAnything();
459 
460  EXPECT_CALL(canopen_chain_node, get_obj(AllOf(Field(&GetObjectRequest::node, node),
461  Field(&GetObjectRequest::object, BRAKE_TEST_DURATION_OBJECT_INDEX)),
462  _))
463  .Times(AtLeast(1));
464 
465  EXPECT_CALL(canopen_chain_node, set_obj(AllOf(Field(&SetObjectRequest::node, node),
466  Field(&SetObjectRequest::object, START_BRAKE_TEST_OBJECT_INDEX)),
467  _))
468  .WillOnce(DoAll(SetArgReferee<1>(start_resp), Return(true)));
469 
470  /**********
471  * Step 2 *
472  **********/
473  BrakeTest srv;
474  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
475  EXPECT_FALSE(srv.response.success);
476  EXPECT_EQ(BrakeTestErrorCodes::START_BRAKE_TEST_FAILURE, srv.response.error_code.value);
477 
478  /**********
479  * Step 3 *
480  **********/
481  Mock::VerifyAndClearExpectations(&canopen_chain_node);
482  }
483 }
484 
504 TEST_F(CanOpenBraketestAdapterTest, testBrakeTestStatusServiceCallFailure)
505 {
507 
508  for (size_t node_index : NODE_TEST_SET)
509  {
510  std::string node{ NODE_NAMES_PREFIX + std::to_string(node_index + 1) };
511 
512  /**********
513  * Step 1 *
514  **********/
515  // Avoid unmatched expectations
516  canopen_chain_node.expectAnything();
517 
518  EXPECT_CALL(canopen_chain_node, get_obj(AllOf(Field(&GetObjectRequest::node, node),
519  Field(&GetObjectRequest::object, BRAKE_TEST_STATUS_OBJECT_INDEX)),
520  _))
521  .Times(AtLeast(1))
522  .WillRepeatedly(Return(false));
523 
524  /**********
525  * Step 2 *
526  **********/
527  BrakeTest srv;
528  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
529  EXPECT_FALSE(srv.response.success);
530  EXPECT_EQ(BrakeTestErrorCodes::GET_STATUS_FAILURE, srv.response.error_code.value);
531 
532  /**********
533  * Step 3 *
534  **********/
535  Mock::VerifyAndClearExpectations(&canopen_chain_node);
536  }
537 }
538 
558 TEST_F(CanOpenBraketestAdapterTest, testBrakeTestStatusServiceResponseFailure)
559 {
561 
562  GetObjectResponse status_resp;
563  status_resp.success = false;
564 
565  for (size_t node_index : NODE_TEST_SET)
566  {
567  std::string node{ NODE_NAMES_PREFIX + std::to_string(node_index + 1) };
568 
569  /**********
570  * Step 1 *
571  **********/
572  // Avoid unmatched expectations
573  canopen_chain_node.expectAnything();
574 
575  EXPECT_CALL(canopen_chain_node, get_obj(AllOf(Field(&GetObjectRequest::node, node),
576  Field(&GetObjectRequest::object, BRAKE_TEST_STATUS_OBJECT_INDEX)),
577  _))
578  .Times(AtLeast(1))
579  .WillRepeatedly(DoAll(SetArgReferee<1>(status_resp), Return(true)));
580 
581  /**********
582  * Step 2 *
583  **********/
584  BrakeTest srv;
585  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
586  EXPECT_FALSE(srv.response.success);
587  EXPECT_EQ(BrakeTestErrorCodes::GET_STATUS_FAILURE, srv.response.error_code.value);
588 
589  /**********
590  * Step 3 *
591  **********/
592  Mock::VerifyAndClearExpectations(&canopen_chain_node);
593  }
594 }
595 
614 TEST_F(CanOpenBraketestAdapterTest, testBrakeTestStatusUnknown)
615 {
617 
618  GetObjectResponse status_resp;
619  status_resp.success = true;
620  status_resp.value = "\0";
621 
622  for (size_t node_index : NODE_TEST_SET)
623  {
624  std::string node{ NODE_NAMES_PREFIX + std::to_string(node_index + 1) };
625 
626  /**********
627  * Step 1 *
628  **********/
629  // Avoid unmatched expectations
630  canopen_chain_node.expectAnything();
631 
632  EXPECT_CALL(canopen_chain_node, get_obj(AllOf(Field(&GetObjectRequest::node, node),
633  Field(&GetObjectRequest::object, BRAKE_TEST_STATUS_OBJECT_INDEX)),
634  _))
635  .Times(AtLeast(1))
636  .WillRepeatedly(DoAll(SetArgReferee<1>(status_resp), Return(true)));
637 
638  /**********
639  * Step 2 *
640  **********/
641  BrakeTest srv;
642  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
643  EXPECT_FALSE(srv.response.success);
644  EXPECT_EQ(BrakeTestErrorCodes::STATUS_UNKNOWN, srv.response.error_code.value);
645 
646  /**********
647  * Step 3 *
648  **********/
649  Mock::VerifyAndClearExpectations(&canopen_chain_node);
650  }
651 }
652 
671 TEST_F(CanOpenBraketestAdapterTest, testBrakeTestStatusPerformed)
672 {
674 
675  GetObjectResponse status_resp;
676  status_resp.success = true;
677  status_resp.value = "\x01";
678 
679  for (size_t node_index : NODE_TEST_SET)
680  {
681  std::string node{ NODE_NAMES_PREFIX + std::to_string(node_index + 1) };
682 
683  /**********
684  * Step 1 *
685  **********/
686  // Avoid unmatched expectations
687  canopen_chain_node.expectAnything();
688 
689  EXPECT_CALL(canopen_chain_node, get_obj(AllOf(Field(&GetObjectRequest::node, node),
690  Field(&GetObjectRequest::object, BRAKE_TEST_STATUS_OBJECT_INDEX)),
691  _))
692  .Times(AtLeast(1))
693  .WillRepeatedly(DoAll(SetArgReferee<1>(status_resp), Return(true)));
694 
695  /**********
696  * Step 2 *
697  **********/
698  BrakeTest srv;
699  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
700  EXPECT_FALSE(srv.response.success);
701  EXPECT_EQ(BrakeTestErrorCodes::STATUS_PERFORMING, srv.response.error_code.value);
702 
703  /**********
704  * Step 3 *
705  **********/
706  Mock::VerifyAndClearExpectations(&canopen_chain_node);
707  }
708 }
709 
728 TEST_F(CanOpenBraketestAdapterTest, testBrakeTestStatusNotSuccessful)
729 {
731 
732  GetObjectResponse status_resp;
733  status_resp.success = true;
734  status_resp.value = "\x03";
735 
736  for (size_t node_index : NODE_TEST_SET)
737  {
738  std::string node{ NODE_NAMES_PREFIX + std::to_string(node_index + 1) };
739 
740  /**********
741  * Step 1 *
742  **********/
743  // Avoid unmatched expectations
744  canopen_chain_node.expectAnything();
745 
746  EXPECT_CALL(canopen_chain_node, get_obj(AllOf(Field(&GetObjectRequest::node, node),
747  Field(&GetObjectRequest::object, BRAKE_TEST_STATUS_OBJECT_INDEX)),
748  _))
749  .Times(AtLeast(1))
750  .WillRepeatedly(DoAll(SetArgReferee<1>(status_resp), Return(true)));
751 
752  /**********
753  * Step 2 *
754  **********/
755  BrakeTest srv;
756  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
757  EXPECT_FALSE(srv.response.success);
758  EXPECT_EQ(BrakeTestErrorCodes::STATUS_NO_SUCCESS, srv.response.error_code.value);
759 
760  /**********
761  * Step 3 *
762  **********/
763  Mock::VerifyAndClearExpectations(&canopen_chain_node);
764  }
765 }
766 
786 TEST_F(CanOpenBraketestAdapterTest, testBrakeTestStatusNotActivelyControlled)
787 {
789 
790  GetObjectResponse status_resp;
791  status_resp.success = true;
792  status_resp.value = "\x04";
793 
794  for (size_t node_index : NODE_TEST_SET)
795  {
796  std::string node{ NODE_NAMES_PREFIX + std::to_string(node_index + 1) };
797 
798  /**********
799  * Step 1 *
800  **********/
801  // Avoid unmatched expectations
802  canopen_chain_node.expectAnything();
803 
804  EXPECT_CALL(canopen_chain_node, get_obj(AllOf(Field(&GetObjectRequest::node, node),
805  Field(&GetObjectRequest::object, BRAKE_TEST_STATUS_OBJECT_INDEX)),
806  _))
807  .Times(AtLeast(1))
808  .WillRepeatedly(DoAll(SetArgReferee<1>(status_resp), Return(true)));
809 
810  /**********
811  * Step 2 *
812  **********/
813  BrakeTest srv;
814  EXPECT_TRUE(brake_test_srv_client.call(srv)) << "Failed to call brake test service.";
815  EXPECT_FALSE(srv.response.success);
816  EXPECT_EQ(BrakeTestErrorCodes::STATUS_NO_CONTROL, srv.response.error_code.value);
817 
818  /**********
819  * Step 3 *
820  **********/
821  Mock::VerifyAndClearExpectations(&canopen_chain_node);
822  }
823 }
824 
825 } // namespace canopen_braketest_adapter_test
826 
827 int main(int argc, char* argv[])
828 {
829  ros::init(argc, argv, "unittest_brake_test_executor");
830  ros::NodeHandle nh;
831 
833  spinner.start();
834 
835  testing::InitGoogleMock(&argc, argv);
836  return RUN_ALL_TESTS();
837 }
static const std::vector< size_t > NODE_TEST_SET
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void shutdownSetService()
Un-advertise the set service.
void shutdownGetService()
Un-advertise the get service.
int main(int argc, char *argv[])
void spinner()
Executes the brake test for all joints. A brake test is triggered via service call.
TEST_F(CanOpenBraketestAdapterTest, testCANOpenBrakeTestAdapterExceptionDtor)


prbt_hardware_support
Author(s):
autogenerated on Wed Nov 25 2020 03:10:38