blob: 44477d9e80b58551b3bad86660c2771f8597ca05 [file] [log] [blame]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001# ============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
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
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=================================================
16#
17
BjornMagnussonXA72667f12020-04-24 09:20:18 +020018# This script compare two jsons for eqaulity, 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 '????'
21# [
22# {
23# "callbackUrl": "????",
24# "keepAliveIntervalSeconds": "????",
25# "serviceName": "serv2",
26# "timeSinceLastActivitySeconds": "????"
27# },
28# {
29# "callbackUrl": "????",
30# "keepAliveIntervalSeconds": "????",
31# "serviceName": "serv1",
32# "timeSinceLastActivitySeconds": "????"
33# }
34#]
35
36
BjornMagnussonXA80a92002020-03-19 14:31:06 +010037import os
38import json
39import sys
40
BjornMagnussonXA72667f12020-04-24 09:20:18 +020041# # Helper function to compare two json list.
42# # Returns true for equal, false for not equal
43def compare_json_list(list1, list2):
44 if (list1.__len__() != list2.__len__()):
45 return False
46
47 for l in list1:
48 found = False
49 for m in list2:
50 res = compare_json_obj(l, m)
51 if (res):
52 found = True
53 break
54
55 if (not found):
56 return False
57
58 return True
59
BjornMagnussonXA80a92002020-03-19 14:31:06 +010060# Deep compare of two json obects
61# If a parameter value in the target json is set to '????' then the result json value is not checked for the that parameter
BjornMagnussonXA72667f12020-04-24 09:20:18 +020062# Return true for equal json, false for not equal json
63def compare_json_obj(obj1, obj2):
64 if isinstance(obj1, list):
65 if (not isinstance(obj2, list)):
66 return False
67 return compare_json_list(obj1, obj2)
68 elif (isinstance(obj1, dict)):
69 if (not isinstance(obj2, dict)):
70 return False
71 exp = set(obj2.keys()) == set(obj1.keys())
72 if (not exp):
73 return False
74 for k in obj1.keys():
75 val1 = obj1.get(k)
76 val2 = obj2.get(k)
77 if isinstance(val1, list):
78 if (not compare_json_list(val1, val2)):
79 return False
80 elif isinstance(val1, dict):
81 if (not compare_json_obj(val1, val2)):
82 return False
83 else:
84 #Do not check parameter values marked with '????'
85 if ((val1 != "????") and (val2 != val1)) and ((val2 != "????") and (val2 != val1)):
86 return False
BjornMagnussonXA80a92002020-03-19 14:31:06 +010087 else:
BjornMagnussonXA72667f12020-04-24 09:20:18 +020088 return obj1 == obj2
89
90 return True
91
BjornMagnussonXA80a92002020-03-19 14:31:06 +010092
93try:
BjornMagnussonXA72667f12020-04-24 09:20:18 +020094 #Read the input file and compare the two json (target->result)
BjornMagnussonXA80a92002020-03-19 14:31:06 +010095 jsonTarget = json.loads(sys.argv[1])
96 jsonResult = json.loads(sys.argv[2])
BjornMagnussonXA72667f12020-04-24 09:20:18 +020097 res1=compare_json_obj(jsonTarget, jsonResult)
98
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)
104
105 if (res1 and res2):
106 print (0)
107 else:
108 print (1)
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100109
110except Exception as e:
111 print (1)
112sys.exit()