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