Trigger: make dbus properties writable

This change allows to modify 'Sensors', 'ReportNames' and 'Thresholds'
dbus properties of Trigger interface. They are required by Redfish to
implement PATCH functionality for Trigger schema.

Some backend changes were required to enable this functionality, and as
such few improvements were made for existing code:
- NumericThreshold and DiscreteThreshold now have common implementation
  where it was possible.
- Internal sensor info structure for Trigger is now the same as the one
  used for Report. This resulted in breaking compatibility with previous
  Trigger persistency data.
- Added getInfo / getParams methods for Sensor and Threshold interfaces.
  They are used by Trigger dbus getters and persistency mechanism now,
  instead of storing this data in Trigger object.

Testing done:
- Unit tests were expanded and are passing
- dbus setters for Sensors and Thresholds are working and modifications
  are reflected by calling appropriate getters.

Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: I7a14c15a30d78ce872342b5f938aba43c77be9c0
diff --git a/tests/src/test_trigger.cpp b/tests/src/test_trigger.cpp
index 66e9bf0..a2286ee 100644
--- a/tests/src/test_trigger.cpp
+++ b/tests/src/test_trigger.cpp
@@ -1,6 +1,9 @@
 #include "dbus_environment.hpp"
 #include "helpers.hpp"
 #include "mocks/json_storage_mock.hpp"
+#include "mocks/sensor_mock.hpp"
+#include "mocks/threshold_mock.hpp"
+#include "mocks/trigger_factory_mock.hpp"
 #include "mocks/trigger_manager_mock.hpp"
 #include "params/trigger_params.hpp"
 #include "trigger.hpp"
@@ -14,7 +17,7 @@
 using namespace testing;
 using namespace std::literals::string_literals;
 
-static constexpr size_t expectedTriggerVersion = 0;
+static constexpr size_t expectedTriggerVersion = 1;
 
 class TestTrigger : public Test
 {
@@ -35,7 +38,10 @@
 
     std::unique_ptr<TriggerManagerMock> triggerManagerMockPtr =
         std::make_unique<NiceMock<TriggerManagerMock>>();
+    std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr =
+        std::make_unique<NiceMock<TriggerFactoryMock>>();
     testing::NiceMock<StorageMock> storageMock;
+    std::vector<std::shared_ptr<interfaces::Threshold>> thresholdMocks;
     std::unique_ptr<Trigger> sut;
 
     void SetUp() override
@@ -54,12 +60,17 @@
 
     std::unique_ptr<Trigger> makeTrigger(const TriggerParams& params)
     {
+        thresholdMocks =
+            ThresholdMock::makeThresholds(params.thresholdParams());
+
         return std::make_unique<Trigger>(
             DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
             params.id(), params.name(), params.triggerActions(),
-            params.reportIds(), params.sensors(), params.thresholdParams(),
-            std::vector<std::shared_ptr<interfaces::Threshold>>{},
-            *triggerManagerMockPtr, storageMock);
+            std::make_shared<std::vector<std::string>>(
+                params.reportIds().begin(), params.reportIds().end()),
+            std::vector<std::shared_ptr<interfaces::Threshold>>(thresholdMocks),
+            *triggerManagerMockPtr, storageMock, *triggerFactoryMockPtr,
+            SensorMock::makeSensorMocks(params.sensors()));
     }
 
     static interfaces::JsonStorage::FilePath to_file_path(std::string name)
@@ -104,13 +115,18 @@
     EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"), Eq(true));
     EXPECT_THAT(
         getProperty<std::vector<std::string>>(sut->getPath(), "TriggerActions"),
-        Eq(triggerParams.triggerActions()));
+        Eq(utils::transform(
+            triggerParams.triggerActions(),
+            [](const auto& action) { return actionToString(action); })));
     EXPECT_THAT((getProperty<SensorsInfo>(sut->getPath(), "Sensors")),
                 Eq(utils::fromLabeledSensorsInfo(triggerParams.sensors())));
     EXPECT_THAT(
         getProperty<std::vector<std::string>>(sut->getPath(), "ReportNames"),
         Eq(triggerParams.reportIds()));
     EXPECT_THAT(
+        getProperty<bool>(sut->getPath(), "Discrete"),
+        Eq(isTriggerThresholdDiscrete(triggerParams.thresholdParams())));
+    EXPECT_THAT(
         getProperty<TriggerThresholdParams>(sut->getPath(), "Thresholds"),
         Eq(std::visit(utils::FromLabeledThresholdParamConversion(),
                       triggerParams.thresholdParams())));
@@ -124,6 +140,41 @@
     EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"), Eq(name));
 }
 
+TEST_F(TestTrigger, setPropertyReportNames)
+{
+    std::vector<std::string> newNames = {"abc", "one", "two"};
+    EXPECT_THAT(setProperty(sut->getPath(), "ReportNames", newNames),
+                Eq(boost::system::errc::success));
+    EXPECT_THAT(
+        getProperty<std::vector<std::string>>(sut->getPath(), "ReportNames"),
+        Eq(newNames));
+}
+
+TEST_F(TestTrigger, setPropertySensors)
+{
+    EXPECT_CALL(*triggerFactoryMockPtr, updateSensors(_, _));
+    for (const auto& threshold : thresholdMocks)
+    {
+        auto thresholdMockPtr =
+            std::dynamic_pointer_cast<NiceMock<ThresholdMock>>(threshold);
+        EXPECT_CALL(*thresholdMockPtr, updateSensors(_));
+    }
+    SensorsInfo newSensors({std::make_pair(
+        sdbusplus::message::object_path("/abc/def"), "metadata")});
+    EXPECT_THAT(setProperty(sut->getPath(), "Sensors", newSensors),
+                Eq(boost::system::errc::success));
+}
+
+TEST_F(TestTrigger, setPropertyThresholds)
+{
+    EXPECT_CALL(*triggerFactoryMockPtr, updateThresholds(_, _, _, _, _));
+    TriggerThresholdParams newThresholds =
+        std::vector<discrete::ThresholdParam>(
+            {std::make_tuple("discrete threshold", "OK", 10, "12.3")});
+    EXPECT_THAT(setProperty(sut->getPath(), "Thresholds", newThresholds),
+                Eq(boost::system::errc::success));
+}
+
 TEST_F(TestTrigger, checkIfNumericCoversionsAreGood)
 {
     const auto& labeledParamsBase =
@@ -265,7 +316,10 @@
 TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerTriggerActions)
 {
     ASSERT_THAT(storedConfiguration.at("TriggerActions"),
-                Eq(triggerParams.triggerActions()));
+                Eq(utils::transform(triggerParams.triggerActions(),
+                                    [](const auto& action) {
+                                        return actionToString(action);
+                                    })));
 }
 
 TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerReportIds)
@@ -278,8 +332,7 @@
 {
     nlohmann::json expectedItem;
     expectedItem["service"] = "service1";
-    expectedItem["sensorPath"] =
-        "/xyz/openbmc_project/sensors/temperature/BMC_Temp";
+    expectedItem["path"] = "/xyz/openbmc_project/sensors/temperature/BMC_Temp";
     expectedItem["metadata"] = "metadata1";
 
     ASSERT_THAT(storedConfiguration.at("Sensors"), ElementsAre(expectedItem));