blob: 48dcae2e4f7f6791405a51239d3745946f5fb4fe [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:
134 return_dict = {}
135 found = False
136 input_keys = data.keys()
137 for key in input_keys:
138 if key == searchKey and str(data[key]) == str(searchKeyValue):
139 found = True
140 return_dict = data
141 break
142 elif type(data[key]) == dict:
143 result, found = self.search_dictionary(data[key],searchKey)
144 if found == True and str(result) == str(searchKeyValue):
145 return_dict = data
146 break
147 elif type(data[key]) == list:
148 for item in data[key]:
149 if isinstance(item, dict):
150 result, found = self.search_dictionary(data[key], searchKey)
151 if found == True and str(result) == str(searchKeyValue):
152 return_dict = data
153 break
154 if return_dict:
155 break
156 return return_dict
157
158
159 '''
160 @method getFieldValueFromDict
161 @params : search_dict - Dictionary to be searched
162 field - Key to be searched for (ex: account_num)
163 @Returns: Returns the value of the Key that was provided
164 '''
165 def getFieldValueFromDict(self,search_dict, field):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700166 results = ''
167 found = False
168 input_keys = search_dict.keys()
169 for key in input_keys:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800170 print "key...", key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700171 if key == field:
172 results = search_dict[key]
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800173 if not results:
174 found = True
175 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700176 elif type(search_dict[key]) == dict:
177 results, found = self.search_dictionary(search_dict[key],field)
178 if found == True:
179 break
180 elif type(search_dict[key]) == list:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800181 if not search_dict[key]:
182 found = False
You Wangf462ee92016-12-16 13:11:43 -0800183 continue
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700184 for item in search_dict[key]:
185 if isinstance(item, dict):
186 results, found = self.search_dictionary(item, field)
187 if found == True:
188 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800189 if results:
190 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700191
192 return results
193
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800194 def setFieldValueInDict(self,input_dict,field,field_value):
195 input_dict[field]=field_value
196 return input_dict
197
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800198 '''
199 @method getAllFieldValues
200 @params : getJsonDataDictList - List of dictionaries to be searched
201 fieldName - Key to be searched for (ex: instance_id)
202 @Returns: Returns the unique value of the Key that was provided
You Wangf462ee92016-12-16 13:11:43 -0800203 '''
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800204
205 def getAllFieldValues(self, getJsonDataDictList, fieldName):
206 value_list = []
207 uniqValue = ''
208 uniq_list = []
209 for data in getJsonDataDictList:
210 fieldValue = ''
211 fieldValue = self.getFieldValueFromDict(data, fieldName)
212 value_list.append(fieldValue)
213 uniq_list = sorted(set(value_list))
214 if len(uniq_list) == 1:
215 uniqValue = uniq_list[0]
216 else:
217 print "list of values found for ", fieldName, ";", uniq_list
218 return fieldValue
You Wangf462ee92016-12-16 13:11:43 -0800219
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700220 def generate_uuid(self):
221 return uuid.uuid4()
222
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700223 def generate_random_number_from_blacklist(self, blacklist, min=100, max=500, typeTag=False):
224 num = None
225 while num in blacklist or num is None:
226 num = random.randrange(int(min), int(max))
227 if typeTag:
228 return num
229 else:
230 return str(num)
231
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700232 def get_dynamic_resources(self, inputfile, resource):
233 resourceNames = []
234 names = {}
235 dnames = []
236 with open(inputfile, 'r') as f:
237 contents = yaml.load(f)
238 resources = contents[resource]
239 for i in resources:
240 resourceNames.append(i["name"])
241 for i in resourceNames:
242 names['name']=i
243 dnames.append(names.copy())
244 return dnames
245
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700246'''
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700247#Test
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800248dict_list = {
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700249 "humanReadableName": "cordSubscriber-17",
250 "id": 17,
251 "features": {
252 "uplink_speed": 1000000000,
253 "downlink_speed": 1000000000,
254 "status": "enabled"
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800255 },
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700256 "identity": {
257 "account_num": "20",
258 "name": "My House"
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800259 },
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700260 "related": {}
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800261 }
262input_dict = {
263 "s_tag" : "111",
264 "c_tag" : "222",
265 "subscriber" : ""
266 }
267new_value = 3
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700268test = utils()
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700269#data=test.jsonToList("Subscribers.json","SubscriberInfo")
270#print test.jsonToList("Subscribers.json","SubscriberInfo")
271#print "index 1...",test.listToDict(data,1)
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700272#result = test.getDictFromListOfDict(dict_list,"email",21)
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800273#result = test.getFieldValueFromDict(dict_list,"id")
274#result = test.getDictFromListOfDict(dict_list,"account_num",21)
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800275#result = test.setFieldValueInDict(input_dict,"subscriber",new_value)
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700276result = test.getAllFieldValues(list1,"instance_name")
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700277print "finalllllll result....", result
278'''