blob: 3f06c96ea9e85695f045cf5a4639565335207733 [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()))
Patrick Venture1df9e872020-10-08 15:35:01 -070045 .WillOnce(Invoke([&](const std::string& service,
46 const std::string& path, SensorProperties* prop) {
47 prop->scale = -3;
48 prop->value = 10000;
49 prop->unit = "x";
50 }));
Patrick Venture25243232018-06-14 10:34:34 -070051
Patrick Venture8729eb92020-08-10 10:38:44 -070052 DbusActiveRead ar(bus_mock, path, service, std::move(helper));
53
Patrick Venture25243232018-06-14 10:34:34 -070054 ReadReturn r = ar.read();
55 EXPECT_EQ(10, r.value);
56}
57
Patrick Venture563a3562018-10-30 09:31:26 -070058// WARN: getProperties will raise an exception on failure
Patrick Venture25243232018-06-14 10:34:34 -070059// Instead of just not updating the value.
Patrick Venturea0764872020-08-08 07:48:43 -070060
61} // namespace
62} // namespace pid_control