add_trajectory_handler_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
22 #include "google/protobuf/text_format.h"
23 #include "gtest/gtest.h"
24 
25 namespace cartographer {
26 namespace cloud {
27 namespace handlers {
28 namespace {
29 
30 using ::testing::_;
31 using ::testing::ContainerEq;
32 using ::testing::Eq;
33 using ::testing::Return;
34 using ::testing::ReturnRef;
35 using ::testing::Test;
36 using ::testing::Truly;
37 
38 const std::string kMessage = R"(
39  expected_sensor_ids {
40  id: "range_sensor"
41  type: RANGE
42  }
43  expected_sensor_ids {
44  id: "imu_sensor"
45  type: IMU
46  }
47  trajectory_builder_options {
48  trajectory_builder_2d_options {
49  min_range: 20
50  max_range: 30
51  }
52  pure_localization: true
53  initial_trajectory_pose {
54  relative_pose {
55  translation {
56  x: 1 y: 2 z: 3
57  }
58  rotation {
59  w: 4 x: 5 y: 6 z: 7
60  }
61  }
62  to_trajectory_id: 8
63  timestamp: 9
64  }
65  }
66  )";
67 
68 class AddTrajectoryHandlerTest
69  : public testing::HandlerTest<AddTrajectorySignature,
70  AddTrajectoryHandler> {
71  public:
72  void SetUp() override {
74  mock_map_builder_ = common::make_unique<mapping::testing::MockMapBuilder>();
75  EXPECT_CALL(*mock_map_builder_context_,
76  GetLocalSlamResultCallbackForSubscriptions())
77  .WillOnce(Return(nullptr));
78  EXPECT_CALL(*mock_map_builder_context_, map_builder())
79  .WillOnce(ReturnRef(*mock_map_builder_));
80  }
81 
82  protected:
83  std::set<mapping::TrajectoryBuilderInterface::SensorId> ParseSensorIds(
84  const proto::AddTrajectoryRequest &request) {
85  std::set<mapping::TrajectoryBuilderInterface::SensorId> expected_sensor_ids;
86  for (const auto &sensor_id : request.expected_sensor_ids()) {
87  expected_sensor_ids.insert(cloud::FromProto(sensor_id));
88  }
89  return expected_sensor_ids;
90  }
91 
92  std::unique_ptr<mapping::testing::MockMapBuilder> mock_map_builder_;
93 };
94 
95 TEST_F(AddTrajectoryHandlerTest, NoLocalSlamUploader) {
96  SetNoLocalTrajectoryUploader();
97  proto::AddTrajectoryRequest request;
98  EXPECT_TRUE(
99  google::protobuf::TextFormat::ParseFromString(kMessage, &request));
100  EXPECT_CALL(*mock_map_builder_,
101  AddTrajectoryBuilder(ContainerEq(ParseSensorIds(request)),
103  &request.trajectory_builder_options())),
104  _))
105  .WillOnce(Return(13));
106  test_server_->SendWrite(request);
107  EXPECT_EQ(test_server_->response().trajectory_id(), 13);
108 }
109 
110 TEST_F(AddTrajectoryHandlerTest, WithLocalSlamUploader) {
111  SetMockLocalTrajectoryUploader();
112  proto::AddTrajectoryRequest request;
113  EXPECT_TRUE(
114  google::protobuf::TextFormat::ParseFromString(kMessage, &request));
115  EXPECT_CALL(*mock_map_builder_,
116  AddTrajectoryBuilder(ContainerEq(ParseSensorIds(request)),
118  &request.trajectory_builder_options())),
119  _))
120  .WillOnce(Return(13));
121  auto upstream_trajectory_builder_options =
122  request.trajectory_builder_options();
123  upstream_trajectory_builder_options.clear_trajectory_builder_2d_options();
124  upstream_trajectory_builder_options.clear_trajectory_builder_3d_options();
125  upstream_trajectory_builder_options.set_pure_localization(false);
126  EXPECT_CALL(*mock_local_trajectory_uploader_,
127  AddTrajectory(Eq(13), ParseSensorIds(request),
129  &upstream_trajectory_builder_options))));
130  test_server_->SendWrite(request);
131  EXPECT_EQ(test_server_->response().trajectory_id(), 13);
132 }
133 
134 } // namespace
135 } // namespace handlers
136 } // namespace cloud
137 } // namespace cartographer
mapping::TrajectoryBuilderInterface::SensorId FromProto(const proto::SensorId &proto)
std::unique_ptr< mapping::testing::MockMapBuilder > mock_map_builder_
ProtoPredicateType BuildProtoPredicateEquals(const google::protobuf::Message *proto)


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58