test: common: associations

Test software associations in common code.
Write necessary wrappers in example device to use the protected members.

Change-Id: If7c38f12472699672ed8a4c1b3e1c99c398cdba5
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/test/common/software/meson.build b/test/common/software/meson.build
index e67ee78..62c97de 100644
--- a/test/common/software/meson.build
+++ b/test/common/software/meson.build
@@ -1,5 +1,9 @@
 
-testcases = ['software_get_random_softwareid', 'software_config']
+testcases = [
+    'software_get_random_softwareid',
+    'software_config',
+    'software_association',
+]
 
 foreach t : testcases
     test(
diff --git a/test/common/software/software_association.cpp b/test/common/software/software_association.cpp
new file mode 100644
index 0000000..c8da550
--- /dev/null
+++ b/test/common/software/software_association.cpp
@@ -0,0 +1,124 @@
+#include "../exampledevice/example_device.hpp"
+
+#include <phosphor-logging/lg2.hpp>
+#include <sdbusplus/asio/connection.hpp>
+#include <sdbusplus/asio/object_server.hpp>
+#include <sdbusplus/async.hpp>
+#include <sdbusplus/server.hpp>
+#include <xyz/openbmc_project/Association/Definitions/client.hpp>
+
+#include <memory>
+
+#include <gtest/gtest.h>
+
+PHOSPHOR_LOG2_USING;
+
+using namespace phosphor::software;
+using namespace phosphor::software::example_device;
+
+constexpr const char* exampleEndpoint = "/xyz/example_endpoint";
+
+class SoftwareAssocTest : public testing::Test
+{
+  protected:
+    SoftwareAssocTest() :
+        exampleUpdater(ctx, "swVersion"), device(exampleUpdater.getDevice()),
+        objPathCurrentSoftware(
+            reinterpret_cast<ExampleSoftware*>(device->softwareCurrent.get())
+                ->objectPath),
+        busName(exampleUpdater.getBusName())
+    {}
+    ~SoftwareAssocTest() noexcept override {}
+
+    sdbusplus::async::context ctx;
+    ExampleCodeUpdater exampleUpdater;
+    std::unique_ptr<ExampleDevice>& device;
+
+    std::string objPathCurrentSoftware;
+
+    std::string busName;
+
+  public:
+    SoftwareAssocTest(const SoftwareAssocTest&) = delete;
+    SoftwareAssocTest(SoftwareAssocTest&&) = delete;
+    SoftwareAssocTest& operator=(const SoftwareAssocTest&) = delete;
+    SoftwareAssocTest& operator=(SoftwareAssocTest&&) = delete;
+};
+
+sdbusplus::async::task<> testSoftwareAssociationMissing(
+    sdbusplus::async::context& ctx, const std::string& objPathCurrentSoftware,
+    const std::string& busName)
+{
+    auto client =
+        sdbusplus::client::xyz::openbmc_project::association::Definitions<>(ctx)
+            .service(busName)
+            .path(objPathCurrentSoftware);
+
+    // by default there is no association on the software
+    try
+    {
+        co_await client.associations();
+
+        EXPECT_TRUE(false);
+    }
+    catch (std::exception& e)
+    {
+        error(e.what());
+    }
+
+    ctx.request_stop();
+    co_return;
+}
+
+TEST_F(SoftwareAssocTest, TestSoftwareAssociationMissing)
+{
+    ctx.spawn(
+        testSoftwareAssociationMissing(ctx, objPathCurrentSoftware, busName));
+    ctx.run();
+}
+
+sdbusplus::async::task<> testSoftwareAssociation(
+    sdbusplus::async::context& ctx, std::unique_ptr<ExampleDevice>& device,
+    const std::string& objPathCurrentSoftware, const std::string& busName,
+    bool createRunningAssoc, std::string expectAssociation)
+{
+    auto client =
+        sdbusplus::client::xyz::openbmc_project::association::Definitions<>(ctx)
+            .service(busName)
+            .path(objPathCurrentSoftware);
+
+    reinterpret_cast<ExampleSoftware*>(device->softwareCurrent.get())
+        ->createInventoryAssociation(createRunningAssoc, exampleEndpoint);
+
+    try
+    {
+        auto res = co_await client.associations();
+
+        EXPECT_EQ(res.size(), 1);
+        EXPECT_EQ(std::get<0>(res[0]), expectAssociation);
+        EXPECT_EQ(std::get<2>(res[0]), exampleEndpoint);
+    }
+    catch (std::exception& e)
+    {
+        error(e.what());
+        EXPECT_TRUE(false);
+    }
+
+    ctx.request_stop();
+    co_return;
+}
+
+TEST_F(SoftwareAssocTest, TestSoftwareAssociationRunning)
+{
+    ctx.spawn(testSoftwareAssociation(ctx, device, objPathCurrentSoftware,
+                                      busName, true, "running"));
+    ctx.run();
+}
+
+TEST_F(SoftwareAssocTest, TestSoftwareAssociationActivating)
+{
+    // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.Branch)
+    ctx.spawn(testSoftwareAssociation(ctx, device, objPathCurrentSoftware,
+                                      busName, false, "activating"));
+    ctx.run();
+}