zabbix-agent2/tests/zbxmockjson.c
/******************************************************************************
 *                                                                            *
 * Function: __zbx_mock_assert_json_eq                                        *
 *                                                                            *
 * Purpose: compares returned json with expected                              *
 *                                                                            *
 * Comments: The comparison is done by first flattening both jsons into       *
 *           key(jsonpath)-value pairs, sorting by keys and then comparing    *
 *           the resulting lists.                                             *
 *           If expected value starts with / then regular expression match is *
 *           performed.                                                       *
 *                                                                            *
 ******************************************************************************/
zbx_mock_assert_json_eq

Purpose: compares returned json with expected

Comments: The comparison is done by first flattening both jsons into
key(jsonpath)-value pairs, sorting by keys and then comparing
the resulting lists.
If expected value starts with / then regular expression match is
performed.
zbx_mock_assert_json_eq Purpose : compares returned json with expected Comments : The comparison is done by first flattening both jsons into key ( jsonpath ) - value pairs sorting by keys and then comparing the resulting lists . If expected value starts with / then regular expression match is performed .
void	__zbx_mock_assert_json_eq(const char *file, int line, const char *prefix_msg, const char *expected_value,
		const char *returned_value)
{
	struct zbx_json_parse	jp_expected, jp_returned;
	zbx_vector_ptr_pair_t	props_expected, props_returned;
	int			i, props_num;
	if (FAIL == zbx_json_open(expected_value, &jp_expected))
		_FAIL(file, line, prefix_msg, "Expected value is not a valid JSON object: %s", zbx_json_strerror());
	if (FAIL == zbx_json_open(returned_value, &jp_returned))
		_FAIL(file, line, prefix_msg, "Returned value is not a valid JSON object: %s", zbx_json_strerror());
	zbx_vector_ptr_pair_create(&props_expected);
	zbx_vector_ptr_pair_create(&props_returned);
	json_flatten(&jp_expected, &props_expected);
	json_flatten(&jp_returned, &props_returned);
	props_num = MIN(props_expected.values_num, props_returned.values_num);
	for (i = 0; i < props_num; i++)
	{
		zbx_ptr_pair_t	*pair_expected = &props_expected.values[i];
		zbx_ptr_pair_t	*pair_returned = &props_returned.values[i];
		if (0 != strcmp(pair_expected->first, pair_returned->first))
		{
			_FAIL(file, line, prefix_msg, "Expected key \"%s\" while got \"%s\"", pair_expected->first,
					pair_returned->first);
		}
		if ('/' == *(char *)pair_expected->second)
		{
			char	*pattern = (char *)pair_expected->second + 1;
			if (NULL == zbx_regexp_match(pair_returned->second, pattern, NULL))
			{
				_FAIL(file, line, prefix_msg, "Key \"%s\" value \"%s\" does not match pattern \"%s\"",
						pair_returned->first, pair_returned->second, pattern);
			}
		}
		else
		{
			if (0 != strcmp(pair_expected->second, pair_returned->second))
			{
				_FAIL(file, line, prefix_msg, "Expected key \"%s\" value \"%s\" while got \"%s\"",
						pair_expected->first, pair_expected->second, pair_returned->second);
			}
		}
	}
	if (i < props_expected.values_num)
	{
		zbx_ptr_pair_t	*pair_expected = &props_expected.values[i];
		_FAIL(file, line, prefix_msg, "Expected key \"%s\" while got nothing", pair_expected->first);
	}
	if (i < props_returned.values_num)
	{
		zbx_ptr_pair_t	*pair_returned = &props_returned.values[i];
		_FAIL(file, line, prefix_msg, "Did not expect key \"%s\"", pair_returned->first);
	}
	for (i = 0; i < props_expected.values_num; i++)
	{
		zbx_free(props_expected.values[i].first);
		zbx_free(props_expected.values[i].second);
	}
	zbx_vector_ptr_pair_destroy(&props_expected);
	for (i = 0; i < props_returned.values_num; i++)
	{
		zbx_free(props_returned.values[i].first);
		zbx_free(props_returned.values[i].second);
	}
	zbx_vector_ptr_pair_destroy(&props_returned);
}
void __zbx_mock_assert_json_eq ( const char * file , int line , const char * prefix_msg , const char * expected_value , const char * returned_value ) { struct zbx_json_parse jp_expected , jp_returned ; zbx_vector_ptr_pair_t props_expected , props_returned ; int i , props_num ; if ( FAIL == zbx_json_open ( expected_value , & jp_expected ) ) _FAIL ( file , line , prefix_msg , " " , zbx_json_strerror ( ) ) ; if ( FAIL == zbx_json_open ( returned_value , & jp_returned ) ) _FAIL ( file , line , prefix_msg , " " , zbx_json_strerror ( ) ) ; zbx_vector_ptr_pair_create ( & props_expected ) ; zbx_vector_ptr_pair_create ( & props_returned ) ; json_flatten ( & jp_expected , & props_expected ) ; json_flatten ( & jp_returned , & props_returned ) ; props_num = MIN ( props_expected . values_num , props_returned . values_num ) ; for ( i = 0 ; i < props_num ; i ++ ) { zbx_ptr_pair_t * pair_expected = & props_expected . values [ i ] ; zbx_ptr_pair_t * pair_returned = & props_returned . values [ i ] ; if ( 0 != strcmp ( pair_expected -> first , pair_returned -> first ) ) { _FAIL ( file , line , prefix_msg , " \" \" \" \" " , pair_expected -> first , pair_returned -> first ) ; } if ( ' ' == * ( char * ) pair_expected -> second ) { char * pattern = ( char * ) pair_expected -> second + 1 ; if ( NULL == zbx_regexp_match ( pair_returned -> second , pattern , NULL ) ) { _FAIL ( file , line , prefix_msg , " \" \" \" \" \" \" " , pair_returned -> first , pair_returned -> second , pattern ) ; } } else { if ( 0 != strcmp ( pair_expected -> second , pair_returned -> second ) ) { _FAIL ( file , line , prefix_msg , " \" \" \" \" \" \" " , pair_expected -> first , pair_expected -> second , pair_returned -> second ) ; } } } if ( i < props_expected . values_num ) { zbx_ptr_pair_t * pair_expected = & props_expected . values [ i ] ; _FAIL ( file , line , prefix_msg , " \" \" " , pair_expected -> first ) ; } if ( i < props_returned . values_num ) { zbx_ptr_pair_t * pair_returned = & props_returned . values [ i ] ; _FAIL ( file , line , prefix_msg , " \" \" " , pair_returned -> first ) ; } for ( i = 0 ; i < props_expected . values_num ; i ++ ) { zbx_free ( props_expected . values [ i ] . first ) ; zbx_free ( props_expected . values [ i ] . second ) ; } zbx_vector_ptr_pair_destroy ( & props_expected ) ; for ( i = 0 ; i < props_returned . values_num ; i ++ ) { zbx_free ( props_returned . values [ i ] . first ) ; zbx_free ( props_returned . values [ i ] . second ) ; } zbx_vector_ptr_pair_destroy ( & props_returned ) ; }
Function: __zbx_mock_assert_json_eq                                        
                                                                            
 Purpose: compares returned json with expected                              
                                                                            
 Comments: The comparison is done by first flattening both jsons into       
           key(jsonpath)-value pairs, sorting by keys and then comparing    
           the resulting lists.
Function : __zbx_mock_assert_json_eq Purpose : compares returned json with expected Comments : The comparison is done by first flattening both jsons into key ( jsonpath ) - value pairs sorting by keys and then comparing the resulting lists .