Skip to content

Commit

Permalink
Added unit test code for webcfg_generic.c
Browse files Browse the repository at this point in the history
  • Loading branch information
nlrcomcast committed Oct 9, 2023
1 parent b26c0f0 commit ff5e3dc
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ webcfg is the client implementation of webconfig.
mkdir build
cd build
cmake ..
makeUpdated codecov is showing
make
make test
```

12 changes: 12 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,16 @@ target_link_libraries (test_rbus_fr -lcunit -lwrp-c -lcimplog -lmsgpackc -lcurl
target_link_libraries (test_rbus_fr gcov -Wl,--no-as-needed )
endif (WEBCONFIG_BIN_SUPPORT)

#-------------------------------------------------------------------------------
# test_generic
#-------------------------------------------------------------------------------
if (WEBCONFIG_BIN_SUPPORT)
add_test(NAME test_generic COMMAND ${MEMORY_CHECK} ./test_generic)
add_executable(test_generic test_generic.c ../src/webcfg_generic.c)
target_link_libraries (test_generic -lcunit -lmsgpackc -lcimplog -ltrower-base64 -lwdmp-c -lcjson -lrbus)

target_link_libraries (test_generic gcov -Wl,--no-as-needed )
endif (WEBCONFIG_BIN_SUPPORT)

# Code coverage

Expand All @@ -316,6 +325,8 @@ COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_multipart_unittest.dir/__/src --output-file test_multipart_unittest.info
COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_webcfgpack.dir/__/src --output-file test_webcfgpack.info
COMMAND lcov -q --capture --directory
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/test_generic.dir/__/src --output-file test_generic.info

COMMAND lcov
-a test_events_supp.info
Expand All @@ -328,6 +339,7 @@ COMMAND lcov
-a test_root.info
-a test_multipart_unittest.info
-a test_webcfgpack.info
-a test_generic.info
--output-file coverage.info

COMMAND genhtml coverage.info
Expand Down
226 changes: 226 additions & 0 deletions tests/test_generic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
#include <rbus/rbus.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include "../src/webcfg_generic.h"
int Get_Mqtt_LocationId( char *pString);
int Get_Mqtt_NodeId( char *pString);
int Get_Mqtt_Broker( char *pString);
int Get_Mqtt_Port( char *pString);
char* Get_Mqtt_ClientId();
#define UNUSED(x) (void )(x)
#define CCSP_Msg_Bus_OK 100
#define CCSP_Msg_Bus_ERROR 102
#define CCSP_Msg_BUS_TIMEOUT 191
#define CCSP_ERR_INVALID_PARAMETER_VALUE 9007
#define CCSP_ERR_UNSUPPORTED_NAMESPACE 204

bool isRbusEnabled()
{

return true;
}
int set_rbus_ForceSync(char* pString, int *pStatus)
{
UNUSED(pString);
UNUSED(pStatus);
return 1;
}
int get_rbus_ForceSync(char** pString, char **transactionId )
{
UNUSED(pString);
UNUSED(transactionId);
return 1;
}
void setValues_rbus(const param_t paramVal[], const unsigned int paramCount, const int setType,char *transactionId, money_trace_spans *timeSpan, WDMP_STATUS *retStatus, int *ccspRetStatus)
{
UNUSED(paramVal);
UNUSED(paramCount);
UNUSED(setType);
UNUSED(transactionId);
UNUSED(timeSpan);
UNUSED(retStatus);
UNUSED(ccspRetStatus);
}
void sendNotification_rbus(char *payload, char *source, char *destination)
{
UNUSED(payload);
UNUSED(source);
UNUSED(destination);
}
rbusError_t registerRBUSEventElement()
{
return RBUS_ERROR_SUCCESS;
}
rbusError_t removeRBUSEventElement()
{
static int i = 0;
if(i == 0)
{
i=1;
return RBUS_ERROR_SUCCESS;
}
else
{
i=0;
return RBUS_ERROR_BUS_ERROR;
}
}
// Test function for rbus_GetValueFromDB
void test_setAttributes(void)
{
param_t *attArr = NULL;
setAttributes(attArr, 1, NULL, NULL);
}

void test_mapStatus(void)
{
WDMP_STATUS ret = WDMP_FAILURE;
ret = mapStatus(CCSP_Msg_Bus_OK);
CU_ASSERT_EQUAL(ret, WDMP_SUCCESS);
ret = mapStatus(CCSP_Msg_BUS_TIMEOUT);
CU_ASSERT_EQUAL(ret, WDMP_ERR_TIMEOUT);
ret = mapStatus(CCSP_ERR_INVALID_PARAMETER_VALUE);
CU_ASSERT_EQUAL(ret, WDMP_ERR_INVALID_PARAMETER_VALUE);
ret = mapStatus(CCSP_ERR_UNSUPPORTED_NAMESPACE);
CU_ASSERT_EQUAL(ret, WDMP_ERR_UNSUPPORTED_NAMESPACE);
ret = mapStatus(CCSP_Msg_Bus_ERROR);
CU_ASSERT_EQUAL(ret, WDMP_FAILURE);
}

void test_unregisterWebcfgEvent()
{
int ret = 0;
ret = unregisterWebcfgEvent();
CU_ASSERT_EQUAL(ret, 1);
ret = unregisterWebcfgEvent();
CU_ASSERT_EQUAL(ret, 0);
}

void test_registerWebcfgEvent()
{
WebConfigEventCallback webcfgEventCB;
webcfgEventCB = NULL;
int ret = 0;
ret = registerWebcfgEvent(webcfgEventCB);
CU_ASSERT_EQUAL(ret, 1);
}

void test_sendNotification()
{
sendNotification(NULL,NULL,NULL);
}

void test_setValues()
{
int ccspStatus=0;
WDMP_STATUS ret = WDMP_FAILURE;
setValues(NULL, 0, 0, NULL, NULL, &ret, &ccspStatus);
}

void test_getForceSync()
{
char* syncTransID = NULL;
char* ForceSyncDoc = NULL;
int ret = -1;
ret = getForceSync(&ForceSyncDoc, &syncTransID);
CU_ASSERT_EQUAL(ret, 0);
}

void test_setForceSync()
{
int ret = -1;
ret = setForceSync("", "", 0);
CU_ASSERT_EQUAL(ret, 0);
}

void test_set_global_systemReadyTime()
{
set_global_systemReadyTime("test1");
char *pString = NULL;
pString = get_global_systemReadyTime();
CU_ASSERT_STRING_EQUAL(pString, "test1");
set_global_systemReadyTime("test2");
pString = get_global_systemReadyTime();
CU_ASSERT_STRING_EQUAL(pString, "test2");
}

void test_Get_Mqtt_LocationId()
{
int ret = -1;
ret = Get_Mqtt_LocationId("test1");
CU_ASSERT_EQUAL(ret, 0);
}

void test_Get_Mqtt_NodeId()
{
int ret = -1;
ret = Get_Mqtt_NodeId("test1");
CU_ASSERT_EQUAL(ret, 0);
}

void test_Get_Mqtt_Broker()
{
int ret = -1;
ret = Get_Mqtt_Broker("test1");
CU_ASSERT_EQUAL(ret, 0);
}

void test_Get_Mqtt_Port()
{
int ret = -1;
ret = Get_Mqtt_Port("test1");
CU_ASSERT_EQUAL(ret, 0);
}

void test_Get_Mqtt_ClientId()
{
Get_Mqtt_ClientId();
}

void add_suites( CU_pSuite *suite )
{
*suite = CU_add_suite( "tests", NULL, NULL );
CU_add_test( *suite, "test setAttributes", test_setAttributes);
CU_add_test( *suite, "test mapStatus", test_mapStatus);
CU_add_test( *suite, "test unregisterWebcfgEvent", test_unregisterWebcfgEvent);
CU_add_test( *suite, "test registerWebcfgEvent", test_registerWebcfgEvent);
CU_add_test( *suite, "test sendNotification", test_sendNotification);
CU_add_test( *suite, "test setValues", test_setValues);
CU_add_test( *suite, "test getForceSync", test_getForceSync);
CU_add_test( *suite, "test setForceSync", test_setForceSync);
CU_add_test( *suite, "test set_global_systemReadyTime", test_set_global_systemReadyTime);
CU_add_test( *suite, "test Get_Mqtt_LocationId", test_Get_Mqtt_LocationId);
CU_add_test( *suite, "test Get_Mqtt_NodeId", test_Get_Mqtt_NodeId);
CU_add_test( *suite, "test Get_Mqtt_Broker", test_Get_Mqtt_Broker);
CU_add_test( *suite, "test Get_Mqtt_Port", test_Get_Mqtt_Port);
CU_add_test( *suite, "test Get_Mqtt_ClientId", test_Get_Mqtt_ClientId);
}

int main( int argc, char *argv[] )
{
unsigned rv = 1;
CU_pSuite suite = NULL;

(void ) argc;
(void ) argv;

if( CUE_SUCCESS == CU_initialize_registry() ) {
add_suites( &suite );

if( NULL != suite ) {
CU_basic_set_mode( CU_BRM_VERBOSE );
CU_basic_run_tests();
printf( "\n" );
CU_basic_show_failures( CU_get_failure_list() );
printf( "\n\n" );
rv = CU_get_number_of_tests_failed();
}

CU_cleanup_registry();

}

return rv;
}

0 comments on commit ff5e3dc

Please sign in to comment.