From b982d26b9cd57751c2f7a58f76b0721a74288e75 Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Tue, 16 Apr 2024 18:41:56 +0000 Subject: [PATCH] add more test --- .../src/RSUConfigurationList.cpp | 13 +++++++++---- .../src/RSUHealthMonitorPlugin.cpp | 6 +++++- .../src/RSUHealthMonitorPlugin.h | 7 ------- .../test/test_RSUConfigurationList.cpp | 11 +++++++++++ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp index 2f4e9485d..f2bbccf02 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp @@ -22,6 +22,7 @@ namespace RSUHealthMonitor void RSUConfigurationList::parseRSUs(std::string rsuConfigsStr) { auto json = parseJson(rsuConfigsStr); + std::vector tempConfigs; RSUConfiguration config; auto rsuArray = json["RSUS"]; if (!rsuArray.isArray()) @@ -41,11 +42,12 @@ namespace RSUHealthMonitor if (rsuArray[i].isMember(SNMPPortKey)) { - config.snmpPort = atoi(rsuArray[i][SNMPPortKey].asCString()); + auto port = atoi(rsuArray[i][SNMPPortKey].asCString()); + port != 0 ? config.snmpPort = port : throw RSUConfigurationException("Invalid port number in string format."); } else { - throw RSUConfigurationException("SNMP port does not exist."); + throw RSUConfigurationException("Either SNMP port does not exist."); } if (rsuArray[i].isMember(AuthPassPhraseKey)) @@ -75,15 +77,18 @@ namespace RSUHealthMonitor { throw RSUConfigurationException("RSU mib version does not exist."); } - configs.push_back(config); + tempConfigs.push_back(config); } + // Only update RSU configurations when all configs are processed correctly. + configs.clear(); + configs.assign(tempConfigs.begin(), tempConfigs.end()); } RSUMibVersion RSUConfigurationList::strToMibVersion(std::string &mibVersionStr) { boost::trim_left(mibVersionStr); boost::trim_right(mibVersionStr); - // Support RSU MIB version 4.1 + // Only support RSU MIB version 4.1 if (boost::iequals(mibVersionStr, RSU4_1_str)) { return RSUMibVersion::RSUMIB_V_4_1; diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp index 1c0128fef..59f9b825c 100755 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp @@ -38,12 +38,16 @@ namespace RSUHealthMonitor lock_guard lock(_configMutex); GetConfigValue("Interval", _interval); GetConfigValue("RSUConfigurationList", _rsuConfigListStr); - _rsuConfigListPtr->parseRSUs(_rsuConfigListStr); try { + _rsuConfigListPtr->parseRSUs(_rsuConfigListStr); _rsuStatusTimer->ChangeFrequency(_timerThId, std::chrono::milliseconds(_interval * SEC_TO_MILLI)); } + catch (const RSUConfigurationException &ex) + { + PLOG(logERROR) << "Cannot update RSU configurations due to error: " << ex.what(); + } catch (const tmx::TmxException &ex) { PLOG(logERROR) << ex.what(); diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h index 1e5d21b91..e41ec8d8c 100755 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h @@ -18,13 +18,6 @@ namespace RSUHealthMonitor private: mutex _configMutex; uint16_t _interval; - // string _rsuIp; - // uint16_t _snmpPort; - // string _authPassPhrase; - // string _securityUser; - // string _securityLevel; - // string _rsuMIBVersionStr; - // RSUMibVersion _rsuMibVersion; string _rsuConfigListStr; shared_ptr _rsuConfigListPtr; shared_ptr _rsuWorker; diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp index f048babc3..c7d637e3f 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp @@ -15,6 +15,10 @@ namespace RSUHealthMonitor std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; rsuConfigList->parseRSUs(rsuConfigsStr); ASSERT_EQ(2, rsuConfigList->getConfigs().size()); + std::stringstream ss; + ss << rsuConfigList->getConfigs()[0]; + std::string expected = "RSUIp: 192.168.XX.XX, SNMPPort: 161, User: authOnlyUser, AuthPassPhrase: dummy, SecurityLevel: authPriv, RSUMIBVersion: RSU 4.1"; + ASSERT_EQ(expected, ss.str()); } TEST_F(test_RSUConfigurationList, parseAndGetConfigs_MalformatJSON) { @@ -40,6 +44,13 @@ namespace RSUHealthMonitor ASSERT_EQ(0, rsuConfigList->getConfigs().size()); } + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_INVALID_SNMPPORT) + { + auto rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.01.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"INVALID_PORT\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; + ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + } + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_Missing_AuthPassPhrase) { ASSERT_EQ(0, rsuConfigList->getConfigs().size());