Fix sensor count not updating

When ReadingsParamaters are updated, sensor count may change. This
action should resize buffer and update value of "AppendLimit" property
in case it was not set by user.

Testing done:
-UTs passing

Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: I96486a808a3cce83c09936262304d0a1f55b3fd7
diff --git a/src/report.cpp b/src/report.cpp
index c2a0371..70cd8f7 100644
--- a/src/report.cpp
+++ b/src/report.cpp
@@ -221,6 +221,18 @@
     }
 }
 
+void Report::updateSensorCount(const uint64_t newSensorCount)
+{
+    if (sensorCount != newSensorCount)
+    {
+        sensorCount = newSensorCount;
+        if (!appendLimit.has_value())
+        {
+            reportIface->signal_property("AppendLimit");
+        }
+    }
+}
+
 std::unique_ptr<sdbusplus::asio::dbus_interface>
     Report::makeReportInterface(const interfaces::ReportFactory& reportFactory)
 {
@@ -340,6 +352,8 @@
                 utils::transform(metrics, [](const auto& metric) {
                     return metric->dumpConfiguration();
                 }));
+            updateSensorCount(getSensorCount(metrics));
+            setReadingBuffer(reportUpdates);
             persistency = storeConfiguration();
             oldVal = std::move(newVal);
             return 1;
diff --git a/src/report.hpp b/src/report.hpp
index 74c979b..856e369 100644
--- a/src/report.hpp
+++ b/src/report.hpp
@@ -102,6 +102,7 @@
                               const ReportingType reportingTypeIn) const;
     void setReadingBuffer(const ReportUpdates newReportUpdates);
     void setReportUpdates(const ReportUpdates newReportUpdates);
+    void updateSensorCount(const uint64_t newSensorCount);
     static uint64_t getSensorCount(
         const std::vector<std::shared_ptr<interfaces::Metric>>& metrics);
     interfaces::JsonStorage::FilePath reportFileName() const;
diff --git a/tests/src/test_report.cpp b/tests/src/test_report.cpp
index d05620b..ea3b4f8 100644
--- a/tests/src/test_report.cpp
+++ b/tests/src/test_report.cpp
@@ -109,6 +109,10 @@
             metricMocks.emplace_back(std::make_shared<NiceMock<MetricMock>>());
             ON_CALL(*metricMocks[i], dumpConfiguration())
                 .WillByDefault(Return(metricParameters[i]));
+            ON_CALL(*metricMocks[i], sensorCount())
+                .WillByDefault(Return(metricParameters[i]
+                                          .at_label<tstring::SensorPath>()
+                                          .size()));
         }
 
         return utils::convContainer<std::shared_ptr<interfaces::Metric>>(
@@ -281,6 +285,34 @@
                 Eq(newParams));
 }
 
+TEST_F(TestReport, setReadingParametersWithNewParamsUpdatesSensorCount)
+{
+    auto report =
+        makeReport(ReportParams()
+                       .appendLimit(std::numeric_limits<uint64_t>::max())
+                       .reportId("DefaultAppendLimit"));
+
+    ReadingParameters newParams = toReadingParameters(
+        std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
+            {LabeledSensorInfo{"Service",
+                               "/xyz/openbmc_project/sensors/power/psu",
+                               "NewMetadata123"}},
+            OperationType::avg,
+            "NewMetricId123",
+            CollectionTimeScope::startup,
+            CollectionDuration(250ms)}}});
+    auto metrics = getMetricsFromReadingParams(newParams);
+
+    EXPECT_CALL(*reportFactoryMock, updateMetrics(_, _, _))
+        .WillOnce(SetArgReferee<0>(metrics));
+    EXPECT_THAT(setProperty(report->getPath(), "ReadingParametersFutureVersion",
+                            newParams)
+                    .value(),
+                Eq(boost::system::errc::success));
+    EXPECT_THAT(getProperty<uint64_t>(report->getPath(), "AppendLimit"),
+                Eq(1ull));
+}
+
 TEST_F(TestReport, setReadingParametersWithTooLongMetricId)
 {
     const ReadingParameters currentValue =