s3_file_uploader_action_server_handler_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 
21 #include <aws/core/Aws.h>
22 #include <aws/s3/S3Client.h>
23 #include <boost/shared_ptr.hpp>
24 
25 #include <file_uploader_msgs/UploadFilesAction.h>
26 #include <file_uploader_msgs/UploadFilesActionGoal.h>
27 #include <file_uploader_msgs/UploadFilesGoal.h>
30 
31 #include <ros/ros.h>
32 
33 using namespace Aws::S3;
34 
35 using ::testing::Return;
36 using ::testing::_;
37 using ::testing::ContainerEq;
38 using ::testing::Invoke;
39 
41 {
42 public:
43  MockGoalHandle() = default;
45  (void) copy;
46  };
47  MOCK_METHOD0(setRejected, void());
48  MOCK_METHOD0(setAccepted, void());
49  MOCK_METHOD0(setAborted, void());
50  MOCK_METHOD0(setSucceeded, void());
51  MOCK_METHOD2(setCanceled, void(const file_uploader_msgs::UploadFilesResult&, const std::string &));
52  MOCK_METHOD2(setAborted, void(const file_uploader_msgs::UploadFilesResult&, const std::string &));
53  MOCK_METHOD2(setSucceeded, void(const file_uploader_msgs::UploadFilesResult&, const std::string &));
54 
55  MOCK_CONST_METHOD0(getGoalStatus, actionlib_msgs::GoalStatus());
56  MOCK_CONST_METHOD0(getGoal, boost::shared_ptr<file_uploader_msgs::UploadFilesGoal>());
57  MOCK_CONST_METHOD1(publishFeedback, void(file_uploader_msgs::UploadFilesFeedback &));
58 };
59 
60 class S3FileUploaderActionServerHandlerTests: public ::testing::Test
61 {
62 protected:
63  std::shared_ptr<MockS3UploadManager> upload_manager;
64  std::shared_ptr<MockGoalHandle> goal_handle;
66 public:
68  upload_manager(std::make_shared<MockS3UploadManager>()),
69  goal_handle(std::make_shared<MockGoalHandle>()),
70  goal(new file_uploader_msgs::UploadFilesGoal())
71  {
72  // do nothing
73  }
74  void givenUploadManagerAvailability(bool isAvailable) {
75  EXPECT_CALL(*upload_manager, IsAvailable()).WillRepeatedly(Return(isAvailable));
76  }
77 
79  givenUploadManagerAvailability(false);
80  }
81 
83  givenUploadManagerAvailability(true);
84  }
85 
87  EXPECT_CALL(*upload_manager, IsAvailable()).Times(2).WillOnce(Return(false)).WillOnce(Return(true));
88  }
89 
90  void givenUploadGoal() {
91  goal->files.push_back("test_file_name");
92  goal->upload_location = "my/upload/dir";
93  EXPECT_CALL(*goal_handle, getGoal()).WillRepeatedly(Return(goal));
94  actionlib_msgs::GoalStatus goal_status;
95  goal_status.status = actionlib_msgs::GoalStatus::PENDING;
96  ON_CALL(*goal_handle, getGoalStatus()).WillByDefault(Return(goal_status));
97  }
98 
99  void givenUploadWithOutcome(Model::PutObjectOutcome outcome) {
100  auto upload_files_action = [outcome](
101  const std::vector<UploadDescription> & upload_desc,
102  const std::string & text,
103  const boost::function<void (const std::vector<UploadDescription>&)>& feedback_fn) {
104  (void) text;
105  feedback_fn(upload_desc);
106  return outcome;
107  };
108  EXPECT_CALL(*upload_manager, UploadFiles(_, _, _)).WillOnce(Invoke(upload_files_action));
109  }
110 
112  // Successful outcomes have a result
113  givenUploadWithOutcome(Model::PutObjectOutcome(Model::PutObjectResult()));
114  }
115 
117  // Failed outcome have an error
118  givenUploadWithOutcome(Model::PutObjectOutcome(Aws::Client::AWSError<S3Errors>(S3Errors::INTERNAL_FAILURE, false)));
119  }
120 
121  void givenGoalHandleWithStatus(int status) {
122  actionlib_msgs::GoalStatus goal_status;
123  goal_status.status = status;
124  EXPECT_CALL(*goal_handle, getGoalStatus()).WillRepeatedly(Return(goal_status));
125  }
126 
128  givenGoalHandleWithStatus(actionlib_msgs::GoalStatus::PREEMPTING);
129  }
130 
132  givenGoalHandleWithStatus(actionlib_msgs::GoalStatus::PENDING);
133  }
134 
136  EXPECT_CALL(*goal_handle, setRejected());
137  }
138 
140  EXPECT_CALL(*goal_handle, setAccepted());
141  }
142 
144  EXPECT_CALL(*upload_manager, CancelUpload());
145  }
146 
148  EXPECT_CALL(*goal_handle, setSucceeded(_, _));
149  }
150 
152  EXPECT_CALL(*goal_handle, setCanceled(_, _));
153  }
154 
156  EXPECT_CALL(*goal_handle, setAborted(_, _));
157  }
158 };
159 
161 {
162  givenUnAvailableUploadManager();
163  assertGoalIsRejected();
164 
165  S3FileUploaderActionServerHandler<MockGoalHandle>::UploadToS3(*upload_manager, std::string("bucket_"), *goal_handle);
166 }
167 
169 {
170  givenAvailableUploadManager();
171  assertGoalIsAccepted();
172  givenUploadGoal();
173  givenSuccessfullUpload();
174  assertGoalIsSuccess();
175 
176  S3FileUploaderActionServerHandler<MockGoalHandle>::UploadToS3(*upload_manager, std::string("bucket_"), *goal_handle);
177 }
178 
180 {
181  givenAvailableUploadManager();
182  assertGoalIsAccepted();
183  givenUploadGoal();
184  givenFailedUpload();
185  assertGoalIsAborted();
186 
187  S3FileUploaderActionServerHandler<MockGoalHandle>::UploadToS3(*upload_manager, std::string("bucket_"), *goal_handle);
188 }
189 
191 {
192  givenAvailableUploadManager();
193  assertGoalIsAccepted();
194  givenUploadGoal();
195  givenSuccessfullUpload();
196  givenGoalHandleCancelRequested();
197  assertGoalIsCanceled();
198 
199  S3FileUploaderActionServerHandler<MockGoalHandle>::UploadToS3(*upload_manager, std::string("bucket_"), *goal_handle);
200 }
201 
202 
204 {
205  asssertUploadIsCanceled();
206 
208 }
209 
210 int main(int argc, char ** argv)
211 {
212  Aws::SDKOptions options;
213  Aws::InitAPI(options);
214  ros::Time::init();
215  ::testing::InitGoogleTest(&argc, argv);
216  auto result = RUN_ALL_TESTS();
217  Aws::ShutdownAPI(options);
218  return result;
219 }
int main(int argc, char **argv)
TEST_F(S3FileUploaderActionServerHandlerTests, TestInactiveUploadManager)
options
static void init()
static void UploadToS3(S3UploadManager &upload_manager, const std::string &bucket, T &goal_handle)
static void CancelUploadToS3(S3UploadManager &upload_manager)
boost::shared_ptr< file_uploader_msgs::UploadFilesGoal > goal
MockGoalHandle(const MockGoalHandle &copy)


s3_file_uploader
Author(s): AWS RoboMaker
autogenerated on Tue Jun 1 2021 02:51:30