Skip to content

Commit 3994c3e

Browse files
committed
adds query compatible tests to SQS
1 parent b2b9255 commit 3994c3e

File tree

5 files changed

+169
-7
lines changed

5 files changed

+169
-7
lines changed

cmake/sdksCommon.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ list(APPEND SDK_TEST_PROJECT_LIST "s3-crt:tests/aws-cpp-sdk-s3-crt-integration-t
106106
list(APPEND SDK_TEST_PROJECT_LIST "s3-encryption:tests/aws-cpp-sdk-s3-encryption-tests,tests/aws-cpp-sdk-s3-encryption-integration-tests")
107107
list(APPEND SDK_TEST_PROJECT_LIST "s3control:tests/aws-cpp-sdk-s3control-integration-tests")
108108
list(APPEND SDK_TEST_PROJECT_LIST "sqs:tests/aws-cpp-sdk-sqs-integration-tests")
109+
list(APPEND SDK_TEST_PROJECT_LIST "sqs:tests/aws-cpp-sdk-sqs-unit-tests")
109110
list(APPEND SDK_TEST_PROJECT_LIST "transfer:tests/aws-cpp-sdk-transfer-tests")
110111
list(APPEND SDK_TEST_PROJECT_LIST "text-to-speech:tests/aws-cpp-sdk-text-to-speech-tests,tests/aws-cpp-sdk-polly-sample")
111112
list(APPEND SDK_TEST_PROJECT_LIST "transcribestreaming:tests/aws-cpp-sdk-transcribestreaming-integ-tests")
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
add_project(aws-cpp-sdk-sqs-unit-tests
2+
"Unit Tests for the SQS SDK Client"
3+
aws-cpp-sdk-sqs
4+
testing-resources
5+
aws_test_main
6+
aws-cpp-sdk-core)
7+
8+
add_definitions(-DRESOURCES_DIR="${CMAKE_CURRENT_SOURCE_DIR}/resources")
9+
10+
if(MSVC AND BUILD_SHARED_LIBS)
11+
add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1)
12+
endif()
13+
14+
enable_testing()
15+
16+
if(PLATFORM_ANDROID AND BUILD_SHARED_LIBS)
17+
add_library(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/SQSUnitTests.cpp)
18+
else()
19+
add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/SQSUnitTests.cpp)
20+
endif()
21+
22+
set_compiler_flags(${PROJECT_NAME})
23+
set_compiler_warnings(${PROJECT_NAME})
24+
25+
target_link_libraries(${PROJECT_NAME} ${PROJECT_LIBS})
26+
27+
if(MSVC AND BUILD_SHARED_LIBS)
28+
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/DELAYLOAD:aws-cpp-sdk-sqs.dll /DELAYLOAD:aws-cpp-sdk-core.dll")
29+
target_link_libraries(${PROJECT_NAME} delayimp.lib)
30+
endif()
31+
32+
include(GoogleTest)
33+
gtest_add_tests(TARGET ${PROJECT_NAME})
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}

tests/testing-resources/include/aws/testing/mocks/http/MockHttpClient.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
static const char MockHttpAllocationTag[] = "MockHttp";
2020

21+
2122
class MockHttpClient : public Aws::Http::HttpClient
2223
{
2324
public:
25+
using ResponseCallbackTuple = std::pair<std::shared_ptr<Aws::Http::HttpResponse>, std::function<void (Aws::IOStream&)>>;
26+
2427
std::shared_ptr<Aws::Http::HttpResponse> MakeRequest(const std::shared_ptr<Aws::Http::HttpRequest>& request,
2528
Aws::Utils::RateLimits::RateLimiterInterface* readLimiter = nullptr,
2629
Aws::Utils::RateLimits::RateLimiterInterface* writeLimiter = nullptr) const override
@@ -34,13 +37,14 @@ class MockHttpClient : public Aws::Http::HttpClient
3437

3538
if (m_responsesToUse.size() > 0)
3639
{
37-
std::shared_ptr<Aws::Http::HttpResponse> responseToUse = m_responsesToUse.front();
40+
ResponseCallbackTuple responseToUse = m_responsesToUse.front();
3841
m_responsesToUse.pop();
39-
if (responseToUse)
42+
if (responseToUse.first)
4043
{
41-
responseToUse->SetOriginatingRequest(request);
44+
responseToUse.first->SetOriginatingRequest(request);
45+
responseToUse.second(responseToUse.first->GetResponseBody());
4246
}
43-
return responseToUse;
47+
return responseToUse.first;
4448
}
4549
return Aws::MakeShared<Aws::Http::Standard::StandardHttpResponse>(MockHttpAllocationTag, request);
4650
}
@@ -54,18 +58,19 @@ class MockHttpClient : public Aws::Http::HttpClient
5458

5559
//these will be cleaned up by the aws client, so if you are testing an aws client, don't worry about freeing the memory
5660
//when you are finished.
57-
void AddResponseToReturn(const std::shared_ptr<Aws::Http::HttpResponse>& response) { m_responsesToUse.push(response); }
61+
void AddResponseToReturn(const std::shared_ptr<Aws::Http::HttpResponse>& response) { m_responsesToUse.emplace(response, [](Aws::IOStream&) -> void {}); }
62+
void AddResponseToReturn(const std::shared_ptr<Aws::Http::HttpResponse>& response, const std::function<void (Aws::IOStream&)>& callbackFucntion) { m_responsesToUse.emplace(response, callbackFucntion); }
5863

5964
void Reset()
6065
{
6166
m_requestsMade.clear();
62-
Aws::Queue<std::shared_ptr<Aws::Http::HttpResponse> > empty;
67+
Aws::Queue<ResponseCallbackTuple> empty;
6368
std::swap(m_responsesToUse, empty);
6469
}
6570

6671
private:
6772
mutable Aws::Vector<Aws::Http::Standard::StandardHttpRequest> m_requestsMade;
68-
mutable Aws::Queue< std::shared_ptr<Aws::Http::HttpResponse> > m_responsesToUse;
73+
mutable Aws::Queue<ResponseCallbackTuple> m_responsesToUse;
6974
};
7075

7176
class MockHttpClientFactory : public Aws::Http::HttpClientFactory

tools/scripts/run_integration_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def main():
4040
"aws-cpp-sdk-dynamodb-unit-tests",
4141
"aws-cpp-sdk-dynamodb-integration-tests",
4242
"aws-cpp-sdk-sqs-integration-tests",
43+
"aws-cpp-sdk-sqs-unit-tests",
4344
"aws-cpp-sdk-s3-integration-tests",
4445
"aws-cpp-sdk-s3-unit-tests",
4546
"aws-cpp-sdk-s3-crt-integration-tests",

0 commit comments

Comments
 (0)