blob: 996a110ae4dd73f49e3702e73c1fb3b80160fbdf [file]
#include "location_utils.hpp"
#include <memory>
#include <optional>
#include <gtest/gtest.h>
#include "async_resp.hpp"
#include <nlohmann/json.hpp>
using redfish::location_util::getLocationType;
using redfish::location_util::isConnector;
TEST(LocationUtility, getLocationTypeValid) {
EXPECT_EQ(
getLocationType("xyz.openbmc_project.Inventory.Connector.Backplane"),
"Backplane");
EXPECT_EQ(getLocationType("xyz.openbmc_project.Inventory.Connector.Bay"),
"Bay");
EXPECT_EQ(
getLocationType("xyz.openbmc_project.Inventory.Connector.Connector"),
"Connector");
EXPECT_EQ(getLocationType("xyz.openbmc_project.Inventory.Connector.Slot"),
"Slot");
EXPECT_EQ(getLocationType("xyz.openbmc_project.Inventory.Connector.Embedded"),
"Embedded");
EXPECT_EQ(getLocationType("xyz.openbmc_project.Inventory.Connector.Socket"),
"Socket");
}
TEST(LocationUtility, getLocationTypeInvalid) {
EXPECT_EQ(getLocationType(""), std::nullopt);
EXPECT_EQ(getLocationType("xyz.openbmc_project.Inventory.Connector.RANDOM"),
std::nullopt);
EXPECT_EQ(getLocationType("xyz.openbmc_project.Inventory.Connector"),
std::nullopt);
EXPECT_EQ(getLocationType("xyz.openbmc_project.Inventory."), std::nullopt);
EXPECT_EQ(getLocationType("xyz.openbmc_project.Item.Connector"),
std::nullopt);
EXPECT_EQ(getLocationType("xyz.openbmc_project.Inventory.Connector.BAY"),
std::nullopt);
EXPECT_EQ(getLocationType("xyz.openbmc_project.Inventory.Connector.Bay2"),
std::nullopt);
}
TEST(LocationUtility, isConnector) {
EXPECT_TRUE(isConnector("xyz.openbmc_project.Inventory.Connector.Backplane"));
EXPECT_TRUE(isConnector("xyz.openbmc_project.Inventory.Connector.Bay"));
EXPECT_TRUE(isConnector("xyz.openbmc_project.Inventory.Connector.Connector"));
EXPECT_TRUE(isConnector("xyz.openbmc_project.Inventory.Connector.Slot"));
EXPECT_TRUE(isConnector("xyz.openbmc_project.Inventory.Connector.Embedded"));
EXPECT_TRUE(isConnector("xyz.openbmc_project.Inventory.Connector.Socket"));
EXPECT_FALSE(isConnector(""));
EXPECT_FALSE(isConnector("xyz.openbmc_project.Inventory.Connector.RANDOM"));
EXPECT_FALSE(isConnector("xyz.openbmc_project.Inventory.Connector"));
EXPECT_FALSE(isConnector("xyz.openbmc_project.Inventory."));
EXPECT_FALSE(isConnector("xyz.openbmc_project.Item.Connector"));
}
TEST(LocationUtility, updateLocationContextEmpty) {
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
nlohmann::json::json_pointer jsonPtr("/Location");
redfish::location_util::updateLocationContext(asyncResp, jsonPtr, "Label1");
EXPECT_EQ(asyncResp->res.jsonValue["Location"]["PartLocationContext"],
"Label1");
}
TEST(LocationUtility, updateLocationContextAppend) {
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
nlohmann::json::json_pointer jsonPtr("/Location");
asyncResp->res.jsonValue["Location"]["PartLocationContext"] = "ChildLabel";
redfish::location_util::updateLocationContext(asyncResp, jsonPtr,
"ParentLabel");
EXPECT_EQ(asyncResp->res.jsonValue["Location"]["PartLocationContext"],
"ParentLabel/ChildLabel");
}
TEST(LocationUtility, updateOemServiceLabel) {
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
nlohmann::json::json_pointer jsonPtr("/Location");
redfish::location_util::updateOemServiceLabel(asyncResp, jsonPtr,
"MyServiceLabel");
EXPECT_EQ(asyncResp->res.jsonValue["Location"]["Google"]["LocationContext"]
["ServiceLabel"],
"MyServiceLabel");
}
TEST(LocationUtility, updateLocationOemDevpathEmpty) {
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
nlohmann::json::json_pointer jsonPtr("/Location");
redfish::location_util::updateLocationOemDevpath(asyncResp, jsonPtr,
"Label1");
EXPECT_EQ(asyncResp->res.jsonValue["Location"]["Oem"]["Google"]["Devpath"],
"/phys/Label1");
}
TEST(LocationUtility, updateLocationOemDevpathAppend) {
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
nlohmann::json::json_pointer jsonPtr("/Location");
asyncResp->res.jsonValue["Location"]["Oem"]["Google"]["Devpath"] =
"/phys/Child";
redfish::location_util::updateLocationOemDevpath(asyncResp, jsonPtr,
"Parent");
EXPECT_EQ(asyncResp->res.jsonValue["Location"]["Oem"]["Google"]["Devpath"],
"/phys/Parent/Child");
}
TEST(LocationUtility, updateLocationOemDevpathInvalidPrefix) {
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
nlohmann::json::json_pointer jsonPtr("/Location");
asyncResp->res.jsonValue["Location"]["Oem"]["Google"]["Devpath"] =
"invalid/Child";
redfish::location_util::updateLocationOemDevpath(asyncResp, jsonPtr,
"Parent");
// Should remain unmodified because of the missing /phys/ prefix
EXPECT_EQ(asyncResp->res.jsonValue["Location"]["Oem"]["Google"]["Devpath"],
"invalid/Child");
}