Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 1 | import pexpect,os |
| 2 | import time |
| 3 | import json |
| 4 | import collections |
| 5 | import sys |
| 6 | import robot |
| 7 | import os.path |
| 8 | from os.path import expanduser |
Kailash Khalasi | b6e87fc | 2017-04-18 15:08:19 -0700 | [diff] [blame] | 9 | import uuid |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 10 | |
| 11 | class 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 25 | ''' |
| 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 Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 32 | def compare_dict(self, dict1, dict2): |
| 33 | print "input data", dict1 |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 34 | print "get data", dict2 |
| 35 | if dict1 == None or dict2 == None: |
| 36 | return False |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 37 | if type(dict1) is not dict or type(dict2) is not dict: |
| 38 | return False |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 39 | if dict1 == {}: |
| 40 | return True |
| 41 | return self.compare_dict_recursive(dict1, dict2) |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 42 | |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 43 | ''' |
| 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 49 | for key1,value1 in dict1.items(): |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 50 | 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 61 | return True |
| 62 | |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 63 | ''' |
You Wang | 3964e84 | 2016-12-09 12:04:32 -0800 | [diff] [blame] | 64 | @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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 84 | @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.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 105 | 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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 114 | return key_value,found |
| 115 | ''' |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 116 | @method getDictFromListOfDict |
| 117 | return key_value,found |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 118 | @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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 163 | results = '' |
| 164 | found = False |
| 165 | input_keys = search_dict.keys() |
| 166 | for key in input_keys: |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 167 | print "key...", key |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 168 | if key == field: |
| 169 | results = search_dict[key] |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 170 | if not results: |
| 171 | found = True |
| 172 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 173 | 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.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 178 | if not search_dict[key]: |
| 179 | found = False |
You Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 180 | continue |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 181 | 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.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 186 | if results: |
| 187 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 188 | |
| 189 | return results |
| 190 | |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 191 | def setFieldValueInDict(self,input_dict,field,field_value): |
| 192 | input_dict[field]=field_value |
| 193 | return input_dict |
| 194 | |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame] | 195 | ''' |
| 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 Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 200 | ''' |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame] | 201 | |
| 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 Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 216 | |
Kailash Khalasi | b6e87fc | 2017-04-18 15:08:19 -0700 | [diff] [blame] | 217 | def generate_uuid(self): |
| 218 | return uuid.uuid4() |
| 219 | |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 220 | ''' |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 221 | #Test |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 222 | dict_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 | } |
| 236 | input_dict = { |
| 237 | "s_tag" : "111", |
| 238 | "c_tag" : "222", |
| 239 | "subscriber" : "" |
| 240 | } |
| 241 | new_value = 3 |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 242 | test = utils() |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 243 | #data=test.jsonToList("Subscribers.json","SubscriberInfo") |
| 244 | #print test.jsonToList("Subscribers.json","SubscriberInfo") |
| 245 | #print "index 1...",test.listToDict(data,1) |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 246 | #result = test.getDictFromListOfDict(dict_list,"email",21) |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 247 | #result = test.getFieldValueFromDict(dict_list,"id") |
| 248 | #result = test.getDictFromListOfDict(dict_list,"account_num",21) |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame] | 249 | #result = test.setFieldValueInDict(input_dict,"subscriber",new_value) |
| 250 | result = test.getAllFieldValues(list1,"instance_name") |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 251 | print "finalllllll result....", result |
| 252 | ''' |