Updated test env documentation
[nonrtric.git] / test / common / compare_json.py
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
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
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 '????'
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
37 import os
38 import json
39 import sys
40
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__()):
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
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)):
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
87     else:
88         return obj1 == obj2
89
90     return True
91
92
93 try:
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)
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)
109
110 except Exception as e:
111     print (1)
112 sys.exit()