1 # ============LICENSE_START===============================================
2 # Copyright (C) 2020 Nordix Foundation. All rights reserved.
3 # ========================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ============LICENSE_END=================================================
18 # This script compare two jsons for equality, taken into account that the parameter values
19 # marked with '????' are not checked (only the parameter name need to exist)
20 # Example of target json with '????'
23 # "callbackUrl": "????",
24 # "keepAliveIntervalSeconds": "????",
25 # "serviceName": "serv2",
26 # "timeSinceLastActivitySeconds": "????"
29 # "callbackUrl": "????",
30 # "keepAliveIntervalSeconds": "????",
31 # "serviceName": "serv1",
32 # "timeSinceLastActivitySeconds": "????"
41 # # Helper function to compare two json list.
42 # # Returns true for equal, false for not equal
43 def compare_json_list(list1, list2):
44 if (list1.__len__() != list2.__len__()):
50 res = compare_json_obj(l, m)
60 # Deep compare of two json objects
61 # If a parameter value in the target json is set to '????' then the result json value is not checked for the that parameter
62 # Return true for equal json, false for not equal json
63 def compare_json_obj(obj1, obj2):
64 if isinstance(obj1, list):
65 if (not isinstance(obj2, list)):
67 return compare_json_list(obj1, obj2)
68 elif (isinstance(obj1, dict)):
69 if (not isinstance(obj2, dict)):
71 exp = set(obj2.keys()) == set(obj1.keys())
77 if isinstance(val1, list):
78 if (not compare_json_list(val1, val2)):
80 elif isinstance(val1, dict):
81 if (not compare_json_obj(val1, val2)):
84 #Do not check parameter values marked with '????'
85 if ((val1 != "????") and (val2 != val1)) and ((val2 != "????") and (val2 != val1)):
94 #Read the input file and compare the two json (target->result)
95 jsonTarget = json.loads(sys.argv[1])
96 jsonResult = json.loads(sys.argv[2])
97 res1=compare_json_obj(jsonTarget, jsonResult)
99 #Read the json again (in case the previous calls has re-arranged the jsons)
100 jsonTarget = json.loads(sys.argv[1])
101 jsonResult = json.loads(sys.argv[2])
102 #Compare the opposite order (result->target) to catch special duplicate json key cases
103 res2=compare_json_obj(jsonResult, jsonTarget)
110 except Exception as e: