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