Skip to content

Commit

Permalink
backend: add SetReturnToLaunchAltitude to action
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasVautherin committed Aug 30, 2018
1 parent 0bcf550 commit b908e6c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
23 changes: 23 additions & 0 deletions backend/src/plugins/action/action_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,29 @@ class ActionServiceImpl final : public rpc::action::ActionService::Service {
return grpc::Status::OK;
}

grpc::Status
SetReturnToLaunchAltitude(grpc::ServerContext * /* context */,
const rpc::action::SetReturnToLaunchAltitudeRequest *request,
rpc::action::SetReturnToLaunchAltitudeResponse *response) override
{
if (request != nullptr) {
const auto requested_altitude = request->relative_altitude_m();
const auto action_result =
_action.set_return_to_launch_return_altitude(requested_altitude);

if (response != nullptr) {
auto *rpc_action_result = new rpc::action::ActionResult();
rpc_action_result->set_result(
static_cast<rpc::action::ActionResult::Result>(action_result));
rpc_action_result->set_result_str(action_result_str(action_result));

response->set_allocated_action_result(rpc_action_result);
}
}

return grpc::Status::OK;
}

private:
Action &_action;
};
Expand Down
53 changes: 53 additions & 0 deletions backend/test/action_service_impl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ std::string
transitionToMCAndGetTranslatedResult(const dronecode_sdk::ActionResult transition_to_fw_result);
std::string
getReturnToLaunchAltitudeAndGetTranslatedResult(const dronecode_sdk::ActionResult action_result);
std::string
setReturnToLaunchAltitudeAndGetTranslatedResult(const dronecode_sdk::ActionResult action_result);

class ActionServiceImplTest : public ::testing::TestWithParam<InputPair> {};

Expand Down Expand Up @@ -442,6 +444,57 @@ TEST_F(ActionServiceImplTest, getReturnToLaunchAltitudeDoesNotCrashWithNullRespo
actionService.GetReturnToLaunchAltitude(nullptr, nullptr, nullptr);
}

TEST_P(ActionServiceImplTest, setReturnToLaunchAltitudeResultIsTranslatedCorrectly)
{
const auto rpc_result = setReturnToLaunchAltitudeAndGetTranslatedResult(GetParam().second);
EXPECT_EQ(rpc_result, GetParam().first);
}

std::string
setReturnToLaunchAltitudeAndGetTranslatedResult(const dronecode_sdk::ActionResult action_result)
{
MockAction action;
ON_CALL(action, set_return_to_launch_return_altitude(_)).WillByDefault(Return(action_result));
ActionServiceImpl actionService(action);
dronecode_sdk::rpc::action::SetReturnToLaunchAltitudeRequest request;
request.set_relative_altitude_m(ARBITRARY_ALTITUDE);
dronecode_sdk::rpc::action::SetReturnToLaunchAltitudeResponse response;

actionService.SetReturnToLaunchAltitude(nullptr, &request, &response);

return ActionResult::Result_Name(response.action_result().result());
}

TEST_F(ActionServiceImplTest, setReturnToLaunchAltitudeDoesNotCrashWithNullParams)
{
MockAction action;
ActionServiceImpl actionService(action);

actionService.SetReturnToLaunchAltitude(nullptr, nullptr, nullptr);
}

TEST_F(ActionServiceImplTest, setReturnToLaunchAltitudeCallsSetter)
{
MockAction action;
ActionServiceImpl actionService(action);
EXPECT_CALL(action, set_return_to_launch_return_altitude(_)).Times(1);
dronecode_sdk::rpc::action::SetReturnToLaunchAltitudeRequest request;

actionService.SetReturnToLaunchAltitude(nullptr, &request, nullptr);
}

TEST_P(ActionServiceImplTest, setReturnToLaunchAltitudeSetsRightValue)
{
MockAction action;
ActionServiceImpl actionService(action);
float expected_altitude = ARBITRARY_ALTITUDE;
EXPECT_CALL(action, set_return_to_launch_return_altitude(expected_altitude)).Times(1);
dronecode_sdk::rpc::action::SetReturnToLaunchAltitudeRequest request;
request.set_relative_altitude_m(expected_altitude);

actionService.SetReturnToLaunchAltitude(nullptr, &request, nullptr);
}

INSTANTIATE_TEST_CASE_P(ActionResultCorrespondences,
ActionServiceImplTest,
::testing::ValuesIn(generateInputPairs()));
Expand Down
1 change: 1 addition & 0 deletions plugins/action/mocks/action_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MockAction {
MOCK_CONST_METHOD0(get_max_speed, std::pair<ActionResult, float>());
MOCK_CONST_METHOD1(set_max_speed, ActionResult(float));
MOCK_CONST_METHOD0(get_return_to_launch_return_altitude, std::pair<ActionResult, float>());
MOCK_CONST_METHOD1(set_return_to_launch_return_altitude, ActionResult(float));
};

} // namespace testing
Expand Down

0 comments on commit b908e6c

Please sign in to comment.