requester: Modified MctpDiscovery class

Modified MctpDiscovery class to take list of managers instead of single
fwManager. The change is for adding platform-mc manager.
Added loadStaticEndpoints API for MCTP layer which doesn't implement
/xyz/openbmc_project/MCTP/Endpoint.Interface

The patch is part of implementation of design document below.
https://gerrit.openbmc-project.xyz/c/openbmc/docs/+/47252

Signed-off-by: Gilbert Chen <gilbert.chen@arm.com>
Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
Change-Id: I1e1673504583a87f2a9bc3adf76fb49c2dc30254
diff --git a/requester/test/mctp_endpoint_discovery_test.cpp b/requester/test/mctp_endpoint_discovery_test.cpp
new file mode 100644
index 0000000..df390a0
--- /dev/null
+++ b/requester/test/mctp_endpoint_discovery_test.cpp
@@ -0,0 +1,137 @@
+#include "config.h"
+
+#include "common/utils.hpp"
+#include "requester/test/mock_mctp_discovery_handler_intf.hpp"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using ::testing::_;
+
+TEST(MctpEndpointDiscoveryTest, SingleHandleMctpEndpoint)
+{
+    auto& bus = pldm::utils::DBusHandler::getBus();
+    pldm::MockManager manager;
+
+    EXPECT_CALL(manager, handleMctpEndpoints(_)).Times(1);
+
+    auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>(
+        bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager});
+    mctpDiscoveryHandler = nullptr;
+}
+
+TEST(MctpEndpointDiscoveryTest, MultipleHandleMctpEndpoints)
+{
+    auto& bus = pldm::utils::DBusHandler::getBus();
+    pldm::MockManager manager1;
+    pldm::MockManager manager2;
+
+    EXPECT_CALL(manager1, handleMctpEndpoints(_)).Times(1);
+    EXPECT_CALL(manager2, handleMctpEndpoints(_)).Times(1);
+
+    auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>(
+        bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager1,
+                                                                    &manager2});
+    mctpDiscoveryHandler = nullptr;
+}
+
+TEST(MctpEndpointDiscoveryTest, goodGetMctpInfos)
+{
+    auto& bus = pldm::utils::DBusHandler::getBus();
+    pldm::MockManager manager;
+    pldm::MctpInfos mctpInfos;
+
+    auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>(
+        bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager});
+    mctpDiscoveryHandler->getMctpInfos(mctpInfos);
+    EXPECT_EQ(mctpInfos.size(), 0);
+}
+
+TEST(MctpEndpointDiscoveryTest, goodAddToExistingMctpInfos)
+{
+    auto& bus = pldm::utils::DBusHandler::getBus();
+    pldm::MockManager manager;
+    const pldm::MctpInfos& mctpInfos = {
+        pldm::MctpInfo(11, pldm::emptyUUID, "", 1),
+        pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1)};
+
+    auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>(
+        bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager});
+    mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos);
+    EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 2);
+    pldm::MctpInfo mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back();
+    EXPECT_EQ(std::get<0>(mctpInfo), 12);
+    EXPECT_EQ(std::get<2>(mctpInfo), "abc");
+    EXPECT_EQ(std::get<3>(mctpInfo), 1);
+}
+
+TEST(MctpEndpointDiscoveryTest, badAddToExistingMctpInfos)
+{
+    auto& bus = pldm::utils::DBusHandler::getBus();
+    pldm::MockManager manager;
+    const pldm::MctpInfos& mctpInfos = {
+        pldm::MctpInfo(11, pldm::emptyUUID, "", 1)};
+
+    auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>(
+        bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager});
+    mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos);
+    EXPECT_NE(mctpDiscoveryHandler->existingMctpInfos.size(), 2);
+}
+
+TEST(MctpEndpointDiscoveryTest, goodRemoveFromExistingMctpInfos)
+{
+    auto& bus = pldm::utils::DBusHandler::getBus();
+    pldm::MockManager manager;
+    const pldm::MctpInfos& mctpInfos = {
+        pldm::MctpInfo(11, pldm::emptyUUID, "def", 2),
+        pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1)};
+
+    auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>(
+        bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager});
+    mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos);
+    EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 2);
+    pldm::MctpInfo mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back();
+    EXPECT_EQ(std::get<0>(mctpInfo), 12);
+    EXPECT_EQ(std::get<2>(mctpInfo), "abc");
+    EXPECT_EQ(std::get<3>(mctpInfo), 1);
+    pldm::MctpInfos removedInfos;
+    pldm::MctpInfos remainMctpInfos;
+    remainMctpInfos.emplace_back(pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1));
+
+    mctpDiscoveryHandler->removeFromExistingMctpInfos(remainMctpInfos,
+                                                      removedInfos);
+    EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 1);
+    mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back();
+    EXPECT_EQ(std::get<0>(mctpInfo), 12);
+    EXPECT_EQ(std::get<2>(mctpInfo), "abc");
+    EXPECT_EQ(std::get<3>(mctpInfo), 1);
+    EXPECT_EQ(removedInfos.size(), 1);
+    mctpInfo = removedInfos.back();
+    EXPECT_EQ(std::get<0>(mctpInfo), 11);
+    EXPECT_EQ(std::get<2>(mctpInfo), "def");
+    EXPECT_EQ(std::get<3>(mctpInfo), 2);
+}
+
+TEST(MctpEndpointDiscoveryTest, goodRemoveEndpoints)
+{
+    auto& bus = pldm::utils::DBusHandler::getBus();
+    pldm::MockManager manager;
+    const pldm::MctpInfos& mctpInfos = {
+        pldm::MctpInfo(11, pldm::emptyUUID, "def", 2),
+        pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1)};
+
+    auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>(
+        bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager});
+    mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos);
+    EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 2);
+    pldm::MctpInfo mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back();
+    EXPECT_EQ(std::get<0>(mctpInfo), 12);
+    EXPECT_EQ(std::get<2>(mctpInfo), "abc");
+    EXPECT_EQ(std::get<3>(mctpInfo), 1);
+    sdbusplus::message_t msg = sdbusplus::bus::new_default().new_method_call(
+        "xyz.openbmc_project.sdbusplus.test.Object",
+        "/xyz/openbmc_project/sdbusplus/test/object",
+        "xyz.openbmc_project.sdbusplus.test.Object", "Unused");
+    mctpDiscoveryHandler->removeEndpoints(msg);
+    EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 0);
+}
diff --git a/requester/test/meson.build b/requester/test/meson.build
index 29f4303..a08a990 100644
--- a/requester/test/meson.build
+++ b/requester/test/meson.build
@@ -1,6 +1,13 @@
+test_src = declare_dependency(
+          sources: [
+            '../mctp_endpoint_discovery.cpp',
+            '../../common/utils.cpp',
+          ])
+
 tests = [
   'handler_test',
   'request_test',
+  'mctp_endpoint_discovery_test',
 ]
 
 foreach t : tests
@@ -16,6 +23,7 @@
                          phosphor_logging_dep,
                          sdbusplus,
                          sdeventplus,
+                         test_src,
                     ]),
        workdir: meson.current_source_dir())
 endforeach
diff --git a/requester/test/mock_mctp_discovery_handler_intf.hpp b/requester/test/mock_mctp_discovery_handler_intf.hpp
new file mode 100644
index 0000000..8aada96
--- /dev/null
+++ b/requester/test/mock_mctp_discovery_handler_intf.hpp
@@ -0,0 +1,20 @@
+#pragma once
+
+#include "requester/mctp_endpoint_discovery.hpp"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+namespace pldm
+{
+
+class MockManager : public pldm::MctpDiscoveryHandlerIntf
+{
+  public:
+    MOCK_METHOD(void, handleMctpEndpoints, (const MctpInfos& mctpInfos),
+                (override));
+    MOCK_METHOD(void, handleRemovedMctpEndpoints, (const MctpInfos& mctpInfos),
+                (override));
+};
+
+} // namespace pldm