blob: 8ac5930ad57230da61b07468a869a7e8f208e39b [file]
#include "power_supply_metrics.hpp"
#include <memory>
#include <string>
#include <gtest/gtest.h>
#include "boost/beast/http/status.hpp" // NOLINT
#include "test/redfish-core/lib/snapshot_fixture.hpp"
#include <nlohmann/json.hpp>
#include "managed_store.hpp"
#include "managed_store_types.hpp"
#include "test/g3/mock_managed_store_test.hpp"
#include "sdbusplus/message/native_types.hpp"
namespace redfish {
namespace {
using ::managedStore::KeyType;
using ::managedStore::ManagedType;
using ::managedStore::ValueType;
const std::string chassisId = "platform1"; // NOLINT(runtime/string)
const std::string failChassisId = chassisId + "@"; // NOLINT(runtime/string)
const std::string validChassisPath = // NOLINT(runtime/string)
"/xyz/openbmc_project/inventory/system/board/platform1";
const std::string powerSupplyId = "Bigchassis2_1"; // NOLINT(runtime/string)
const std::string failPowerSupplyId = // NOLINT(runtime/string)
powerSupplyId + "@";
const std::string powerSensorPath = // NOLINT(runtime/string)
"/xyz/openbmc_project/sensors/power/powersensor";
TEST_F(SnapshotFixture_Platform11_Config4, handlePowerSupplyMetricsGetTest) {
// insert the power sensor
KeyType key(
ManagedType::kManagedProperty, "xyz.openbmc_project.EntityManager",
sdbusplus::message::object_path(
"/xyz/openbmc_project/inventory/system/board/" + powerSupplyId),
"xyz.openbmc_project.Inventory.Item.PowerSupply", "Sensor");
std::shared_ptr<ValueType> mockSensorPath =
managedStore::MockManagedStoreTest::CreateValueType(
std::string(powerSensorPath));
ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>(
managedStore::GetManagedObjectStore())
->upsertMockObjectIntoManagedStore(key, mockSensorPath)
.ok());
// insert the power sensor value
const double sensorValue = 123.45;
KeyType key2(ManagedType::kManagedProperty, "xyz.openbmc_project.PSUSensor",
sdbusplus::message::object_path(powerSensorPath),
"xyz.openbmc_project.Sensor.Value", "Value");
std::shared_ptr<ValueType> mockSensorValue =
managedStore::MockManagedStoreTest::CreateValueType(sensorValue);
ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>(
managedStore::GetManagedObjectStore())
->upsertMockObjectIntoManagedStore(key2, mockSensorValue)
.ok());
handlePowerSupplyMetricsGet(app_, CreateRequest(), share_async_resp_,
chassisId, powerSupplyId);
RunIoUntilDone();
nlohmann::json& json = share_async_resp_->res.jsonValue;
EXPECT_TRUE(json.contains("@odata.id"));
EXPECT_TRUE(json.contains("@odata.type"));
EXPECT_TRUE(json.contains("InputPowerWatts"));
EXPECT_TRUE(json["InputPowerWatts"].contains("DataSourceUri"));
EXPECT_TRUE(json["InputPowerWatts"].contains("Reading"));
EXPECT_EQ(json["InputPowerWatts"]["Reading"], sensorValue);
}
TEST_F(SnapshotFixture_Platform11_Config4,
handlePowerSupplyMetricsGetNotFound) {
// insert the power sensor
KeyType key(
ManagedType::kManagedProperty, "xyz.openbmc_project.EntityManager",
sdbusplus::message::object_path(
"/xyz/openbmc_project/inventory/system/board/" + failPowerSupplyId),
"xyz.openbmc_project.Inventory.Item.PowerSupply", "Sensor");
std::shared_ptr<ValueType> mockSensorPath =
managedStore::MockManagedStoreTest::CreateValueType(
std::string(powerSensorPath));
ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>(
managedStore::GetManagedObjectStore())
->upsertMockObjectIntoManagedStore(key, mockSensorPath)
.ok());
// insert the power sensor value
const double sensorValue = 123.45;
KeyType key2(ManagedType::kManagedProperty, "xyz.openbmc_project.PSUSensor",
sdbusplus::message::object_path(powerSensorPath),
"xyz.openbmc_project.Sensor.Value", "Value");
std::shared_ptr<ValueType> mockSensorValue =
managedStore::MockManagedStoreTest::CreateValueType(sensorValue);
ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>(
managedStore::GetManagedObjectStore())
->upsertMockObjectIntoManagedStore(key2, mockSensorValue)
.ok());
handlePowerSupplyMetricsGet(app_, CreateRequest(), share_async_resp_,
failChassisId, powerSupplyId);
RunIoUntilDone();
EXPECT_EQ(share_async_resp_->res.result(),
boost::beast::http::status::not_found);
}
TEST_F(SnapshotFixture_Platform11_Config4, handlePowerSupplyMetricsHeadTest) {
handlePowerSupplyMetricsHead(app_, CreateRequest(), share_async_resp_,
chassisId, powerSupplyId);
RunIoUntilDone();
EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok);
}
TEST_F(SnapshotFixture_Platform11_Config4,
handlePowerSupplyMetricsHeadNotFound) {
handlePowerSupplyMetricsHead(app_, CreateRequest(), share_async_resp_,
failChassisId, powerSupplyId);
RunIoUntilDone();
EXPECT_EQ(share_async_resp_->res.result(),
boost::beast::http::status::not_found);
}
} // anonymous namespace
} // namespace redfish