|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | +#include <aws/core/Aws.h> |
| 6 | +#include <aws/core/auth/AWSCredentials.h> |
| 7 | +#include <aws/core/monitoring/MonitoringInterface.h> |
| 8 | +#include <aws/sqs/SQSClient.h> |
| 9 | +#include <aws/sqs/model/GetQueueUrlRequest.h> |
| 10 | +#include <aws/testing/MemoryTesting.h> |
| 11 | +#include <aws/testing/mocks/aws/auth/MockAWSHttpResourceClient.h> |
| 12 | +#include <aws/testing/mocks/http/MockHttpClient.h> |
| 13 | +#include <gtest/gtest.h> |
| 14 | + |
| 15 | +#include <memory> |
| 16 | + |
| 17 | +using namespace Aws; |
| 18 | +using namespace Aws::Client; |
| 19 | +using namespace Aws::Auth; |
| 20 | +using namespace Aws::Monitoring; |
| 21 | +using namespace Aws::Http; |
| 22 | +using namespace Aws::Http::Standard; |
| 23 | +using namespace Aws::SQS; |
| 24 | +using namespace Aws::SQS::Model; |
| 25 | + |
| 26 | +namespace { |
| 27 | +const char* LOG_TAG = "SQSDUnitTest"; |
| 28 | +} // namespace |
| 29 | + |
| 30 | +class SQSUnitTest : public testing::Test { |
| 31 | + protected: |
| 32 | + static void SetUpTestSuite() { |
| 33 | +#ifdef USE_AWS_MEMORY_MANAGEMENT |
| 34 | + test_memory_system.reset(new ExactTestMemorySystem(1024, 128)); |
| 35 | + options_.memoryManagementOptions.memoryManager = test_memory_system.get(); |
| 36 | +#endif |
| 37 | + InitAPI(options_); |
| 38 | + mock_client_factory_ = Aws::MakeShared<MockHttpClientFactory>(LOG_TAG); |
| 39 | + mock_http_client_ = Aws::MakeShared<MockHttpClient>(LOG_TAG); |
| 40 | + mock_client_factory_->SetClient(mock_http_client_); |
| 41 | + SetHttpClientFactory(mock_client_factory_); |
| 42 | + AWSCredentials credentials{"mock", "credentials"}; |
| 43 | + client_ = Aws::MakeShared<SQSClient>("ALLOCATION_TAG", credentials); |
| 44 | + } |
| 45 | + |
| 46 | + static void TearDownTestSuite() { |
| 47 | + mock_client_factory_.reset(); |
| 48 | + mock_http_client_.reset(); |
| 49 | + client_.reset(); |
| 50 | + ShutdownAPI(options_); |
| 51 | +#ifdef USE_AWS_MEMORY_MANAGEMENT |
| 52 | + EXPECT_EQ(test_memory_system->GetCurrentOutstandingAllocations(), 0ULL); |
| 53 | + EXPECT_EQ(test_memory_system->GetCurrentBytesAllocated(), 0ULL); |
| 54 | + EXPECT_TRUE(test_memory_system->IsClean()); |
| 55 | + if (test_memory_system->GetCurrentOutstandingAllocations() != 0ULL) FAIL(); |
| 56 | + if (test_memory_system->GetCurrentBytesAllocated() != 0ULL) FAIL(); |
| 57 | + if (!test_memory_system->IsClean()) FAIL(); |
| 58 | + test_memory_system.reset(); |
| 59 | +#endif |
| 60 | + } |
| 61 | + |
| 62 | + static SDKOptions options_; |
| 63 | + static std::shared_ptr<MockHttpClient> mock_http_client_; |
| 64 | + static std::shared_ptr<MockHttpClientFactory> mock_client_factory_; |
| 65 | + static std::shared_ptr<SQSClient> client_; |
| 66 | +#ifdef USE_AWS_MEMORY_MANAGEMENT |
| 67 | + static std::unique_ptr<ExactTestMemorySystem> test_memory_system; |
| 68 | +#endif |
| 69 | +}; |
| 70 | + |
| 71 | +SDKOptions SQSUnitTest::options_; |
| 72 | +std::shared_ptr<MockHttpClient> SQSUnitTest::mock_http_client_ = nullptr; |
| 73 | +std::shared_ptr<MockHttpClientFactory> SQSUnitTest::mock_client_factory_ = nullptr; |
| 74 | +std::shared_ptr<SQSClient> SQSUnitTest::client_ = nullptr; |
| 75 | +#ifdef USE_AWS_MEMORY_MANAGEMENT |
| 76 | +std::unique_ptr<ExactTestMemorySystem> SQSUnitTest::test_memory_system = nullptr; |
| 77 | +#endif |
| 78 | + |
| 79 | +TEST_F(SQSUnitTest, CanParseCodeField) { |
| 80 | + // Mock query compatible response |
| 81 | + const auto error_response_stream = Aws::MakeShared<StandardHttpRequest>(LOG_TAG, "mockuri", HttpMethod::HTTP_GET); |
| 82 | + error_response_stream->SetResponseStreamFactory(Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); |
| 83 | + auto error_response = Aws::MakeShared<StandardHttpResponse>(LOG_TAG, error_response_stream); |
| 84 | + error_response->SetResponseCode(HttpResponseCode::BAD_REQUEST); |
| 85 | + error_response->AddHeader("Content-Type", "application/x-amz-json-1.0"); |
| 86 | + error_response->AddHeader("x-amzn-query-error", "AWS.SimpleQueueService.NonExistentQueue;Sender"); |
| 87 | + error_response->AddHeader("x-amzn-RequestId", "a918fbf2-457a-4fe1-99ba-5685ce220fc1"); |
| 88 | + |
| 89 | + // Add mocked response to client |
| 90 | + mock_http_client_->Reset(); |
| 91 | + mock_http_client_->AddResponseToReturn(error_response, [](IOStream& bodyStream) -> void { |
| 92 | + bodyStream << R"({"__type": "com.amazonaws.sqs#QueueDoesNotExist","message": "For those who come after"})"; |
| 93 | + }); |
| 94 | + const GetQueueUrlRequest request{}; |
| 95 | + const auto response = client_->GetQueueUrl(request); |
| 96 | + EXPECT_TRUE(!response.IsSuccess()); |
| 97 | + EXPECT_TRUE(!response.GetError().GetExceptionName().empty()); |
| 98 | + EXPECT_EQ("AWS.SimpleQueueService.NonExistentQueue", response.GetError().GetExceptionName()); |
| 99 | + EXPECT_EQ(SQSErrors::QUEUE_DOES_NOT_EXIST, response.GetError().GetErrorType()); |
| 100 | +} |
| 101 | + |
| 102 | +TEST_F(SQSUnitTest, CanHandleMissingCodeField) { |
| 103 | + // Mock query compatible response |
| 104 | + const auto error_response_stream = Aws::MakeShared<StandardHttpRequest>(LOG_TAG, "mockuri", HttpMethod::HTTP_GET); |
| 105 | + error_response_stream->SetResponseStreamFactory(Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); |
| 106 | + auto error_response = Aws::MakeShared<StandardHttpResponse>(LOG_TAG, error_response_stream); |
| 107 | + error_response->SetResponseCode(HttpResponseCode::BAD_REQUEST); |
| 108 | + error_response->AddHeader("Content-Type", "application/x-amz-json-1.0"); |
| 109 | + error_response->AddHeader("x-amzn-RequestId", "a918fbf2-457a-4fe1-99ba-5685ce220fc1"); |
| 110 | + |
| 111 | + // Add mocked response to client |
| 112 | + mock_http_client_->Reset(); |
| 113 | + mock_http_client_->AddResponseToReturn(error_response, [](IOStream& bodyStream) -> void { |
| 114 | + bodyStream << R"({"__type": "com.amazonaws.sqs#QueueDoesNotExist","message": "For those who come after"})"; |
| 115 | + }); |
| 116 | + const GetQueueUrlRequest request{}; |
| 117 | + const auto response = client_->GetQueueUrl(request); |
| 118 | + EXPECT_TRUE(!response.IsSuccess()); |
| 119 | + EXPECT_TRUE(!response.GetError().GetExceptionName().empty()); |
| 120 | + EXPECT_EQ("QueueDoesNotExist", response.GetError().GetExceptionName()); |
| 121 | + EXPECT_EQ(SQSErrors::QUEUE_DOES_NOT_EXIST, response.GetError().GetErrorType()); |
| 122 | +} |
0 commit comments