You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | import requests, json, sys, time |
| 3 | sys.path.append('utils') |
| 4 | from readProperties import readProperties |
| 5 | |
| 6 | class RestApi(object): |
| 7 | ''' |
| 8 | Functions for testing CORD API with POST, GET, PUT, DELETE method |
| 9 | ''' |
| 10 | def __init__(self): |
| 11 | self.rp = readProperties("../Properties/RestApiProperties.py") |
| 12 | self.controllerIP = self.getValueFromProperties("SERVER_IP") |
| 13 | self.controllerPort = self.getValueFromProperties("SERVER_PORT") |
| 14 | self.user = self.getValueFromProperties("USER") |
| 15 | self.password = self.getValueFromProperties("PASSWD") |
| 16 | self.jsonHeader = {'Content-Type': 'application/json'} |
| 17 | |
| 18 | def getValueFromProperties(self, key): |
| 19 | ''' |
| 20 | Get and return values from properties file |
| 21 | ''' |
| 22 | rawValue = self.rp.getValueProperties(key) |
| 23 | value = rawValue.replace("'","") |
| 24 | return value |
| 25 | |
| 26 | def getURL(self, key): |
| 27 | ''' |
| 28 | Get REST API suffix from key and return the full URL |
| 29 | ''' |
| 30 | urlSuffix = self.getValueFromProperties(key) |
| 31 | url = "http://" + self.controllerIP + ":" + self.controllerPort + urlSuffix |
| 32 | return url |
| 33 | |
| 34 | def checkResult(self, resp, expectedStatus): |
| 35 | ''' |
| 36 | Check if the status code in resp equals to the expected number. |
| 37 | Return True or False based on the check result. |
| 38 | ''' |
| 39 | if resp.status_code == expectedStatus: |
| 40 | print "Test passed: " + str(resp.status_code) + ": " + resp.text |
| 41 | return True |
| 42 | else: |
| 43 | print "Test failed: " + str(resp.status_code) + ": " + resp.text |
| 44 | return False |
| 45 | |
| 46 | def getSubscriberIdFromAccountNum(self, jsonDataList, accountNum): |
| 47 | ''' |
| 48 | Search in each json data in the given list to find and return the |
| 49 | subscriber id that corresponds to the given account number. |
| 50 | ''' |
| 51 | # Here we assume subscriber id starts from 1 |
| 52 | subscriberId = 0 |
| 53 | try: |
| 54 | for jsonData in jsonDataList: |
| 55 | if jsonData["identity"]["account_num"] == str(accountNum): |
| 56 | subscriberId = jsonData["id"] |
| 57 | break |
| 58 | return subscriberId |
| 59 | except KeyError: |
| 60 | print "Something wrong with the json data provided: ", jsonData |
| 61 | return -1 |
| 62 | |
| 63 | def ApiPost(self, key, jsonData): |
| 64 | url = self.getURL(key) |
| 65 | data = json.dumps(jsonData) |
| 66 | resp = requests.post(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password)) |
| 67 | passed = self.checkResult(resp, requests.codes.created) |
| 68 | return passed |
| 69 | |
| 70 | def ApiGet(self, key, urlSuffix=""): |
| 71 | url = self.getURL(key) + urlSuffix |
| 72 | resp = requests.get(url, auth=(self.user, self.password)) |
| 73 | passed = self.checkResult(resp, requests.codes.ok) |
| 74 | if not passed: |
| 75 | return None |
| 76 | else: |
| 77 | return resp.json() |
| 78 | |
| 79 | def ApiPut(self, key, jsonData, urlSuffix=""): |
| 80 | url = self.getURL(key) + urlSuffix |
| 81 | data = json.dumps(jsonData) |
| 82 | resp = requests.put(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password)) |
| 83 | passed = self.checkResult(resp, requests.codes.ok) |
| 84 | return passed |
| 85 | |
| 86 | def ApiDelete(self, key, urlSuffix=""): |
| 87 | url = self.getURL(key) + urlSuffix |
| 88 | resp = requests.delete(url, auth=(self.user, self.password)) |
| 89 | passed = self.checkResult(resp, requests.codes.no_content) |
| 90 | return passed |
| 91 | |
| 92 | if __name__ == '__main__': |
| 93 | test = RestApi() |
| 94 | key = "TENANT_SUBSCRIBER" |
| 95 | account_num = 5 |
| 96 | result = test.ApiPost(key, {"identity":{"account_num":str(account_num)}}) |
| 97 | time.sleep(5) |
| 98 | result = test.ApiGet(key) |
| 99 | subId = test.getSubscriberIdFromAccountNum(result, account_num) |
| 100 | urlSuffix = str(subId) + "/" |
| 101 | time.sleep(5) |
| 102 | result = test.ApiPut(key, {"identity":{"name":"My House 2"}}, urlSuffix) |
| 103 | time.sleep(5) |
| 104 | result = test.ApiDelete(key, urlSuffix) |