From ce3f450b97b56fbf639cd4a271a3ba124b5d6406 Mon Sep 17 00:00:00 2001 From: Matt Spinler Date: Wed, 22 Jan 2020 15:44:35 -0600 Subject: PEL: Add Section::getJSON() implementer test Test that the PEL section classes that provide a getJSON() implementation return valid JSON. Signed-off-by: Matt Spinler Change-Id: I10dc6bbd0e1e4d63e6416928e9951ac6f85ba774 --- test/openpower-pels/pel_test.cpp | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/openpower-pels/pel_test.cpp b/test/openpower-pels/pel_test.cpp index 32f8d5e..2cf58d7 100644 --- a/test/openpower-pels/pel_test.cpp +++ b/test/openpower-pels/pel_test.cpp @@ -344,3 +344,44 @@ TEST_F(PELTest, SysInfoSectionTest) auto version = json["BMC Version ID"].get(); EXPECT_EQ(version, "ABCD1234"); } + +// Test that the sections that override +// virtual std::optional Section::getJSON() const +// return valid JSON. +TEST_F(PELTest, SectionJSONTest) +{ + auto data = pelDataFactory(TestPELType::pelSimple); + PEL pel{data}; + + // Check that all JSON returned from the sections is + // parseable by nlohmann::json, which will throw an + // exception and fail the test if there is a problem. + + // The getJSON() response needs to be wrapped in a { } to make + // actual valid JSON (PEL::toJSON() usually handles that). + + auto jsonString = pel.privateHeader().getJSON(); + + // PrivateHeader always prints JSON + ASSERT_TRUE(jsonString); + *jsonString = '{' + *jsonString + '}'; + auto json = nlohmann::json::parse(*jsonString); + + jsonString = pel.userHeader().getJSON(); + + // UserHeader always prints JSON + ASSERT_TRUE(jsonString); + *jsonString = '{' + *jsonString + '}'; + json = nlohmann::json::parse(*jsonString); + + for (const auto& section : pel.optionalSections()) + { + // The optional sections may or may not have implemented getJSON(). + jsonString = section->getJSON(); + if (jsonString) + { + *jsonString = '{' + *jsonString + '}'; + auto json = nlohmann::json::parse(*jsonString); + } + } +} -- cgit v1.2.1