diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/redfish-core/include/redfish_aggregator_test.cpp | 107 |
1 files changed, 106 insertions, 1 deletions
diff --git a/test/redfish-core/include/redfish_aggregator_test.cpp b/test/redfish-core/include/redfish_aggregator_test.cpp index 1b3f9244be..665446c137 100644 --- a/test/redfish-core/include/redfish_aggregator_test.cpp +++ b/test/redfish-core/include/redfish_aggregator_test.cpp @@ -1,9 +1,13 @@ #include "redfish_aggregator.hpp" +#include <nlohmann/json.hpp> + #include <gtest/gtest.h> // IWYU pragma: keep namespace redfish { +namespace +{ TEST(IsPropertyUri, SupportedPropertyReturnsTrue) { @@ -11,7 +15,6 @@ TEST(IsPropertyUri, SupportedPropertyReturnsTrue) EXPECT_TRUE(isPropertyUri("@odata.id")); EXPECT_TRUE(isPropertyUri("Image")); EXPECT_TRUE(isPropertyUri("MetricProperty")); - EXPECT_TRUE(isPropertyUri("OriginOfCondition")); EXPECT_TRUE(isPropertyUri("TaskMonitor")); EXPECT_TRUE(isPropertyUri("target")); } @@ -29,6 +32,7 @@ TEST(IsPropertyUri, SpeificallyIgnoredPropertyReturnsFalse) EXPECT_FALSE(isPropertyUri("@odata.context")); EXPECT_FALSE(isPropertyUri("Destination")); EXPECT_FALSE(isPropertyUri("HostName")); + EXPECT_FALSE(isPropertyUri("OriginOfCondition")); } TEST(IsPropertyUri, UnsupportedPropertyReturnsFalse) @@ -136,4 +140,105 @@ TEST(addPrefixToItem, TopLevelCollections) EXPECT_EQ(jsonRequest["@odata.id"], initial); } } + +TEST(addPrefixes, ParseJsonObject) +{ + nlohmann::json parameter; + parameter["Name"] = "/redfish/v1/Chassis/fakeName"; + parameter["@odata.id"] = "/redfish/v1/Chassis/fakeChassis"; + + addPrefixes(parameter, "abcd"); + EXPECT_EQ(parameter["Name"], "/redfish/v1/Chassis/fakeName"); + EXPECT_EQ(parameter["@odata.id"], "/redfish/v1/Chassis/abcd_fakeChassis"); +} + +TEST(addPrefixes, ParseJsonArray) +{ + nlohmann::json array = nlohmann::json::parse(R"( + { + "Conditions": [ + { + "Message": "This is a test", + "@odata.id": "/redfish/v1/Chassis/TestChassis" + }, + { + "Message": "This is also a test", + "@odata.id": "/redfish/v1/Chassis/TestChassis2" + } + ] + } + )", + nullptr, false); + + addPrefixes(array, "5B42"); + EXPECT_EQ(array["Conditions"][0]["@odata.id"], + "/redfish/v1/Chassis/5B42_TestChassis"); + EXPECT_EQ(array["Conditions"][1]["@odata.id"], + "/redfish/v1/Chassis/5B42_TestChassis2"); +} + +TEST(addPrefixes, ParseJsonObjectNestedArray) +{ + nlohmann::json objWithArray = nlohmann::json::parse(R"( + { + "Status": { + "Conditions": [ + { + "Message": "This is a test", + "MessageId": "Test", + "OriginOfCondition": { + "@odata.id": "/redfish/v1/Chassis/TestChassis" + }, + "Severity": "Critical" + } + ], + "Health": "Critical", + "State": "Enabled" + } + } + )", + nullptr, false); + + addPrefixes(objWithArray, "5B42"); + nlohmann::json& array = objWithArray["Status"]["Conditions"]; + EXPECT_EQ(array[0]["OriginOfCondition"]["@odata.id"], + "/redfish/v1/Chassis/5B42_TestChassis"); +} + +// Attempts to perform prefix fixing on a response with response code "result". +// Fixing should always occur +void assertProcessResponse(unsigned result) +{ + nlohmann::json jsonResp; + jsonResp["@odata.id"] = "/redfish/v1/Chassis/TestChassis"; + jsonResp["Name"] = "Test"; + + crow::Response resp; + resp.body() = + jsonResp.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); + resp.addHeader("Content-Type", "application/json"); + resp.result(result); + + auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); + RedfishAggregator::processResponse("prefix", asyncResp, resp); + + EXPECT_EQ(asyncResp->res.jsonValue["Name"], "Test"); + EXPECT_EQ(asyncResp->res.jsonValue["@odata.id"], + "/redfish/v1/Chassis/prefix_TestChassis"); + EXPECT_EQ(asyncResp->res.resultInt(), result); +} + +TEST(processResponse, validResponseCodes) +{ + assertProcessResponse(100); + assertProcessResponse(200); + assertProcessResponse(204); + assertProcessResponse(300); + assertProcessResponse(404); + assertProcessResponse(405); + assertProcessResponse(500); + assertProcessResponse(507); +} + +} // namespace } // namespace redfish |