blob: 87d15cad7380b95a432b10277f155caaa1f0e5dc [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
Kailash Khalasi2adbad82017-05-15 14:53:40 -070010import random
11import re
Kailash Khalasi86e231e2017-06-06 13:13:43 -070012import yaml
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070013
14class utils(object):
15
16 @staticmethod
17 def listToDict(alist, intListIndex):
18 dictInfo = alist[int(intListIndex)]
19 return dictInfo
20
21 @staticmethod
22 def jsonToList(strFile, strListName):
23 data = json.loads(open(strFile).read())
24 #print "data...",data
25 dataList = data[strListName]
26 return dataList
27
Suchitra.Vemuri85220062016-10-25 10:44:11 -070028 '''
29 @method compare_dict
30 @Description: validates if contents of dict1 exists in dict2
31 @params: dict1 = input_data entered through api
32 dict2 = retrieved data from GET method
33 returns True if contents of dict1 exists in dict2
34 '''
You Wang507c4562016-11-23 13:36:09 -080035 def compare_dict(self, dict1, dict2):
36 print "input data", dict1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070037 print "get data", dict2
38 if dict1 == None or dict2 == None:
39 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070040 if type(dict1) is not dict or type(dict2) is not dict:
41 return False
You Wang507c4562016-11-23 13:36:09 -080042 if dict1 == {}:
43 return True
44 return self.compare_dict_recursive(dict1, dict2)
Suchitra.Vemuri85220062016-10-25 10:44:11 -070045
You Wang507c4562016-11-23 13:36:09 -080046 '''
47 @method compare_dict_recursive
48 @Description: recursive function to validate if dict1 is a subset of dict2
49 returns True if contents of dict1 exists in dict2
50 '''
51 def compare_dict_recursive(self, dict1, dict2):
Suchitra.Vemuri85220062016-10-25 10:44:11 -070052 for key1,value1 in dict1.items():
You Wang507c4562016-11-23 13:36:09 -080053 if key1 not in dict2.keys():
54 print "Missing key", key1, "in dict2"
55 return False
56 value2 = dict2[key1]
57 if type(value1) is dict and type(value2) is dict:
58 if not self.compare_dict_recursive(value1, value2):
59 return False
60 else:
61 if value2 != value1:
62 print "Values of key", key1, "in two dicts are not equal"
63 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070064 return True
65
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070066 '''
You Wang3964e842016-12-09 12:04:32 -080067 @method compare_list_of_dicts
68 @Description: validates if contents of dicts in list1 exists in dicts of list2
69 returns True if for each dict in list1, there's a dict in list2 that contains its content
70 '''
71 def compare_list_of_dicts(self, list1, list2):
72 for dict1 in list1:
73 if dict1 == {}:
74 continue
75 key = dict1.keys()[0]
76 value = dict1[key]
77 dict2 = self.getDictFromListOfDict(list2, key, value)
78 if dict2 == {}:
79 print "Comparison failed: no dictionaries found in list2 with key", key, "and value", value
80 return False
81 if self.compare_dict(dict1, dict2) == False:
82 print "Comparison failed: dictionary", dict1, "is not a subset of dictionary", dict2
83 return False
84 return True
85
86 '''
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070087 @method search_dictionary
88 @Description: Searches for a key in the provided nested dictionary
89 @params: input_dict = dictionary to be searched
90 search_key = name of the key to be searched for
91 returns two values: search_key value and status of the search.
92 True if found (False when not found)
93
94 '''
95 def search_dictionary(self,input_dict, search_key):
96 input_keys = input_dict.keys()
97 key_value = ''
98 found = False
99 for key in input_keys:
100 if key == search_key:
101 key_value = input_dict[key]
102 found = True
103 break
104 elif type(input_dict[key]) == dict:
105 key_value, found = self.search_dictionary(input_dict[key],search_key)
106 if found == True:
107 break
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800108 elif type(input_dict[key]) == list:
109 if not input_dict[key]:
110 found = False
111 break
112 for item in input_dict[key]:
113 if isinstance(item, dict):
114 key_value, found = self.search_dictionary(item, search_key)
115 if found == True:
116 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700117 return key_value,found
118 '''
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800119 @method getDictFromListOfDict
120 return key_value,found
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700121 @Description: Searches for the dictionary in the provided list of dictionaries
122 that matches the value of the key provided
123 @params : List of dictionaries(getResponse Data from the URL),
124 SearchKey - Key that needs to be searched for (ex: account_num)
125 searchKeyValue - Value of the searchKey (ex: 21)
126 @Returns: Dictionary returned when match found for searchKey with the corresponding
127 searchKeyValue provided
128 '''
129
130 def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue):
131 return_dict = {}
132 result = ''
133 for data in getJsonDataList:
Suchitra Vemurif58de922017-06-14 12:53:42 -0700134 print "data", data
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700135 return_dict = {}
136 found = False
137 input_keys = data.keys()
138 for key in input_keys:
139 if key == searchKey and str(data[key]) == str(searchKeyValue):
140 found = True
141 return_dict = data
Suchitra Vemurif58de922017-06-14 12:53:42 -0700142 print "return_dict",return_dict
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700143 break
144 elif type(data[key]) == dict:
145 result, found = self.search_dictionary(data[key],searchKey)
146 if found == True and str(result) == str(searchKeyValue):
147 return_dict = data
148 break
149 elif type(data[key]) == list:
150 for item in data[key]:
151 if isinstance(item, dict):
152 result, found = self.search_dictionary(data[key], searchKey)
153 if found == True and str(result) == str(searchKeyValue):
154 return_dict = data
155 break
156 if return_dict:
157 break
158 return return_dict
159
160
161 '''
162 @method getFieldValueFromDict
163 @params : search_dict - Dictionary to be searched
164 field - Key to be searched for (ex: account_num)
165 @Returns: Returns the value of the Key that was provided
166 '''
167 def getFieldValueFromDict(self,search_dict, field):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700168 results = ''
169 found = False
170 input_keys = search_dict.keys()
171 for key in input_keys:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800172 print "key...", key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700173 if key == field:
174 results = search_dict[key]
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800175 if not results:
176 found = True
177 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700178 elif type(search_dict[key]) == dict:
179 results, found = self.search_dictionary(search_dict[key],field)
180 if found == True:
181 break
182 elif type(search_dict[key]) == list:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800183 if not search_dict[key]:
184 found = False
You Wangf462ee92016-12-16 13:11:43 -0800185 continue
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700186 for item in search_dict[key]:
187 if isinstance(item, dict):
188 results, found = self.search_dictionary(item, field)
189 if found == True:
190 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800191 if results:
192 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700193
194 return results
195
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800196 def setFieldValueInDict(self,input_dict,field,field_value):
197 input_dict[field]=field_value
198 return input_dict
199
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800200 '''
201 @method getAllFieldValues
202 @params : getJsonDataDictList - List of dictionaries to be searched
203 fieldName - Key to be searched for (ex: instance_id)
204 @Returns: Returns the unique value of the Key that was provided
You Wangf462ee92016-12-16 13:11:43 -0800205 '''
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800206
207 def getAllFieldValues(self, getJsonDataDictList, fieldName):
208 value_list = []
209 uniqValue = ''
210 uniq_list = []
211 for data in getJsonDataDictList:
212 fieldValue = ''
213 fieldValue = self.getFieldValueFromDict(data, fieldName)
214 value_list.append(fieldValue)
215 uniq_list = sorted(set(value_list))
216 if len(uniq_list) == 1:
217 uniqValue = uniq_list[0]
218 else:
219 print "list of values found for ", fieldName, ";", uniq_list
220 return fieldValue
You Wangf462ee92016-12-16 13:11:43 -0800221
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700222 def generate_uuid(self):
223 return uuid.uuid4()
224
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700225 def generate_random_number_from_blacklist(self, blacklist, min=100, max=500, typeTag=False):
226 num = None
227 while num in blacklist or num is None:
228 num = random.randrange(int(min), int(max))
229 if typeTag:
230 return num
231 else:
232 return str(num)
233
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700234 def get_dynamic_resources(self, inputfile, resource):
235 resourceNames = []
236 names = {}
237 dnames = []
238 with open(inputfile, 'r') as f:
239 contents = yaml.load(f)
240 resources = contents[resource]
241 for i in resources:
242 resourceNames.append(i["name"])
243 for i in resourceNames:
244 names['name']=i
245 dnames.append(names.copy())
246 return dnames
247
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700248'''
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700249#Test
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800250dict_list = {
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700251 "humanReadableName": "cordSubscriber-17",
252 "id": 17,
253 "features": {
254 "uplink_speed": 1000000000,
255 "downlink_speed": 1000000000,
256 "status": "enabled"
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800257 },
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700258 "identity": {
259 "account_num": "20",
260 "name": "My House"
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800261 },
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700262 "related": {}
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800263 }
264input_dict = {
265 "s_tag" : "111",
266 "c_tag" : "222",
267 "subscriber" : ""
268 }
269new_value = 3
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700270test = utils()
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700271#data=test.jsonToList("Subscribers.json","SubscriberInfo")
272#print test.jsonToList("Subscribers.json","SubscriberInfo")
273#print "index 1...",test.listToDict(data,1)
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700274#result = test.getDictFromListOfDict(dict_list,"email",21)
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800275#result = test.getFieldValueFromDict(dict_list,"id")
276#result = test.getDictFromListOfDict(dict_list,"account_num",21)
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800277#result = test.setFieldValueInDict(input_dict,"subscriber",new_value)
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700278result = test.getAllFieldValues(list1,"instance_name")
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700279print "finalllllll result....", result
280'''