16 #include <gmock/gmock.h> 17 #include <gtest/gtest.h> 19 #include <aws/core/Aws.h> 20 #include <aws/core/config/AWSProfileConfigLoader.h> 21 #include <aws/core/utils/HashingUtils.h> 22 #include <aws/core/utils/Outcome.h> 23 #include <aws/core/utils/logging/AWSLogging.h> 24 #include <aws/core/utils/logging/DefaultLogSystem.h> 26 #include <aws/lex/LexRuntimeServiceClient.h> 34 using testing::Return;
35 using testing::Invoke;
36 using testing::ElementsAreArray;
37 using testing::UnorderedElementsAreArray;
40 using lex_common_msgs::AudioTextConversation;
49 const lex_common_msgs::KeyValue &right) {
50 return left.key == right.key && left.value == right.value;
57 MOCK_METHOD2(PostContent,
61 typedef std::pair<std::string, std::string>
SlotPair;
76 operator lex_common_msgs::KeyValue & ()
const {
77 key_value.key = data.first;
78 key_value.value = data.second;
89 TEST(LexNodeSuite, BuildLexNodeWithEmptyParams)
93 ASSERT_EQ(ErrorCode::INVALID_ARGUMENT, error);
108 const std::shared_ptr<MockPostContentInterface> & mock_post_content,
109 const std::shared_ptr<AudioTextConversation::Request> & test_request,
110 std::shared_ptr<AudioTextConversation::Response> & test_result)
112 auto lex_node = std::make_shared<Aws::Lex::LexNode>();
115 ASSERT_EQ(ErrorCode::SUCCESS, error);
118 AsyncSpinner executor(1);
122 auto client = nh.
serviceClient<lex_common_msgs::AudioTextConversation>(
125 ASSERT_TRUE(client.exists()) <<
"Lex node service was not ready in time";
126 AWS_LOG_INFO(__func__,
"Sending lex request");
127 ASSERT_EQ(will_succeed, client.call(*test_request, *test_result));
129 AWS_LOG_INFO(__func__,
"Lex request complete");
135 TEST(LexNodeSuite, TestLexServiceFailedPostContent)
137 auto test_request = std::make_shared<AudioTextConversation::Request>();
138 test_request->text_request =
"text_request_test";
139 test_request->content_type =
"content_type_test";
140 test_request->accept_type =
"accept_type_test";
141 test_request->audio_request.data = std::vector<std::uint8_t>{1, 2, 3};
142 auto mock_post_content = std::make_shared<MockPostContentInterface>();
143 EXPECT_CALL(*mock_post_content, PostContent(_, _))
144 .WillOnce(Return(ErrorCode::FAILED_POST_CONTENT));
145 auto result = std::make_shared<AudioTextConversation::Response>();
153 TEST(LexNodeSuite, TestLexServiceSuccess)
155 ROS_DEBUG(
"Starting TestLexServiceSuccess");
156 auto test_request = std::make_shared<AudioTextConversation::Request>();
157 test_request->text_request =
"text_request_test";
158 test_request->content_type =
"content_type_test";
159 test_request->accept_type =
"accept_type_test";
160 test_request->audio_request.data = std::vector<std::uint8_t>{1, 2, 3};
169 test_response.
slots = {{
"slot_1_key",
"slot_1_value"}, {
"slot_2_key",
"slot_2_value"}};
170 std::vector<PairKeyValue> slots;
171 std::transform(test_response.
slots.begin(), test_response.
slots.end(), std::back_inserter(
175 auto mock_post_content = std::make_shared<MockPostContentInterface>();
178 EXPECT_EQ(test_request->content_type, request.content_type);
179 EXPECT_EQ(test_request->text_request, request.text_request);
180 EXPECT_EQ(test_request->audio_request.data.size(), request.audio_request.size());
181 EXPECT_THAT(request.audio_request, ElementsAreArray(test_request->audio_request.data));
189 response.slots = test_response.
slots;
190 return ErrorCode::SUCCESS;
192 EXPECT_CALL(*mock_post_content, PostContent(_, _)).WillOnce(Invoke(record_content));
193 auto result = std::make_shared<AudioTextConversation::Response>();
196 EXPECT_EQ(test_response.
text_response, result->text_response);
198 ASSERT_THAT(result->audio_response.data, ElementsAreArray(test_response.
audio_response));
199 EXPECT_EQ(test_response.
dialog_state, result->dialog_state);
200 EXPECT_EQ(test_response.
intent_name, result->intent_name);
202 ASSERT_THAT(result->slots, UnorderedElementsAreArray(slots));
212 std::cout <<
"Signal interrupt" << std::endl;
216 int main(
int argc,
char ** argv) {
219 Aws::Utils::Logging::InitializeAWSLogging(
220 Aws::MakeShared<Aws::Utils::Logging::AWSROSLogger>(
"test_node"));
221 ::testing::InitGoogleMock(&argc, argv);
222 auto result = RUN_ALL_TESTS();
223 Aws::Utils::Logging::ShutdownAWSLogging();
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
std::string session_attributes
PairKeyValue(const SlotPair &data_pair)
void h_sig_sigint(int signum)
lex_common_msgs::KeyValue key_value
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
TEST(LexNodeSuite, BuildLexNodeWithEmptyParams)
ErrorCode Init(std::shared_ptr< PostContentInterface > lex_interactor)
std::string message_format_type
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
int main(int argc, char **argv)
std::unordered_map< std::string, std::string > slots
bool operator==(const lex_common_msgs::KeyValue &left, const lex_common_msgs::KeyValue &right)
ros1 messages do not provide == operators
std::string text_response
ROSCPP_DECL void shutdown()
std::vector< uint8_t > audio_response
std::pair< std::string, std::string > SlotPair
void ExecuteLexServiceTest(bool will_succeed, const std::shared_ptr< MockPostContentInterface > &mock_post_content, const std::shared_ptr< AudioTextConversation::Request > &test_request, std::shared_ptr< AudioTextConversation::Response > &test_result)