1 #include "async_resp.hpp"
2 #include "generated/enums/resource.hpp"
3 #include "http_response.hpp"
4 #include "systems.hpp"
5 
6 #include <boost/asio/error.hpp>
7 #include <boost/beast/http/status.hpp>
8 #include <boost/system/linux_error.hpp>
9 #include <nlohmann/json.hpp>
10 
11 #include <memory>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 
16 #include <gtest/gtest.h>
17 
18 namespace redfish
19 {
20 namespace
21 {
22 
TEST(GetAllowedHostTransition,UnexpectedError)23 TEST(GetAllowedHostTransition, UnexpectedError)
24 {
25     auto response = std::make_shared<bmcweb::AsyncResp>();
26     boost::system::error_code ec = boost::asio::error::invalid_argument;
27     std::vector<std::string> allowedHostTransitions;
28 
29     afterGetAllowedHostTransitions(response, ec, allowedHostTransitions);
30 
31     EXPECT_EQ(response->res.result(),
32               boost::beast::http::status::internal_server_error);
33 }
34 
TEST(GetAllowedHostTransition,NoPropOnDbus)35 TEST(GetAllowedHostTransition, NoPropOnDbus)
36 {
37     auto response = std::make_shared<bmcweb::AsyncResp>();
38     boost::system::error_code ec =
39         boost::system::linux_error::bad_request_descriptor;
40     std::vector<std::string> allowedHostTransitions;
41 
42     afterGetAllowedHostTransitions(response, ec, allowedHostTransitions);
43 
44     nlohmann::json::array_t parameters;
45     nlohmann::json::object_t parameter;
46     parameter["Name"] = "ResetType";
47     parameter["Required"] = true;
48     parameter["DataType"] = "String";
49     nlohmann::json::array_t allowed;
50     allowed.emplace_back(resource::ResetType::ForceOff);
51     allowed.emplace_back(resource::ResetType::PowerCycle);
52     allowed.emplace_back(resource::ResetType::Nmi);
53     allowed.emplace_back(resource::ResetType::On);
54     allowed.emplace_back(resource::ResetType::ForceOn);
55     allowed.emplace_back(resource::ResetType::ForceRestart);
56     allowed.emplace_back(resource::ResetType::GracefulRestart);
57     allowed.emplace_back(resource::ResetType::GracefulShutdown);
58     parameter["AllowableValues"] = std::move(allowed);
59     parameters.emplace_back(std::move(parameter));
60 
61     EXPECT_EQ(response->res.jsonValue["Parameters"], parameters);
62 }
63 
TEST(GetAllowedHostTransition,NoForceRestart)64 TEST(GetAllowedHostTransition, NoForceRestart)
65 {
66     auto response = std::make_shared<bmcweb::AsyncResp>();
67     boost::system::error_code ec;
68 
69     std::vector<std::string> allowedHostTransitions = {
70         "xyz.openbmc_project.State.Host.Transition.On",
71         "xyz.openbmc_project.State.Host.Transition.Off",
72         "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot",
73     };
74 
75     afterGetAllowedHostTransitions(response, ec, allowedHostTransitions);
76 
77     nlohmann::json::array_t parameters;
78     nlohmann::json::object_t parameter;
79     parameter["Name"] = "ResetType";
80     parameter["Required"] = true;
81     parameter["DataType"] = "String";
82     nlohmann::json::array_t allowed;
83     allowed.emplace_back(resource::ResetType::ForceOff);
84     allowed.emplace_back(resource::ResetType::PowerCycle);
85     allowed.emplace_back(resource::ResetType::Nmi);
86     allowed.emplace_back(resource::ResetType::On);
87     allowed.emplace_back(resource::ResetType::ForceOn);
88     allowed.emplace_back(resource::ResetType::GracefulShutdown);
89     allowed.emplace_back(resource::ResetType::GracefulRestart);
90     parameter["AllowableValues"] = std::move(allowed);
91     parameters.emplace_back(std::move(parameter));
92 
93     EXPECT_EQ(response->res.jsonValue["Parameters"], parameters);
94 }
95 
TEST(GetAllowedHostTransition,AllSupported)96 TEST(GetAllowedHostTransition, AllSupported)
97 {
98     auto response = std::make_shared<bmcweb::AsyncResp>();
99     boost::system::error_code ec;
100 
101     std::vector<std::string> allowedHostTransitions = {
102         "xyz.openbmc_project.State.Host.Transition.On",
103         "xyz.openbmc_project.State.Host.Transition.Off",
104         "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot",
105         "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot",
106     };
107 
108     afterGetAllowedHostTransitions(response, ec, allowedHostTransitions);
109 
110     nlohmann::json::array_t parameters;
111     nlohmann::json::object_t parameter;
112     parameter["Name"] = "ResetType";
113     parameter["Required"] = true;
114     parameter["DataType"] = "String";
115     nlohmann::json::array_t allowed;
116     allowed.emplace_back(resource::ResetType::ForceOff);
117     allowed.emplace_back(resource::ResetType::PowerCycle);
118     allowed.emplace_back(resource::ResetType::Nmi);
119     allowed.emplace_back(resource::ResetType::On);
120     allowed.emplace_back(resource::ResetType::ForceOn);
121     allowed.emplace_back(resource::ResetType::GracefulShutdown);
122     allowed.emplace_back(resource::ResetType::GracefulRestart);
123     allowed.emplace_back(resource::ResetType::ForceRestart);
124     parameter["AllowableValues"] = std::move(allowed);
125     parameters.emplace_back(std::move(parameter));
126 
127     EXPECT_EQ(response->res.jsonValue["Parameters"], parameters);
128 }
129 
130 } // namespace
131 } // namespace redfish
132