1 #include "async_resp.hpp" 2 #include "manager_diagnostic_data.hpp" 3 4 #include <nlohmann/json.hpp> 5 6 #include <memory> 7 8 #include <gtest/gtest.h> 9 10 namespace redfish 11 { 12 namespace 13 { 14 15 using json_pointer = nlohmann::json::json_pointer; 16 17 void testDataGetNoError(boost::system::error_code ec) 18 { 19 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); 20 21 setBytesProperty(asyncResp, 22 json_pointer("/MemoryStatistics/FreeStorageSpace"), ec, 0); 23 EXPECT_TRUE(asyncResp->res.jsonValue.is_null()); 24 EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok); 25 } 26 27 TEST(ManagerDiagnosticDataTest, ManagerDataGetServerUnreachable) 28 { 29 testDataGetNoError(boost::asio::error::basic_errors::host_unreachable); 30 } 31 32 TEST(ManagerDiagnosticDataTest, ManagerDataGetPathInvalid) 33 { 34 testDataGetNoError(boost::system::linux_error::bad_request_descriptor); 35 } 36 37 void verifyError(crow::Response& res) 38 { 39 EXPECT_EQ(res.result(), boost::beast::http::status::internal_server_error); 40 res.clear(); 41 } 42 43 TEST(ManagerDiagnosticDataTest, ManagerDataGetFailure) 44 { 45 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); 46 boost::system::error_code ec = boost::asio::error::operation_aborted; 47 48 setBytesProperty(asyncResp, 49 json_pointer("/MemoryStatistics/FreeStorageSpace"), ec, 0); 50 verifyError(asyncResp->res); 51 } 52 53 TEST(ManagerDiagnosticDataTest, ManagerDataGetNullPtr) 54 { 55 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); 56 57 setPercentProperty( 58 asyncResp, 59 nlohmann::json::json_pointer("/MemoryStatistics/FreeStorageSpace"), {}, 60 std::numeric_limits<double>::quiet_NaN()); 61 EXPECT_EQ(asyncResp->res.jsonValue["FreeStorageSpaceKiB"], nullptr); 62 } 63 64 TEST(ManagerDiagnosticDataTest, ManagerDataGetSuccess) 65 { 66 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); 67 68 setBytesProperty(asyncResp, json_pointer("/FreeStorageSpaceKiB"), {}, 69 204800.0); 70 EXPECT_EQ(asyncResp->res.jsonValue["FreeStorageSpaceKiB"].get<int64_t>(), 71 200); 72 } 73 74 } // namespace 75 } // namespace redfish 76