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