blob: edaa61f6e527bf16525e61d67859c7b2eb5fe34c [file] [log] [blame]
You Wang2e97a012016-10-21 16:09:52 -07001#!/usr/bin/env python
Suchitra.Vemuri85220062016-10-25 10:44:11 -07002import requests, json, os, sys, time
3#sys.path.append('common-utils')
4sys.path.append(os.path.join(sys.path[0],'utils'))
You Wang2e97a012016-10-21 16:09:52 -07005from readProperties import readProperties
6
Suchitra.Vemuri85220062016-10-25 10:44:11 -07007class restApi(object):
You Wang2e97a012016-10-21 16:09:52 -07008 '''
9 Functions for testing CORD API with POST, GET, PUT, DELETE method
10 '''
11 def __init__(self):
12 self.rp = readProperties("../Properties/RestApiProperties.py")
13 self.controllerIP = self.getValueFromProperties("SERVER_IP")
14 self.controllerPort = self.getValueFromProperties("SERVER_PORT")
15 self.user = self.getValueFromProperties("USER")
16 self.password = self.getValueFromProperties("PASSWD")
17 self.jsonHeader = {'Content-Type': 'application/json'}
18
19 def getValueFromProperties(self, key):
20 '''
21 Get and return values from properties file
22 '''
23 rawValue = self.rp.getValueProperties(key)
24 value = rawValue.replace("'","")
25 return value
26
27 def getURL(self, key):
28 '''
29 Get REST API suffix from key and return the full URL
30 '''
31 urlSuffix = self.getValueFromProperties(key)
32 url = "http://" + self.controllerIP + ":" + self.controllerPort + urlSuffix
33 return url
34
35 def checkResult(self, resp, expectedStatus):
36 '''
37 Check if the status code in resp equals to the expected number.
38 Return True or False based on the check result.
39 '''
40 if resp.status_code == expectedStatus:
41 print "Test passed: " + str(resp.status_code) + ": " + resp.text
42 return True
43 else:
44 print "Test failed: " + str(resp.status_code) + ": " + resp.text
45 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070046 '''
47 @method getAccountNum
48 @Returns AccountNumber for the subscriber
49 @params: jsonData is Dictionary
50 '''
51 def getAccountNum(self, jsonData):
52 print type(str(jsonData['identity']['account_num']))
53 return jsonData['identity']['account_num']
You Wang2e97a012016-10-21 16:09:52 -070054
Suchitra.Vemuri85220062016-10-25 10:44:11 -070055 def getSubscriberId(self, jsonDataList, accountNum):
You Wang2e97a012016-10-21 16:09:52 -070056 '''
57 Search in each json data in the given list to find and return the
58 subscriber id that corresponds to the given account number.
59 '''
60 # Here we assume subscriber id starts from 1
61 subscriberId = 0
62 try:
63 for jsonData in jsonDataList:
64 if jsonData["identity"]["account_num"] == str(accountNum):
65 subscriberId = jsonData["id"]
66 break
Suchitra.Vemuri85220062016-10-25 10:44:11 -070067 return str(subscriberId)
You Wang2e97a012016-10-21 16:09:52 -070068 except KeyError:
69 print "Something wrong with the json data provided: ", jsonData
70 return -1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070071 '''
72 Retrieve the correct jsonDict from the List of json objects returned
73 from Get Reponse
74 Account Number is the one used to post "Data"
75 '''
76 def getJsonDictOfAcctNum(self, getResponseList, AccountNum):
77 getJsonDict = {}
78 try:
79 for data in getResponseList:
80 if data['identity']['account_num'] == AccountNum:
81 getJsonDict = data
82 break
83 return getJsonDict
84 except KeyError:
85 print "Could not find the related account number in Get Resonse Data"
86 return -1
You Wang2e97a012016-10-21 16:09:52 -070087
88 def ApiPost(self, key, jsonData):
89 url = self.getURL(key)
90 data = json.dumps(jsonData)
91 resp = requests.post(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password))
92 passed = self.checkResult(resp, requests.codes.created)
93 return passed
94
95 def ApiGet(self, key, urlSuffix=""):
96 url = self.getURL(key) + urlSuffix
97 resp = requests.get(url, auth=(self.user, self.password))
98 passed = self.checkResult(resp, requests.codes.ok)
99 if not passed:
100 return None
101 else:
102 return resp.json()
103
104 def ApiPut(self, key, jsonData, urlSuffix=""):
Suchitra.Vemuri85220062016-10-25 10:44:11 -0700105 url = self.getURL(key) + urlSuffix + "/"
You Wang2e97a012016-10-21 16:09:52 -0700106 data = json.dumps(jsonData)
107 resp = requests.put(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password))
108 passed = self.checkResult(resp, requests.codes.ok)
109 return passed
110
111 def ApiDelete(self, key, urlSuffix=""):
112 url = self.getURL(key) + urlSuffix
113 resp = requests.delete(url, auth=(self.user, self.password))
114 passed = self.checkResult(resp, requests.codes.no_content)
115 return passed
116
Suchitra.Vemuri85220062016-10-25 10:44:11 -0700117#test
118'''
You Wang2e97a012016-10-21 16:09:52 -0700119if __name__ == '__main__':
120 test = RestApi()
121 key = "TENANT_SUBSCRIBER"
122 account_num = 5
123 result = test.ApiPost(key, {"identity":{"account_num":str(account_num)}})
124 time.sleep(5)
125 result = test.ApiGet(key)
126 subId = test.getSubscriberIdFromAccountNum(result, account_num)
127 urlSuffix = str(subId) + "/"
128 time.sleep(5)
129 result = test.ApiPut(key, {"identity":{"name":"My House 2"}}, urlSuffix)
130 time.sleep(5)
131 result = test.ApiDelete(key, urlSuffix)
Suchitra.Vemuri85220062016-10-25 10:44:11 -0700132'''
133'''
134test = restApi()
135key = "TENANT_SUBSCRIBER"
136#jsonGetData = test.ApiGet(key,"71")
137jsonResponse = test.ApiPut(key,{"identity":{"name":"My House 22"}},"71")
138print "========="
139'''