*** Settings ***
Documentation     Test suite for Deployment verification
Suite Setup       Read InputFile
Test Template     Verify Deployment 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/Ch_Deployment.json
${PATHFILE2}      ${CURDIR}/data/putDeployment.json

*** Test Cases ***    TYPE                       LISTINDEX
Test Post Deployment-1
                      CREATE                     0

Test Get Deployment-1
                      RETRIEVE                   0
                      #Test Edit Deployment-1    PUT          0

Test Delete Deployment-1
                      DELETE                     0

Test Post Deployment-2
                      CREATE                     1

Test Get Deployment-2
                      RETRIEVE                   1
                      #Test Edit Deployment-2    PUT          1

Test Delete Deployment-2
                      DELETE                     1

*** Keywords ***
Read InputFile
    ${deploymentList}=    utils.jsonToList    ${PATHFILE}    DeploymentInfo
    Set Suite Variable    ${dlist}    ${deploymentList}
    ${putDeploymentList}=    utils.jsonToList    ${PATHFILE2}    DeploymentInfo
    Set Suite Variable    ${putList}    ${putDeploymentList}

Verify Deployment functionality
    [Arguments]    ${type}    ${listIndex}
    Run Keyword If    "${type}" == "CREATE"    Test Post Deployment API    ${listIndex}
    Run Keyword If    "${type}" == "RETRIEVE"    Test Get Deployment API    ${listIndex}
    Run Keyword If    "${type}" == "PUT"    Test Edit Deployment API    ${listIndex}
    Run Keyword If    "${type}" == "DELETE"    Test Delete Deployment API    ${listIndex}

Test Post Deployment API
    [Arguments]    ${listIndex}
    ${deploymentList} =    Get Variable Value    ${dlist}
    ${deploymentDict}=    utils.listToDict    ${deploymentList}    ${listIndex}
    ${api_result}=    restApi.ApiPost    CH_CORE_DEPLOYMENTS    ${deploymentDict}
    Should Be True    ${api_result}

Test Get Deployment API
    [Arguments]    ${listIndex}
    ${json_result}=    restApi.ApiGet    CH_CORE_DEPLOYMENTS
    ${json_result_list}=    Get From Dictionary    ${json_result}    items
    Log    ${json_result_list}
    ${deploymentList}=    Get Variable Value    ${dlist}
    ${deploymentDict}=    utils.listToDict    ${deploymentList}    ${listIndex}
    ${name}=    utils.getFieldValueFromDict    ${deploymentDict}    name
    ${getJsonDict}=    utils.getDictFromListOfDict    ${json_result_list}    name    ${name}
    ${test_result}=    utils.compare_dict    ${deploymentDict}    ${getJsonDict}
    Should Be True    ${test_result}

Test Edit Deployment API
    [Arguments]    ${listIndex}
    ${get_result}=    restApi.ApiGet    CH_CORE_DEPLOYMENTS
    ${putDeploymentList}=    Get Variable Value    ${putList}
    ${putDeploymentDict}=    utils.listToDict    ${putDeploymentList}    ${listIndex}
    ${name}=    utils.getFieldValueFromDict    ${putDeploymentDict}    name
    ${deploymentDict}=    utils.getDictFromListofDict    ${get_result}    name    ${name}
    ${deploymentID}=    utils.getFieldValueFromDict    ${deploymentDict}    id
    ${api_result}=    restApi.ApiPut    CH_CORE_DEPLOYMENTS    ${putDeploymentDict}    ${deploymentID}
    Should Be True    ${api_result}
    ${getResultAfterPut}=    restApi.ApiGet    CH_CORE_DEPLOYMENTS    ${deploymentID}
    ${test_result}=    utils.compare_dict    ${putDeploymentDict}    ${getResultAfterPut}
    Should Be True    ${test_result}

Test Delete Deployment API
    [Arguments]    ${listIndex}
    ${json_result}=    restApi.ApiGet    CH_CORE_DEPLOYMENTS
    ${json_result_list}=    Get From Dictionary    ${json_result}    items
    ${deploymentList}=    Get Variable Value    ${dlist}
    ${deploymentDict}=    utils.listToDict    ${deploymentList}    ${listIndex}
    ${name}=    utils.getFieldValueFromDict    ${deploymentDict}    name
    Log    ${name}
    ${deploymentDict}=    utils.getDictFromListofDict    ${json_result_list}    name    ${name}
    Log    ${deploymentDict}
    ${deploymentId}=    utils.getFieldValueFromDict    ${deploymentDict}    id
    Log    ${deploymentId}
    ${test_result}=    restApi.ApiChameleonDelete    CH_CORE_DEPLOYMENTS    ${deploymentId}
    Should Be True    ${test_result}
