Subscriber API Framework and Robot testcase files

Change-Id: Ie46904ce417ee43d1fc4b2a7c188bba74e0ef030
diff --git a/src/test/cord-api/Framework/restApi.py b/src/test/cord-api/Framework/restApi.py
index 86ca3ab..edaa61f 100644
--- a/src/test/cord-api/Framework/restApi.py
+++ b/src/test/cord-api/Framework/restApi.py
@@ -1,9 +1,10 @@
 #!/usr/bin/env python
-import requests, json, sys, time
-sys.path.append('utils')
+import requests, json, os, sys, time
+#sys.path.append('common-utils')
+sys.path.append(os.path.join(sys.path[0],'utils'))
 from readProperties import readProperties
 
-class RestApi(object):
+class restApi(object):
     '''
     Functions for testing CORD API with POST, GET, PUT, DELETE method
     '''
@@ -42,8 +43,16 @@
         else:
             print "Test failed: " + str(resp.status_code) + ": " + resp.text
             return False
+    '''
+    @method getAccountNum
+    @Returns AccountNumber for the subscriber
+    @params: jsonData is Dictionary
+    '''
+    def getAccountNum(self, jsonData):
+        print type(str(jsonData['identity']['account_num']))
+        return jsonData['identity']['account_num']
 
-    def getSubscriberIdFromAccountNum(self, jsonDataList, accountNum):
+    def getSubscriberId(self, jsonDataList, accountNum):
         '''
         Search in each json data in the given list to find and return the
         subscriber id that corresponds to the given account number.
@@ -55,10 +64,26 @@
                 if jsonData["identity"]["account_num"] == str(accountNum):
                     subscriberId = jsonData["id"]
                     break
-            return subscriberId
+            return str(subscriberId)
         except KeyError:
             print "Something wrong with the json data provided: ", jsonData
             return -1
+    '''
+     Retrieve the correct jsonDict from the List of json objects returned
+     from Get Reponse
+     Account Number is the one used to post "Data"
+    '''
+    def getJsonDictOfAcctNum(self, getResponseList, AccountNum):
+        getJsonDict = {}
+        try:
+            for data in getResponseList:
+                if data['identity']['account_num'] == AccountNum:
+                   getJsonDict = data
+                   break
+            return getJsonDict
+        except KeyError:
+            print "Could not find the related account number in Get Resonse Data"
+            return -1
 
     def ApiPost(self, key, jsonData):
         url = self.getURL(key)
@@ -77,7 +102,7 @@
             return resp.json()
 
     def ApiPut(self, key, jsonData, urlSuffix=""):
-        url = self.getURL(key) + urlSuffix
+        url = self.getURL(key) + urlSuffix + "/"
         data = json.dumps(jsonData)
         resp = requests.put(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password))
         passed = self.checkResult(resp, requests.codes.ok)
@@ -89,6 +114,8 @@
         passed = self.checkResult(resp, requests.codes.no_content)
         return passed
 
+#test
+'''
 if __name__ == '__main__':
     test = RestApi()
     key = "TENANT_SUBSCRIBER"
@@ -102,3 +129,11 @@
     result = test.ApiPut(key, {"identity":{"name":"My House 2"}}, urlSuffix)
     time.sleep(5)
     result = test.ApiDelete(key, urlSuffix)
