Skip to content

Commit

Permalink
address code smell
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-du-car committed Apr 16, 2024
1 parent c8fcc43 commit 8e1afef
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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){};
};
}
16 changes: 8 additions & 8 deletions src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RSUConfiguration> tempConfigs;
Expand All @@ -42,7 +42,7 @@ namespace RSUHealthMonitor

if (rsuArray[i].isMember(SNMPPortKey))
{
auto port = atoi(rsuArray[i][SNMPPortKey].asCString());
auto port = static_cast<uint16_t>(atoi(rsuArray[i][SNMPPortKey].asCString()));
port != 0 ? config.snmpPort = port : throw RSUConfigurationException("Invalid port number in string format.");
}
else
Expand Down Expand Up @@ -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);
Expand All @@ -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<std::string> nameMibs = {"UNKOWN MIB",
"RSU 4.1",
"NTCIP 1218"};
return os << nameMibs[static_cast<int>(mib)];
}

std::ostream &operator<<(std::ostream &os, const RSUConfiguration &config)
Expand Down
8 changes: 4 additions & 4 deletions src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 8e1afef

Please sign in to comment.