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=5766622fdeaa98c5163298e1b1f43ce30fd160d7;hpb=7d31c11783b82cb058005242e3d2d0c932c9a04d;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 5766622..22c993d 100644 --- a/near-rt-ric-simulator/test/common/compare_json.py +++ b/near-rt-ric-simulator/test/common/compare_json.py @@ -91,23 +91,37 @@ def compare_json(obj1, obj2): return True -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) +# 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 -except Exception as e: - print (1) -sys.exit() \ No newline at end of file +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