lex_node_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 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 
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
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>
25 
26 #include <aws/lex/LexRuntimeServiceClient.h>
28 
30 
31 #include <lex_node/lex_node.h>
32 #include <ros/ros.h>
33 
34 using testing::Return;
35 using testing::Invoke;
36 using testing::ElementsAreArray;
37 using testing::UnorderedElementsAreArray;
38 using testing::_;
39 
40 using lex_common_msgs::AudioTextConversation;
42 
43 namespace lex_common_msgs
44 {
48 bool operator==(const lex_common_msgs::KeyValue &left,
49  const lex_common_msgs::KeyValue &right) {
50  return left.key == right.key && left.value == right.value;
51 }
52 } // namespace lex_common_msgs
53 
55 {
56  public:
57  MOCK_METHOD2(PostContent,
58  ErrorCode(const Aws::Lex::LexRequest & request, Aws::Lex::LexResponse & response));
59 };
60 
61 typedef std::pair<std::string, std::string> SlotPair;
62 
67 {
69  mutable lex_common_msgs::KeyValue key_value;
70 
71  explicit PairKeyValue(const SlotPair & data_pair)
72  {
73  data = data_pair;
74  }
75 
76  operator lex_common_msgs::KeyValue & () const {
77  key_value.key = data.first;
78  key_value.value = data.second;
79  return key_value;
80  }
81  operator SlotPair &() {
82  return data;
83  }
84 };
85 
89 TEST(LexNodeSuite, BuildLexNodeWithEmptyParams)
90 {
91  Aws::Lex::LexNode lex_node;
92  ErrorCode error = lex_node.Init(nullptr);
93  ASSERT_EQ(ErrorCode::INVALID_ARGUMENT, error);
94 }
95 
107  bool will_succeed,
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)
111 {
112  auto lex_node = std::make_shared<Aws::Lex::LexNode>();
113 
114  Aws::Lex::ErrorCode error = lex_node->Init(mock_post_content);
115  ASSERT_EQ(ErrorCode::SUCCESS, error);
116 
117  using ros::AsyncSpinner;
118  AsyncSpinner executor(1);
119  ros::Duration timeout(20);
120  executor.start();
121  ros::NodeHandle nh("~");
122  auto client = nh.serviceClient<lex_common_msgs::AudioTextConversation>(
123  "lex_conversation");
124  client.waitForExistence(timeout);
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));
128  executor.stop();
129  AWS_LOG_INFO(__func__, "Lex request complete");
130 }
131 
135 TEST(LexNodeSuite, TestLexServiceFailedPostContent)
136 {
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>();
146  ExecuteLexServiceTest(false, mock_post_content, test_request, result);
147  // EXPECT_EQ(ErrorCode::FAILED_POST_CONTENT, (ErrorCode) result->error_code);
148 }
149 
153 TEST(LexNodeSuite, TestLexServiceSuccess)
154 {
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};
161 
162  Aws::Lex::LexResponse test_response;
163  test_response.text_response = "text_response_test";
164  test_response.message_format_type = "message_format_type_test";
165  test_response.intent_name = "intent_name_test";
166  test_response.dialog_state = "dialog_state_test";
167  test_response.audio_response = std::vector<std::uint8_t>{4, 5};
168  test_response.session_attributes = "session_attributes_test";
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(
172  slots), [](const SlotPair &pair) {
173  return PairKeyValue(pair);
174  });
175  auto mock_post_content = std::make_shared<MockPostContentInterface>();
176  auto record_content = [&test_request, &test_response](const Aws::Lex::LexRequest & request,
177  Aws::Lex::LexResponse & response) -> ErrorCode {
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));
182 
183  response.text_response = test_response.text_response;
184  response.message_format_type = test_response.message_format_type;
185  response.intent_name = test_response.intent_name;
186  response.dialog_state = test_response.dialog_state;
187  response.audio_response = test_response.audio_response;
188  response.session_attributes = test_response.session_attributes;
189  response.slots = test_response.slots;
190  return ErrorCode::SUCCESS;
191  };
192  EXPECT_CALL(*mock_post_content, PostContent(_, _)).WillOnce(Invoke(record_content));
193  auto result = std::make_shared<AudioTextConversation::Response>();
194  ExecuteLexServiceTest(true, mock_post_content, test_request, result);
195 
196  EXPECT_EQ(test_response.text_response, result->text_response);
197 // EXPECT_EQ(test_response.session_attributes, result->session_attributes);
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);
201  EXPECT_EQ(test_response.message_format_type, result->message_format_type);
202  ASSERT_THAT(result->slots, UnorderedElementsAreArray(slots));
203 }
204 
210 void h_sig_sigint(int signum)
211 {
212  std::cout << "Signal interrupt" << std::endl;
213  ros::shutdown();
214 }
215 
216 int main(int argc, char ** argv) {
217  signal(SIGINT, h_sig_sigint);
218  ros::init(argc, argv, "test_node");
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();
224  ros::shutdown();
225  return result;
226 }
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)
Definition: lex_node.cpp:53
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::string intent_name
std::pair< std::string, std::string > SlotPair
std::string dialog_state
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)
#define ROS_DEBUG(...)


lex_node
Author(s): AWS RoboMaker
autogenerated on Fri Mar 5 2021 03:13:38