unit-test: Move removeAssociationEndpoints()

Make it easier to unit test and continue reduction of main.cpp

Change-Id: I2c0eac5c301687acab14add627586170020e4fe4
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
diff --git a/src/associations.cpp b/src/associations.cpp
index 3ae7db4..9bb7a34 100644
--- a/src/associations.cpp
+++ b/src/associations.cpp
@@ -74,3 +74,40 @@
         assocOwners.erase(owners);
     }
 }
+
+void removeAssociationEndpoints(
+    sdbusplus::asio::object_server& objectServer, const std::string& assocPath,
+    const boost::container::flat_set<std::string>& endpointsToRemove,
+    AssociationInterfaces& assocInterfaces)
+{
+    auto assoc = assocInterfaces.find(assocPath);
+    if (assoc == assocInterfaces.end())
+    {
+        return;
+    }
+
+    auto& endpointsInDBus = std::get<endpointsPos>(assoc->second);
+
+    for (const auto& endpointToRemove : endpointsToRemove)
+    {
+        auto e = std::find(endpointsInDBus.begin(), endpointsInDBus.end(),
+                           endpointToRemove);
+
+        if (e != endpointsInDBus.end())
+        {
+            endpointsInDBus.erase(e);
+        }
+    }
+
+    if (endpointsInDBus.empty())
+    {
+        objectServer.remove_interface(std::get<ifacePos>(assoc->second));
+        std::get<ifacePos>(assoc->second) = nullptr;
+        std::get<endpointsPos>(assoc->second).clear();
+    }
+    else
+    {
+        std::get<ifacePos>(assoc->second)
+            ->set_property("endpoints", endpointsInDBus);
+    }
+}
diff --git a/src/associations.hpp b/src/associations.hpp
index 8ebef43..d926006 100644
--- a/src/associations.hpp
+++ b/src/associations.hpp
@@ -58,3 +58,21 @@
                        sdbusplus::asio::object_server& server,
                        AssociationOwnersType& assocOwners,
                        AssociationInterfaces& assocInterfaces);
+
+/** @brief Remove input paths from endpoints of an association
+ *
+ * If the last endpoint was removed, then remove the whole
+ * association object, otherwise just set the property
+ *
+ * @param[in] objectServer        - sdbus system object
+ * @param[in] assocPath           - Path of the object that contains the
+ *                                  org.openbmc.Associations
+ * @param[in] endpointsToRemove   - Endpoints to remove
+ * @param[in,out] assocInterfaces - Associations endpoints
+ *
+ * @return Void, objectServer and assocInterfaces updated if needed
+ */
+void removeAssociationEndpoints(
+    sdbusplus::asio::object_server& objectServer, const std::string& assocPath,
+    const boost::container::flat_set<std::string>& endpointsToRemove,
+    AssociationInterfaces& assocInterfaces);
diff --git a/src/main.cpp b/src/main.cpp
index e5d566e..44fe063 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -130,46 +130,6 @@
 #endif
 };
 
-// Remove paths from the endpoints property of an association.
-// If the last endpoint was removed, then remove the whole
-// association object, otherwise just set the property.
-void removeAssociationEndpoints(
-    sdbusplus::asio::object_server& objectServer, const std::string& assocPath,
-    const std::string& owner,
-    const boost::container::flat_set<std::string>& endpointsToRemove)
-{
-    auto assoc = associationInterfaces.find(assocPath);
-    if (assoc == associationInterfaces.end())
-    {
-        return;
-    }
-
-    auto& endpointsInDBus = std::get<endpointsPos>(assoc->second);
-
-    for (const auto& endpointToRemove : endpointsToRemove)
-    {
-        auto e = std::find(endpointsInDBus.begin(), endpointsInDBus.end(),
-                           endpointToRemove);
-
-        if (e != endpointsInDBus.end())
-        {
-            endpointsInDBus.erase(e);
-        }
-    }
-
-    if (endpointsInDBus.empty())
-    {
-        objectServer.remove_interface(std::get<ifacePos>(assoc->second));
-        std::get<ifacePos>(assoc->second) = nullptr;
-        std::get<endpointsPos>(assoc->second).clear();
-    }
-    else
-    {
-        std::get<ifacePos>(assoc->second)
-            ->set_property("endpoints", endpointsInDBus);
-    }
-}
-
 // Based on the latest values of the org.openbmc.Associations.associations
 // property, passed in via the newAssociations param, check if any of the
 // paths in the xyz.openbmc_project.Association.endpoints D-Bus property
@@ -207,8 +167,9 @@
         auto newEndpoints = newAssociations.find(originalAssocPath);
         if (newEndpoints == newAssociations.end())
         {
-            removeAssociationEndpoints(objectServer, originalAssocPath, owner,
-                                       originalEndpoints);
+            removeAssociationEndpoints(objectServer, originalAssocPath,
+                                       originalEndpoints,
+                                       associationInterfaces);
         }
         else
         {
@@ -228,7 +189,7 @@
             if (!toRemove.empty())
             {
                 removeAssociationEndpoints(objectServer, originalAssocPath,
-                                           owner, toRemove);
+                                           toRemove, associationInterfaces);
             }
         }
     }