Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 1 | #include "dbus/dbusactiveread.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 2 | #include "test/dbushelper_mock.hpp" |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 3 | |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 4 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 5 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 6 | #include <memory> |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 11 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 12 | namespace pid_control |
| 13 | { |
| 14 | namespace |
| 15 | { |
| 16 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 17 | using ::testing::_; |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 18 | using ::testing::Invoke; |
| 19 | using ::testing::NotNull; |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 20 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 21 | TEST(DbusActiveReadTest, BoringConstructorTest) |
| 22 | { |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 23 | // Verify we can construct it. |
| 24 | |
| 25 | sdbusplus::SdBusMock sdbus_mock; |
| 26 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 27 | auto helper = std::make_unique<DbusHelperMock>(); |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 28 | std::string path = "/asdf"; |
| 29 | std::string service = "asdfasdf.asdfasdf"; |
| 30 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 31 | DbusActiveRead ar(bus_mock, path, service, std::move(helper)); |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 34 | TEST(DbusActiveReadTest, Read_VerifyCallsToDbusForValue) |
| 35 | { |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 36 | // 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 Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 40 | auto helper = std::make_unique<DbusHelperMock>(); |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 41 | std::string path = "/asdf"; |
| 42 | std::string service = "asdfasdf.asdfasdf"; |
| 43 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 44 | EXPECT_CALL(*helper, getProperties(service, path, NotNull())) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 45 | .WillOnce( |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 46 | Invoke([&](const std::string& service, const std::string& path, |
| 47 | struct SensorProperties* prop) { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 48 | prop->scale = -3; |
| 49 | prop->value = 10000; |
| 50 | prop->unit = "x"; |
| 51 | })); |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 52 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 53 | DbusActiveRead ar(bus_mock, path, service, std::move(helper)); |
| 54 | |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 55 | ReadReturn r = ar.read(); |
| 56 | EXPECT_EQ(10, r.value); |
| 57 | } |
| 58 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 59 | // WARN: getProperties will raise an exception on failure |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 60 | // Instead of just not updating the value. |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 61 | |
| 62 | } // namespace |
| 63 | } // namespace pid_control |