libpldmresponder: create PDR when the D-Bus object path exists

On a system where this D-Bus object path does not exist, it should
not create a PDR when the D-Bus object specified here is not found.

Tested: In the PDR config files, the PDR is successfully created when
the objectPath of the D-Bus exists, otherwise, skipped the current
config and the PDR is not created.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: Ib59fa79cc28d5712161bfb971c37c86c363bafb0
diff --git a/test/libpldmresponder_platform_test.cpp b/test/libpldmresponder_platform_test.cpp
index 60df7bf..22ededa 100644
--- a/test/libpldmresponder_platform_test.cpp
+++ b/test/libpldmresponder_platform_test.cpp
@@ -7,6 +7,8 @@
 #include "libpldmresponder/platform_state_effecter.hpp"
 #include "mocked_utils.hpp"
 
+#include <sdbusplus/test/sdbus_mock.hpp>
+
 #include <iostream>
 
 using namespace pldm::utils;
@@ -15,6 +17,10 @@
 using namespace pldm::responder::pdr;
 using namespace pldm::responder::pdr_utils;
 
+using ::testing::_;
+using ::testing::Return;
+using ::testing::StrEq;
+
 TEST(getPDR, testGoodPath)
 {
     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
@@ -26,9 +32,14 @@
         reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
     request->request_count = 100;
 
+    MockdBusHandler mockedUtils;
+    EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
+        .Times(5)
+        .WillRepeatedly(Return("foo.bar"));
+
     auto pdrRepo = pldm_pdr_init();
-    Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
-                    pdrRepo, nullptr, nullptr);
+    Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
+                    "./event_jsons/good", pdrRepo, nullptr, nullptr);
     Repo repo(pdrRepo);
     ASSERT_EQ(repo.empty(), false);
     auto response = handler.getPDR(req, requestPayloadLength);
@@ -58,9 +69,14 @@
         reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
     request->request_count = 1;
 
+    MockdBusHandler mockedUtils;
+    EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
+        .Times(5)
+        .WillRepeatedly(Return("foo.bar"));
+
     auto pdrRepo = pldm_pdr_init();
-    Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
-                    pdrRepo, nullptr, nullptr);
+    Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
+                    "./event_jsons/good", pdrRepo, nullptr, nullptr);
     Repo repo(pdrRepo);
     ASSERT_EQ(repo.empty(), false);
     auto response = handler.getPDR(req, requestPayloadLength);
@@ -84,9 +100,14 @@
     request->record_handle = 100000;
     request->request_count = 1;
 
+    MockdBusHandler mockedUtils;
+    EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
+        .Times(5)
+        .WillRepeatedly(Return("foo.bar"));
+
     auto pdrRepo = pldm_pdr_init();
-    Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
-                    pdrRepo, nullptr, nullptr);
+    Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
+                    "./event_jsons/good", pdrRepo, nullptr, nullptr);
     Repo repo(pdrRepo);
     ASSERT_EQ(repo.empty(), false);
     auto response = handler.getPDR(req, requestPayloadLength);
@@ -108,9 +129,14 @@
         reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
     request->record_handle = 1;
 
+    MockdBusHandler mockedUtils;
+    EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
+        .Times(5)
+        .WillRepeatedly(Return("foo.bar"));
+
     auto pdrRepo = pldm_pdr_init();
-    Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
-                    pdrRepo, nullptr, nullptr);
+    Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
+                    "./event_jsons/good", pdrRepo, nullptr, nullptr);
     Repo repo(pdrRepo);
     ASSERT_EQ(repo.empty(), false);
     auto response = handler.getPDR(req, requestPayloadLength);
@@ -134,9 +160,14 @@
         reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
     request->request_count = 100;
 
+    MockdBusHandler mockedUtils;
+    EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
+        .Times(5)
+        .WillRepeatedly(Return("foo.bar"));
+
     auto pdrRepo = pldm_pdr_init();
-    Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
-                    pdrRepo, nullptr, nullptr);
+    Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
+                    "./event_jsons/good", pdrRepo, nullptr, nullptr);
     Repo repo(pdrRepo);
     ASSERT_EQ(repo.empty(), false);
     auto response = handler.getPDR(req, requestPayloadLength);
@@ -184,11 +215,16 @@
 
 TEST(setStateEffecterStatesHandler, testGoodRequest)
 {
+    MockdBusHandler mockedUtils;
+    EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
+        .Times(5)
+        .WillRepeatedly(Return("foo.bar"));
+
     auto inPDRRepo = pldm_pdr_init();
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
-    Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
-                    inPDRRepo, nullptr, nullptr);
+    Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
+                    "./event_jsons/good", inPDRRepo, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
     pdr_utils::PdrEntry e;
