blob: 04c94f9b93fee312d5f66d5810fc3201cd0db63e [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
9
10class 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.Vemuri85220062016-10-25 10:44:11 -070024 '''
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 Wang507c4562016-11-23 13:36:09 -080031 def compare_dict(self, dict1, dict2):
32 print "input data", dict1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070033 print "get data", dict2
34 if dict1 == None or dict2 == None:
35 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070036 if type(dict1) is not dict or type(dict2) is not dict:
37 return False
You Wang507c4562016-11-23 13:36:09 -080038 if dict1 == {}:
39 return True
40 return self.compare_dict_recursive(dict1, dict2)
Suchitra.Vemuri85220062016-10-25 10:44:11 -070041
You Wang507c4562016-11-23 13:36:09 -080042 '''
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.Vemuri85220062016-10-25 10:44:11 -070048 for key1,value1 in dict1.items():
You Wang507c4562016-11-23 13:36:09 -080049 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.Vemuri85220062016-10-25 10:44:11 -070060 return True
61
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070062 '''
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.Vemuri8be18802016-11-16 16:59:54 -080084 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.Vemuri32e03c22016-11-03 11:57:53 -070093 return key_value,found
94 '''
Suchitra.Vemuri8be18802016-11-16 16:59:54 -080095 @method getDictFromListOfDict
96 return key_value,found
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070097 @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.Vemurid2035342016-11-22 17:44:40 -0800142 print "search_dict", search_dict, "field...", field
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700143 results = ''
144 found = False
145 input_keys = search_dict.keys()
146 for key in input_keys:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800147 print "key...", key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700148 if key == field:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800149 print "entered if..."
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700150 results = search_dict[key]
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800151 print "results...", results
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800152 if not results:
153 found = True
154 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700155 elif type(search_dict[key]) == dict:
156 results, found = self.search_dictionary(search_dict[key],field)
157 if found == True:
158 break
159 elif type(search_dict[key]) == list:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800160 if not search_dict[key]:
161 found = False
162 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700163 for item in search_dict[key]:
164 if isinstance(item, dict):
165 results, found = self.search_dictionary(item, field)
166 if found == True:
167 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800168 if results:
169 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700170
171 return results
172
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800173 def setFieldValueInDict(self,input_dict,field,field_value):
174 input_dict[field]=field_value
175 return input_dict
176
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700177'''
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700178#Test
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800179dict_list = {
180 "humanReadableName": "cordSubscriber-17",
181 "id": 17,
182 "features": {
183 "uplink_speed": 1000000000,
184 "downlink_speed": 1000000000,
185 "status": "enabled"
186 },
187 "identity": {
188 "account_num": "20",
189 "name": "My House"
190 },
191 "related": {}
192 }
193input_dict = {
194 "s_tag" : "111",
195 "c_tag" : "222",
196 "subscriber" : ""
197 }
198new_value = 3
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700199test = utils()
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700200#data=test.jsonToList("Subscribers.json","SubscriberInfo")
201#print test.jsonToList("Subscribers.json","SubscriberInfo")
202#print "index 1...",test.listToDict(data,1)
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800203#result = test.getDictFromListOfDict(dict_list,"email",21)
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800204#result = test.getFieldValueFromDict(dict_list,"id")
205#result = test.getDictFromListOfDict(dict_list,"account_num",21)
206result = test.setFieldValueInDict(input_dict,"subscriber",new_value)
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700207print "finalllllll result....", result
208'''