blob: 562dd73f6cd6832124d7416faab1aa78eaf207d1 [file] [log] [blame]
Patrick Venture25243232018-06-14 10:34:34 -07001#include "dbus/dbusactiveread.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07002#include "test/dbushelper_mock.hpp"
Patrick Venture25243232018-06-14 10:34:34 -07003
Patrick Venture25243232018-06-14 10:34:34 -07004#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -07005
Patrick Venture8729eb92020-08-10 10:38:44 -07006#include <memory>
Patrick Venture25243232018-06-14 10:34:34 -07007#include <string>
8
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07009#include <gmock/gmock.h>
10#include <gtest/gtest.h>
Patrick Venture25243232018-06-14 10:34:34 -070011
Patrick Venturea0764872020-08-08 07:48:43 -070012namespace pid_control
13{
14namespace
15{
16
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070017using ::testing::_;
Patrick Venture25243232018-06-14 10:34:34 -070018using ::testing::Invoke;
19using ::testing::NotNull;
Patrick Venture25243232018-06-14 10:34:34 -070020
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070021TEST(DbusActiveReadTest, BoringConstructorTest)
22{
Patrick Venture25243232018-06-14 10:34:34 -070023 // Verify we can construct it.
24
25 sdbusplus::SdBusMock sdbus_mock;
26 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
Patrick Venture8729eb92020-08-10 10:38:44 -070027 auto helper = std::make_unique<DbusHelperMock>();
Patrick Venture25243232018-06-14 10:34:34 -070028 std::string path = "/asdf";
29 std::string service = "asdfasdf.asdfasdf";
30
Patrick Venture8729eb92020-08-10 10:38:44 -070031 DbusActiveRead ar(bus_mock, path, service, std::move(helper));
Patrick Venture25243232018-06-14 10:34:34 -070032}
33
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070034TEST(DbusActiveReadTest, Read_VerifyCallsToDbusForValue)
35{
Patrick Venture25243232018-06-14 10:34:34 -070036 // Verify it calls to get the value from dbus when requested.
37
38 sdbusplus::SdBusMock sdbus_mock;
39 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
Patrick Venture8729eb92020-08-10 10:38:44 -070040 auto helper = std::make_unique<DbusHelperMock>();
Patrick Venture25243232018-06-14 10:34:34 -070041 std::string path = "/asdf";
42 std::string service = "asdfasdf.asdfasdf";
43
Patrick Venture9b936922020-08-10 11:28:39 -070044 EXPECT_CALL(*helper, getProperties(service, path, NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080045 .WillOnce(Invoke([&]([[maybe_unused]] const std::string& service,
46 [[maybe_unused]] const std::string& path,
47 SensorProperties* prop) {
Patrick Williamse1dbb592023-10-20 11:19:22 -050048 prop->scale = -3;
49 prop->value = 10000;
50 prop->unit = "x";
51 }));
Patrick Venture25243232018-06-14 10:34:34 -070052
Patrick Venture8729eb92020-08-10 10:38:44 -070053 DbusActiveRead ar(bus_mock, path, service, std::move(helper));
54
Patrick Venture25243232018-06-14 10:34:34 -070055 ReadReturn r = ar.read();
56 EXPECT_EQ(10, r.value);
57}
58
Patrick Venture563a3562018-10-30 09:31:26 -070059// WARN: getProperties will raise an exception on failure
Patrick Venture25243232018-06-14 10:34:34 -070060// Instead of just not updating the value.
Patrick Venturea0764872020-08-08 07:48:43 -070061
62} // namespace
63} // namespace pid_control