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
|
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#include "async_resp.hpp"
#include "dbus_utility.hpp"
#include "utils/name_utils.hpp"
#include <boost/beast/http/status.hpp>
#include <boost/system/errc.hpp>
#include <boost/system/error_code.hpp>
#include <memory>
#include <string>
#include <gtest/gtest.h>
namespace redfish::name_utils
{
namespace
{
TEST(NameUtils, PrettyNameOverridesDefault)
{
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
asyncResp->res.jsonValue["Name"] = "Default Name";
boost::system::error_code ec;
afterGetPrettyName(asyncResp, ""_json_pointer, ec, "Pretty Name");
EXPECT_EQ(asyncResp->res.jsonValue["Name"], "Pretty Name");
EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
}
TEST(NameUtils, EmptyPrettyNamePreservesDefault)
{
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
asyncResp->res.jsonValue["Name"] = "Default Name";
boost::system::error_code ec;
afterGetPrettyName(asyncResp, ""_json_pointer, ec, "");
EXPECT_EQ(asyncResp->res.jsonValue["Name"], "Default Name");
EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
}
TEST(NameUtils, ErrorPreservesDefault)
{
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
asyncResp->res.jsonValue["Name"] = "Default Name";
boost::system::error_code ec =
boost::system::errc::make_error_code(boost::system::errc::io_error);
afterGetPrettyName(asyncResp, ""_json_pointer, ec, "Pretty Name");
EXPECT_EQ(asyncResp->res.jsonValue["Name"], "Default Name");
EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
}
TEST(NameUtils, PrettyNameUsesJsonPointer)
{
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
asyncResp->res.jsonValue["Name"] = "Default Name";
asyncResp->res.jsonValue["Nested"]["Name"] = "Nested Default Name";
boost::system::error_code ec;
afterGetPrettyName(asyncResp, "/Nested"_json_pointer, ec,
"Nested Pretty Name");
EXPECT_EQ(asyncResp->res.jsonValue["Name"], "Default Name");
EXPECT_EQ(asyncResp->res.jsonValue["Nested"]["Name"], "Nested Pretty Name");
EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
}
TEST(NameUtils, MissingInventoryItemUsesDefault)
{
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
dbus::utility::MapperServiceMap services;
getPrettyName(asyncResp, services, "/xyz/openbmc_project/inventory/test",
"Default Name");
EXPECT_EQ(asyncResp->res.jsonValue["Name"], "Default Name");
EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
}
// When Inventory.Item is absent but other interfaces exist, default is used
TEST(NameUtils, NoInventoryItemInterfaceUsesDefault)
{
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
dbus::utility::MapperServiceMap services = {
{"xyz.openbmc_project.FooService",
{"xyz.openbmc_project.Inventory.Item.Cpu",
"xyz.openbmc_project.State.Decorator.OperationalStatus"}}};
getPrettyName(asyncResp, services, "/xyz/openbmc_project/inventory/test",
"Default Name");
EXPECT_EQ(asyncResp->res.jsonValue["Name"], "Default Name");
EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
}
// Verify all three afterGetPrettyName rules hold with a nested JSON pointer
TEST(NameUtils, ErrorWithJsonPointerPreservesDefault)
{
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
asyncResp->res.jsonValue["Nested"]["Name"] = "Default Name";
boost::system::error_code ec =
boost::system::errc::make_error_code(boost::system::errc::io_error);
afterGetPrettyName(asyncResp, "/Nested"_json_pointer, ec, "Pretty Name");
EXPECT_EQ(asyncResp->res.jsonValue["Nested"]["Name"], "Default Name");
EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
}
TEST(NameUtils, EmptyPrettyNameWithJsonPointerPreservesDefault)
{
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
asyncResp->res.jsonValue["Nested"]["Name"] = "Default Name";
boost::system::error_code ec;
afterGetPrettyName(asyncResp, "/Nested"_json_pointer, ec, "");
EXPECT_EQ(asyncResp->res.jsonValue["Nested"]["Name"], "Default Name");
EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
}
TEST(NameUtils, PrettyNameUsesArrayJsonPointer)
{
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
asyncResp->res.jsonValue["Members"][0]["Name"] = "First";
asyncResp->res.jsonValue["Members"][1]["Name"] = "Second";
boost::system::error_code ec;
afterGetPrettyName(asyncResp, "/Members/0"_json_pointer, ec, "Pretty Name");
EXPECT_EQ(asyncResp->res.jsonValue["Members"][0]["Name"], "Pretty Name");
EXPECT_EQ(asyncResp->res.jsonValue["Members"][1]["Name"], "Second");
EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
}
} // namespace
} // namespace redfish::name_utils
|