lex_interactor_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 
16 #include <lex_common/lex_common.h>
17 
18 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
20 
21 #include <aws/core/utils/Outcome.h>
22 #include <aws/lex/LexRuntimeServiceClient.h>
23 #include <aws/core/utils/memory/AWSMemory.h>
24 #include <aws/lex/model/PostContentResult.h>
25 
27 
28 #include <algorithm>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 #include <istream>
33 #include <ostream>
34 #include <vector>
35 
36 namespace
37 {
38 
39 template <typename T>
40 class Mover
41 {
42 public:
43  Mover(T && object)
44  : object(std::move(object)),
45  valid(true) {}
46 
47  Mover(const Mover<T> & other)
48  : object(const_cast<T &&>(other.object)),
49  valid(true)
50  {
51  assert(other.valid);
52  other.valid = false;
53  }
54 
55  Mover & operator=(const Mover & other)
56  {
57  assert(other.valid);
58  object = const_cast<T &&>(other.object);
59  other.valid = false;
60  valid = true;
61  }
62 
63  T & get()
64  {
65  assert(valid);
66  return object;
67  }
68 
69  const T & get() const
70  {
71  assert(valid);
72  return object;
73  }
74 
75 private:
76  T object;
77  mutable bool valid;
78 };
79 
80 template <typename T>
81 inline Mover<T> Movable(T && object)
82 {
83  return Mover<T>(std::move(object));
84 }
85 
86 }
87 
88 namespace Aws
89 {
90 namespace Lex
91 {
92 using Aws::LexRuntimeService::Model::PostContentOutcome;
93 using Aws::LexRuntimeService::Model::PostContentRequest;
94 using Aws::LexRuntimeService::Model::PostContentResult;
95 using Aws::LexRuntimeService::LexRuntimeServiceErrors;
96 using testing::Return;
97 
99 {
100 public:
101  std::string user_id = "user_id";
102  std::string bot_alias = "bot_alias";
103  std::string bot_name = "bot_name";
104 
105  void ConfigureLexConfiguration(LexConfiguration & lex_configuration)
106  {
107  lex_configuration.user_id = user_id;
108  lex_configuration.bot_alias = bot_alias;
109  lex_configuration.bot_name = bot_name;
110  }
111 };
112 
114  : public Aws::LexRuntimeService::LexRuntimeServiceClient
115 {
116 public:
117  virtual ~LexRuntimeServiceClientMock() = default;
118 
119  MOCK_CONST_METHOD1(PostContent_, Mover<PostContentOutcome>(const PostContentRequest & request));
120 
121  PostContentOutcome PostContent(const PostContentRequest & request) const
122  {
123  return std::move(PostContent_(request).get());
124  }
125 };
126 
127 class TestLexInteractor : public ::testing::Test
128 {
129 protected:
130  std::shared_ptr<LexConfiguration> lex_configuration;
131  std::shared_ptr<LexRuntimeServiceClientMock> lex_runtime_client;
134  PostContentRequest default_expected_request;
136  Aws::LexRuntimeService::Model::PostContentResult result;
137 
138  void SetUp() override
139  {
140  lex_runtime_client = std::make_shared<LexRuntimeServiceClientMock>();
141  lex_configuration = std::make_shared<LexConfiguration>();
142  TestLexConfiguration configuration;
143  configuration.ConfigureLexConfiguration(*lex_configuration);
144 
145  default_request.accept_type = "accept_type";
146  default_request.content_type = "content_type";
147  default_request.text_request = "";
148 
149  default_expected_request
150  .WithBotAlias(lex_configuration->bot_alias.c_str())
151  .WithBotName(lex_configuration->bot_name.c_str())
152  .WithAccept(default_request.accept_type.c_str())
153  .WithUserId(lex_configuration->user_id.c_str());
154  default_expected_request.SetBotAlias(lex_configuration->bot_alias.c_str());
155  default_expected_request.SetContentType(default_request.content_type.c_str());
156  test_data.ConfigureExampleResult(result);
157  }
158 };
159 
160 TEST_F(TestLexInteractor, TestLexInteractorFailedConfiguration) {
162  lex_interactor.ConfigureAwsLex(nullptr, nullptr));
164  lex_interactor.ConfigureAwsLex(lex_configuration, nullptr));
166  lex_interactor.ConfigureAwsLex(nullptr, lex_runtime_client));
167 }
168 
169 TEST_F(TestLexInteractor, TestLexInteractorPostContentText) {
170  ASSERT_EQ(ErrorCode::SUCCESS, lex_interactor.ConfigureAwsLex(lex_configuration,
171  lex_runtime_client));
172  default_request.audio_request = std::vector<uint8_t>{1, 2, 3, 4, 5, 6, 7};
173  auto lex_configuration = std::make_shared<LexConfiguration>();
174  auto io_stream = std::make_shared<StringStream>();
175  std::copy(default_request.audio_request.begin(),
176  default_request.audio_request.end(), std::ostream_iterator<unsigned char>(*io_stream));
177  default_expected_request.SetBody(io_stream);
178  EXPECT_CALL(*lex_runtime_client, PostContent_(default_expected_request))
179  .WillOnce(testing::Return(Movable(PostContentOutcome(std::move(result)))));
180  // Aws::Client::AWSError<LexRuntimeServiceErrors>
181  LexResponse response;
182  ASSERT_EQ(ErrorCode::SUCCESS, lex_interactor.PostContent(default_request, response));
183  test_data.ExpectEq(response);
184 }
185 
186 TEST_F(TestLexInteractor, TestLexInteractorPostContentAudio) {
187  default_request.audio_request = std::vector<uint8_t>{1, 2, 3, 4, 5, 6, 7};
188  auto io_stream = std::make_shared<StringStream>();
189  std::copy(default_request.audio_request.begin(),
190  default_request.audio_request.end(), std::ostream_iterator<unsigned char>(*io_stream));
191  default_expected_request.SetBody(io_stream);
192 
193  ASSERT_EQ(ErrorCode::SUCCESS, lex_interactor.ConfigureAwsLex(lex_configuration,
194  lex_runtime_client));
195  TestData test_data;
196  PostContentResult result;
197  test_data.ConfigureExampleResult(result);
198  EXPECT_CALL(*lex_runtime_client, PostContent_(default_expected_request))
199  .WillOnce(testing::Return(Movable(PostContentOutcome(std::move(result)))));
200 
201  LexResponse response;
202  ASSERT_EQ(ErrorCode::SUCCESS, lex_interactor.PostContent(default_request, response));
203  test_data.ExpectEq(response);
204 }
205 
206 TEST_F(TestLexInteractor, TestLexInteractorPostContentFailed) {
207  ASSERT_EQ(ErrorCode::SUCCESS, lex_interactor.ConfigureAwsLex(lex_configuration,
208  lex_runtime_client));
209 
210  EXPECT_CALL(*lex_runtime_client, PostContent_(testing::_))
211  .WillOnce(testing::Return(Movable(PostContentOutcome(
212  Aws::Client::AWSError<LexRuntimeServiceErrors>(LexRuntimeServiceErrors::ACCESS_DENIED,
213  false)))));
214  LexResponse response;
215  ASSERT_EQ(ErrorCode::FAILED_POST_CONTENT, lex_interactor.PostContent(default_request, response));
216 }
217 
218 TEST_F(TestLexInteractor, TestLexInteractorPostContentRetry) {
219  ASSERT_EQ(ErrorCode::SUCCESS, lex_interactor.ConfigureAwsLex(lex_configuration,
220  lex_runtime_client));
221 
222  EXPECT_CALL(*lex_runtime_client, PostContent_(testing::_))
223  .WillOnce(testing::Return(Movable(PostContentOutcome(
224  Aws::Client::AWSError<LexRuntimeServiceErrors>(LexRuntimeServiceErrors::REQUEST_TIMEOUT,
225  true)))));
226  LexResponse response;
227  ASSERT_EQ(ErrorCode::RETRY_POST_CONTENT, lex_interactor.PostContent(default_request, response));
228 }
229 
230 } // namespace Lex
231 } // namespace Aws
std::string text_request
Definition: lex_common.h:64
std::shared_ptr< LexRuntimeServiceClientMock > lex_runtime_client
std::string content_type
Definition: lex_common.h:66
TEST_F(TestLexInteractor, TestLexInteractorPostContentRetry)
Aws::LexRuntimeService::Model::PostContentResult result
std::shared_ptr< LexConfiguration > lex_configuration
PostContentRequest default_expected_request
PostContentOutcome PostContent(const PostContentRequest &request) const
std::string accept_type
Definition: lex_common.h:63
void ConfigureExampleResult(Aws::LexRuntimeService::Model::PostContentResult &example_result)
Definition: test_utils.h:50
void ConfigureLexConfiguration(LexConfiguration &lex_configuration)


lex_common
Author(s): AWS RoboMaker
autogenerated on Sat Mar 6 2021 03:43:24