Skip to content

Commit

Permalink
add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-du-car committed Apr 16, 2024
1 parent 97ca2da commit b982d26
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
13 changes: 9 additions & 4 deletions src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace RSUHealthMonitor
void RSUConfigurationList::parseRSUs(std::string rsuConfigsStr)
{
auto json = parseJson(rsuConfigsStr);
std::vector<RSUConfiguration> tempConfigs;
RSUConfiguration config;
auto rsuArray = json["RSUS"];
if (!rsuArray.isArray())
Expand All @@ -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))
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ namespace RSUHealthMonitor
lock_guard<mutex> lock(_configMutex);
GetConfigValue<uint16_t>("Interval", _interval);
GetConfigValue<string>("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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RSUConfigurationList> _rsuConfigListPtr;
shared_ptr<RSUHealthMonitorWorker> _rsuWorker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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());
Expand Down

0 comments on commit b982d26

Please sign in to comment.