blob: 1eaa299674d3cd61aae2806893c40741f3062726 [file] [log] [blame]
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -07001import pexpect,os
2import time
3import json
4import collections
5import sys
6import robot
7import os.path
8from os.path import expanduser
9
10class utils(object):
11
12 @staticmethod
13 def listToDict(alist, intListIndex):
14 dictInfo = alist[int(intListIndex)]
15 return dictInfo
16
17 @staticmethod
18 def jsonToList(strFile, strListName):
19 data = json.loads(open(strFile).read())
20 #print "data...",data
21 dataList = data[strListName]
22 return dataList
23
Suchitra.Vemuri85220062016-10-25 10:44:11 -070024 '''
25 @method compare_dict
26 @Description: validates if contents of dict1 exists in dict2
27 @params: dict1 = input_data entered through api
28 dict2 = retrieved data from GET method
29 returns True if contents of dict1 exists in dict2
30 '''
You Wang507c4562016-11-23 13:36:09 -080031 def compare_dict(self, dict1, dict2):
32 print "input data", dict1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070033 print "get data", dict2
34 if dict1 == None or dict2 == None:
35 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070036 if type(dict1) is not dict or type(dict2) is not dict:
37 return False
You Wang507c4562016-11-23 13:36:09 -080038 if dict1 == {}:
39 return True
40 return self.compare_dict_recursive(dict1, dict2)
Suchitra.Vemuri85220062016-10-25 10:44:11 -070041
You Wang507c4562016-11-23 13:36:09 -080042 '''
43 @method compare_dict_recursive
44 @Description: recursive function to validate if dict1 is a subset of dict2
45 returns True if contents of dict1 exists in dict2
46 '''
47 def compare_dict_recursive(self, dict1, dict2):
Suchitra.Vemuri85220062016-10-25 10:44:11 -070048 for key1,value1 in dict1.items():
You Wang507c4562016-11-23 13:36:09 -080049 if key1 not in dict2.keys():
50 print "Missing key", key1, "in dict2"
51 return False
52 value2 = dict2[key1]
53 if type(value1) is dict and type(value2) is dict:
54 if not self.compare_dict_recursive(value1, value2):
55 return False
56 else:
57 if value2 != value1:
58 print "Values of key", key1, "in two dicts are not equal"
59 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070060 return True
61
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070062 '''
You Wang3964e842016-12-09 12:04:32 -080063 @method compare_list_of_dicts
64 @Description: validates if contents of dicts in list1 exists in dicts of list2
65 returns True if for each dict in list1, there's a dict in list2 that contains its content
66 '''
67 def compare_list_of_dicts(self, list1, list2):
68 for dict1 in list1:
69 if dict1 == {}:
70 continue
71 key = dict1.keys()[0]
72 value = dict1[key]
73 dict2 = self.getDictFromListOfDict(list2, key, value)
74 if dict2 == {}:
75 print "Comparison failed: no dictionaries found in list2 with key", key, "and value", value
76 return False
77 if self.compare_dict(dict1, dict2) == False:
78 print "Comparison failed: dictionary", dict1, "is not a subset of dictionary", dict2
79 return False
80 return True
81
82 '''
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070083 @method search_dictionary
84 @Description: Searches for a key in the provided nested dictionary
85 @params: input_dict = dictionary to be searched
86 search_key = name of the key to be searched for
87 returns two values: search_key value and status of the search.
88 True if found (False when not found)
89
90 '''
91 def search_dictionary(self,input_dict, search_key):
92 input_keys = input_dict.keys()
93 key_value = ''
94 found = False
95 for key in input_keys:
96 if key == search_key:
97 key_value = input_dict[key]
98 found = True
99 break
100 elif type(input_dict[key]) == dict:
101 key_value, found = self.search_dictionary(input_dict[key],search_key)
102 if found == True:
103 break
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800104 elif type(input_dict[key]) == list:
105 if not input_dict[key]:
106 found = False
107 break
108 for item in input_dict[key]:
109 if isinstance(item, dict):
110 key_value, found = self.search_dictionary(item, search_key)
111 if found == True:
112 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700113 return key_value,found
114 '''
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800115 @method getDictFromListOfDict
116 return key_value,found
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700117 @Description: Searches for the dictionary in the provided list of dictionaries
118 that matches the value of the key provided
119 @params : List of dictionaries(getResponse Data from the URL),
120 SearchKey - Key that needs to be searched for (ex: account_num)
121 searchKeyValue - Value of the searchKey (ex: 21)
122 @Returns: Dictionary returned when match found for searchKey with the corresponding
123 searchKeyValue provided
124 '''
125
126 def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue):
127 return_dict = {}
128 result = ''
129 for data in getJsonDataList:
130 return_dict = {}
131 found = False
132 input_keys = data.keys()
133 for key in input_keys:
134 if key == searchKey and str(data[key]) == str(searchKeyValue):
135 found = True
136 return_dict = data
137 break
138 elif type(data[key]) == dict:
139 result, found = self.search_dictionary(data[key],searchKey)
140 if found == True and str(result) == str(searchKeyValue):
141 return_dict = data
142 break
143 elif type(data[key]) == list:
144 for item in data[key]:
145 if isinstance(item, dict):
146 result, found = self.search_dictionary(data[key], searchKey)
147 if found == True and str(result) == str(searchKeyValue):
148 return_dict = data
149 break
150 if return_dict:
151 break
152 return return_dict
153
154
155 '''
156 @method getFieldValueFromDict
157 @params : search_dict - Dictionary to be searched
158 field - Key to be searched for (ex: account_num)
159 @Returns: Returns the value of the Key that was provided
160 '''
161 def getFieldValueFromDict(self,search_dict, field):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700162 results = ''
163 found = False
164 input_keys = search_dict.keys()
165 for key in input_keys:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800166 print "key...", key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700167 if key == field:
168 results = search_dict[key]
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800169 if not results:
170 found = True
171 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700172 elif type(search_dict[key]) == dict:
173 results, found = self.search_dictionary(search_dict[key],field)
174 if found == True:
175 break
176 elif type(search_dict[key]) == list:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800177 if not search_dict[key]:
178 found = False
You Wangf462ee92016-12-16 13:11:43 -0800179 continue
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700180 for item in search_dict[key]:
181 if isinstance(item, dict):
182 results, found = self.search_dictionary(item, field)
183 if found == True:
184 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800185 if results:
186 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700187
188 return results
189
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800190 def setFieldValueInDict(self,input_dict,field,field_value):
191 input_dict[field]=field_value
192 return input_dict
193
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800194 '''
195 @method getAllFieldValues
196 @params : getJsonDataDictList - List of dictionaries to be searched
197 fieldName - Key to be searched for (ex: instance_id)
198 @Returns: Returns the unique value of the Key that was provided
You Wangf462ee92016-12-16 13:11:43 -0800199 '''
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800200
201 def getAllFieldValues(self, getJsonDataDictList, fieldName):
202 value_list = []
203 uniqValue = ''
204 uniq_list = []
205 for data in getJsonDataDictList:
206 fieldValue = ''
207 fieldValue = self.getFieldValueFromDict(data, fieldName)
208 value_list.append(fieldValue)
209 uniq_list = sorted(set(value_list))
210 if len(uniq_list) == 1:
211 uniqValue = uniq_list[0]
212 else:
213 print "list of values found for ", fieldName, ";", uniq_list
214 return fieldValue
You Wangf462ee92016-12-16 13:11:43 -0800215
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700216'''
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700217#Test
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800218dict_list = {
219 "humanReadableName": "cordSubscriber-17",
220 "id": 17,
221 "features": {
222 "uplink_speed": 1000000000,
223 "downlink_speed": 1000000000,
224 "status": "enabled"
225 },
226 "identity": {
227 "account_num": "20",
228 "name": "My House"
229 },
230 "related": {}
231 }
232input_dict = {
233 "s_tag" : "111",
234 "c_tag" : "222",
235 "subscriber" : ""
236 }
237new_value = 3
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700238test = utils()
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700239#data=test.jsonToList("Subscribers.json","SubscriberInfo")
240#print test.jsonToList("Subscribers.json","SubscriberInfo")
241#print "index 1...",test.listToDict(data,1)
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800242#result = test.getDictFromListOfDict(dict_list,"email",21)
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800243#result = test.getFieldValueFromDict(dict_list,"id")
244#result = test.getDictFromListOfDict(dict_list,"account_num",21)
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800245#result = test.setFieldValueInDict(input_dict,"subscriber",new_value)
246result = test.getAllFieldValues(list1,"instance_name")
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700247print "finalllllll result....", result
248'''