xref: /openbmc/bmcweb/test/redfish-core/include/utils/dbus_utils.cpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 
4 #include "utils/dbus_utils.hpp"
5 
6 #include "async_resp.hpp"
7 #include "http_response.hpp"
8 
9 #include <boost/beast/http/status.hpp>
10 #include <boost/system/errc.hpp>
11 #include <nlohmann/json.hpp>
12 #include <sdbusplus/message.hpp>
13 
14 #include <memory>
15 #include <string>
16 
17 #include <gtest/gtest.h>
18 
19 namespace redfish::details
20 {
21 namespace
22 {
23 
TEST(DbusUtils,AfterPropertySetSuccess)24 TEST(DbusUtils, AfterPropertySetSuccess)
25 {
26     std::shared_ptr<bmcweb::AsyncResp> asyncResp =
27         std::make_shared<bmcweb::AsyncResp>();
28 
29     boost::system::error_code ec;
30     sdbusplus::message_t msg;
31     afterSetProperty(asyncResp, "MyRedfishProperty",
32                      nlohmann::json("MyRedfishValue"), ec, msg);
33 
34     EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::no_content);
35 }
36 
TEST(DbusUtils,AfterActionPropertySetSuccess)37 TEST(DbusUtils, AfterActionPropertySetSuccess)
38 {
39     std::shared_ptr<bmcweb::AsyncResp> asyncResp =
40         std::make_shared<bmcweb::AsyncResp>();
41 
42     boost::system::error_code ec;
43     sdbusplus::message_t msg;
44     afterSetPropertyAction(asyncResp, "MyRedfishProperty",
45                            nlohmann::json("MyRedfishValue"), ec, msg);
46 
47     EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
48     EXPECT_EQ(asyncResp->res.jsonValue,
49               R"({
50                     "@Message.ExtendedInfo": [
51                         {
52                             "@odata.type": "#Message.v1_1_1.Message",
53                             "Message": "The request completed successfully.",
54                             "MessageArgs": [],
55                             "MessageId": "Base.1.19.Success",
56                             "MessageSeverity": "OK",
57                             "Resolution": "None."
58                         }
59                     ]
60                 })"_json);
61 }
62 
TEST(DbusUtils,AfterPropertySetInternalError)63 TEST(DbusUtils, AfterPropertySetInternalError)
64 {
65     std::shared_ptr<bmcweb::AsyncResp> asyncResp =
66         std::make_shared<bmcweb::AsyncResp>();
67 
68     boost::system::error_code ec =
69         boost::system::errc::make_error_code(boost::system::errc::timed_out);
70     sdbusplus::message_t msg;
71     afterSetProperty(asyncResp, "MyRedfishProperty",
72                      nlohmann::json("MyRedfishValue"), ec, msg);
73 
74     EXPECT_EQ(asyncResp->res.result(),
75               boost::beast::http::status::internal_server_error);
76     EXPECT_EQ(asyncResp->res.jsonValue.size(), 1);
77     using nlohmann::literals::operator""_json;
78 
79     EXPECT_EQ(asyncResp->res.jsonValue,
80               R"({
81                     "error": {
82                     "@Message.ExtendedInfo": [
83                         {
84                         "@odata.type": "#Message.v1_1_1.Message",
85                         "Message": "The request failed due to an internal service error.  The service is still operational.",
86                         "MessageArgs": [],
87                         "MessageId": "Base.1.19.InternalError",
88                         "MessageSeverity": "Critical",
89                         "Resolution": "Resubmit the request.  If the problem persists, consider resetting the service."
90                         }
91                     ],
92                     "code": "Base.1.19.InternalError",
93                     "message": "The request failed due to an internal service error.  The service is still operational."
94                     }
95                 })"_json);
96 }
97 
98 } // namespace
99 } // namespace redfish::details
100