blob: e6dcc9d6ba4dc9144925ad05173d91efcc231e7d [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>
5#include <string>
6
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07007#include <gmock/gmock.h>
8#include <gtest/gtest.h>
Patrick Venture25243232018-06-14 10:34:34 -07009
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070010using ::testing::_;
Patrick Venture25243232018-06-14 10:34:34 -070011using ::testing::Invoke;
12using ::testing::NotNull;
Patrick Venture25243232018-06-14 10:34:34 -070013
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070014TEST(DbusActiveReadTest, BoringConstructorTest)
15{
Patrick Venture25243232018-06-14 10:34:34 -070016 // Verify we can construct it.
17
18 sdbusplus::SdBusMock sdbus_mock;
19 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
20 DbusHelperMock helper;
21 std::string path = "/asdf";
22 std::string service = "asdfasdf.asdfasdf";
23
24 DbusActiveRead ar(bus_mock, path, service, &helper);
25}
26
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070027TEST(DbusActiveReadTest, Read_VerifyCallsToDbusForValue)
28{
Patrick Venture25243232018-06-14 10:34:34 -070029 // Verify it calls to get the value from dbus when requested.
30
31 sdbusplus::SdBusMock sdbus_mock;
32 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
33 DbusHelperMock helper;
34 std::string path = "/asdf";
35 std::string service = "asdfasdf.asdfasdf";
36
37 DbusActiveRead ar(bus_mock, path, service, &helper);
38
Patrick Venture563a3562018-10-30 09:31:26 -070039 EXPECT_CALL(helper, getProperties(_, service, path, NotNull()))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070040 .WillOnce(
41 Invoke([&](sdbusplus::bus::bus& bus, const std::string& service,
42 const std::string& path, struct SensorProperties* prop) {
43 prop->scale = -3;
44 prop->value = 10000;
45 prop->unit = "x";
46 }));
Patrick Venture25243232018-06-14 10:34:34 -070047
48 ReadReturn r = ar.read();
49 EXPECT_EQ(10, r.value);
50}
51
Patrick Venture563a3562018-10-30 09:31:26 -070052// WARN: getProperties will raise an exception on failure
Patrick Venture25243232018-06-14 10:34:34 -070053// Instead of just not updating the value.