Abstract fillMessageArgs and unit test it
EventService has a routine for taking a message registry entry and
populate it with data. This ideally should be part of the message
registry namespace, not EventService, as it could be useful to later
patchsets. So break out the method, and write some unit tests to ensure
that it can be relied upon in the future.
Tested: Unit tests ran and passing.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I052d9492f306b63fb72cbf78286370ed0c477430
diff --git a/meson.build b/meson.build
index c9066d4..d9b4f44 100644
--- a/meson.build
+++ b/meson.build
@@ -375,13 +375,14 @@
srcfiles_unittest = [
'http/ut/utility_test.cpp',
'include/ut/dbus_utility_test.cpp',
- 'include/ut/multipart_test.cpp',
'include/ut/http_utility_test.cpp',
'include/ut/human_sort_test.cpp',
+ 'include/ut/multipart_test.cpp',
'redfish-core/ut/configfile_test.cpp',
'redfish-core/ut/hex_utils_test.cpp',
'redfish-core/ut/lock_test.cpp',
'redfish-core/ut/privileges_test.cpp',
+ 'redfish-core/ut/registries_test.cpp',
'redfish-core/ut/stl_utils_test.cpp',
'redfish-core/ut/time_utils_test.cpp',
]
diff --git a/redfish-core/include/event_service_manager.hpp b/redfish-core/include/event_service_manager.hpp
index 3429c02..55485c6 100644
--- a/redfish-core/include/event_service_manager.hpp
+++ b/redfish-core/include/event_service_manager.hpp
@@ -223,7 +223,7 @@
inline int formatEventLogEntry(const std::string& logEntryID,
const std::string& messageID,
- const std::vector<std::string>& messageArgs,
+ const std::span<std::string_view> messageArgs,
std::string timestamp,
const std::string& customText,
nlohmann::json& logEntryJson)
@@ -240,17 +240,7 @@
severity = message->severity;
}
- // Fill the MessageArgs into the Message
- int i = 0;
- for (const std::string& messageArg : messageArgs)
- {
- std::string argStr = "%" + std::to_string(++i);
- size_t argPos = msg.find(argStr);
- if (argPos != std::string::npos)
- {
- msg.replace(argPos, argStr.length(), messageArg);
- }
- }
+ redfish::message_registries::fillMessageArgs(messageArgs, msg);
// Get the Created time from the timestamp. The log timestamp is in
// RFC3339 format which matches the Redfish format except for the
@@ -482,11 +472,14 @@
}
}
+ std::vector<std::string_view> messageArgsView(messageArgs.begin(),
+ messageArgs.end());
+
logEntryArray.push_back({});
nlohmann::json& bmcLogEntry = logEntryArray.back();
- if (event_log::formatEventLogEntry(idStr, messageID, messageArgs,
- timestamp, customText,
- bmcLogEntry) != 0)
+ if (event_log::formatEventLogEntry(idStr, messageID,
+ messageArgsView, timestamp,
+ customText, bmcLogEntry) != 0)
{
BMCWEB_LOG_DEBUG << "Read eventLog entry failed";
continue;
diff --git a/redfish-core/include/registries.hpp b/redfish-core/include/registries.hpp
index e326323..f85d2a5 100644
--- a/redfish-core/include/registries.hpp
+++ b/redfish-core/include/registries.hpp
@@ -14,6 +14,11 @@
// limitations under the License.
*/
#pragma once
+
+#include <span>
+#include <string>
+#include <string_view>
+
namespace redfish::message_registries
{
struct Header
@@ -40,4 +45,21 @@
const char* resolution;
};
using MessageEntry = std::pair<const char*, const Message>;
+
+inline void fillMessageArgs(const std::span<const std::string_view> messageArgs,
+ std::string& msg)
+{
+ int i = 0;
+ for (const std::string_view& messageArg : messageArgs)
+ {
+ std::string argStr = "%" + std::to_string(i + 1);
+ size_t argPos = msg.find(argStr);
+ if (argPos != std::string::npos)
+ {
+ msg.replace(argPos, argStr.length(), messageArg);
+ }
+ i++;
+ }
+}
+
} // namespace redfish::message_registries
diff --git a/redfish-core/ut/registries_test.cpp b/redfish-core/ut/registries_test.cpp
new file mode 100644
index 0000000..8340340
--- /dev/null
+++ b/redfish-core/ut/registries_test.cpp
@@ -0,0 +1,19 @@
+#include "registries.hpp"
+
+#include "gmock/gmock.h"
+
+TEST(RedfishRegistries, fillMessageArgs)
+{
+ using redfish::message_registries::fillMessageArgs;
+ std::string toFill("%1");
+ fillMessageArgs({{"foo"}}, toFill);
+ EXPECT_EQ(toFill, "foo");
+
+ toFill = "";
+ fillMessageArgs({}, toFill);
+ EXPECT_EQ(toFill, "");
+
+ toFill = "%1, %2";
+ fillMessageArgs({{"foo", "bar"}}, toFill);
+ EXPECT_EQ(toFill, "foo, bar");
+}