xref: /openbmc/bmcweb/test/redfish-core/include/utils/systems_utils_test.cpp (revision c2f428f514c84f7cefa46c3e6ee4d3dd2fe2dd52)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 
4 #include "async_resp.hpp"
5 #include "dbus_utility.hpp"
6 #include "utils/systems_utils.hpp"
7 
8 #include <nlohmann/json.hpp>
9 
10 #include <memory>
11 #include <string>
12 
13 #include <gtest/gtest.h>
14 
15 namespace redfish
16 {
17 namespace
18 {
19 
TEST(SystemsUtils,IndexMatchingObjectPath)20 TEST(SystemsUtils, IndexMatchingObjectPath)
21 {
22     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
23 
24     std::string objectPath;
25     std::string service;
26 
27     const dbus::utility::MapperGetSubTreeResponse subtree = {
28         {"/xyz/openbmc_project/control/host1",
29          {{"xyz.openbmc_project.Settings", {}}}},
30         {"/xyz/openbmc_project/control/host2/policy/TPMEnable",
31          {{"xyz.openbmc_project.Settings", {}}}},
32         {"/xyz/openbmc_project/control/host10/policy/TPMEnable",
33          {{"xyz.openbmc_project.Settings", {}}}},
34         {"/xyz/openbmc_project/control/host999/",
35          {{"xyz.openbmc_project.Settings", {}}}}};
36 
37     EXPECT_TRUE(systems_utils::indexMatchingSubTreeMapObjectPath(
38         asyncResp, 1, subtree, objectPath, service));
39     EXPECT_TRUE(systems_utils::indexMatchingSubTreeMapObjectPath(
40         asyncResp, 2, subtree, objectPath, service));
41     EXPECT_TRUE(systems_utils::indexMatchingSubTreeMapObjectPath(
42         asyncResp, 10, subtree, objectPath, service));
43     EXPECT_TRUE(systems_utils::indexMatchingSubTreeMapObjectPath(
44         asyncResp, 999, subtree, objectPath, service));
45     EXPECT_FALSE(systems_utils::indexMatchingSubTreeMapObjectPath(
46         asyncResp, 100, subtree, objectPath, service));
47     EXPECT_FALSE(systems_utils::indexMatchingSubTreeMapObjectPath(
48         asyncResp, 11, subtree, objectPath, service));
49     EXPECT_FALSE(systems_utils::indexMatchingSubTreeMapObjectPath(
50         asyncResp, 0, subtree, objectPath, service));
51 
52     systems_utils::indexMatchingSubTreeMapObjectPath(asyncResp, 1, subtree,
53                                                      objectPath, service);
54     EXPECT_EQ(objectPath, "/xyz/openbmc_project/control/host1");
55     EXPECT_EQ(service, "xyz.openbmc_project.Settings");
56 
57     systems_utils::indexMatchingSubTreeMapObjectPath(asyncResp, 10, subtree,
58                                                      objectPath, service);
59     EXPECT_EQ(objectPath,
60               "/xyz/openbmc_project/control/host10/policy/TPMEnable");
61 
62     systems_utils::indexMatchingSubTreeMapObjectPath(asyncResp, 999, subtree,
63                                                      objectPath, service);
64     EXPECT_EQ(objectPath, "/xyz/openbmc_project/control/host999/");
65 }
66 } // namespace
67 } // namespace redfish
68