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/exampledevice/example_device.cpp b/test/common/exampledevice/example_device.cpp
index 86606fb..9200bb2 100644
--- a/test/common/exampledevice/example_device.cpp
+++ b/test/common/exampledevice/example_device.cpp
@@ -41,13 +41,45 @@
     SoftwareManager(ctx, "ExampleUpdater" + std::to_string(uniqueSuffix))
 {}
 
+ExampleCodeUpdater::ExampleCodeUpdater(sdbusplus::async::context& ctx,
+                                       const char* swVersion) :
+    ExampleCodeUpdater(ctx)
+{
+    const std::string exampleInvObjPath =
+        "/xyz/openbmc_project/inventory/system/board/ExampleBoard/ExampleDevice";
+    auto exampleDevice = std::make_unique<ExampleDevice>(ctx, &(*this));
+
+    devices.insert({exampleInvObjPath, std::move(exampleDevice)});
+
+    if (swVersion)
+    {
+        auto& device = getDevice();
+        device->softwareCurrent =
+            std::make_unique<ExampleSoftware>(ctx, *device);
+        device->softwareCurrent->setVersion(swVersion);
+    }
+}
+
+std::unique_ptr<ExampleDevice>& ExampleCodeUpdater::getDevice()
+{
+    if (devices.empty())
+    {
+        throw std::invalid_argument(
+            "could not find any device, example CU wrongly initialized");
+    }
+
+    auto& deviceRef = devices.begin()->second;
+
+    return reinterpret_cast<std::unique_ptr<ExampleDevice>&>(deviceRef);
+}
+
 sdbusplus::async::task<bool> ExampleCodeUpdater::initDevice(
     const std::string& /*unused*/, const std::string& /*unused*/,
     SoftwareConfig& /*unused*/)
 {
     auto device = std::make_unique<ExampleDevice>(ctx, this);
 
-    device->softwareCurrent = std::make_unique<Software>(ctx, *device);
+    device->softwareCurrent = std::make_unique<ExampleSoftware>(ctx, *device);
 
     device->softwareCurrent->setVersion("v1.0",
                                         SoftwareVersion::VersionPurpose::Other);
@@ -91,3 +123,7 @@
 
     co_return true;
 }
+
+ExampleSoftware::ExampleSoftware(sdbusplus::async::context& ctx,
+                                 ExampleDevice& parent) : Software(ctx, parent)
+{}
diff --git a/test/common/exampledevice/example_device.hpp b/test/common/exampledevice/example_device.hpp
index 247fb07..52da92d 100644
--- a/test/common/exampledevice/example_device.hpp
+++ b/test/common/exampledevice/example_device.hpp
@@ -12,16 +12,26 @@
 namespace phosphor::software::example_device
 {
 
+class ExampleDevice;
+
 class ExampleCodeUpdater : public phosphor::software::manager::SoftwareManager
 {
   public:
     ExampleCodeUpdater(sdbusplus::async::context& ctx,
                        long uniqueSuffix = getRandomId());
 
+    // @param swVersion     if this is nullptr, do not create the software
+    // version.
+    ExampleCodeUpdater(sdbusplus::async::context& ctx, const char* swVersion);
+
+    std::unique_ptr<ExampleDevice>& getDevice();
+
     sdbusplus::async::task<bool> initDevice(const std::string& service,
                                             const std::string& path,
                                             SoftwareConfig& config) final;
 
+    using SoftwareManager::getBusName;
+
   private:
     static long getRandomId();
 };
@@ -34,11 +44,19 @@
 const std::string exampleInvObjPath =
     "/xyz/openbmc_project/inventory/system/board/ExampleBoard/ExampleDevice";
 
+class ExampleSoftware : public Software
+{
+  public:
+    using Software::createInventoryAssociation;
+    using Software::objectPath;
+    ExampleSoftware(sdbusplus::async::context& ctx, ExampleDevice& parent);
+};
+
 class ExampleDevice : public Device
 {
   public:
+    using Device::softwareCurrent;
     using Device::softwarePending;
-    using phosphor::software::device::Device::softwareCurrent;
 
     static SoftwareConfig defaultConfig;