Update Trigger Actions implementation

Dbus trigger action names were modified to reflect separation of
Telemetry Service from Redfish:
- LogToLogService is renamed to LogToJournal,
- RedfishEvent was renamed to LogToRedfishEventLog

Both of those logging actions, now also include trigger id and threshold
name. Threshold naming logic:
- For discrete triggers, it can be specified by user, if left empty it
  will be changed to "{Severity} condition".
- Numeric triggers have no way of naming threshold, instead its type
  will be converted to string, example "UpperWarning"
- Discrete OnChange threshold will always be named "OnChange"

Additionally, defect was found with timestamp attached to Trigger Logs:
it was a steady_clock timestamp instead of system_clock. The function
which was supposed to format it was also working incorrectly, and was
improved to work with milliseconds. This change required major refactor
of unit tests, especially for numeric threshold.

Testing done:
- LogToJournal action is working properly,
- LogToRedfishEventLog action is working properly,
- UTs are passing.

Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: Iae2490682f0e9e2a610b45fd8af5cc5e21e66f35
diff --git a/tests/src/test_discrete_threshold.cpp b/tests/src/test_discrete_threshold.cpp
index b68d1c3..953ddd5 100644
--- a/tests/src/test_discrete_threshold.cpp
+++ b/tests/src/test_discrete_threshold.cpp
@@ -1,6 +1,7 @@
 #include "dbus_environment.hpp"
 #include "discrete_threshold.hpp"
 #include "helpers.hpp"
+#include "mocks/clock_mock.hpp"
 #include "mocks/sensor_mock.hpp"
 #include "mocks/trigger_action_mock.hpp"
 #include "types/duration_types.hpp"
@@ -22,20 +23,24 @@
         std::make_unique<StrictMock<TriggerActionMock>>();
     TriggerActionMock& actionMock = *actionMockPtr;
     std::shared_ptr<DiscreteThreshold> sut;
+    std::string triggerId = "MyTrigger";
+    std::unique_ptr<NiceMock<ClockMock>> clockMockPtr =
+        std::make_unique<NiceMock<ClockMock>>();
 
     std::shared_ptr<DiscreteThreshold>
         makeThreshold(Milliseconds dwellTime, std::string thresholdValue,
-                      discrete::Severity severity = discrete::Severity::ok)
+                      discrete::Severity severity = discrete::Severity::ok,
+                      std::string thresholdName = "treshold name")
     {
         std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
         actions.push_back(std::move(actionMockPtr));
 
         return std::make_shared<DiscreteThreshold>(
-            DbusEnvironment::getIoc(),
+            DbusEnvironment::getIoc(), triggerId,
             utils::convContainer<std::shared_ptr<interfaces::Sensor>>(
                 sensorMocks),
-            std::move(actions), dwellTime, thresholdValue, "treshold name",
-            severity);
+            std::move(actions), dwellTime, thresholdValue, thresholdName,
+            severity, std::move(clockMockPtr));
     }
 
     void SetUp() override
@@ -65,7 +70,7 @@
 
 TEST_F(TestDiscreteThreshold, thresholdIsNotInitializeExpectNoActionCommit)
 {
-    EXPECT_CALL(actionMock, commit(_, _, _)).Times(0);
+    EXPECT_CALL(actionMock, commit(_, _, _, _, _)).Times(0);
 }
 
 class TestDiscreteThresholdValues :
@@ -97,33 +102,55 @@
     EXPECT_THROW(makeThreshold(0ms, GetParam()), std::invalid_argument);
 }
 
