Change the logic of generating the D-Bus object identifier

Presently D-Bus object identifier is generated with the hash of
D-Bus properties, as a result of this if the value of D-Bus property
gets change the D-Bus object identifier will get change.

This commit fixes this problem by keeping the last identifier
value in the manager object so whenever user creates new snmp
manager object it is incremented by 1, hence the object path
becomes unique.

This commit also takes care of that if at any point of time
if snmp manager app restart it gets the same D-Bus identifier
as it was before the restart.

Change-Id: I456c11f7824ff678ae470bc6641f0e0cc7c1f6bc
Signed-off-by: Ratan Gupta <ratagupt@linux.vnet.ibm.com>
diff --git a/test/test_snmp_conf_manager.cpp b/test/test_snmp_conf_manager.cpp
index a681f21..f6111f7 100644
--- a/test/test_snmp_conf_manager.cpp
+++ b/test/test_snmp_conf_manager.cpp
@@ -12,13 +12,16 @@
 namespace snmp
 {
 
+auto managerObjPath = "/xyz/openbmc_test/snmp/manager";
+
 class TestSNMPConfManager : public testing::Test
 {
   public:
     sdbusplus::bus::bus bus;
     ConfManager manager;
     std::string confDir;
-    TestSNMPConfManager() : bus(sdbusplus::bus::new_default()), manager(bus, "")
+    TestSNMPConfManager() :
+        bus(sdbusplus::bus::new_default()), manager(bus, managerObjPath)
     {
         char tmp[] = "/tmp/snmpManager.XXXXXX";
         std::string confDir = mkdtemp(tmp);
@@ -30,9 +33,9 @@
         fs::remove_all(manager.dbusPersistentLocation);
     }
 
-    void createSNMPClient(std::string ipaddress, uint16_t port)
+    std::string createSNMPClient(std::string ipaddress, uint16_t port)
     {
-        manager.client(ipaddress, port);
+        return manager.client(ipaddress, port);
     }
 
     ClientList &getSNMPClients()
@@ -40,52 +43,86 @@
         return manager.clients;
     }
 
+    bool isClientExist(const std::string &ipaddress)
+    {
+        for (const auto &val : manager.clients)
+        {
+            if (val.second.get()->address() == ipaddress)
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
     void deleteSNMPClient(std::string ipaddress)
     {
-        auto &it = manager.clients[ipaddress];
-        it->delete_();
+        for (const auto &val : manager.clients)
+        {
+            if (val.second.get()->address() == ipaddress)
+            {
+                val.second.get()->delete_();
+            }
+        }
     }
 };
 
 // Add single SNMP client
 TEST_F(TestSNMPConfManager, AddSNMPClient)
 {
-    using namespace sdbusplus::xyz::openbmc_project::Common::Error;
+    // check the created object path
+    auto path = createSNMPClient("192.168.1.1", 24);
+    std::string expectedPath = managerObjPath;
+    expectedPath += std::string("/1");
 
-    createSNMPClient("192.168.1.1", 24);
+    EXPECT_EQ(path, expectedPath);
 
+    // check whether the client created
     auto &clients = getSNMPClients();
     EXPECT_EQ(1, clients.size());
-    EXPECT_EQ(true, clients.find("192.168.1.1") != clients.end());
+    EXPECT_EQ(true, isClientExist("192.168.1.1"));
 }
 
 // Add multiple SNMP client
 TEST_F(TestSNMPConfManager, AddMultipleSNMPClient)
 {
-    using namespace sdbusplus::xyz::openbmc_project::Common::Error;
-
+    // add multiple clients and check whether the object path is generated
+    // correctly.
     createSNMPClient("192.168.1.1", 24);
-    createSNMPClient("192.168.1.2", 24);
+    auto path = createSNMPClient("192.168.1.2", 24);
+    std::string expectedPath = managerObjPath;
+    expectedPath += std::string("/2");
 
+    EXPECT_EQ(path, expectedPath);
+
+    // check both the clients get created
     auto &clients = getSNMPClients();
     EXPECT_EQ(2, clients.size());
-    EXPECT_EQ(true, clients.find("192.168.1.1") != clients.end());
-    EXPECT_EQ(true, clients.find("192.168.1.2") != clients.end());
+
+    EXPECT_EQ(true, isClientExist("192.168.1.1"));
+    EXPECT_EQ(true, isClientExist("192.168.1.2"));
 }
 
 // Delete SNMP client
 TEST_F(TestSNMPConfManager, DeleteSNMPClient)
 {
-    using namespace sdbusplus::xyz::openbmc_project::Common::Error;
-
     createSNMPClient("192.168.1.1", 24);
     createSNMPClient("192.168.1.2", 24);
 
     auto &clients = getSNMPClients();
     EXPECT_EQ(2, clients.size());
+
     deleteSNMPClient("192.168.1.1");
-    EXPECT_EQ(1, clients.size());
-    EXPECT_EQ(true, clients.find("192.168.1.2") != clients.end());
+
+    auto path = createSNMPClient("192.168.1.3", 24);
+    std::string expectedPath = managerObjPath;
+    expectedPath += std::string("/3");
+    EXPECT_EQ(path, expectedPath);
+
+    EXPECT_EQ(2, clients.size());
+    EXPECT_EQ(true, isClientExist("192.168.1.2"));
+    EXPECT_EQ(false, isClientExist("192.168.1.1"));
+    EXPECT_EQ(true, isClientExist("192.168.1.3"));
 }
 
 } // namespace snmp