AWSMetricsServiceApiTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AWSMetricsServiceApi.h>
  9. #include <AWSMetricsConstant.h>
  10. #include <MetricsEventBuilder.h>
  11. #include <Framework/JsonObjectHandler.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. namespace AWSMetrics
  14. {
  15. class JsonReaderMock
  16. : public AWSCore::JsonReader
  17. {
  18. public:
  19. virtual ~JsonReaderMock() = default;
  20. MOCK_METHOD0(Ignore, bool());
  21. MOCK_METHOD1(Accept, bool(bool& target));
  22. MOCK_METHOD1(Accept, bool(AZStd::string& target));
  23. MOCK_METHOD1(Accept, bool(int& target));
  24. MOCK_METHOD1(Accept, bool(unsigned& target));
  25. MOCK_METHOD1(Accept, bool(int64_t& target));
  26. MOCK_METHOD1(Accept, bool(uint64_t& target));
  27. MOCK_METHOD1(Accept, bool(double& target));
  28. MOCK_METHOD1(Accept, bool(AWSCore::JsonKeyHandler keyHandler));
  29. MOCK_METHOD1(Accept, bool(AWSCore::JsonArrayHandler arrayHandler));
  30. };
  31. class AWSMetricsServiceApiTest
  32. : public UnitTest::LeakDetectionFixture
  33. {
  34. public:
  35. testing::NiceMock<JsonReaderMock> JsonReader;
  36. };
  37. TEST_F(AWSMetricsServiceApiTest, OnJsonKey_MetricsEventSuccessResponseRecord_AcceptValidKeys)
  38. {
  39. ServiceAPI::PostMetricsEventsResponseEntry responseRecord;
  40. responseRecord.m_result = "ok";
  41. EXPECT_CALL(JsonReader, Accept(responseRecord.m_result)).Times(1);
  42. EXPECT_CALL(JsonReader, Accept(responseRecord.m_errorCode)).Times(1);
  43. EXPECT_CALL(JsonReader, Ignore()).Times(1);
  44. responseRecord.OnJsonKey(AwsMetricsPostMetricsEventsResponseEntryKeyResult, JsonReader);
  45. responseRecord.OnJsonKey(AwsMetricsPostMetricsEventsResponseEntryKeyErrorCode, JsonReader);
  46. responseRecord.OnJsonKey("other", JsonReader);
  47. }
  48. TEST_F(AWSMetricsServiceApiTest, OnJsonKeyWithEvents_MetricsEventSuccessResponseRecord_AcceptValidKeys)
  49. {
  50. // Verifiy that JsonReader accepts valid JSON keys in each event record from a success reponse
  51. ServiceAPI::PostMetricsEventsResponseEntry responseRecord;
  52. responseRecord.m_result = "Ok";
  53. ServiceAPI::PostMetricsEventsResponse response;
  54. response.m_responseEntries.emplace_back(responseRecord);
  55. response.m_failedRecordCount = 0;
  56. response.m_total = 1;
  57. EXPECT_CALL(JsonReader, Accept(response.m_failedRecordCount)).Times(1);
  58. EXPECT_CALL(JsonReader, Accept(response.m_total)).Times(1);
  59. EXPECT_CALL(JsonReader, Accept(::testing::An<AWSCore::JsonArrayHandler>())).Times(1);
  60. EXPECT_CALL(JsonReader, Ignore()).Times(1);
  61. response.OnJsonKey(AwsMetricsPostMetricsEventsResponseKeyFailedRecordCount, JsonReader);
  62. response.OnJsonKey(AwsMetricsPostMetricsEventsResponseKeyTotal, JsonReader);
  63. response.OnJsonKey(AwsMetricsPostMetricsEventsResponseKeyEvents, JsonReader);
  64. response.OnJsonKey("other", JsonReader);
  65. }
  66. TEST_F(AWSMetricsServiceApiTest, OnJsonKey_Error_AcceptValidKeys)
  67. {
  68. ServiceAPI::PostMetricsEventsError error;
  69. error.message = "error message";
  70. error.type = "404";
  71. EXPECT_CALL(JsonReader, Accept(error.message)).Times(1);
  72. EXPECT_CALL(JsonReader, Accept(error.type)).Times(1);
  73. EXPECT_CALL(JsonReader, Ignore()).Times(1);
  74. error.OnJsonKey(AwsMetricsPostMetricsEventsErrorKeyMessage, JsonReader);
  75. error.OnJsonKey(AwsMetricsPostMetricsEventsErrorKeyType, JsonReader);
  76. error.OnJsonKey("other", JsonReader);
  77. }
  78. TEST_F(AWSMetricsServiceApiTest, BuildRequestBody_PostProducerEventsRequest_SerializedMetricsQueue)
  79. {
  80. ServiceAPI::PostMetricsEventsRequest request;
  81. request.parameters.m_metricsQueue = MetricsQueue();
  82. request.parameters.m_metricsQueue.AddMetrics(MetricsEventBuilder().Build());
  83. AWSCore::RequestBuilder requestBuilder{};
  84. EXPECT_TRUE(request.parameters.BuildRequest(requestBuilder));
  85. std::shared_ptr<Aws::StringStream> bodyContent = requestBuilder.GetBodyContent();
  86. ASSERT_NE(nullptr, bodyContent);
  87. std::istreambuf_iterator<AZStd::string::value_type> eos;
  88. AZStd::string bodyString{ std::istreambuf_iterator<AZStd::string::value_type>(*bodyContent), eos };
  89. EXPECT_TRUE(bodyString.contains(AZStd::string::format("{\"%s\":[{\"event_timestamp\":", AwsMetricsPostMetricsEventsRequestParameterKeyEvents)));
  90. }
  91. }