PEL: Add SRC::getErrorDetails() unit test

Tests the registry message string placeholder substitution

Signed-off-by: Harisuddin Mohamed Isa <harisuddin@gmail.com>
Change-Id: If89609a911403a76bd6044f81e0f464e38aec8f2
diff --git a/test/openpower-pels/src_test.cpp b/test/openpower-pels/src_test.cpp
index c1b5116..b966344 100644
--- a/test/openpower-pels/src_test.cpp
+++ b/test/openpower-pels/src_test.cpp
@@ -16,11 +16,89 @@
 #include "extensions/openpower-pels/src.hpp"
 #include "pel_utils.hpp"
 
+#include <fstream>
+
 #include <gtest/gtest.h>
 
 using namespace openpower::pels;
+namespace fs = std::filesystem;
 
-TEST(SRCTest, UnflattenFlattenTestNoCallouts)
+const auto testRegistry = R"(
+{
+"PELs":
+[
+    {
+        "Name": "xyz.openbmc_project.Error.Test",
+        "Subsystem": "bmc_firmware",
+        "SRC":
+        {
+            "ReasonCode": "0xABCD",
+            "Words6To9":
+            {
+                "6":
+                {
+                    "Description": "Component ID",
+                    "AdditionalDataPropSource": "COMPID"
+                },
+                "7":
+                {
+                    "Description": "Failure count",
+                    "AdditionalDataPropSource": "FREQUENCY"
+                },
+                "8":
+                {
+                    "Description": "Time period",
+                    "AdditionalDataPropSource": "DURATION"
+                },
+                "9":
+                {
+                    "Description": "Error code",
+                    "AdditionalDataPropSource": "ERRORCODE"
+                }
+            }
+        },
+        "Documentation":
+        {
+            "Description": "A Component Fault",
+            "Message": "Comp %1 failed %2 times over %3 secs with ErrorCode %4",
+            "MessageArgSources":
+            [
+                "SRCWord6", "SRCWord7", "SRCWord8", "SRCWord9"
+            ]
+        }
+    }
+]
+}
+)";
+
+class SRCTest : public ::testing::Test
+{
+  protected:
+    static void SetUpTestCase()
+    {
+        char path[] = "/tmp/srctestXXXXXX";
+        regDir = mkdtemp(path);
+    }
+
+    static void TearDownTestCase()
+    {
+        fs::remove_all(regDir);
+    }
+
+    static std::string writeData(const char* data)
+    {
+        fs::path path = regDir / "registry.json";
+        std::ofstream stream{path};
+        stream << data;
+        return path;
+    }
+
+    static fs::path regDir;
+};
+
+fs::path SRCTest::regDir{};
+
+TEST_F(SRCTest, UnflattenFlattenTestNoCallouts)
 {
     auto data = pelDataFactory(TestPELType::primarySRCSection);
 
@@ -61,7 +139,7 @@
     EXPECT_EQ(data, newData);
 }
 
-TEST(SRCTest, UnflattenFlattenTest2Callouts)
+TEST_F(SRCTest, UnflattenFlattenTest2Callouts)
 {
     auto data = pelDataFactory(TestPELType::primarySRCSection2Callouts);
 
@@ -98,7 +176,7 @@
 }
 
 // Create an SRC from the message registry
-TEST(SRCTest, CreateTestNoCallouts)
+TEST_F(SRCTest, CreateTestNoCallouts)
 {
     message::Entry entry;
     entry.src.type = 0xBD;
@@ -160,3 +238,24 @@
     EXPECT_EQ(newSRC.asciiString(), src.asciiString());
     EXPECT_FALSE(newSRC.callouts());
 }
+
+// Test the getErrorDetails function
+TEST_F(SRCTest, MessageSubstitutionTest)
+{
+    auto path = SRCTest::writeData(testRegistry);
+    message::Registry registry{path};
+    auto entry = registry.lookup("0xABCD", message::LookupType::reasonCode);
+
+    std::vector<std::string> adData{"COMPID=0x1", "FREQUENCY=0x4",
+                                    "DURATION=30", "ERRORCODE=0x01ABCDEF"};
+    AdditionalData ad{adData};
+
+    SRC src{*entry, ad};
+    EXPECT_TRUE(src.valid());
+
+    auto errorDetails = src.getErrorDetails(registry, DetailLevel::message);
+    ASSERT_TRUE(errorDetails);
+    EXPECT_EQ(
+        errorDetails.value(),
+        "Comp 0x1 failed 0x4 times over 0x1E secs with ErrorCode 0x1ABCDEF");
+}