xref: /openbmc/bmcweb/test/redfish-core/include/utils/systems_utils_test.cpp (revision 2ca561945f9c518e4916cd23c194f89816fdbeee)
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(indexMatchingSubTreeMapObjectPath(asyncResp, 1, subtree,
38                                                   objectPath, service));
39     EXPECT_TRUE(indexMatchingSubTreeMapObjectPath(asyncResp, 2, subtree,
40                                                   objectPath, service));
41     EXPECT_TRUE(indexMatchingSubTreeMapObjectPath(asyncResp, 10, subtree,
42                                                   objectPath, service));
43     EXPECT_TRUE(indexMatchingSubTreeMapObjectPath(asyncResp, 999, subtree,
44                                                   objectPath, service));
45     EXPECT_FALSE(indexMatchingSubTreeMapObjectPath(asyncResp, 100, subtree,
46                                                    objectPath, service));
47     EXPECT_FALSE(indexMatchingSubTreeMapObjectPath(asyncResp, 11, subtree,
48                                                    objectPath, service));
49     EXPECT_FALSE(indexMatchingSubTreeMapObjectPath(asyncResp, 0, subtree,
50                                                    objectPath, service));
51 
52     indexMatchingSubTreeMapObjectPath(asyncResp, 1, subtree, objectPath,
53                                       service);
54     EXPECT_EQ(objectPath, "/xyz/openbmc_project/control/host1");
55     EXPECT_EQ(service, "xyz.openbmc_project.Settings");
56 
57     indexMatchingSubTreeMapObjectPath(asyncResp, 10, subtree, objectPath,
58                                       service);
59     EXPECT_EQ(objectPath,
60               "/xyz/openbmc_project/control/host10/policy/TPMEnable");
61 
62     indexMatchingSubTreeMapObjectPath(asyncResp, 999, subtree, objectPath,
63                                       service);
64     EXPECT_EQ(objectPath, "/xyz/openbmc_project/control/host999/");
65 }
66 } // namespace
67 } // namespace redfish
68