Added generic functions in utils libraries, new test for SynchronizerAPI

Change-Id: If9ba83fe25ffdf79476393874f7d3ebdf0fd28cd
diff --git a/src/test/cord-api/Framework/restApi.py b/src/test/cord-api/Framework/restApi.py
index edaa61f..dd8b7ad 100644
--- a/src/test/cord-api/Framework/restApi.py
+++ b/src/test/cord-api/Framework/restApi.py
@@ -88,12 +88,13 @@
     def ApiPost(self, key, jsonData):
         url = self.getURL(key)
         data = json.dumps(jsonData)
+        print "url, data..", url, data
         resp = requests.post(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password))
         passed = self.checkResult(resp, requests.codes.created)
         return passed
 
     def ApiGet(self, key, urlSuffix=""):
-        url = self.getURL(key) + urlSuffix
+        url = self.getURL(key) + str(urlSuffix)
         resp = requests.get(url, auth=(self.user, self.password))
         passed = self.checkResult(resp, requests.codes.ok)
         if not passed:
@@ -102,14 +103,15 @@
             return resp.json()
 
     def ApiPut(self, key, jsonData, urlSuffix=""):
-        url = self.getURL(key) + urlSuffix + "/"
+        print "urlSuffix....",type(urlSuffix)
+        url = self.getURL(key) + str(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)
         return passed
 
     def ApiDelete(self, key, urlSuffix=""):
-        url = self.getURL(key) + urlSuffix
+        url = self.getURL(key) + str(urlSuffix)
         resp = requests.delete(url, auth=(self.user, self.password))
         passed = self.checkResult(resp, requests.codes.no_content)
         return passed
@@ -132,8 +134,11 @@
 '''
 '''
 test = restApi()
-key = "TENANT_SUBSCRIBER"
-#jsonGetData = test.ApiGet(key,"71")
-jsonResponse = test.ApiPut(key,{"identity":{"name":"My House 22"}},"71")
+key = "UTILS_SYNCHRONIZER"
+#key = "TENANT_SUBSCRIBER"
+jsonGetData = test.ApiGet(key)
+#jsonResponse = test.ApiPut(key,{"identity":{"name":"My House 22"}},"71")
+#jsonResponse = test.ApiPost(key,{"name":"test-2"})
+jsonResponse = test.ApiPut(key,{"name":"test1-changed"},"9")
 print "========="
 '''
diff --git a/src/test/cord-api/Framework/utils/utils.py b/src/test/cord-api/Framework/utils/utils.py
index 292cbf0..34de843 100644
--- a/src/test/cord-api/Framework/utils/utils.py
+++ b/src/test/cord-api/Framework/utils/utils.py
@@ -49,9 +49,104 @@
 	        print "Additional items"
         return True
 
+    '''
+    @method search_dictionary
+    @Description: Searches for a key in the provided nested dictionary
+    @params: input_dict = dictionary to be searched
+             search_key = name of the key to be searched for
+    returns two values: search_key value and status of the search.
+             True if found (False when not found)
+
+    '''
+    def search_dictionary(self,input_dict, search_key):
+        input_keys = input_dict.keys()
+        key_value = ''
+        found = False
+        for key in input_keys:
+            if key == search_key:
+               key_value = input_dict[key]
+               found = True
+               break
+            elif type(input_dict[key]) == dict:
+                 key_value, found = self.search_dictionary(input_dict[key],search_key)
+                 if found == True:
+                    break
+        return key_value,found
+    '''
+    @method getDictFromGetJsonList
+    @Description: Searches for the dictionary in the provided list of dictionaries
+                  that matches the value of the key provided
+    @params : List of dictionaries(getResponse Data from the URL),
+             SearchKey - Key that needs to be searched for (ex: account_num)
+             searchKeyValue - Value of the searchKey (ex: 21)
+    @Returns: Dictionary returned when match found for searchKey with the corresponding
+             searchKeyValue provided
+    '''
+
+    def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue):
+        return_dict = {}
+        result = ''
+        for data in getJsonDataList:
+            return_dict = {}
+            found = False
+            input_keys = data.keys()
+            for key in input_keys:
+                if key == searchKey and str(data[key]) == str(searchKeyValue):
+                   found = True
+                   return_dict = data
+                   break
+                elif type(data[key]) == dict:
+                     result, found = self.search_dictionary(data[key],searchKey)
+                     if found == True and str(result) == str(searchKeyValue):
+                        return_dict = data
+                        break
+                elif type(data[key]) == list:
+                     for item in data[key]:
+                         if isinstance(item, dict):
+                            result, found = self.search_dictionary(data[key], searchKey)
+                            if found == True and str(result) == str(searchKeyValue):
+                               return_dict = data
+                               break
+            if return_dict:
+               break
+        return return_dict
+
+
+    '''
+    @method getFieldValueFromDict
+    @params : search_dict - Dictionary to be searched
+             field - Key to be searched for (ex: account_num)
+    @Returns: Returns the value of the Key that was provided
+    '''
+    def getFieldValueFromDict(self,search_dict, field):
+        fields_found = []
+        results = ''
+        found = False
+        input_keys = search_dict.keys()
+        for key in input_keys:
+            if key == field:
+               results = search_dict[key]
+               found = True
+               break
+            elif type(search_dict[key]) == dict:
+                 results, found = self.search_dictionary(search_dict[key],field)
+                 if found == True:
+                    break
+            elif type(search_dict[key]) == list:
+                 for item in search_dict[key]:
+                     if isinstance(item, dict):
+                        results, found = self.search_dictionary(item, field)
+                        if found == True:
+                           break
+
+        return results
+
+'''
 #Test
