2 # ============LICENSE_START===============================================
3 # Copyright (C) 2020 Nordix Foundation. All rights reserved.
4 # ========================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ============LICENSE_END=================================================
19 # This script compare two jsons for eqaulity, taken into account that the parameter values
20 # marked with '????' are not checked (only the parameter name need to exist)
21 # Example of target json with '????'
24 # "callbackUrl": "????",
25 # "keepAliveIntervalSeconds": "????",
26 # "serviceName": "serv2",
27 # "timeSinceLastActivitySeconds": "????"
30 # "callbackUrl": "????",
31 # "keepAliveIntervalSeconds": "????",
32 # "serviceName": "serv1",
33 # "timeSinceLastActivitySeconds": "????"
42 # # Helper function to compare two json list.
43 # # Returns true for equal, false for not equal
44 def compare_json_list(list1, list2):
45 if (list1.__len__() != list2.__len__()):
51 res = compare_json(l, m)
61 # Deep compare of two json obects
62 # If a parameter value in the target json is set to '????' then the result json value is not checked for the that parameter
63 # Return true for equal json, false for not equal json
64 def compare_json(obj1, obj2):
65 if isinstance(obj1, list):
66 if (not isinstance(obj2, list)):
68 return compare_json_list(obj1, obj2)
69 elif (isinstance(obj1, dict)):
70 if (not isinstance(obj2, dict)):
72 exp = set(obj2.keys()) == set(obj1.keys())
78 if isinstance(val1, list):
79 if (not compare_json_list(val1, val2)):
81 elif isinstance(val1, dict):
82 if (not compare_json(val1, val2)):
85 #Do not check parameter values marked with '????'
86 if ((val1 != "????") and (val2 != val1)) and ((val2 != "????") and (val2 != val1)):
94 # Compare two json object. Returns true if equal, false if not equal
95 # This function is intended to be used from other python scipts using json object (instead of files)
96 def compare(target, result):
98 res1=compare_json(target, result)
99 res2=compare_json(target, result)
107 if __name__ == '__main__':
109 #Read the input file and compare the two json (target->result)
110 jsonTarget = json.loads(sys.argv[1])
111 jsonResult = json.loads(sys.argv[2])
112 res1=compare_json(jsonTarget, jsonResult)
114 #Read the json again (in case the previous calls has re-arranged the jsons)
115 jsonTarget = json.loads(sys.argv[1])
116 jsonResult = json.loads(sys.argv[2])
117 #Compare the opposite order (result->target) to catch special duplicate json key cases
118 res2=compare_json(jsonResult, jsonTarget)