MetricsAttributeTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <AWSMetricsConstant.h>
  9. #include <Framework/JsonWriter.h>
  10. #include <MetricsAttribute.h>
  11. #include <AzCore/UnitTest/TestTypes.h>
  12. namespace AWSMetrics
  13. {
  14. class MetricsAttributeTest
  15. : public UnitTest::LeakDetectionFixture
  16. {
  17. public:
  18. const AZStd::string AttrName = "name";
  19. const AZStd::string StrValue = "value";
  20. const int IntValue = 0;
  21. const double DoubleValue = 0.01;
  22. };
  23. TEST_F(MetricsAttributeTest, SetAttributeName_DefaultConstructor_Success)
  24. {
  25. MetricsAttribute attribute;
  26. attribute.SetName(AttrName.c_str());
  27. ASSERT_EQ(attribute.GetName(), AttrName);
  28. ASSERT_FALSE(attribute.IsDefault());
  29. attribute.SetName(AwsMetricsAttributeKeyEventName);
  30. ASSERT_TRUE(attribute.IsDefault());
  31. }
  32. TEST_F(MetricsAttributeTest, SetAttributeValue_SupportedAttributeTypes_Success)
  33. {
  34. MetricsAttribute attribute;
  35. attribute.SetVal(StrValue);
  36. ASSERT_EQ(AZStd::get<AZStd::string>(attribute.GetVal()), StrValue);
  37. attribute.SetVal(IntValue);
  38. ASSERT_EQ(AZStd::get<int>(attribute.GetVal()), IntValue);
  39. attribute.SetVal(DoubleValue);
  40. ASSERT_EQ(AZStd::get<double>(attribute.GetVal()), DoubleValue);
  41. }
  42. TEST_F(MetricsAttributeTest, GetSizeInBytes_SupportedAttributeTypes_Success)
  43. {
  44. MetricsAttribute strAttr(AttrName, StrValue);
  45. ASSERT_EQ(strAttr.GetSizeInBytes(), sizeof(char) * (AttrName.size() + StrValue.size()));
  46. MetricsAttribute intAttr(AttrName, IntValue);
  47. ASSERT_EQ(intAttr.GetSizeInBytes(), sizeof(char) * AttrName.size() + sizeof(int));
  48. MetricsAttribute doubleAttr(AttrName, DoubleValue);
  49. ASSERT_EQ(doubleAttr.GetSizeInBytes(), sizeof(char) * AttrName.size() + sizeof(double));
  50. }
  51. TEST_F(MetricsAttributeTest, SerializeToJson_SupportedAttributeTypes_Success)
  52. {
  53. std::ostream stream(nullptr);
  54. AWSCore::JsonOutputStream jsonStream{ stream };
  55. AWSCore::JsonWriter strWriter{ jsonStream };
  56. AWSCore::JsonWriter intWriter{ jsonStream };
  57. AWSCore::JsonWriter doubleWriter{ jsonStream };
  58. MetricsAttribute attribute;
  59. attribute.SetName(AttrName.c_str());
  60. attribute.SetVal(StrValue);
  61. ASSERT_TRUE(attribute.SerializeToJson(strWriter));
  62. attribute.SetVal(IntValue);
  63. ASSERT_TRUE(attribute.SerializeToJson(intWriter));
  64. attribute.SetVal(DoubleValue);
  65. ASSERT_TRUE(attribute.SerializeToJson(doubleWriter));
  66. }
  67. TEST_F(MetricsAttributeTest, ReadFromJson_SupportedAttributeTypes_Success)
  68. {
  69. MetricsAttribute attribute;
  70. rapidjson::Value nameVal(rapidjson::kStringType);
  71. nameVal.SetString(rapidjson::StringRef(AttrName.c_str()));
  72. rapidjson::Value strVal(rapidjson::kStringType);
  73. nameVal.SetString(rapidjson::StringRef(StrValue.c_str()));
  74. rapidjson::Value intVal(rapidjson::kNumberType);
  75. intVal.SetInt(IntValue);
  76. rapidjson::Value doubleVal(rapidjson::kNumberType);
  77. doubleVal.SetDouble(DoubleValue);
  78. ASSERT_TRUE(attribute.ReadFromJson(nameVal, strVal));
  79. ASSERT_EQ(attribute.GetName(), nameVal.GetString());
  80. ASSERT_EQ(attribute.GetSizeInBytes(), sizeof(char) * (AZStd::string(nameVal.GetString()).size() + AZStd::string(strVal.GetString()).size()));
  81. ASSERT_TRUE(attribute.ReadFromJson(nameVal, intVal));
  82. ASSERT_EQ(attribute.GetSizeInBytes(), sizeof(char) * AZStd::string(nameVal.GetString()).size() + sizeof(int));
  83. ASSERT_TRUE(attribute.ReadFromJson(nameVal, doubleVal));
  84. ASSERT_EQ(attribute.GetSizeInBytes(), sizeof(char) * AZStd::string(nameVal.GetString()).size() + sizeof(double));
  85. }
  86. TEST_F(MetricsAttributeTest, ReadFromJson_InvalidAttributeType_Fail)
  87. {
  88. MetricsAttribute attribute;
  89. rapidjson::Value nameVal(rapidjson::kStringType);
  90. nameVal.SetString(rapidjson::StringRef(AttrName.c_str()));
  91. rapidjson::Value arrayVal(rapidjson::kArrayType);
  92. ASSERT_FALSE(attribute.ReadFromJson(nameVal, arrayVal));
  93. }
  94. }