summaryrefslogtreecommitdiff
path: root/test/redfish-core/lib/cable_test.cpp
blob: dde0610829628dd304c7f434a5d9f8647ed8624c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors

#include "async_resp.hpp"
#include "cable.hpp"
#include "dbus_utility.hpp"
#include "generated/enums/resource.hpp"
#include "http_response.hpp"

#include <boost/asio/error.hpp>
#include <boost/beast/http/status.hpp>
#include <boost/system/linux_error.hpp>
#include <nlohmann/json.hpp>

#include <limits>
#include <memory>
#include <string>

#include <gtest/gtest.h>

namespace redfish
{
namespace
{

TEST(FillCableProperties, ErrorSetsInternalError)
{
    auto asyncResp = std::make_shared<bmcweb::AsyncResp>();

    fillCableProperties(asyncResp, boost::asio::error::invalid_argument, {});

    EXPECT_EQ(asyncResp->res.result(),
              boost::beast::http::status::internal_server_error);
}

TEST(FillCableProperties, WrongPropertyTypeSetsInternalError)
{
    auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
    dbus::utility::DBusPropertiesMap properties = {
        {"Length", std::string("long")}};

    fillCableProperties(asyncResp, {}, properties);

    EXPECT_EQ(asyncResp->res.result(),
              boost::beast::http::status::internal_server_error);
    EXPECT_FALSE(asyncResp->res.jsonValue.contains("LengthMeters"));
}

TEST(FillCableProperties, EmptyPropertiesLeaveResponseEmpty)
{
    auto asyncResp = std::make_shared<bmcweb::AsyncResp>();

    fillCableProperties(asyncResp, {}, {});

    EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
    EXPECT_TRUE(asyncResp->res.jsonValue.empty());
}

TEST(FillCableProperties, PropertiesMapped)
{
    auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
    dbus::utility::DBusPropertiesMap properties = {
        {"CableTypeDescription", std::string("Optical")}, {"Length", 1.5}};

    fillCableProperties(asyncResp, {}, properties);

    EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
    EXPECT_EQ(asyncResp->res.jsonValue["CableType"], "Optical");
    EXPECT_EQ(asyncResp->res.jsonValue["LengthMeters"], 1.5);
}

TEST(FillCableProperties, NanLengthOmitsLengthMeters)
{
    auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
    dbus::utility::DBusPropertiesMap properties = {
        {"Length", std::numeric_limits<double>::quiet_NaN()}};

    fillCableProperties(asyncResp, {}, properties);

    EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
    EXPECT_FALSE(asyncResp->res.jsonValue.contains("LengthMeters"));
}

TEST(FillCableProperties, InfiniteLengthSetsInternalError)
{
    auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
    dbus::utility::DBusPropertiesMap properties = {
        {"Length", std::numeric_limits<double>::infinity()}};

    fillCableProperties(asyncResp, {}, properties);

    EXPECT_EQ(asyncResp->res.result(),
              boost::beast::http::status::internal_server_error);
    EXPECT_FALSE(asyncResp->res.jsonValue.contains("LengthMeters"));
}

TEST(AfterHandleCableGet, EbadrReturnsNotFound)
{
    auto asyncResp = std::make_shared<bmcweb::AsyncResp>();

    afterHandleCableGet(asyncResp, "cable0",
                        boost::system::linux_error::bad_request_descriptor, {});

    EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::not_found);
}

TEST(AfterHandleCableGet, OtherErrorSetsInternalError)
{
    auto asyncResp = std::make_shared<bmcweb::AsyncResp>();

    afterHandleCableGet(asyncResp, "cable0",
                        boost::asio::error::invalid_argument, {});

    EXPECT_EQ(asyncResp->res.result(),
              boost::beast::http::status::internal_server_error);
}

TEST(AfterHandleCableGet, NoMatchingCableReturnsNotFound)
{
    auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
    dbus::utility::MapperGetSubTreeResponse subtree = {
        {"/xyz/openbmc_project/inventory/system/cable1", {}}};

    afterHandleCableGet(asyncResp, "cable0", {}, subtree);

    EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::not_found);
    EXPECT_FALSE(asyncResp->res.jsonValue.contains("@odata.id"));
}

TEST(AfterHandleCableGet, MatchingCablePopulatesResource)
{
    auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
    dbus::utility::MapperGetSubTreeResponse subtree = {
        {"/xyz/openbmc_project/inventory/system/cable1", {}},
        {"/xyz/openbmc_project/inventory/system/cable0", {}}};

    afterHandleCableGet(asyncResp, "cable0", {}, subtree);

    const nlohmann::json& json = asyncResp->res.jsonValue;
    EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
    EXPECT_EQ(json["@odata.type"], "#Cable.v1_0_0.Cable");
    EXPECT_EQ(json["@odata.id"], "/redfish/v1/Cables/cable0");
    EXPECT_EQ(json["Id"], "cable0");
    EXPECT_EQ(json["Name"], "Cable");
    EXPECT_EQ(json["Status"]["State"], resource::State::Enabled);
    EXPECT_EQ(json["Status"]["Health"], resource::Health::OK);
}

} // namespace
} // namespace redfish