JsonTestUtils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #pragma once
  9. #include <AzTest/AzTest.h>
  10. #include <AzCore/Serialization/Json/JsonSerialization.h>
  11. #include <AzCore/std/containers/vector.h>
  12. #include <AzCore/std/containers/map.h>
  13. #include <AzCore/IO/TextStreamWriters.h>
  14. #include <AzCore/IO/ByteContainerStream.h>
  15. #include <AzCore/JSON/prettywriter.h>
  16. #include <AzCore/Serialization/Json/JsonUtils.h>
  17. namespace UnitTest
  18. {
  19. //! Contains the results of a JSON test operation
  20. struct JsonTestResult
  21. {
  22. struct Report
  23. {
  24. AZ::JsonSerializationResult::ResultCode resultCode{AZ::JsonSerializationResult::Tasks::Clear};
  25. AZStd::string message;
  26. };
  27. //! The ResultCode that was returned by the JSON serializer.
  28. AZ::JsonSerializationResult::ResultCode m_jsonResultCode{AZ::JsonSerializationResult::Tasks::Clear};
  29. //! Contains the set of all reports from the JSON serializer, grouped by json path.
  30. AZStd::map<AZStd::string/*json path*/, AZStd::vector<Report>> m_reports;
  31. //! Returns true if any json field was reported with the given outcome
  32. bool ContainsOutcome(AZ::JsonSerializationResult::Outcomes outcome);
  33. //! Returns true if a message was reported for a specific json field path.
  34. //! @jsonFieldPath - A json path like "/students/3/firstName"
  35. //! @messageSubstring - A substring to find in any message that was reported for the jsonFieldPath
  36. bool ContainsMessage(AZStd::string_view jsonFieldPath, AZStd::string_view messageSubstring);
  37. };
  38. //! Uses JsonSerialization to load JSON data into a reflected object
  39. template<typename T>
  40. JsonTestResult LoadTestDataFromJson(T& object, rapidjson::Value& json)
  41. {
  42. JsonTestResult result;
  43. AZ::JsonDeserializerSettings settings;
  44. settings.m_reporting = [&result](AZStd::string_view message, AZ::JsonSerializationResult::ResultCode resultCode, AZStd::string_view path)
  45. {
  46. JsonTestResult::Report report;
  47. report.message = message;
  48. report.resultCode = resultCode;
  49. result.m_reports[path].emplace_back(AZStd::move(report));
  50. return resultCode;
  51. };
  52. result.m_jsonResultCode = AZ::JsonSerialization::Load(object, json, settings);
  53. return result;
  54. }
  55. //! Uses JsonSerialization to load JSON data from a string into a reflected object
  56. template<typename T>
  57. JsonTestResult LoadTestDataFromJson(T& object, AZStd::string_view jsonText)
  58. {
  59. auto parseResult = AZ::JsonSerializationUtils::ReadJsonString(jsonText);
  60. EXPECT_TRUE(parseResult.IsSuccess()) << parseResult.GetError().c_str();
  61. if (parseResult.IsSuccess())
  62. {
  63. return LoadTestDataFromJson(object, parseResult.GetValue());
  64. }
  65. else
  66. {
  67. JsonTestResult result;
  68. return result;
  69. }
  70. }
  71. //! Uses JsonSerialization to store a reflected object into a JSON string
  72. template<typename T>
  73. JsonTestResult StoreTestDataToJson(const T& object, AZStd::string& jsonText)
  74. {
  75. JsonTestResult result;
  76. AZ::JsonSerializerSettings serializerSettings;
  77. serializerSettings.m_reporting = [&result](AZStd::string_view message, AZ::JsonSerializationResult::ResultCode resultCode, AZStd::string_view path)
  78. {
  79. JsonTestResult::Report report;
  80. report.message = message;
  81. report.resultCode = resultCode;
  82. result.m_reports[path].emplace_back(AZStd::move(report));
  83. return resultCode;
  84. };
  85. rapidjson::Document jsonDocument;
  86. result.m_jsonResultCode = AZ::JsonSerialization::Store(jsonDocument, jsonDocument.GetAllocator(), object, serializerSettings);
  87. AZ::JsonSerializationUtils::WriteJsonSettings writeSettings;
  88. writeSettings.m_maxDecimalPlaces = 5;
  89. auto writeResult = AZ::JsonSerializationUtils::WriteJsonString(jsonDocument, jsonText, writeSettings);
  90. EXPECT_TRUE(writeResult.IsSuccess());
  91. return result;
  92. }
  93. //! Compares JSON strings, ignoring whitespace and casing.
  94. void ExpectSimilarJson(AZStd::string_view a, AZStd::string_view b);
  95. }