Code refactoring and bug fixes for config file handler. Added new test cases for config file handler
Some checks failed
EIVE/eive-obsw/pipeline/head There was a failure building this commit

This commit is contained in:
petriVM18 2022-07-05 14:56:31 +02:00
parent d9060734b0
commit 5e4d07bd25
4 changed files with 38 additions and 16 deletions

View File

@ -15,7 +15,7 @@ static constexpr int PARAM1_DEFAULT = 905;
enum ParamIds : uint8_t {
PARAM0 = 0,
PARAM1 = 1,
PARAM2 = 2,
};

View File

@ -86,11 +86,23 @@ template <typename T> ReturnValue_t GlobalConfigHandler::setConfigFileValue(Para
return result;
}
std:: string paramString;
paramString=PARAM_KEY_MAP[paramID];
//Check if key exists in map before setting value. No check is done in setValue! Somehow PARAM_KEY_MAP.count(paramID) == 0 does not work
if (paramString.empty() == true) {
#if OBSW_VERBOSE_LEVEL >= 1
sif::info << "GlobalConfigHandler::setConfigFileValue ParamId " << PARAM_KEY_MAP[paramID]<<" not found!" << std::endl;
#endif
triggerEvent(SET_CONFIGFILEVALUE_FAILED, 1, 0);
return RETURN_FAILED;
}
resultSet=setValue(PARAM_KEY_MAP[paramID], data);
if(resultSet!=RETURN_OK){
triggerEvent(SET_CONFIGFILEVALUE_FAILED, 0, 0);
#if OBSW_VERBOSE_LEVEL >= 1
sif::info << "GlobalConfigHandler::setConfigFileValue set json failed with " << result << std::endl;
sif::info << "GlobalConfigHandler::setConfigFileValue set json failed with " << resultSet << std::endl;
#endif
}
@ -121,7 +133,7 @@ template <typename T> ReturnValue_t GlobalConfigHandler::getConfigFileValue(Para
if (resultGet!= RETURN_OK){
triggerEvent(GET_CONFIGFILEVALUE_FAILED, 0, 0);
#if OBSW_VERBOSE_LEVEL >= 1
sif::info << "GlobalConfigHandler::getConfigFileValue lock mutex failed with " << result << std::endl;
sif::info << "GlobalConfigHandler::getConfigFileValue getValue failed with " << resultGet << std::endl;
#endif
}
result=unlockConfigFile();
@ -132,7 +144,7 @@ template <typename T> ReturnValue_t GlobalConfigHandler::getConfigFileValue(Para
return result;
}
return result;
return resultGet;
}
ReturnValue_t GlobalConfigHandler::resetConfigFileValues(){
@ -146,6 +158,8 @@ ReturnValue_t GlobalConfigHandler::resetConfigFileValues(){
}
insertValue(PARAM_KEY_MAP[PARAM0], PARAM0_DEFAULT);
insertValue(PARAM_KEY_MAP[PARAM1], PARAM1_DEFAULT);
result=unlockConfigFile();
if (result!= RETURN_OK){
#if OBSW_VERBOSE_LEVEL >= 1
@ -170,7 +184,7 @@ ReturnValue_t GlobalConfigHandler::WriteConfigFile(){
if(resultWrite!=RETURN_OK){
triggerEvent(WRITE_CONFIGFILE_FAILED, 0, 0);
#if OBSW_VERBOSE_LEVEL >= 1
sif::info << "GlobalConfigHandler::WriteConfigFile write json failed with " << result << std::endl;
sif::info << "GlobalConfigHandler::WriteConfigFile write json failed with " << resultWrite << std::endl;
#endif
}
@ -199,7 +213,7 @@ ReturnValue_t GlobalConfigHandler::ReadConfigFile(){
if(resultRead!=RETURN_OK){
triggerEvent(READ_CONFIGFILE_FAILED, 0, 0);
#if OBSW_VERBOSE_LEVEL >= 1
sif::info << "GlobalConfigHandler::ReadConfigFile read json failed with " << result << std::endl;
sif::info << "GlobalConfigHandler::ReadConfigFile read json failed with " << resultRead << std::endl;
#endif
}
@ -215,7 +229,13 @@ ReturnValue_t GlobalConfigHandler::ReadConfigFile(){
}
ReturnValue_t GlobalConfigHandler::ResetConfigFile(){
ReturnValue_t result = RETURN_OK;
resetConfigFileValues();
result=resetConfigFileValues();
if (result!= RETURN_OK){
#if OBSW_VERBOSE_LEVEL >= 1
sif::info << "GlobalConfigHandler::ResetConfigFile failed with " << result << std::endl;
#endif
return result;
}
result =writeJsonFile();
return result;
}

View File

@ -55,7 +55,8 @@ public:
template <typename T> ReturnValue_t setConfigFileValue(ParamIds paramID, T data);
template <typename T> ReturnValue_t getConfigFileValue(ParamIds paramID, T& data);
ReturnValue_t resetConfigFileValues();
ReturnValue_t ResetConfigFile();
ReturnValue_t WriteConfigFile();
std::string getConfigFileName();
@ -64,14 +65,11 @@ private:
ReturnValue_t lockConfigFile();
ReturnValue_t unlockConfigFile();
ReturnValue_t ReadConfigFile();
ReturnValue_t ResetConfigFile();
ReturnValue_t resetConfigFileValues();
ReturnValue_t setConfigFileName(std::string configFileName);
ReturnValue_t ReadConfigFile();
MessageQueueIF* commandQueue;

View File

@ -12,13 +12,13 @@
TEST_CASE("Configfile Handler", "[ConfigHandler]") {
sif::debug<<"Testcase config file handler"<<std::endl;
testEnvironment::initialize();
//Init handler
GlobalConfigHandler confighandler= GlobalConfigHandler(objects::CONFIG_TEST,"JSON.config");
REQUIRE(confighandler.initialize() == HasReturnvaluesIF::RETURN_OK);
//Reset handler
REQUIRE(confighandler.resetConfigFileValues() == HasReturnvaluesIF::RETURN_OK);
REQUIRE(confighandler.ResetConfigFile() == HasReturnvaluesIF::RETURN_OK);
//Get and set double as well as int values
double doubleData=0.0;
@ -52,10 +52,14 @@ TEST_CASE("Configfile Handler", "[ConfigHandler]") {
REQUIRE(confighandler.getConfigFileName()=="JSON.config");
//Reset and check if it worked
REQUIRE(confighandler.resetConfigFileValues() == HasReturnvaluesIF::RETURN_OK);
REQUIRE(confighandler.ResetConfigFile() == HasReturnvaluesIF::RETURN_OK);
doubleData=0.0;
REQUIRE(confighandler.getConfigFileValue(PARAM0, doubleData) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(doubleData==5.0);
//Test invalid Parameter
REQUIRE(confighandler.getConfigFileValue(PARAM2, doubleData) != HasReturnvaluesIF::RETURN_OK);//NVMParameterBase::KEY_NOT_EXISTS is private, why?
REQUIRE(confighandler.setConfigFileValue(PARAM2, doubleData) != HasReturnvaluesIF::RETURN_OK);
}