regulators: Modify monitorSensors() to use Services

Modify the monitorSensors() method in the System, Chassis, Device, and
Rail classes to have a new first parameter: Services& services.

Modify SensorMonitoring::execute() to log messages using the new
Journal interface.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I05820cfa94bc3d15dee7a1714a3d253720aa7f92
diff --git a/phosphor-regulators/test/chassis_tests.cpp b/phosphor-regulators/test/chassis_tests.cpp
index 639dc33..08304d3 100644
--- a/phosphor-regulators/test/chassis_tests.cpp
+++ b/phosphor-regulators/test/chassis_tests.cpp
@@ -301,6 +301,12 @@
 {
     // Test where no devices were specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         // Create Chassis
         std::vector<std::unique_ptr<Device>> devices{};
         std::unique_ptr<Chassis> chassis =
@@ -314,14 +320,17 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Call monitorSensors().  Should do nothing.
-        journal::clear();
-        chassisPtr->monitorSensors(system);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        chassisPtr->monitorSensors(services, system);
     }
 
     // Test where devices were specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         std::vector<std::unique_ptr<Device>> devices{};
 
         // Create PMBusReadSensorAction
@@ -375,9 +384,6 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Call monitorSensors()
-        journal::clear();
-        chassisPtr->monitorSensors(system);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        chassisPtr->monitorSensors(services, system);
     }
 }
diff --git a/phosphor-regulators/test/device_tests.cpp b/phosphor-regulators/test/device_tests.cpp
index 690a510..62cf97e 100644
--- a/phosphor-regulators/test/device_tests.cpp
+++ b/phosphor-regulators/test/device_tests.cpp
@@ -455,6 +455,12 @@
 {
     // Test where Rails were not specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         // Create mock I2CInterface.  A two-byte read should NOT occur.
         std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
             std::make_unique<i2c::MockedI2CInterface>();
@@ -479,15 +485,18 @@
         chassisVec.emplace_back(std::move(chassis));
         System system{std::move(rules), std::move(chassisVec)};
 
-        // Call monitorSensors().  Should do nothing.
-        journal::clear();
-        devicePtr->monitorSensors(system, *chassisPtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        // Call monitorSensors().
+        devicePtr->monitorSensors(services, system, *chassisPtr);
     }
 
     // Test where Rails were specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         std::vector<std::unique_ptr<Rail>> rails{};
 
         // Create PMBusReadSensorAction
@@ -542,9 +551,6 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Call monitorSensors().
-        journal::clear();
-        devicePtr->monitorSensors(system, *chassisPtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        devicePtr->monitorSensors(services, system, *chassisPtr);
     }
 }
diff --git a/phosphor-regulators/test/rail_tests.cpp b/phosphor-regulators/test/rail_tests.cpp
index 190e48e..d5f8db2 100644
--- a/phosphor-regulators/test/rail_tests.cpp
+++ b/phosphor-regulators/test/rail_tests.cpp
@@ -18,7 +18,6 @@
 #include "configuration.hpp"
 #include "device.hpp"
 #include "i2c_interface.hpp"
-#include "journal.hpp"
 #include "mock_action.hpp"
 #include "mock_journal.hpp"
 #include "mock_services.hpp"
@@ -217,6 +216,12 @@
 {
     // Test where SensorMonitoring was not specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         // Create mock I2CInterface.  A two-byte read should NOT occur.
         std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
             std::make_unique<i2c::MockedI2CInterface>();
@@ -250,15 +255,18 @@
         chassisVec.emplace_back(std::move(chassis));
         System system{std::move(rules), std::move(chassisVec)};
 
-        // Call monitorSensors().  Should do nothing.
-        journal::clear();
-        railPtr->monitorSensors(system, *chassisPtr, *devicePtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        // Call monitorSensors().
+        railPtr->monitorSensors(services, system, *chassisPtr, *devicePtr);
     }
 
     // Test where SensorMonitoring was specified in constructor
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         // Create PMBusReadSensorAction
         pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
         uint8_t command = 0x8C;
@@ -313,10 +321,7 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Call monitorSensors().
-        journal::clear();
-        railPtr->monitorSensors(system, *chassisPtr, *devicePtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        railPtr->monitorSensors(services, system, *chassisPtr, *devicePtr);
     }
 }
 
