entityName: move pieces into handler object
Move different items used by the handler into the handler and enable
unit-testing by parameterizing different aspects of the code.
Tested: Only ran unit-tests (added new ones).
Change-Id: Ia3b4b5792c0ac1ae5bc6513eadfc9ee35f7a369f
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/handler_unittest.cpp b/test/handler_unittest.cpp
index c1f4093..a4f9281 100644
--- a/test/handler_unittest.cpp
+++ b/test/handler_unittest.cpp
@@ -1,6 +1,8 @@
#include "errors.hpp"
#include "handler.hpp"
+#include <fstream>
+#include <nlohmann/json.hpp>
#include <string>
#include <tuple>
@@ -27,6 +29,70 @@
EXPECT_THROW(h.getRxPackets("eth0/../../"), IpmiException);
}
+TEST(HandlerTest, readNameFromConfigInstanceVariety)
+{
+ // Make sure it handles the failures and successes as we expect.
+ struct testCase
+ {
+ std::string type;
+ std::uint8_t instance;
+ std::string expectedName;
+ };
+
+ std::vector<testCase> tests = {
+ {"cpu", 5, ""},
+ {"cpu", 3, "CPU2"},
+ };
+
+ auto j2 = R"(
+ {
+ "cpu": [
+ {"instance": 1, "name": "CPU0"},
+ {"instance": 2, "name": "CPU1"},
+ {"instance": 3, "name": "CPU2"},
+ {"instance": 4, "name": "CPU3"}
+ ]
+ }
+ )"_json;
+
+ for (const auto& test : tests)
+ {
+ EXPECT_STREQ(test.expectedName.c_str(),
+ readNameFromConfig(test.type, test.instance, j2).c_str());
+ }
+}
+
+// TODO: If we can test with phosphor-logging in the future, there are more
+// failure cases.
+
+TEST(HandlerTest, getEntityNameWithNameNotFoundExcepts)
+{
+ const char* testFilename = "test.json";
+ std::string contents = R"({"cpu": [{"instance": 1, "name": "CPU0"}]})";
+ std::ofstream outputJson(testFilename);
+ outputJson << contents;
+ outputJson.flush();
+ outputJson.close();
+
+ Handler h(testFilename);
+ EXPECT_THROW(h.getEntityName(0x03, 2), IpmiException);
+ (void)std::remove(testFilename);
+}
+
+TEST(HandlerTest, getEntityNameWithNameFoundReturnsIt)
+{
+ const char* testFilename = "test.json";
+ std::string contents = R"({"cpu": [{"instance": 1, "name": "CPU0"}]})";
+ std::ofstream outputJson(testFilename);
+ outputJson << contents;
+ outputJson.flush();
+ outputJson.close();
+
+ Handler h(testFilename);
+ EXPECT_STREQ("CPU0", h.getEntityName(0x03, 1).c_str());
+ (void)std::remove(testFilename);
+}
+
// TODO: Add checks for other functions of handler.
} // namespace ipmi