Load the associations definitions
Parse the JSON file to load in the _associations
data structures. Any failures will cause an exception
to be thrown that will crash the app.
The JSON looks like:
[
{
"path": "The relative path of the inventory object to create the
org.openbmc.Associations interface on."
"endpoints":
[
{
"types":
{
"fType": "The forward association type."
"rType": "The reverse association type."
},
"paths":
[
"The list of association endpoints for this
inventory path and association type."
]
}
]
}
]
Change-Id: I098fdc607f0c3ab2861f9b33e3e0d46e4989bd7a
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/test/associations_test.cpp b/test/associations_test.cpp
new file mode 100644
index 0000000..357659d
--- /dev/null
+++ b/test/associations_test.cpp
@@ -0,0 +1,302 @@
+#include "association_manager.hpp"
+
+#include <filesystem>
+#include <fstream>
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::inventory::manager::associations;
+namespace fs = std::filesystem;
+
+static const auto goodJson = R"(
+[
+ {
+ "path": "system/PS0",
+ "endpoints":
+ [
+ {
+ "types":
+ {
+ "rType": "inventory",
+ "fType": "sensors"
+ },
+ "paths":
+ [
+ "power/ps0_input_power",
+ "voltage/ps0_input_voltage",
+ "current/ps0_output_current",
+ "voltage/ps0_output_voltage"
+ ]
+ },
+ {
+ "types":
+ {
+ "rType": "inventory",
+ "fType": "fans"
+ },
+ "paths":
+ [
+ "fan_tach/ps0_fan"
+ ]
+ }
+ ]
+ },
+ {
+ "path": "system/fan42",
+ "endpoints":
+ [
+ {
+ "types":
+ {
+ "rType": "inventory",
+ "fType": "sensors"
+ },
+ "paths":
+ [
+ "fan_tach/fan42"
+ ]
+ },
+ {
+ "types":
+ {
+ "rType": "inventory",
+ "fType": "led"
+ },
+ "paths":
+ [
+ "led/fan42"
+ ]
+ }
+ ]
+ }
+])";
+
+// Malformed JSON
+static const auto badJson0 = R"(
+ "hello": world
+})";
+
+// Uses 'blah' instead of 'paths'
+static const auto badJson1 = R"(
+[
+ {
+ "blah": "system/PS0",
+ "endpoints":
+ [
+ {
+ "types":
+ {
+ "fType": "inventory",
+ "rType": "sensors"
+ },
+ "paths":
+ [
+ "ps0_input_power",
+ ]
+ }
+ ]
+ }
+])";
+
+// Uses 'blah' instead of 'rType'
+static const auto badJson2 = R"(
+[
+ {
+ "paths": "system/PS0",
+ "endpoints":
+ [
+ {
+ "types":
+ {
+ "blah": "inventory",
+ "fType": "sensors"
+ },
+ "paths":
+ [
+ "ps0_input_power",
+ ]
+ }
+ ]
+ }
+])";
+
+// Missing the endpoints/paths array
+static const auto badJson3 = R"(
+[
+ {
+ "paths": "system/PS0",
+ "endpoints":
+ [
+ {
+ "types":
+ {
+ "rType": "inventory",
+ "fType": "sensors"
+ }
+ }
+ ]
+ }
+])";
+
+class AssocsTest : public ::testing::Test
+{
+ protected:
+ AssocsTest() : ::testing::Test(), bus(sdbusplus::bus::new_default())
+ {
+ }
+
+ fs::path jsonDir;
+ sdbusplus::bus::bus bus;
+
+ virtual void SetUp()
+ {
+ char dir[] = {"assocTestXXXXXX"};
+ jsonDir = mkdtemp(dir);
+ }
+
+ virtual void TearDown()
+ {
+ fs::remove_all(jsonDir);
+ }
+
+ std::string writeFile(const char* data)
+ {
+ fs::path path = jsonDir / "associations.json";
+
+ std::ofstream f{path};
+ f << data;
+ f.close();
+
+ return path;
+ }
+};
+
+TEST_F(AssocsTest, TEST_NO_JSON)
+{
+ try
+ {
+ Manager m{bus};
+ EXPECT_TRUE(false);
+ }
+ catch (std::exception& e)
+ {
+ }
+}
+
+TEST_F(AssocsTest, TEST_GOOD_JSON)
+{
+ auto path = writeFile(goodJson);
+ Manager m(bus, path);
+
+ const auto& a = m.getAssociationsConfig();
+ EXPECT_EQ(a.size(), 2);
+
+ {
+ auto x = a.find("/xyz/openbmc_project/inventory/system/PS0");
+ EXPECT_NE(x, a.end());
+
+ auto& endpoints = x->second;
+ EXPECT_EQ(endpoints.size(), 2);
+
+ {
+ auto& types = std::get<0>(endpoints[0]);
+ EXPECT_EQ(std::get<0>(types), "sensors");
+ EXPECT_EQ(std::get<1>(types), "inventory");
+
+ auto& paths = std::get<1>(endpoints[0]);
+ EXPECT_EQ(paths.size(), 4);
+ }
+ {
+ auto& types = std::get<0>(endpoints[1]);
+ EXPECT_EQ(std::get<0>(types), "fans");
+ EXPECT_EQ(std::get<1>(types), "inventory");
+
+ auto& paths = std::get<1>(endpoints[1]);
+ EXPECT_EQ(paths.size(), 1);
+ }
+ }
+ {
+ auto x = a.find("/xyz/openbmc_project/inventory/system/fan42");
+ EXPECT_NE(x, a.end());
+
+ auto& endpoints = x->second;
+ EXPECT_EQ(endpoints.size(), 2);
+
+ {
+ auto& types = std::get<0>(endpoints[0]);
+ EXPECT_EQ(std::get<0>(types), "sensors");
+ EXPECT_EQ(std::get<1>(types), "inventory");
+
+ auto& paths = std::get<1>(endpoints[0]);
+ EXPECT_EQ(paths.size(), 1);
+ }
+ {
+ auto& types = std::get<0>(endpoints[1]);
+ EXPECT_EQ(std::get<0>(types), "led");
+ EXPECT_EQ(std::get<1>(types), "inventory");
+
+ auto& paths = std::get<1>(endpoints[1]);
+ EXPECT_EQ(paths.size(), 1);
+ }
+ }
+}
+
+TEST_F(AssocsTest, TEST_BAD_JSON0)
+{
+ auto path = writeFile(badJson0);
+
+ try
+ {
+ Manager m(bus, path);
+
+ EXPECT_TRUE(false);
+ }
+ catch (std::exception& e)
+ {
+ }
+}
+
+TEST_F(AssocsTest, TEST_BAD_JSON1)
+{
+ auto path = writeFile(badJson1);
+
+ try
+ {
+ Manager m(bus, path);
+
+ EXPECT_TRUE(false);
+ }
+ catch (std::exception& e)
+ {
+ }
+}
+
+TEST_F(AssocsTest, TEST_BAD_JSON2)
+{
+ auto path = writeFile(badJson2);
+
+ try
+ {
+ Manager m(bus, path);
+
+ EXPECT_TRUE(false);
+ }
+ catch (std::exception& e)
+ {
+ }
+}
+
+TEST_F(AssocsTest, TEST_BAD_JSON3)
+{
+ auto path = writeFile(badJson3);
+
+ try
+ {
+ Manager m(bus, path);
+
+ EXPECT_TRUE(false);
+ }
+ catch (std::exception& e)
+ {
+ }
+}