Fix SubTreePathsById when given ID is invalid
The commit 7a93d51 makes `getAssociatedSubTreePathsById()` to return
empty paths if there are no associated subtrees. However, it also
returns empty paths as a success, even if the given id is not valid.
This is to check the invalid case and to return NotFound.
For example, currently, this gives the success
- GET /redfish/v1/Chassis/INVALID/PowerSubsystem/PowerSupplies
Tested:
- GET with the invalid id and check the return condition.
```
busctl call -j "xyz.openbmc_project.ObjectMapper" "/xyz/openbmc_project/object_mapper" \
"xyz.openbmc_project.ObjectMapper" "GetAssociatedSubTreePathsById" ssassas \
"InvalidChassis" \
"/xyz/openbmc_project/inventory" \
1 "xyz.openbmc_project.Inventory.Item.Chassis" \
"powered_by" \
1 "xyz.openbmc_project.Inventory.Item.PowerSupply"
-->
Call failed: The resource is not found.
```
Change-Id: If4f621e105bc1a5c443c1ec8b7762f57748e1d10
Signed-off-by: Myung Bae <myungbae@us.ibm.com>
diff --git a/src/handler.cpp b/src/handler.cpp
index 7705e3c..8caba42 100644
--- a/src/handler.cpp
+++ b/src/handler.cpp
@@ -361,18 +361,26 @@
ResourceNotFound();
}
+ bool validId = false;
std::vector<std::string> output;
for (const auto& path : interfaceMap)
{
const auto& thisPath = path.first;
- // Skip exact match on stripped search term or
- // the path does not end with the id
- if (thisPath == objectPathStripped || !thisPath.ends_with("/" + id))
+ // Skip the path does not end with the id
+ if (!thisPath.ends_with("/" + id))
{
continue;
}
+ // Valid if id is matching
+ validId = true;
+
+ // Skip exact match on stripped search term
+ if (thisPath == objectPathStripped)
+ {
+ continue;
+ }
if (thisPath.starts_with(objectPath))
{
for (const auto& interfaceMap : path.second)
@@ -390,6 +398,11 @@
}
}
}
+ if (!validId)
+ {
+ throw sdbusplus::xyz::openbmc_project::Common::Error::
+ ResourceNotFound();
+ }
return output;
}
diff --git a/src/test/handler.cpp b/src/test/handler.cpp
index 46d998c..a295fbc 100644
--- a/src/test/handler.cpp
+++ b/src/test/handler.cpp
@@ -449,10 +449,11 @@
std::vector<std::string> endpointvalidInterfaces = {"test_interface_1",
"test_interface_2"};
// invalid id
- ASSERT_TRUE(getAssociatedSubTreeById(interfaceMap, associationMap, "childx",
- path, subtreeInterfaces, "descendent",
- endpointvalidInterfaces)
- .empty());
+ EXPECT_THROW(
+ getAssociatedSubTreeById(interfaceMap, associationMap, "childx", path,
+ subtreeInterfaces, "descendent",
+ endpointvalidInterfaces),
+ sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound);
// invalid subtreeInterfaces
ASSERT_TRUE(getAssociatedSubTreeById(interfaceMap, associationMap, "child",
@@ -521,10 +522,11 @@
std::vector<std::string> endpointvalidInterfaces = {"test_interface_1",
"test_interface_2"};
// invalid id
- ASSERT_TRUE(getAssociatedSubTreePathsById(
- interfaceMap, associationMap, "childx", path,
- subtreeInterfaces, "descendent", endpointvalidInterfaces)
- .empty());
+ EXPECT_THROW(
+ getAssociatedSubTreePathsById(interfaceMap, associationMap, "childx",
+ path, subtreeInterfaces, "descendent",
+ endpointvalidInterfaces),
+ sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound);
// invalid subtreeInterfaces
ASSERT_TRUE(getAssociatedSubTreePathsById(