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 |
| 9 | |
| 10 | class 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 24 | ''' |
| 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 Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 31 | def compare_dict(self, dict1, dict2): |
| 32 | print "input data", dict1 |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 33 | print "get data", dict2 |
| 34 | if dict1 == None or dict2 == None: |
| 35 | return False |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 36 | if type(dict1) is not dict or type(dict2) is not dict: |
| 37 | return False |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 38 | if dict1 == {}: |
| 39 | return True |
| 40 | return self.compare_dict_recursive(dict1, dict2) |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 41 | |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 42 | ''' |
| 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 48 | for key1,value1 in dict1.items(): |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 49 | 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 60 | return True |
| 61 | |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 62 | ''' |
| 63 | @method search_dictionary |
| 64 | @Description: Searches for a key in the provided nested dictionary |
| 65 | @params: input_dict = dictionary to be searched |
| 66 | search_key = name of the key to be searched for |
| 67 | returns two values: search_key value and status of the search. |
| 68 | True if found (False when not found) |
| 69 | |
| 70 | ''' |
| 71 | def search_dictionary(self,input_dict, search_key): |
| 72 | input_keys = input_dict.keys() |
| 73 | key_value = '' |
| 74 | found = False |
| 75 | for key in input_keys: |
| 76 | if key == search_key: |
| 77 | key_value = input_dict[key] |
| 78 | found = True |
| 79 | break |
| 80 | elif type(input_dict[key]) == dict: |
| 81 | key_value, found = self.search_dictionary(input_dict[key],search_key) |
| 82 | if found == True: |
| 83 | break |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 84 | elif type(input_dict[key]) == list: |
| 85 | if not input_dict[key]: |
| 86 | found = False |
| 87 | break |
| 88 | for item in input_dict[key]: |
| 89 | if isinstance(item, dict): |
| 90 | key_value, found = self.search_dictionary(item, search_key) |
| 91 | if found == True: |
| 92 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 93 | return key_value,found |
| 94 | ''' |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 95 | @method getDictFromListOfDict |
| 96 | return key_value,found |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 97 | @Description: Searches for the dictionary in the provided list of dictionaries |
| 98 | that matches the value of the key provided |
| 99 | @params : List of dictionaries(getResponse Data from the URL), |
| 100 | SearchKey - Key that needs to be searched for (ex: account_num) |
| 101 | searchKeyValue - Value of the searchKey (ex: 21) |
| 102 | @Returns: Dictionary returned when match found for searchKey with the corresponding |
| 103 | searchKeyValue provided |
| 104 | ''' |
| 105 | |
| 106 | def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue): |
| 107 | return_dict = {} |
| 108 | result = '' |
| 109 | for data in getJsonDataList: |
| 110 | return_dict = {} |
| 111 | found = False |
| 112 | input_keys = data.keys() |
| 113 | for key in input_keys: |
| 114 | if key == searchKey and str(data[key]) == str(searchKeyValue): |
| 115 | found = True |
| 116 | return_dict = data |
| 117 | break |
| 118 | elif type(data[key]) == dict: |
| 119 | result, found = self.search_dictionary(data[key],searchKey) |
| 120 | if found == True and str(result) == str(searchKeyValue): |
| 121 | return_dict = data |
| 122 | break |
| 123 | elif type(data[key]) == list: |
| 124 | for item in data[key]: |
| 125 | if isinstance(item, dict): |
| 126 | result, found = self.search_dictionary(data[key], searchKey) |
| 127 | if found == True and str(result) == str(searchKeyValue): |
| 128 | return_dict = data |
| 129 | break |
| 130 | if return_dict: |
| 131 | break |
| 132 | return return_dict |
| 133 | |
| 134 | |
| 135 | ''' |
| 136 | @method getFieldValueFromDict |
| 137 | @params : search_dict - Dictionary to be searched |
| 138 | field - Key to be searched for (ex: account_num) |
| 139 | @Returns: Returns the value of the Key that was provided |
| 140 | ''' |
| 141 | def getFieldValueFromDict(self,search_dict, field): |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 142 | results = '' |
| 143 | found = False |
| 144 | input_keys = search_dict.keys() |
| 145 | for key in input_keys: |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 146 | print "key...", key |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 147 | if key == field: |
| 148 | results = search_dict[key] |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 149 | if not results: |
| 150 | found = True |
| 151 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 152 | elif type(search_dict[key]) == dict: |
| 153 | results, found = self.search_dictionary(search_dict[key],field) |
| 154 | if found == True: |
| 155 | break |
| 156 | elif type(search_dict[key]) == list: |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 157 | if not search_dict[key]: |
| 158 | found = False |
| 159 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 160 | for item in search_dict[key]: |
| 161 | if isinstance(item, dict): |
| 162 | results, found = self.search_dictionary(item, field) |
| 163 | if found == True: |
| 164 | break |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 165 | if results: |
| 166 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 167 | |
| 168 | return results |
| 169 | |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 170 | def setFieldValueInDict(self,input_dict,field,field_value): |
| 171 | input_dict[field]=field_value |
| 172 | return input_dict |
| 173 | |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame^] | 174 | ''' |
| 175 | @method getAllFieldValues |
| 176 | @params : getJsonDataDictList - List of dictionaries to be searched |
| 177 | fieldName - Key to be searched for (ex: instance_id) |
| 178 | @Returns: Returns the unique value of the Key that was provided |
| 179 | ''' |
| 180 | |
| 181 | def getAllFieldValues(self, getJsonDataDictList, fieldName): |
| 182 | value_list = [] |
| 183 | uniqValue = '' |
| 184 | uniq_list = [] |
| 185 | for data in getJsonDataDictList: |
| 186 | fieldValue = '' |
| 187 | fieldValue = self.getFieldValueFromDict(data, fieldName) |
| 188 | value_list.append(fieldValue) |
| 189 | uniq_list = sorted(set(value_list)) |
| 190 | if len(uniq_list) == 1: |
| 191 | uniqValue = uniq_list[0] |
| 192 | else: |
| 193 | print "list of values found for ", fieldName, ";", uniq_list |
| 194 | return fieldValue |
| 195 | |
| 196 | |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 197 | ''' |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 198 | #Test |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 199 | dict_list = { |
| 200 | "humanReadableName": "cordSubscriber-17", |
| 201 | "id": 17, |
| 202 | "features": { |
| 203 | "uplink_speed": 1000000000, |
| 204 | "downlink_speed": 1000000000, |
| 205 | "status": "enabled" |
| 206 | }, |
| 207 | "identity": { |
| 208 | "account_num": "20", |
| 209 | "name": "My House" |
| 210 | }, |
| 211 | "related": {} |
| 212 | } |
| 213 | input_dict = { |
| 214 | "s_tag" : "111", |
| 215 | "c_tag" : "222", |
| 216 | "subscriber" : "" |
| 217 | } |
| 218 | new_value = 3 |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 219 | test = utils() |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 220 | #data=test.jsonToList("Subscribers.json","SubscriberInfo") |
| 221 | #print test.jsonToList("Subscribers.json","SubscriberInfo") |
| 222 | #print "index 1...",test.listToDict(data,1) |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 223 | #result = test.getDictFromListOfDict(dict_list,"email",21) |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 224 | #result = test.getFieldValueFromDict(dict_list,"id") |
| 225 | #result = test.getDictFromListOfDict(dict_list,"account_num",21) |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame^] | 226 | #result = test.setFieldValueInDict(input_dict,"subscriber",new_value) |
| 227 | result = test.getAllFieldValues(list1,"instance_name") |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 228 | print "finalllllll result....", result |
| 229 | ''' |