lex_common_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 
17 #include <lex_common/lex_common.h>
18 
19 #include <gtest/gtest.h>
20 #include <gmock/gmock.h>
21 
22 #include <aws/core/utils/Outcome.h>
23 #include <aws/lex/LexRuntimeServiceClient.h>
24 #include <aws/core/utils/memory/AWSMemory.h>
25 #include <aws/lex/model/PostContentResult.h>
26 
29 
30 #include <memory>
31 #include <string>
32 #include <utility>
33 #include <istream>
34 #include <ostream>
35 
36 namespace Aws
37 {
38 namespace Lex
39 {
40 class LexCommonTestFixture : public ::testing::Test
41 {
42 protected:
44  Aws::LexRuntimeService::Model::PostContentResult result;
45 
46  void SetUp() override
47  {
48  test_data.ConfigureExampleResult(result);
49  }
50 };
51 
52 TEST_F(LexCommonTestFixture, CopyResultFailureInvalidJson) {
53  {
54  auto io_stream = new Aws::StringStream();
55  *io_stream << "test_text";
56  result.ReplaceBody(io_stream);
57  }
58  std::string invalid_json = "This causes failure not valid json";
59  const unsigned char * json = reinterpret_cast<const unsigned char *>(
60  invalid_json.c_str());
61  Aws::Utils::ByteBuffer bb(json, invalid_json.size());
62  result.SetSlots(Aws::Utils::HashingUtils::Base64Encode(bb));
63  LexResponse response;
64  ASSERT_EQ(ErrorCode::INVALID_RESULT, CopyResult(result, response));
65 }
66 
67 TEST_F(LexCommonTestFixture, CopyResultSuccessValidJson) {
68  {
69  auto io_stream = new Aws::StringStream();
70  *io_stream << "test_text";
71  result.ReplaceBody(io_stream);
72  }
73  Aws::Utils::Json::JsonValue value;
74  value.WithString("key", "value");
75  auto value_str = value.View().WriteReadable();
76  const unsigned char * json = reinterpret_cast<const unsigned char *>(
77  value_str.c_str());
78  Aws::Utils::ByteBuffer bb(json, value_str.size());
79  result.SetSlots(Aws::Utils::HashingUtils::Base64Encode(bb));
80  LexResponse response;
81  ASSERT_EQ(ErrorCode::SUCCESS, CopyResult(result, response));
82  EXPECT_EQ(1, response.slots.size());
83  EXPECT_EQ("value", response.slots["key"]);
84 }
85 
86 TEST_F(LexCommonTestFixture, CopyResultSuccessNoSlots) {
87  {
88  auto io_stream = new Aws::StringStream();
89  *io_stream << "test_text";
90  result.ReplaceBody(io_stream);
91  }
92  LexResponse response;
93  ASSERT_EQ(ErrorCode::SUCCESS, CopyResult(result, response));
94  test_data.ExpectEq(response);
95 }
96 
97 TEST(BuildLexInteractor, TestBuildInteractorError) {
98  auto param_mock = std::make_shared<Aws::Client::ParameterReaderMock>();
99  Aws::Client::SetupMockReader(AwsError::AWS_ERR_OK,
100  AwsError::AWS_ERR_OK,
101  AwsError::AWS_ERR_NOT_FOUND,
102  *param_mock);
103 
104  LexInteractor lex_interactor;
106  BuildLexInteractor(param_mock, lex_interactor));
107 }
108 
109 TEST(BuildLexInteractor, TestBuildInteractorSuccess) {
110  auto param_mock = std::make_shared<Aws::Client::ParameterReaderMock>();
111  Aws::Client::SetupMockReader(AwsError::AWS_ERR_OK,
112  AwsError::AWS_ERR_OK,
113  AwsError::AWS_ERR_OK,
114  *param_mock);
115  ::testing::DefaultValue<AwsError>::Set(AwsError::AWS_ERR_OK);
116 
117  LexInteractor lex_interactor;
118  ASSERT_EQ(ErrorCode::SUCCESS,
119  BuildLexInteractor(param_mock, lex_interactor));
120 }
121 
122 } // namespace Lex
123 } // namespace Aws
void SetupMockReader(AwsError user_id_error, AwsError bot_name_error, AwsError bot_alias_error, ParameterReaderMock &mock_reader)
Aws::LexRuntimeService::Model::PostContentResult result
ErrorCode BuildLexInteractor(std::shared_ptr< Client::ParameterReaderInterface > params, LexInteractor &lex_interactor)
Utility function to configure a LexInteractor.
Definition: lex_common.h:185
ErrorCode CopyResult(Aws::LexRuntimeService::Model::PostContentResult &result, LexResponse &response)
Copy the PostContentRestult to a LexResponse.
Definition: lex_common.cpp:62
TEST_F(LexCommonTestFixture, CopyResultFailureInvalidJson)
std::unordered_map< std::string, std::string > slots
Definition: lex_common.h:79
TEST(BuildLexInteractor, TestBuildInteractorError)
void ConfigureExampleResult(Aws::LexRuntimeService::Model::PostContentResult &example_result)
Definition: test_utils.h:50
void ExpectEq(const LexResponse &response)
Definition: test_utils.h:65


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