*** 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           CORDRobot
Library           ImportResource  resources=CORDRobot

*** Variables ***
${USER}           admin
${PASSWORD}       admin
${PATHFILE}       ${CURDIR}/data/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}=    CORDRobot.jsonToList    ${PATHFILE}    DeploymentInfo
    Set Suite Variable    ${dlist}    ${deploymentList}
    ${putDeploymentList}=    CORDRobot.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}=    CORDRobot.listToDict    ${deploymentList}    ${listIndex}
    ${api_result}=    CORDRobot.ApiPost    CORE_DEPLOYMENTS    ${deploymentDict}
    Should Be True    ${api_result}

Test Get Deployment API
    [Arguments]    ${listIndex}
    ${json_result}=    CORDRobot.ApiGet   CORE_DEPLOYMENTS
    Log    ${json_result}
    ${deploymentList}=    Get Variable Value    ${dlist}
    ${deploymentDict}=    CORDRobot.listToDict    ${deploymentList}    ${listIndex}
    ${name}=    CORDRobot.getFieldValueFromDict    ${deploymentDict}   name
    ${getJsonDict}=    CORDRobot.getDictFromListOfDict    ${json_result}    name    ${name}
    ${test_result}=    CORDRobot.compare_dict    ${deploymentDict}    ${getJsonDict}
    Should Be True    ${test_result}

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

Test Delete Deployment API
    [Arguments]    ${listIndex}
    ${json_result}=    CORDRobot.ApiGet    CORE_DEPLOYMENTS
    ${deploymentList}=    Get Variable Value    ${dlist}
    ${deploymentDict}=    CORDRobot.listToDict    ${deploymentList}    ${listIndex}
    ${name}=    CORDRobot.getFieldValueFromDict    ${deploymentDict}    name
    Log    ${name}
    ${deploymentDict}=    CORDRobot.getDictFromListofDict    ${json_result}    name  ${name}
    Log    ${deploymentDict}
    ${deploymentId}=    CORDRobot.getFieldValueFromDict    ${deploymentDict}    id
    Log    ${deploymentId}
    ${test_result}=    CORDRobot.ApiDelete    CORE_DEPLOYMENTS    ${deploymentId}
    Should Be True    ${test_result}