diff --git a/phosphor-regulators/test/sensor_monitoring_tests.cpp b/phosphor-regulators/test/sensor_monitoring_tests.cpp
index 79c3b92..8f111eb 100644
--- a/phosphor-regulators/test/sensor_monitoring_tests.cpp
+++ b/phosphor-regulators/test/sensor_monitoring_tests.cpp
@@ -18,9 +18,9 @@
 #include "configuration.hpp"
 #include "device.hpp"
 #include "i2c_interface.hpp"
-#include "journal.hpp"
 #include "mock_action.hpp"
 #include "mock_journal.hpp"
+#include "mock_services.hpp"
 #include "mocked_i2c_interface.hpp"
 #include "pmbus_read_sensor_action.hpp"
 #include "pmbus_utils.hpp"
@@ -60,6 +60,12 @@
 {
     // Test where works
     {
+        // Create mock services.  No logging should occur.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
         // Create PMBusReadSensorAction
         pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
         uint8_t command = 0x8C;
@@ -115,14 +121,26 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Execute sensorMonitoring
-        journal::clear();
-        sensorMonitoringPtr->execute(system, *chassisPtr, *devicePtr, *railPtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        EXPECT_EQ(journal::getErrMessages().size(), 0);
+        sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr,
+                                     *railPtr);
     }
 
     // Test where fails
     {
+        // Create mock services.  Expect logError() to be called.
+        MockServices services{};
+        MockJournal& journal = services.getMockJournal();
+        EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+        std::vector<std::string> expectedErrMessagesException{
+            "I2CException: Failed to write byte: bus /dev/i2c-1, addr 0x70",
+            "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, "
+            "format: linear_11 }"};
+
+        EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(1);
+        EXPECT_CALL(journal,
+                    logError("Unable to monitor sensors for rail vio2"))
+            .Times(1);
+
         // Create PMBusReadSensorAction
         pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
         uint8_t command = 0x8C;
@@ -180,15 +198,8 @@
         System system{std::move(rules), std::move(chassisVec)};
 
         // Execute sensorMonitoring
-        journal::clear();
-        sensorMonitoringPtr->execute(system, *chassisPtr, *devicePtr, *railPtr);
-        EXPECT_EQ(journal::getDebugMessages().size(), 0);
-        std::vector<std::string> expectedErrMessages{
-            "I2CException: Failed to write byte: bus /dev/i2c-1, addr 0x70",
-            "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, "
-            "format: linear_11 }",
-            "Unable to monitor sensors for rail vio2"};
-        EXPECT_EQ(journal::getErrMessages(), expectedErrMessages);
+        sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr,
+                                     *railPtr);
     }
 }
 
diff --git a/phosphor-regulators/test/system_tests.cpp b/phosphor-regulators/test/system_tests.cpp
index 7adc5d8..5d1d720 100644
--- a/phosphor-regulators/test/system_tests.cpp
+++ b/phosphor-regulators/test/system_tests.cpp
@@ -16,7 +16,6 @@
 #include "chassis.hpp"
 #include "device.hpp"
 #include "id_map.hpp"
-#include "journal.hpp"
 #include "mock_journal.hpp"
 #include "mock_services.hpp"
 #include "mocked_i2c_interface.hpp"
@@ -201,6 +200,12 @@
 
 TEST(SystemTests, MonitorSensors)
 {
+    // Create mock services.  No logging should occur.
+    MockServices services{};
+    MockJournal& journal = services.getMockJournal();
+    EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
+    EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
+
     // Create PMBusReadSensorAction
     pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
     uint8_t command = 0x8C;
@@ -252,8 +257,5 @@
     System system{std::move(rules), std::move(chassisVec)};
 
     // Call monitorSensors()
-    journal::clear();
-    system.monitorSensors();
-    EXPECT_EQ(journal::getDebugMessages().size(), 0);
-    EXPECT_EQ(journal::getErrMessages().size(), 0);
+    system.monitorSensors(services);
 }