+'''
+'''
+test = restApi()
+key = "TENANT_SUBSCRIBER"
+#jsonGetData = test.ApiGet(key,"71")
+jsonResponse = test.ApiPut(key,{"identity":{"name":"My House 22"}},"71")
+print "========="
+'''
diff --git a/src/test/cord-api/Framework/utils/utils.py b/src/test/cord-api/Framework/utils/utils.py
index 5c4efb5..292cbf0 100644
--- a/src/test/cord-api/Framework/utils/utils.py
+++ b/src/test/cord-api/Framework/utils/utils.py
@@ -21,6 +21,34 @@
         dataList = data[strListName]
         return dataList
 
+    '''
+    @method compare_dict
+    @Description: validates if contents of dict1 exists in dict2
+    @params: dict1 = input_data entered through api
+             dict2 = retrieved data from GET method
+    returns True if contents of dict1 exists in dict2
+    '''
+
+    @staticmethod
+    def compare_dict(dict1, dict2):
+        print "input_data", dict1
+        print "get data", dict2
+        if dict1 == None or dict2 == None:
+           return False
+
+        if type(dict1) is not dict or type(dict2) is not dict:
+           return False
+
+        for key1,value1 in dict1.items():
+            try:
+                if key1 in dict2:
+                   for key2, value2 in value1.items():
+                       if value2 != dict2[key1][key2]:
+                          return False
+            except:
+	        print "Additional items"
+        return True
+
 #Test
 #test = utils()
 #data=test.jsonToList("Subscribers.json","SubscriberInfo")
diff --git a/src/test/cord-api/Tests/SubscriberTest.txt b/src/test/cord-api/Tests/SubscriberTest.txt
new file mode 100644
index 0000000..ea06113
--- /dev/null
+++ b/src/test/cord-api/Tests/SubscriberTest.txt
@@ -0,0 +1,106 @@
+*** Settings ***
+Documentation     Test suite for Subscriber verification
+Suite Setup       Read InputFile
+Test Template     Verify Subscriber functionality
+Library           Collections
+Library           String
+Library           OperatingSystem
+Library           XML
+Library           RequestsLibrary
+Library           ../Framework/utils/utils.py
+Library           ../Framework/restApi.py
+
+*** Variables ***
+${USER}           admin
+${PASSWORD}       admin
+${PATHFILE}       ${CURDIR}/data/Subscriber.json
+${PATHFILE2}      ${CURDIR}/data/putSubscriber.json
+
+*** Test Cases ***    TYPE        LISTINDEX
+Test Subscriber-1     CREATE      0
+
+Test Get_Subscriber-1
+                      RETRIEVE    0
+
+Test Edit Subscriber-1
+                      PUT         0
+
+Test Delete Subscriber Data-1
+                      DELETE      0
+
+Test Subscriber-2     CREATE      1
+
+Test Get_Subscriber-2
+                      RETRIEVE    1
+
+Test Edit Subscriber Data-2
+                      PUT         1
+
+Test Delete Subscriber-2
+                      DELETE      1
+
+Test Subscriber-3     CREATE      2
+
+Test Get_Subcriber-3
+                      RETRIEVE    2
+
+Test Edit Subscriber-3
+                      PUT         2
+
+Test Delete Subscriber-3
+                      DELETE      2
+
+*** Keywords ***
+Read InputFile
+    ${subscriberList} =    utils.jsonToList    ${PATHFILE}    SubscriberInfo
+    Set Suite Variable    ${slist}    ${subscriberList}
+    ${putSubscriberList}=    utils.jsonToList    ${PATHFILE2}    SubscriberInfo
+    Set Suite Variable    ${putList}    ${putSubscriberList}
+
+Verify Subscriber functionality
+    [Arguments]    ${type}    ${listIndex}
+    Run Keyword If    "${type}" == "CREATE"    Test Post Subscriber API    ${listIndex}
+    Run Keyword If    "${type}" == "RETRIEVE"    Test Get Subscriber API    ${listIndex}
+    Run Keyword If    "${type}" == "DELETE"    Test Delete Subscriber API    ${listIndex}
+    Run Keyword If    "${type}" == "PUT"    Test Edit Subscriber API    ${listIndex}
+
+Test Post Subscriber API
+    [Arguments]    ${listIndex}
+    ${subscriberList} =    Get Variable Value    ${slist}
+    ${subscriberDict}=    utils.listToDict    ${subscriberList}    ${listIndex}
+    ${api_result}=    restApi.ApiPost    TENANT_SUBSCRIBER    ${subscriberDict}
+    Should Be True    ${api_result}
+
+Test Get Subscriber API
+    [Arguments]    ${listIndex}
+    ${json_result}=    restApi.ApiGet    TENANT_SUBSCRIBER
+    Log    ${json_result}
+    ${subscriberList}=    Get Variable Value    ${slist}
+    ${subscriberDict}=    utils.listToDict    ${subscriberList}    ${listIndex}
+    ${AccountNum}=    restApi.getAccountNum    ${subscriberDict}
+    ${getJsonDict}=    restApi.getJsonDictOfAcctNum    ${json_result}    ${AccountNum}
+    ${test_result}=    utils.compare_dict    ${subscriberDict}    ${getJsonDict}
+    Should Be True    ${test_result}
+
+Test Edit Subscriber API
+    [Arguments]    ${listIndex}
+    ${get_result}=    restApi.ApiGet    TENANT_SUBSCRIBER
+    ${putSubscriberList}=    Get Variable Value    ${putList}
+    ${putSubscriberDict}=    utils.listToDict    ${putSubscriberList}    ${listIndex}
+    ${AcctNum}=    restApi.getAccountNum    ${putSubscriberDict}
+    ${subscriberID}=    restApi.getSubscriberId    ${get_result}    ${AcctNum}
+    ${api_result}=    restApi.ApiPut    TENANT_SUBSCRIBER    ${putSubscriberDict}    ${subscriberID}
+    Should Be True    ${api_result}
+    ${getResultAfterPut}=    restApi.ApiGet    TENANT_SUBSCRIBER    ${subscriberID}
+    ${test_result}=    utils.compare_dict    ${putSubscriberDict}    ${getResultAfterPut}
+    Should Be True    ${test_result}
+
+Test Delete Subscriber API
+    [Arguments]    ${listIndex}
+    ${json_result}=    restApi.ApiGet    TENANT_SUBSCRIBER
+    ${subscriberList}=    Get Variable Value    ${slist}
+    ${subscriberDict}=    utils.listToDict    ${subscriberList}    ${listIndex}
+    ${AcctNum}=    restApi.getAccountNum    ${subscriberDict}
+    ${subscriberId}=    restApi.getSubscriberId    ${json_result}    ${AcctNum}
+    ${test_result}=    restApi.ApiDelete    TENANT_SUBSCRIBER    ${subscriberId}
+    Should Be True    ${test_result}
diff --git a/src/test/cord-api/Tests/data/Subscriber.json b/src/test/cord-api/Tests/data/Subscriber.json
index db9685f..8605026 100644
--- a/src/test/cord-api/Tests/data/Subscriber.json
+++ b/src/test/cord-api/Tests/data/Subscriber.json
@@ -1,2 +1,44 @@
-# Test data
-
+{
+    "SubscriberInfo" : [
+    {
+        "humanReadableName": "cordSubscriber-2",
+        "id": 2,
+        "features": {
+            "cdn": true,
+            "uplink_speed": 1000000000,
+            "downlink_speed": 1000000000,
+            "uverse": true,
+            "status": "enabled"
+        },
+        "identity": {
+            "account_num": "20",
+            "name": "My House"
+        }
+    },
+    {
+        "humanReadableName": "cordSubscriber-3",
+        "id": 3,
+        "features": {
+            "cdn": false,
+            "uplink_speed": 500000000,
+            "downlink_speed": 500000000,
+            "uverse": false,
+            "status": "enabled"
+        },
+        "identity": {
+            "account_num": "21",
+            "name": "My House"
+        }
+    },
+    {
+        "features": {
+            "cdn": true,
+            "uplink_speed": 500000000
+        },
+        "identity": {
+            "account_num": "22",
+            "name": "My House"
+        }
+    }
+  ]
+}
diff --git a/src/test/cord-api/Tests/data/putSubscriber.json b/src/test/cord-api/Tests/data/putSubscriber.json
new file mode 100644
index 0000000..d73e7a2
--- /dev/null
+++ b/src/test/cord-api/Tests/data/putSubscriber.json
@@ -0,0 +1,34 @@
+{
+    "SubscriberInfo" : [
+    {
+        "features": {
+            "uplink_speed": 2000000000,
+            "downlink_speed": 2000000000
+        },
+        "identity": {
+            "account_num": "20",
+            "name": "My House - edited"
+        }
+    },
+    {
+        "features": {
+            "cdn": true,
+            "downlink_speed": 100000000,
+            "status": "enabled"
+        },
+        "identity": {
+            "account_num": "21",
+            "name": "My House2 - Edited"
+        }
+    },
+    {
+        "features": {
+            "cdn": false
+        },
+        "identity": {
+            "account_num": "22",
+            "name": "My House3 - Edited"
+        }
+    }
+  ]
+}