1 #include "app.hpp"
2 #include "async_resp.hpp"
3 #include "chassis.hpp"
4 #include "dbus_utility.hpp"
5 #include "generated/enums/chassis.hpp"
6 #include "http_request.hpp"
7 #include "http_response.hpp"
8 
9 #include <boost/beast/core/string_type.hpp>
10 #include <boost/beast/http/status.hpp>
11 #include <boost/beast/http/verb.hpp>
12 #include <nlohmann/json.hpp>
13 
14 #include <functional>
15 #include <memory>
16 #include <string>
17 #include <system_error>
18 #include <utility>
19 
20 #include <gtest/gtest.h>
21 
22 namespace redfish
23 {
24 namespace
25 {
26 
27 void assertChassisResetActionInfoGet(const std::string& chassisId,
28                                      crow::Response& res)
29 {
30     EXPECT_EQ(res.jsonValue["@odata.type"], "#ActionInfo.v1_1_2.ActionInfo");
31     EXPECT_EQ(res.jsonValue["@odata.id"],
32               "/redfish/v1/Chassis/" + chassisId + "/ResetActionInfo");
33     EXPECT_EQ(res.jsonValue["Name"], "Reset Action Info");
34 
35     EXPECT_EQ(res.jsonValue["Id"], "ResetActionInfo");
36 
37     nlohmann::json::array_t parameters;
38     nlohmann::json::object_t parameter;
39     parameter["Name"] = "ResetType";
40     parameter["Required"] = true;
41     parameter["DataType"] = "String";
42     nlohmann::json::array_t allowed;
43     allowed.emplace_back("PowerCycle");
44     parameter["AllowableValues"] = std::move(allowed);
45     parameters.emplace_back(std::move(parameter));
46 
47     EXPECT_EQ(res.jsonValue["Parameters"], parameters);
48 }
49 
50 TEST(HandleChassisResetActionInfoGet, StaticAttributesAreExpected)
51 {
52     auto response = std::make_shared<bmcweb::AsyncResp>();
53     std::error_code err;
54     crow::Request request{{boost::beast::http::verb::get, "/whatever", 11},
55                           err};
56 
57     std::string fakeChassis = "fakeChassis";
58     response->res.setCompleteRequestHandler(
59         std::bind_front(assertChassisResetActionInfoGet, fakeChassis));
60 
61     crow::App app;
62     handleChassisResetActionInfoGet(app, request, response, fakeChassis);
63 }
64 
65 TEST(TranslateChassisTypeToRedfish, TranslationsAreExpected)
66 {
67     ASSERT_EQ(
68         chassis::ChassisType::Blade,
69         translateChassisTypeToRedfish(
70             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Blade"));
71     ASSERT_EQ(
72         chassis::ChassisType::Component,
73         translateChassisTypeToRedfish(
74             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Component"));
75     ASSERT_EQ(
76         chassis::ChassisType::Enclosure,
77         translateChassisTypeToRedfish(
78             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Enclosure"));
79     ASSERT_EQ(
80         chassis::ChassisType::Module,
81         translateChassisTypeToRedfish(
82             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Module"));
83     ASSERT_EQ(
84         chassis::ChassisType::RackMount,
85         translateChassisTypeToRedfish(
86             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.RackMount"));
87     ASSERT_EQ(
88         chassis::ChassisType::StandAlone,
89         translateChassisTypeToRedfish(
90             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.StandAlone"));
91     ASSERT_EQ(
92         chassis::ChassisType::StorageEnclosure,
93         translateChassisTypeToRedfish(
94             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.StorageEnclosure"));
95     ASSERT_EQ(
96         chassis::ChassisType::Zone,
97         translateChassisTypeToRedfish(
98             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Zone"));
99     ASSERT_EQ(
100         chassis::ChassisType::Invalid,
101         translateChassisTypeToRedfish(
102             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Unknown"));
103 }
104 
105 TEST(HandleChassisProperties, TypeFound)
106 {
107     auto response = std::make_shared<bmcweb::AsyncResp>();
108     auto properties = dbus::utility::DBusPropertiesMap();
109     properties.emplace_back(
110         std::string("Type"),
111         dbus::utility::DbusVariantType(
112             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.RackMount"));
113     handleChassisProperties(response, properties);
114     ASSERT_EQ("RackMount", response->res.jsonValue["ChassisType"]);
115 
116     response = std::make_shared<bmcweb::AsyncResp>();
117     properties.clear();
118     properties.emplace_back(
119         std::string("Type"),
120         dbus::utility::DbusVariantType(
121             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.StandAlone"));
122     handleChassisProperties(response, properties);
123     ASSERT_EQ("StandAlone", response->res.jsonValue["ChassisType"]);
124 }
125 
126 TEST(HandleChassisProperties, BadTypeFound)
127 {
128     auto response = std::make_shared<bmcweb::AsyncResp>();
129     auto properties = dbus::utility::DBusPropertiesMap();
130     properties.emplace_back(
131         std::string("Type"),
132         dbus::utility::DbusVariantType(
133             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Unknown"));
134     handleChassisProperties(response, properties);
135     ASSERT_FALSE(response->res.jsonValue.contains("ChassisType"));
136 }
137 
138 TEST(HandleChassisProperties, FailToGetProperty)
139 {
140     auto response = std::make_shared<bmcweb::AsyncResp>();
141     auto properties = dbus::utility::DBusPropertiesMap();
142     properties.emplace_back(std::string("Type"),
143                             dbus::utility::DbusVariantType(123));
144     handleChassisProperties(response, properties);
145     ASSERT_EQ(boost::beast::http::status::internal_server_error,
146               response->res.result());
147 }
148 
149 TEST(HandleChassisProperties, TypeNotFound)
150 {
151     auto response = std::make_shared<bmcweb::AsyncResp>();
152     auto properties = dbus::utility::DBusPropertiesMap();
153     handleChassisProperties(response, properties);
154     ASSERT_EQ("RackMount", response->res.jsonValue["ChassisType"]);
155 }
156 
157 } // namespace
158 } // namespace redfish
159