blob: fb1a797b554e37f87db3a8035c709b6d64e7a9d5 [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
Kailash Khalasib6e87fc2017-04-18 15:08:19 -07009import uuid
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070010
11class utils(object):
12
13 @staticmethod
14 def listToDict(alist, intListIndex):
15 dictInfo = alist[int(intListIndex)]
16 return dictInfo
17
18 @staticmethod
19 def jsonToList(strFile, strListName):
20 data = json.loads(open(strFile).read())
21 #print "data...",data
22 dataList = data[strListName]
23 return dataList
24
Suchitra.Vemuri85220062016-10-25 10:44:11 -070025 '''
26 @method compare_dict
27 @Description: validates if contents of dict1 exists in dict2
28 @params: dict1 = input_data entered through api
29 dict2 = retrieved data from GET method
30 returns True if contents of dict1 exists in dict2
31 '''
You Wang507c4562016-11-23 13:36:09 -080032 def compare_dict(self, dict1, dict2):
33 print "input data", dict1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070034 print "get data", dict2
35 if dict1 == None or dict2 == None:
36 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070037 if type(dict1) is not dict or type(dict2) is not dict:
38 return False
You Wang507c4562016-11-23 13:36:09 -080039 if dict1 == {}:
40 return True
41 return self.compare_dict_recursive(dict1, dict2)
Suchitra.Vemuri85220062016-10-25 10:44:11 -070042
You Wang507c4562016-11-23 13:36:09 -080043 '''
44 @method compare_dict_recursive
45 @Description: recursive function to validate if dict1 is a subset of dict2
46 returns True if contents of dict1 exists in dict2
47 '''
48 def compare_dict_recursive(self, dict1, dict2):
Suchitra.Vemuri85220062016-10-25 10:44:11 -070049 for key1,value1 in dict1.items():
You Wang507c4562016-11-23 13:36:09 -080050 if key1 not in dict2.keys():
51 print "Missing key", key1, "in dict2"
52 return False
53 value2 = dict2[key1]
54 if type(value1) is dict and type(value2) is dict:
55 if not self.compare_dict_recursive(value1, value2):
56 return False
57 else:
58 if value2 != value1:
59 print "Values of key", key1, "in two dicts are not equal"
60 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070061 return True
62
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070063 '''
You Wang3964e842016-12-09 12:04:32 -080064 @method compare_list_of_dicts
65 @Description: validates if contents of dicts in list1 exists in dicts of list2
66 returns True if for each dict in list1, there's a dict in list2 that contains its content
67 '''
68 def compare_list_of_dicts(self, list1, list2):
69 for dict1 in list1:
70 if dict1 == {}:
71 continue
72 key = dict1.keys()[0]
73 value = dict1[key]
74 dict2 = self.getDictFromListOfDict(list2, key, value)
75 if dict2 == {}:
76 print "Comparison failed: no dictionaries found in list2 with key", key, "and value", value
77 return False
78 if self.compare_dict(dict1, dict2) == False:
79 print "Comparison failed: dictionary", dict1, "is not a subset of dictionary", dict2
80 return False
81 return True
82
83 '''
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070084 @method search_dictionary
85 @Description: Searches for a key in the provided nested dictionary
86 @params: input_dict = dictionary to be searched
87 search_key = name of the key to be searched for
88 returns two values: search_key value and status of the search.
89 True if found (False when not found)
90
91 '''
92 def search_dictionary(self,input_dict, search_key):
93 input_keys = input_dict.keys()
94 key_value = ''
95 found = False
96 for key in input_keys:
97 if key == search_key:
98 key_value = input_dict[key]
99 found = True
100 break
101 elif type(input_dict[key]) == dict:
102 key_value, found = self.search_dictionary(input_dict[key],search_key)
103 if found == True:
104 break
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800105 elif type(input_dict[key]) == list:
106 if not input_dict[key]:
107 found = False
108 break
109 for item in input_dict[key]:
110 if isinstance(item, dict):
111 key_value, found = self.search_dictionary(item, search_key)
112 if found == True:
113 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700114 return key_value,found
115 '''
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800116 @method getDictFromListOfDict
117 return key_value,found
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700118 @Description: Searches for the dictionary in the provided list of dictionaries
119 that matches the value of the key provided
120 @params : List of dictionaries(getResponse Data from the URL),
121 SearchKey - Key that needs to be searched for (ex: account_num)
122 searchKeyValue - Value of the searchKey (ex: 21)
123 @Returns: Dictionary returned when match found for searchKey with the corresponding
124 searchKeyValue provided
125 '''
126
127 def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue):
128 return_dict = {}
129 result = ''
130 for data in getJsonDataList:
131 return_dict = {}
132 found = False
133 input_keys = data.keys()
134 for key in input_keys:
135 if key == searchKey and str(data[key]) == str(searchKeyValue):
136 found = True
137 return_dict = data
138 break
139 elif type(data[key]) == dict:
140 result, found = self.search_dictionary(data[key],searchKey)
141 if found == True and str(result) == str(searchKeyValue):
142 return_dict = data
143 break
144 elif type(data[key]) == list:
145 for item in data[key]:
146 if isinstance(item, dict):
147 result, found = self.search_dictionary(data[key], searchKey)
148 if found == True and str(result) == str(searchKeyValue):
149 return_dict = data
150 break
151 if return_dict:
152 break
153 return return_dict
154
155
156 '''
157 @method getFieldValueFromDict
158 @params : search_dict - Dictionary to be searched
159 field - Key to be searched for (ex: account_num)
160 @Returns: Returns the value of the Key that was provided
161 '''
162 def getFieldValueFromDict(self,search_dict, field):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700163 results = ''
164 found = False
165 input_keys = search_dict.keys()
166 for key in input_keys:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800167 print "key...", key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700168 if key == field:
169 results = search_dict[key]
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800170 if not results:
171 found = True
172 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700173 elif type(search_dict[key]) == dict:
174 results, found = self.search_dictionary(search_dict[key],field)
175 if found == True:
176 break
177 elif type(search_dict[key]) == list:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800178 if not search_dict[key]:
179 found = False
You Wangf462ee92016-12-16 13:11:43 -0800180 continue
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700181 for item in search_dict[key]:
182 if isinstance(item, dict):
183 results, found = self.search_dictionary(item, field)
184 if found == True:
185 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800186 if results:
187 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700188
189 return results
190
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800191 def setFieldValueInDict(self,input_dict,field,field_value):
192 input_dict[field]=field_value
193 return input_dict
194
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800195 '''
196 @method getAllFieldValues
197 @params : getJsonDataDictList - List of dictionaries to be searched
198 fieldName - Key to be searched for (ex: instance_id)
199 @Returns: Returns the unique value of the Key that was provided
You Wangf462ee92016-12-16 13:11:43 -0800200 '''
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800201
202 def getAllFieldValues(self, getJsonDataDictList, fieldName):
203 value_list = []
204 uniqValue = ''
205 uniq_list = []
206 for data in getJsonDataDictList:
207 fieldValue = ''
208 fieldValue = self.getFieldValueFromDict(data, fieldName)
209 value_list.append(fieldValue)
210 uniq_list = sorted(set(value_list))
211 if len(uniq_list) == 1:
212 uniqValue = uniq_list[0]
213 else:
214 print "list of values found for ", fieldName, ";", uniq_list
215 return fieldValue
You Wangf462ee92016-12-16 13:11:43 -0800216
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700217 def generate_uuid(self):
218 return uuid.uuid4()
219
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700220'''
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700221#Test
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800222dict_list = {
223 "humanReadableName": "cordSubscriber-17",
224 "id": 17,
225 "features": {
226 "uplink_speed": 1000000000,
227 "downlink_speed": 1000000000,
228 "status": "enabled"
229 },
230 "identity": {
231 "account_num": "20",
232 "name": "My House"
233 },
234 "related": {}
235 }
236input_dict = {
237 "s_tag" : "111",
238 "c_tag" : "222",
239 "subscriber" : ""
240 }
241new_value = 3
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700242test = utils()
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700243#data=test.jsonToList("Subscribers.json","SubscriberInfo")
244#print test.jsonToList("Subscribers.json","SubscriberInfo")
245#print "index 1...",test.listToDict(data,1)
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800246#result = test.getDictFromListOfDict(dict_list,"email",21)
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800247#result = test.getFieldValueFromDict(dict_list,"id")
248#result = test.getDictFromListOfDict(dict_list,"account_num",21)
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800249#result = test.setFieldValueInDict(input_dict,"subscriber",new_value)
250result = test.getAllFieldValues(list1,"instance_name")
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700251print "finalllllll result....", result
252'''