From 8e1afef999927f0721d38bc6bf15d188be2eb3ba Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Tue, 16 Apr 2024 19:34:02 +0000 Subject: [PATCH] address code smell --- .../src/RSUConfigurationException.h | 2 +- .../src/RSUConfigurationList.cpp | 16 ++++++++-------- .../src/RSUConfigurationList.h | 8 ++++---- .../src/RSUHealthMonitorPlugin.cpp | 2 +- .../test/test_RSUConfigurationList.cpp | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h index 3d4be81ae..81e02a16e 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h @@ -6,6 +6,6 @@ namespace RSUHealthMonitor class RSUConfigurationException : public std::runtime_error { public: - RSUConfigurationException(const char *msg) : runtime_error(msg){}; + explicit RSUConfigurationException(const char *msg) : runtime_error(msg){}; }; } \ No newline at end of file diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp index f2bbccf02..baf7f3cfb 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp @@ -3,7 +3,7 @@ namespace RSUHealthMonitor { - Json::Value RSUConfigurationList::parseJson(std::string rsuConfigsStr) + Json::Value RSUConfigurationList::parseJson(std::string &rsuConfigsStr) const { JSONCPP_STRING err; Json::Value root; @@ -19,7 +19,7 @@ namespace RSUHealthMonitor return root; } - void RSUConfigurationList::parseRSUs(std::string rsuConfigsStr) + void RSUConfigurationList::parseRSUs(std::string &rsuConfigsStr) { auto json = parseJson(rsuConfigsStr); std::vector tempConfigs; @@ -42,7 +42,7 @@ namespace RSUHealthMonitor if (rsuArray[i].isMember(SNMPPortKey)) { - auto port = atoi(rsuArray[i][SNMPPortKey].asCString()); + auto port = static_cast(atoi(rsuArray[i][SNMPPortKey].asCString())); port != 0 ? config.snmpPort = port : throw RSUConfigurationException("Invalid port number in string format."); } else @@ -84,7 +84,7 @@ namespace RSUHealthMonitor configs.assign(tempConfigs.begin(), tempConfigs.end()); } - RSUMibVersion RSUConfigurationList::strToMibVersion(std::string &mibVersionStr) + RSUMibVersion RSUConfigurationList::strToMibVersion(std::string &mibVersionStr) const { boost::trim_left(mibVersionStr); boost::trim_right(mibVersionStr); @@ -108,10 +108,10 @@ namespace RSUHealthMonitor std::ostream &operator<<(std::ostream &os, const RSUMibVersion &mib) { - const std::string nameMibs[] = {"UNKOWN MIB", - "RSU 4.1", - "NTCIP 1218"}; - return os << nameMibs[mib]; + const std::vector nameMibs = {"UNKOWN MIB", + "RSU 4.1", + "NTCIP 1218"}; + return os << nameMibs[static_cast(mib)]; } std::ostream &operator<<(std::ostream &os, const RSUConfiguration &config) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h index 2512430e8..14f9bded1 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h @@ -17,7 +17,7 @@ namespace RSUHealthMonitor static constexpr const char *RSU4_1_str = "RSU4.1"; static constexpr const char *RSU1218_str = "RSU1218"; - enum RSUMibVersion + enum class RSUMibVersion { UNKOWN_MIB_V = 0, RSUMIB_V_4_1 = 1, @@ -44,8 +44,8 @@ namespace RSUHealthMonitor * @param rsuConfigsStr A JSON string includes all RSUs related configrations. * @return JSON::Value A JSON object that includes RSUS information. */ - Json::Value parseJson(std::string rsuConfigsStr); - RSUMibVersion strToMibVersion(std::string &mibVersionStr); + Json::Value parseJson(std::string &rsuConfigsStr) const; + RSUMibVersion strToMibVersion(std::string &mibVersionStr) const; public: RSUConfigurationList() = default; @@ -54,7 +54,7 @@ namespace RSUHealthMonitor * @brief Parse RSUs configrations in JSON string representation, and update the memeber of list of RSUConfiguration struct. * @param rsuConfigsStr A JSON string includes all RSUs related configrations. */ - void parseRSUs(std::string rsuConfigsStr); + void parseRSUs(std::string &rsuConfigsStr); /** * @brief Get a list of RSUConfiguration struct. */ diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp index 59f9b825c..ea4a0ce09 100755 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp @@ -17,7 +17,7 @@ namespace RSUHealthMonitor _timerThId = _rsuStatusTimer->AddPeriodicTick([this]() { this->monitorRSUs(); - PLOG(logINFO) << "Updating RSU _interval: " << _interval; }, + PLOG(logINFO) << "Monitoring RSU at interval (second): " << _interval; }, std::chrono::milliseconds(_interval * SEC_TO_MILLI)); _rsuStatusTimer->Start(); } diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp index c7d637e3f..1ba516cdc 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp @@ -46,7 +46,7 @@ namespace RSUHealthMonitor 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\" }] }"; + std::string 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()); }