diff --git a/cartographer_grpc/framework/testing/rpc_handler_test_server.h b/cartographer_grpc/framework/testing/rpc_handler_test_server.h index 79e98f2..e56e1e2 100644 --- a/cartographer_grpc/framework/testing/rpc_handler_test_server.h +++ b/cartographer_grpc/framework/testing/rpc_handler_test_server.h @@ -87,6 +87,10 @@ class RpcHandlerTestServer : public Server { WaitForHandlerCompletion(RpcHandlerWrapper::ON_FINISH); } + const typename RpcHandlerType::ResponseType &response() { + return client_.response(); + } + private: using ClientWriter = ::grpc::internal::ClientWriterFactory< typename RpcHandlerType::RequestType>; diff --git a/cartographer_grpc/handlers/add_fixed_frame_pose_data_handler_test.cc b/cartographer_grpc/handlers/add_fixed_frame_pose_data_handler_test.cc index e655e2d..ed2abb8 100644 --- a/cartographer_grpc/handlers/add_fixed_frame_pose_data_handler_test.cc +++ b/cartographer_grpc/handlers/add_fixed_frame_pose_data_handler_test.cc @@ -15,7 +15,7 @@ */ #include "cartographer_grpc/handlers/add_fixed_frame_pose_data_handler.h" -#include "cartographer_grpc/testing/add_data_handler_test.h" +#include "cartographer_grpc/testing/handler_test.h" #include "cartographer_grpc/testing/test_helpers.h" #include "google/protobuf/text_format.h" #include "gtest/gtest.h" @@ -52,7 +52,7 @@ const std::string kMessage = R"PROTO( })PROTO"; using AddFixedFramePoseDataHandlerTest = - testing::AddDataHandlerTest; + testing::HandlerTest; TEST_F(AddFixedFramePoseDataHandlerTest, NoLocalSlamUploader) { proto::AddFixedFramePoseDataRequest request; diff --git a/cartographer_grpc/handlers/add_imu_data_handler_test.cc b/cartographer_grpc/handlers/add_imu_data_handler_test.cc index 504a74a..7094b3b 100644 --- a/cartographer_grpc/handlers/add_imu_data_handler_test.cc +++ b/cartographer_grpc/handlers/add_imu_data_handler_test.cc @@ -15,7 +15,7 @@ */ #include "cartographer_grpc/handlers/add_imu_data_handler.h" -#include "cartographer_grpc/testing/add_data_handler_test.h" +#include "cartographer_grpc/testing/handler_test.h" #include "cartographer_grpc/testing/test_helpers.h" #include "google/protobuf/text_format.h" #include "gtest/gtest.h" @@ -48,7 +48,7 @@ const std::string kMessage = R"PROTO( } })PROTO"; -using AddImuDataHandlerTest = testing::AddDataHandlerTest; +using AddImuDataHandlerTest = testing::HandlerTest; TEST_F(AddImuDataHandlerTest, NoLocalSlamUploader) { proto::AddImuDataRequest request; diff --git a/cartographer_grpc/handlers/add_landmark_data_handler_test.cc b/cartographer_grpc/handlers/add_landmark_data_handler_test.cc index a3f273d..fc982c0 100644 --- a/cartographer_grpc/handlers/add_landmark_data_handler_test.cc +++ b/cartographer_grpc/handlers/add_landmark_data_handler_test.cc @@ -15,7 +15,7 @@ */ #include "cartographer_grpc/handlers/add_landmark_data_handler.h" -#include "cartographer_grpc/testing/add_data_handler_test.h" +#include "cartographer_grpc/testing/handler_test.h" #include "cartographer_grpc/testing/test_helpers.h" #include "google/protobuf/text_format.h" #include "gtest/gtest.h" @@ -56,8 +56,7 @@ const std::string kMessage = R"PROTO( } })PROTO"; -using AddLandmarkDataHandlerTest = - testing::AddDataHandlerTest; +using AddLandmarkDataHandlerTest = testing::HandlerTest; TEST_F(AddLandmarkDataHandlerTest, NoLocalSlamUploader) { proto::AddLandmarkDataRequest request; diff --git a/cartographer_grpc/handlers/add_odometry_data_handler_test.cc b/cartographer_grpc/handlers/add_odometry_data_handler_test.cc index 8f464c7..22f63f1 100644 --- a/cartographer_grpc/handlers/add_odometry_data_handler_test.cc +++ b/cartographer_grpc/handlers/add_odometry_data_handler_test.cc @@ -15,7 +15,7 @@ */ #include "cartographer_grpc/handlers/add_odometry_data_handler.h" -#include "cartographer_grpc/testing/add_data_handler_test.h" +#include "cartographer_grpc/testing/handler_test.h" #include "cartographer_grpc/testing/test_helpers.h" #include "google/protobuf/text_format.h" #include "gtest/gtest.h" @@ -51,8 +51,7 @@ const std::string kMessage = R"PROTO( } })PROTO"; -using AddOdometryDataHandlerTest = - testing::AddDataHandlerTest; +using AddOdometryDataHandlerTest = testing::HandlerTest; TEST_F(AddOdometryDataHandlerTest, NoLocalSlamUploader) { proto::AddOdometryDataRequest request; diff --git a/cartographer_grpc/handlers/add_rangefind_data_handler_test.cc b/cartographer_grpc/handlers/add_rangefind_data_handler_test.cc index cc820ba..7deace0 100644 --- a/cartographer_grpc/handlers/add_rangefind_data_handler_test.cc +++ b/cartographer_grpc/handlers/add_rangefind_data_handler_test.cc @@ -15,7 +15,7 @@ */ #include "cartographer_grpc/handlers/add_rangefinder_data_handler.h" -#include "cartographer_grpc/testing/add_data_handler_test.h" +#include "cartographer_grpc/testing/handler_test.h" #include "cartographer_grpc/testing/test_helpers.h" #include "google/protobuf/text_format.h" #include "gtest/gtest.h" @@ -50,7 +50,7 @@ const std::string kMessage = R"PROTO( })PROTO"; using AddRangefinderDataHandlerTest = - testing::AddDataHandlerTest; + testing::HandlerTest; TEST_F(AddRangefinderDataHandlerTest, NoLocalSlamUploader) { proto::AddRangefinderDataRequest request; diff --git a/cartographer_grpc/handlers/add_trajectory_handler_test.cc b/cartographer_grpc/handlers/add_trajectory_handler_test.cc new file mode 100644 index 0000000..995ec64 --- /dev/null +++ b/cartographer_grpc/handlers/add_trajectory_handler_test.cc @@ -0,0 +1,140 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cartographer_grpc/handlers/add_trajectory_handler.h" +#include "cartographer_grpc/sensor/serialization.h" +#include "cartographer_grpc/testing/handler_test.h" +#include "cartographer_grpc/testing/mock_map_builder.h" +#include "cartographer_grpc/testing/test_helpers.h" +#include "google/protobuf/text_format.h" +#include "gtest/gtest.h" + +namespace cartographer_grpc { +namespace handlers { +namespace { + +using ::testing::_; +using ::testing::ContainerEq; +using ::testing::Eq; +using ::testing::Return; +using ::testing::ReturnRef; +using ::testing::Test; +using ::testing::Truly; + +const std::string kMessage = R"PROTO( + expected_sensor_ids { + id: "range_sensor" + type: RANGE + } + expected_sensor_ids { + id: "imu_sensor" + type: IMU + } + trajectory_builder_options { + trajectory_builder_2d_options { + min_range: 20 + max_range: 30 + } + pure_localization: true + initial_trajectory_pose { + relative_pose { + translation { + x: 1 + y: 2 + z: 3 + } + rotation { + x: 4 + y: 5 + z: 6 + w: 7 + } + } + to_trajectory_id: 8 + timestamp: 9 + } + } + )PROTO"; + +class AddTrajectoryHandlerTest + : public testing::HandlerTest { + public: + void SetUp() override { + testing::HandlerTest::SetUp(); + mock_map_builder_ = + cartographer::common::make_unique(); + EXPECT_CALL(*mock_map_builder_context_, + GetLocalSlamResultCallbackForSubscriptions()) + .WillOnce(Return(nullptr)); + EXPECT_CALL(*mock_map_builder_context_, map_builder()) + .WillOnce(ReturnRef(*mock_map_builder_)); + } + + protected: + std::set + ParseSensorIds(const proto::AddTrajectoryRequest &request) { + std::set + expected_sensor_ids; + for (const auto &sensor_id : request.expected_sensor_ids()) { + expected_sensor_ids.insert(sensor::FromProto(sensor_id)); + } + return expected_sensor_ids; + } + + std::unique_ptr mock_map_builder_; +}; + +TEST_F(AddTrajectoryHandlerTest, NoLocalSlamUploader) { + SetNoLocalTrajectoryUploader(); + proto::AddTrajectoryRequest request; + EXPECT_TRUE( + google::protobuf::TextFormat::ParseFromString(kMessage, &request)); + EXPECT_CALL(*mock_map_builder_, + AddTrajectoryBuilder(ContainerEq(ParseSensorIds(request)), + Truly(testing::BuildProtoPredicateEquals( + &request.trajectory_builder_options())), + _)) + .WillOnce(Return(13)); + test_server_->SendWrite(request); + EXPECT_EQ(test_server_->response().trajectory_id(), 13); +} + +TEST_F(AddTrajectoryHandlerTest, WithLocalSlamUploader) { + SetMockLocalTrajectoryUploader(); + proto::AddTrajectoryRequest request; + EXPECT_TRUE( + google::protobuf::TextFormat::ParseFromString(kMessage, &request)); + EXPECT_CALL(*mock_map_builder_, + AddTrajectoryBuilder(ContainerEq(ParseSensorIds(request)), + Truly(testing::BuildProtoPredicateEquals( + &request.trajectory_builder_options())), + _)) + .WillOnce(Return(13)); + auto upstream_trajectory_builder_options = + request.trajectory_builder_options(); + upstream_trajectory_builder_options.clear_trajectory_builder_2d_options(); + upstream_trajectory_builder_options.clear_trajectory_builder_3d_options(); + EXPECT_CALL(*mock_local_trajectory_uploader_, + AddTrajectory(Eq(13), ParseSensorIds(request), + Truly(testing::BuildProtoPredicateEquals( + &upstream_trajectory_builder_options)))); + test_server_->SendWrite(request); + EXPECT_EQ(test_server_->response().trajectory_id(), 13); +} + +} // namespace +} // namespace handlers +} // namespace cartographer_grpc diff --git a/cartographer_grpc/testing/add_data_handler_test.h b/cartographer_grpc/testing/handler_test.h similarity index 90% rename from cartographer_grpc/testing/add_data_handler_test.h rename to cartographer_grpc/testing/handler_test.h index 00c4781..c0fb773 100644 --- a/cartographer_grpc/testing/add_data_handler_test.h +++ b/cartographer_grpc/testing/handler_test.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef CARTOGRAPHER_GRPC_TESTING_ADD_DATA_HANDLER_TEST_H -#define CARTOGRAPHER_GRPC_TESTING_ADD_DATA_HANDLER_TEST_H +#ifndef CARTOGRAPHER_GRPC_TESTING_HANDLER_TEST_H +#define CARTOGRAPHER_GRPC_TESTING_HANDLER_TEST_H #include "cartographer/common/make_unique.h" #include "cartographer_grpc/framework/testing/rpc_handler_test_server.h" @@ -30,7 +30,7 @@ using ::testing::Return; using ::testing::Test; template -class AddDataHandlerTest : public Test { +class HandlerTest : public Test { public: void SetUp() override { test_server_ = cartographer::common::make_unique< @@ -63,4 +63,4 @@ class AddDataHandlerTest : public Test { } // namespace testing } // namespace cartographer_grpc -#endif // CARTOGRAPHER_GRPC_TESTING_ADD_DATA_HANDLER_TEST_H +#endif // CARTOGRAPHER_GRPC_TESTING_HANDLER_TEST_H