Fix deleteSNMPClient method in Unit Test file
The previous method can only delete one piece of data, and if the
last piece of data is deleted, an error will be reported and the UT
will fail.
Tested: UT passed.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: If1e0b4170a0a58fb30d4e28c55fed68b0d0f5e28
diff --git a/snmp_client.hpp b/snmp_client.hpp
index 429c947..766bed5 100644
--- a/snmp_client.hpp
+++ b/snmp_client.hpp
@@ -82,9 +82,10 @@
*/
void delete_() override;
- private:
/** Client ID. */
Id id;
+
+ private:
/** @brief Parent D-Bus Object. */
ConfManager& parent;
};
diff --git a/test/test_snmp_conf_manager.cpp b/test/test_snmp_conf_manager.cpp
index c5d3277..911f1f1 100644
--- a/test/test_snmp_conf_manager.cpp
+++ b/test/test_snmp_conf_manager.cpp
@@ -61,13 +61,22 @@
return false;
}
- void deleteSNMPClient(std::string ipaddress)
+ void deleteSNMPClient(const std::string& ipaddress)
{
+ std::vector<size_t> ids{};
for (const auto& val : manager.clients)
{
if (val.second.get()->address() == ipaddress)
{
- val.second.get()->delete_();
+ ids.emplace_back(val.second.get()->id);
+ }
+ }
+
+ for (const auto& id : ids)
+ {
+ if (manager.clients.contains(id))
+ {
+ manager.clients.at(id)->delete_();
}
}
}
@@ -121,15 +130,17 @@
{
createSNMPClient("192.168.1.1", 24);
createSNMPClient("192.168.1.2", 24);
+ createSNMPClient("192.168.1.1", 25);
auto& clients = getSNMPClients();
- EXPECT_EQ(2U, clients.size());
+ EXPECT_EQ(3U, clients.size());
deleteSNMPClient("192.168.1.1");
+ EXPECT_EQ(1U, clients.size());
auto path = createSNMPClient("192.168.1.3", 24);
std::string expectedPath = managerObjPath;
- expectedPath += std::string("/3");
+ expectedPath += std::string("/4");
EXPECT_EQ(path, expectedPath);
EXPECT_EQ(2U, clients.size());