@@ -204,14 +240,13 @@
     std::string value = "xyz.openbmc_project.Foo.Bar.V1";
     PropertyValue propertyValue = value;
 
-    MockdBusHandler handlerObj;
     DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
                             "propertyName", "string"};
 
-    EXPECT_CALL(handlerObj, setDbusProperty(dbusMapping, propertyValue))
+    EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
         .Times(2);
     auto rc = platform_state_effecter::setStateEffecterStatesHandler<
-        MockdBusHandler, Handler>(handlerObj, handler, 0x1, stateField);
+        MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
     ASSERT_EQ(rc, 0);
 
     pldm_pdr_destroy(inPDRRepo);
@@ -220,11 +255,16 @@
 
 TEST(setStateEffecterStatesHandler, testBadRequest)
 {
+    MockdBusHandler mockedUtils;
+    EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
+        .Times(5)
+        .WillRepeatedly(Return("foo.bar"));
+
     auto inPDRRepo = pldm_pdr_init();
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
-    Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
-                    inPDRRepo, nullptr, nullptr);
+    Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
+                    "./event_jsons/good", inPDRRepo, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
     pdr_utils::PdrEntry e;
@@ -238,20 +278,19 @@
     stateField.push_back({PLDM_REQUEST_SET, 3});
     stateField.push_back({PLDM_REQUEST_SET, 4});
 
-    MockdBusHandler handlerObj;
     auto rc = platform_state_effecter::setStateEffecterStatesHandler<
-        MockdBusHandler, Handler>(handlerObj, handler, 0x1, stateField);
+        MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
     ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
 
     rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
                                                                 Handler>(
-        handlerObj, handler, 0x9, stateField);
+        mockedUtils, handler, 0x9, stateField);
     ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID);
 
     stateField.push_back({PLDM_REQUEST_SET, 4});
     rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
                                                                 Handler>(
-        handlerObj, handler, 0x1, stateField);
+        mockedUtils, handler, 0x1, stateField);
     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
 
     pldm_pdr_destroy(inPDRRepo);
@@ -260,11 +299,16 @@
 
 TEST(setNumericEffecterValueHandler, testGoodRequest)
 {
+    MockdBusHandler mockedUtils;
+    EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
+        .Times(5)
+        .WillRepeatedly(Return("foo.bar"));
+
     auto inPDRRepo = pldm_pdr_init();
     auto numericEffecterPdrRepo = pldm_pdr_init();
     Repo numericEffecterPDRs(numericEffecterPdrRepo);
-    Handler handler("./pdr_jsons/state_effecter/good", "", inPDRRepo, nullptr,
-                    nullptr);
+    Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good", "",
+                    inPDRRepo, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
 
@@ -280,15 +324,14 @@
     uint32_t effecterValue = 2100000000; // 2036-07-18 21:20:00
     PropertyValue propertyValue = static_cast<uint32_t>(effecterValue);
 
-    MockdBusHandler handlerObj;
     DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
                             "propertyName", "uint64_t"};
-    EXPECT_CALL(handlerObj, setDbusProperty(dbusMapping, propertyValue))
+    EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
         .Times(1);
 
     auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
         MockdBusHandler, Handler>(
-        handlerObj, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
+        mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
         reinterpret_cast<uint8_t*>(&effecterValue), 4);
     ASSERT_EQ(rc, 0);
 
@@ -298,11 +341,16 @@
 
 TEST(setNumericEffecterValueHandler, testBadRequest)
 {
+    MockdBusHandler mockedUtils;
+    EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
+        .Times(5)
+        .WillRepeatedly(Return("foo.bar"));
+
     auto inPDRRepo = pldm_pdr_init();
     auto numericEffecterPdrRepo = pldm_pdr_init();
     Repo numericEffecterPDRs(numericEffecterPdrRepo);
-    Handler handler("./pdr_jsons/state_effecter/good", "", inPDRRepo, nullptr,
-                    nullptr);
+    Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good", "",
+                    inPDRRepo, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
 
@@ -316,10 +364,9 @@
 
     uint16_t effecterId = 3;
     uint64_t effecterValue = 9876543210;
-    MockdBusHandler handlerObj;
     auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
         MockdBusHandler, Handler>(
-        handlerObj, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_SINT32,
+        mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_SINT32,
         reinterpret_cast<uint8_t*>(&effecterValue), 3);
     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);