X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=near-rt-ric-simulator%2Ftest%2Fcommon%2Fcompare_json.py;h=22c993df89710a856399eaec2f7d3f9926c248d5;hb=2d07867317ef49df4cd003899dcc7fe4c01b0352;hp=53bddb1731596e1e65e819a2e1019d2dac61b2d9;hpb=21be2e0c890fb9604d0add2942fcff19b6fe2fb2;p=sim%2Fa1-interface.git diff --git a/near-rt-ric-simulator/test/common/compare_json.py b/near-rt-ric-simulator/test/common/compare_json.py index 53bddb1..22c993d 100644 --- a/near-rt-ric-simulator/test/common/compare_json.py +++ b/near-rt-ric-simulator/test/common/compare_json.py @@ -16,51 +16,112 @@ # ============LICENSE_END================================================= # -# Deep compare of two json obects -# If a parameter value in the target json is set to '????' then the result json value is not checked for that parameter -# Any included json array will be sorted before comparison +# This script compare two jsons for eqaulity, taken into account that the parameter values +# marked with '????' are not checked (only the parameter name need to exist) +# Example of target json with '????' +# [ +# { +# "callbackUrl": "????", +# "keepAliveIntervalSeconds": "????", +# "serviceName": "serv2", +# "timeSinceLastActivitySeconds": "????" +# }, +# { +# "callbackUrl": "????", +# "keepAliveIntervalSeconds": "????", +# "serviceName": "serv1", +# "timeSinceLastActivitySeconds": "????" +# } +#] -import sys + +import os import json +import sys -def compare_json(jsonTarget,jsonResult): - - - if isinstance(jsonTarget, dict): - if (len(jsonTarget) != len(jsonResult)): - return 1 - for key in jsonTarget.keys(): - if (jsonResult.get(key) is None): - return 1 - res=compare_json(jsonTarget.get(key), jsonResult.get(key)) - if (res != 0): - return 1 - elif isinstance(jsonTarget, list): - if (len(jsonTarget) != len(jsonResult)): - return 1 - jsonTarget.sort() - jsonResult.sort() - for i in range(len(jsonTarget)): - res=compare_json(jsonTarget[i], jsonResult[i]) - if (res != 0): - return 1 - else: - if (jsonTarget != "????") and (jsonTarget != jsonResult): - return 1 - return 0 +# # Helper function to compare two json list. +# # Returns true for equal, false for not equal +def compare_json_list(list1, list2): + if (list1.__len__() != list2.__len__()): + return False + for l in list1: + found = False + for m in list2: + res = compare_json(l, m) + if (res): + found = True + break -try: - jsonTarget = json.loads(sys.argv[1]) - jsonResult = json.loads(sys.argv[2]) + if (not found): + return False + + return True + +# Deep compare of two json obects +# If a parameter value in the target json is set to '????' then the result json value is not checked for the that parameter +# Return true for equal json, false for not equal json +def compare_json(obj1, obj2): + if isinstance(obj1, list): + if (not isinstance(obj2, list)): + return False + return compare_json_list(obj1, obj2) + elif (isinstance(obj1, dict)): + if (not isinstance(obj2, dict)): + return False + exp = set(obj2.keys()) == set(obj1.keys()) + if (not exp): + return False + for k in obj1.keys(): + val1 = obj1.get(k) + val2 = obj2.get(k) + if isinstance(val1, list): + if (not compare_json_list(val1, val2)): + return False + elif isinstance(val1, dict): + if (not compare_json(val1, val2)): + return False + else: + #Do not check parameter values marked with '????' + if ((val1 != "????") and (val2 != val1)) and ((val2 != "????") and (val2 != val1)): + return False + else: + return obj1 == obj2 - print(compare_json(jsonTarget,jsonResult)) + return True -except Exception as e: - print (1) -sys.exit() +# Compare two json object. Returns true if equal, false if not equal +# This function is intended to be used from other python scipts using json object (instead of files) +def compare(target, result): + try: + res1=compare_json(target, result) + res2=compare_json(target, result) + if (res1 and res2): + return True + else: + return False + except Exception: + return False +if __name__ == '__main__': + try: + #Read the input file and compare the two json (target->result) + jsonTarget = json.loads(sys.argv[1]) + jsonResult = json.loads(sys.argv[2]) + res1=compare_json(jsonTarget, jsonResult) + #Read the json again (in case the previous calls has re-arranged the jsons) + jsonTarget = json.loads(sys.argv[1]) + jsonResult = json.loads(sys.argv[2]) + #Compare the opposite order (result->target) to catch special duplicate json key cases + res2=compare_json(jsonResult, jsonTarget) + if (res1 and res2): + print (0) + else: + print (1) + except Exception: + print (1) + sys.exit() \ No newline at end of file