5766622fdeaa98c5163298e1b1f43ce30fd160d7
[sim/a1-interface.git] / near-rt-ric-simulator / test / common / compare_json.py
1
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
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
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=================================================
17 #
18
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 '????'
22 # [
23 #   {
24 #     "callbackUrl": "????",
25 #     "keepAliveIntervalSeconds": "????",
26 #     "serviceName": "serv2",
27 #     "timeSinceLastActivitySeconds": "????"
28 #   },
29 #   {
30 #     "callbackUrl": "????",
31 #     "keepAliveIntervalSeconds": "????",
32 #     "serviceName": "serv1",
33 #     "timeSinceLastActivitySeconds": "????"
34 #   }
35 #]
36
37
38 import os
39 import json
40 import sys
41
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__()):
46         return False
47
48     for l in list1:
49         found = False
50         for m in list2:
51             res = compare_json(l, m)
52             if (res):
53                 found = True
54                 break
55
56         if (not found):
57             return False
58
59     return True
60
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)):
67             return False
68         return compare_json_list(obj1, obj2)
69     elif (isinstance(obj1, dict)):
70         if (not isinstance(obj2, dict)):
71             return False
72         exp = set(obj2.keys()) == set(obj1.keys())
73         if (not exp):
74             return False
75         for k in obj1.keys():
76             val1 = obj1.get(k)
77             val2 = obj2.get(k)
78             if isinstance(val1, list):
79                 if (not compare_json_list(val1, val2)):
80                     return False
81             elif isinstance(val1, dict):
82                 if (not compare_json(val1, val2)):
83                     return False
84             else:
85                 #Do not check parameter values marked with '????'
86                 if ((val1 != "????") and (val2 != val1)) and ((val2 != "????") and (val2 != val1)):
87                     return False
88     else:
89         return obj1 == obj2
90
91     return True
92
93
94 try:
95     #Read the input file and compare the two json (target->result)
96     jsonTarget = json.loads(sys.argv[1])
97     jsonResult = json.loads(sys.argv[2])
98     res1=compare_json(jsonTarget, jsonResult)
99
100     #Read the json again (in case the previous calls has re-arranged the jsons)
101     jsonTarget = json.loads(sys.argv[1])
102     jsonResult = json.loads(sys.argv[2])
103     #Compare the opposite order (result->target) to catch special duplicate json key cases
104     res2=compare_json(jsonResult, jsonTarget)
105
106     if (res1 and res2):
107         print (0)
108     else:
109         print (1)
110
111 except Exception as e:
112     print (1)
113 sys.exit()