+class TestDiscreteThresholdInit : public TestDiscreteThreshold
+{
+    void SetUp() override
+    {}
+};
+
+TEST_F(TestDiscreteThresholdInit, nonEmptyNameIsNotChanged)
+{
+    auto sut = makeThreshold(0ms, "12.3", discrete::Severity::ok, "non-empty");
+    EXPECT_THAT(
+        std::get<discrete::LabeledThresholdParam>(sut->getThresholdParam())
+            .at_label<utils::tstring::UserId>(),
+        Eq("non-empty"));
+}
+
+TEST_F(TestDiscreteThresholdInit, emptyNameIsChanged)
+{
+    auto sut = makeThreshold(0ms, "12.3", discrete::Severity::ok, "");
+    EXPECT_THAT(
+        std::get<discrete::LabeledThresholdParam>(sut->getThresholdParam())
+            .at_label<utils::tstring::UserId>(),
+        Not(Eq("")));
+}
+
 struct DiscreteParams
 {
     struct UpdateParams
     {
         size_t sensor;
-        Milliseconds timestamp;
         double value;
         Milliseconds sleepAfter;
 
-        UpdateParams(size_t sensor, Milliseconds timestamp, double value,
+        UpdateParams(size_t sensor, double value,
                      Milliseconds sleepAfter = 0ms) :
             sensor(sensor),
-            timestamp(timestamp), value(value), sleepAfter(sleepAfter)
+            value(value), sleepAfter(sleepAfter)
         {}
     };
 
     struct ExpectedParams
     {
         size_t sensor;
-        Milliseconds timestamp;
         double value;
         Milliseconds waitMin;
 
-        ExpectedParams(size_t sensor, Milliseconds timestamp, double value,
+        ExpectedParams(size_t sensor, double value,
                        Milliseconds waitMin = 0ms) :
             sensor(sensor),
-            timestamp(timestamp), value(value), waitMin(waitMin)
+            value(value), waitMin(waitMin)
         {}
     };
 
@@ -155,21 +182,19 @@
     {
         *os << "{ DwellTime: " << o.dwellTime.count() << "ms ";
         *os << ", ThresholdValue: " << o.thresholdValue;
-        *os << ", Updates: ";
-        for (const auto& [index, timestamp, value, sleepAfter] : o.updates)
+        *os << ", Updates: [ ";
+        for (const auto& [index, value, sleepAfter] : o.updates)
         {
-            *os << "{ SensorIndex: " << index
-                << ", Timestamp: " << timestamp.count() << ", Value: " << value
+            *os << "{ SensorIndex: " << index << ", Value: " << value
                 << ", SleepAfter: " << sleepAfter.count() << "ms }, ";
         }
-        *os << "Expected: ";
-        for (const auto& [index, timestamp, value, waitMin] : o.expected)
+        *os << " ] Expected: [ ";
+        for (const auto& [index, value, waitMin] : o.expected)
         {
-            *os << "{ SensorIndex: " << index
-                << ", Timestamp: " << timestamp.count() << ", Value: " << value
+            *os << "{ SensorIndex: " << index << ", Value: " << value
                 << ", waitMin: " << waitMin.count() << "ms }, ";
         }
-        *os << " }";
+        *os << " ] }";
     }
 
     std::vector<UpdateParams> updates;
@@ -200,11 +225,12 @@
 
         InSequence seq;
 
-        for (const auto& [index, timestamp, value, waitMin] :
-             GetParam().expected)
+        for (const auto& [index, value, waitMin] : GetParam().expected)
         {
             EXPECT_CALL(actionMock,
-                        commit(sensorNames[index], timestamp, value))
+                        commit(triggerId, Optional(StrEq("treshold name")),
+                               sensorNames[index], _,
+                               TriggerValue(GetParam().thresholdValue)))
                 .WillOnce(DoAll(
                     InvokeWithoutArgs([idx = index, &timestamps] {
                         timestamps[idx] =
@@ -215,16 +241,14 @@
 
         auto start = std::chrono::high_resolution_clock::now();
 
-        for (const auto& [index, timestamp, value, sleepAfter] :
-             GetParam().updates)
+        for (const auto& [index, value, sleepAfter] : GetParam().updates)
         {
-            sut->sensorUpdated(*sensorMocks[index], timestamp, value);
+            sut->sensorUpdated(*sensorMocks[index], 42ms, value);
             sleep(sleepAfter);
         }
 
         EXPECT_THAT(DbusEnvironment::waitForFutures("commit"), true);
-        for (const auto& [index, timestamp, value, waitMin] :
-             GetParam().expected)
+        for (const auto& [index, value, waitMin] : GetParam().expected)
         {
             EXPECT_THAT(timestamps[index] - start, Ge(waitMin));
         }
@@ -236,7 +260,6 @@
   public:
     void SetUp() override
     {
-
         for (size_t idx = 0; idx < sensorMocks.size(); idx++)
         {
             ON_CALL(*sensorMocks.at(idx), getName())
@@ -247,26 +270,20 @@
     }
 };
 
-INSTANTIATE_TEST_SUITE_P(_, TestDiscreteThresholdNoDwellTime,
-                         Values(DiscreteParams()
-                                    .ThresholdValue("90.0")
-                                    .Updates({{0, 1ms, 80.0}, {0, 2ms, 89.0}})
-                                    .Expected({}),
-                                DiscreteParams()
-                                    .ThresholdValue("90.0")
-                                    .Updates({{0, 1ms, 80.0},
-                                              {0, 2ms, 90.0},
-                                              {0, 3ms, 80.0},
-                                              {0, 4ms, 90.0}})
-                                    .Expected({{0, 2ms, 90.0}, {0, 4ms, 90.0}}),
-                                DiscreteParams()
-                                    .ThresholdValue("90.0")
-                                    .Updates({{0, 1ms, 90.0},
-                                              {0, 2ms, 99.0},
-                                              {1, 3ms, 100.0},
-                                              {1, 4ms, 90.0}})
-                                    .Expected({{0, 1ms, 90.0},
-                                               {1, 4ms, 90.0}})));
+INSTANTIATE_TEST_SUITE_P(
+    _, TestDiscreteThresholdNoDwellTime,
+    Values(DiscreteParams()
+               .ThresholdValue("90.0")
+               .Updates({{0, 80.0}, {0, 89.0}})
+               .Expected({}),
+           DiscreteParams()
+               .ThresholdValue("90.0")
+               .Updates({{0, 80.0}, {0, 90.0}, {0, 80.0}, {0, 90.0}})
+               .Expected({{0, 90.0}, {0, 90.0}}),
+           DiscreteParams()
+               .ThresholdValue("90.0")
+               .Updates({{0, 90.0}, {0, 99.0}, {1, 100.0}, {1, 90.0}})
+               .Expected({{0, 90.0}, {1, 90.0}})));
 
 TEST_P(TestDiscreteThresholdNoDwellTime, senorsIsUpdatedMultipleTimes)
 {
@@ -293,31 +310,31 @@
     Values(DiscreteParams()
                .DwellTime(200ms)
                .ThresholdValue("90.0")
-               .Updates({{0, 1ms, 90.0, 100ms}, {0, 2ms, 91.0}, {0, 3ms, 90.0}})
-               .Expected({{0, 3ms, 90.0, 300ms}}),
+               .Updates({{0, 90.0, 100ms}, {0, 91.0}, {0, 90.0}})
+               .Expected({{0, 90.0, 300ms}}),
            DiscreteParams()
                .DwellTime(100ms)
                .ThresholdValue("90.0")
-               .Updates({{0, 1ms, 90.0, 100ms}})
-               .Expected({{0, 1ms, 90.0, 100ms}}),
+               .Updates({{0, 90.0, 100ms}})
+               .Expected({{0, 90.0, 100ms}}),
            DiscreteParams()
                .DwellTime(1000ms)
                .ThresholdValue("90.0")
-               .Updates({{0, 1ms, 90.0, 700ms},
-                         {0, 1ms, 91.0, 100ms},
-                         {0, 1ms, 90.0, 300ms},
-                         {0, 1ms, 91.0, 100ms}})
+               .Updates({{0, 90.0, 700ms},
+                         {0, 91.0, 100ms},
+                         {0, 90.0, 300ms},
+                         {0, 91.0, 100ms}})
                .Expected({}),
            DiscreteParams()
                .DwellTime(200ms)
                .ThresholdValue("90.0")
-               .Updates({{0, 1ms, 90.0},
-                         {1, 2ms, 89.0, 100ms},
-                         {1, 3ms, 90.0, 100ms},
-                         {1, 4ms, 89.0, 100ms},
-                         {1, 5ms, 90.0, 300ms},
-                         {1, 6ms, 89.0, 100ms}})
-               .Expected({{0, 1ms, 90, 200ms}, {1, 5ms, 90, 500ms}})));
+               .Updates({{0, 90.0},
+                         {1, 89.0, 100ms},
+                         {1, 90.0, 100ms},
+                         {1, 89.0, 100ms},
+                         {1, 90.0, 300ms},
+                         {1, 89.0, 100ms}})
+               .Expected({{0, 90, 200ms}, {1, 90, 500ms}})));
 
 TEST_P(TestDiscreteThresholdWithDwellTime, senorsIsUpdatedMultipleTimes)
 {