-#test = utils()
+test = utils()
 #data=test.jsonToList("Subscribers.json","SubscriberInfo")
 #print  test.jsonToList("Subscribers.json","SubscriberInfo")
 #print "index 1...",test.listToDict(data,1)
-
+result = test.getDictFromListOfDict(dict_list,"account_num",21)
+print "finalllllll result....", result
+'''
diff --git a/src/test/cord-api/Properties/RestApiProperties.py b/src/test/cord-api/Properties/RestApiProperties.py
index 4148f2b..143f65a 100644
--- a/src/test/cord-api/Properties/RestApiProperties.py
+++ b/src/test/cord-api/Properties/RestApiProperties.py
@@ -4,3 +4,4 @@
 USER = 'padmin@vicci.org'
 PASSWD = 'letmein'
 TENANT_SUBSCRIBER = '/api/tenant/cord/subscriber/'
+UTILS_SYNCHRONIZER = '/api/utility/synchronizer/'
diff --git a/src/test/cord-api/Tests/SubscriberTest.txt b/src/test/cord-api/Tests/SubscriberTest.txt
index ea06113..4df3faf 100644
--- a/src/test/cord-api/Tests/SubscriberTest.txt
+++ b/src/test/cord-api/Tests/SubscriberTest.txt
@@ -77,8 +77,8 @@
     Log    ${json_result}
     ${subscriberList}=    Get Variable Value    ${slist}
     ${subscriberDict}=    utils.listToDict    ${subscriberList}    ${listIndex}
-    ${AccountNum}=    restApi.getAccountNum    ${subscriberDict}
-    ${getJsonDict}=    restApi.getJsonDictOfAcctNum    ${json_result}    ${AccountNum}
+    ${AccountNum}=    utils.getFieldValueFromDict    ${subscriberDict}    account_num
+    ${getJsonDict}=    utils.getDictFromListOfDict    ${json_result}    account_num    ${AccountNum}
     ${test_result}=    utils.compare_dict    ${subscriberDict}    ${getJsonDict}
     Should Be True    ${test_result}
 
@@ -87,8 +87,9 @@
     ${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}
+    ${AcctNum}=    utils.getFieldValueFromDict    ${putSubscriberDict}    account_num
+    ${subscriberDict}=    utils.getDictFromListofDict    ${get_result}    account_num    ${AcctNum}
+    ${subscriberID}=    utils.getFieldValueFromDict    ${subscriberDict}    id
     ${api_result}=    restApi.ApiPut    TENANT_SUBSCRIBER    ${putSubscriberDict}    ${subscriberID}
     Should Be True    ${api_result}
     ${getResultAfterPut}=    restApi.ApiGet    TENANT_SUBSCRIBER    ${subscriberID}
@@ -100,7 +101,11 @@
     ${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}
+    ${AcctNum}=    utils.getFieldValueFromDict    ${subscriberDict}    account_num
+    Log    ${AcctNum}
+    ${subscriberDict}=    utils.getDictFromListofDict    ${json_result}    account_num    ${AcctNum}
+    Log    ${subscriberDict}
+    ${subscriberId}=    utils.getFieldValueFromDict    ${subscriberDict}    id
+    Log    ${subscriberId}
     ${test_result}=    restApi.ApiDelete    TENANT_SUBSCRIBER    ${subscriberId}
     Should Be True    ${test_result}
diff --git a/src/test/cord-api/Tests/UtilsSynchronizer.txt b/src/test/cord-api/Tests/UtilsSynchronizer.txt
new file mode 100644
index 0000000..ab158ea
--- /dev/null
+++ b/src/test/cord-api/Tests/UtilsSynchronizer.txt
@@ -0,0 +1,84 @@
+*** Settings ***
+Documentation     Test suite for Utility Synchronizer API
+Suite Setup       Read InputFile
+Test Template     Verify Utility Synchronizer 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/UtilsSynchronizer.json
+
+*** Test Cases ***    TYPE        LISTINDEX
+Test Add Synchronizer-1
+                      CREATE      0
+
+Test Get Synchronizer-1
+                      RETRIEVE    0
+
+Test Delete Synchronizer-1
+                      DELETE      0
+
+Test Add Synchronizer-2
+                      CREATE      1
+
+Test Get Synchronizer-2
+                      RETRIEVE    1
+
+Test Delete Synchronizer-2
+                      DELETE      1
+
+Test Add Synchronizer-3
+                      CREATE      2
+
+Test Get Synchronizer-3
+                      RETRIEVE    2
+
+Test Delete Synchronizer-3
+                      DELETE      2
+
+*** Keywords ***
+Read InputFile
+    ${syncList} =    utils.jsonToList    ${PATHFILE}    SynchronizerInfo
+    Set Suite Variable    ${sList}    ${syncList}
+
+Verify Utility Synchronizer functionality
+    [Arguments]    ${type}    ${listIndex}
+    Run Keyword If    "${type}" == "CREATE"    Test Post Utils Synchronizer API    ${listIndex}
+    Run Keyword If    "${type}" == "RETRIEVE"    Test Get Utils Synchronizer API    ${listIndex}
+    Run Keyword If    "${type}" == "DELETE"    Test Delete Utils Synchronizer API    ${listIndex}
+
+Test Post Utils Synchronizer API
+    [Arguments]    ${listIndex}
+    ${syncList} =    Get Variable Value    ${sList}
+    ${syncDict}=    utils.listToDict    ${syncList}    ${listIndex}
+    ${api_result}=    restApi.ApiPost    UTILS_SYNCHRONIZER    ${syncDict}
+    Should Be True    ${api_result}
+
+Test Get Utils Synchronizer API
+    [Arguments]    ${listIndex}
+    ${json_result}=    restApi.ApiGet    UTILS_SYNCHRONIZER
+    Log    ${json_result}
+    ${syncList}=    Get Variable Value    ${sList}
+    ${syncDict}=    utils.listToDict    ${syncList}    ${listIndex}
+    ${syncName}=    utils.getFieldValueFromDict    ${syncDict}    name
+    ${getJsonDict}=    utils.getDictFromListOfDict    ${json_result}    name    ${syncName}
+    ${test_result}=    utils.compare_dict    ${syncDict}    ${getJsonDict}
+    Should Be True    ${json_result}
+
+Test Delete Utils Synchronizer API
+    [Arguments]    ${listIndex}
+    ${json_getresult}=    restApi.ApiGet    UTILS_SYNCHRONIZER
+    ${syncList}=    Get Variable Value    ${sList}
+    ${syncDict}=    utils.listToDict    ${syncList}    ${listIndex}
+    ${syncName}=    utils.getFieldValueFromDict    ${syncDict}    name
+    ${getSyncDict}=    utils.getDictFromListofDict    ${json_getresult}    name    ${syncName}
+    ${syncID}=    utils.getFieldValueFromDict    ${getSyncDict}    id
+    ${test_result}=    restApi.ApiDelete    UTILS_SYNCHRONIZER    ${syncID}
+    Should be True    ${test_result}
diff --git a/src/test/cord-api/Tests/data/UtilsSynchronizer.json b/src/test/cord-api/Tests/data/UtilsSynchronizer.json
new file mode 100644
index 0000000..b37cb90
--- /dev/null
+++ b/src/test/cord-api/Tests/data/UtilsSynchronizer.json
@@ -0,0 +1,13 @@
+{
+    "SynchronizerInfo" : [
+        {
+            "name": "vsg"
+        },
+        {
+            "name": "sample-service1"
+        },
+        {
+            "name": "sample-service2"
+        }
+  ]
+}