*** Settings ***
Documentation     Test suite for Sanity Test of Instance API
Suite Setup       Read InputFile
Test Template     Verify Instance API functionality
Library           Collections
Library           String
Library           OperatingSystem
Library           XML
Library           RequestsLibrary
Library           ../Framework/utils/utils.py
Library           ../Framework/restApi.py

*** Variables ***
${PATHFILE}       ${CURDIR}/data/Ch_SanityInstance.json

*** Test Cases ***    TYPE        LISTINDEX
Test Add Instance-1
                      CREATE      0

Test Get Instance-1
                      RETRIEVE    0

Test Delete Instance-1
                      DELETE      0

Test Add Instance-2
                      CREATE      1

Test Get Instance-2
                      RETRIEVE    1

Test Delete Instance-2
                      DELETE      1

*** Keywords ***
Read InputFile
    ${instanceList} =    utils.jsonToList    ${PATHFILE}    InstanceInfo
    Set Suite Variable    ${iList}    ${instanceList}

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

Test Post Instances
    [Arguments]    ${listIndex}
    ${instanceList} =    Get Variable Value    ${iList}
    ${instanceDict}=    utils.listToDict    ${instanceList}    ${listIndex}
    ${api_result}=    restApi.ApiPost    CH_CORE_INSTANCES    ${instanceDict}
    Should Be True    ${api_result}

Test Get Instances
    [Arguments]    ${listIndex}
    ${json_result}=    restApi.ApiGet    CH_CORE_INSTANCES
    Log    ${json_result}
    ${json_result_list}=    Get From Dictionary    ${json_result}    items
    Log    ${json_result_list}
    ${instanceList}=    Get Variable Value    ${iList}
    ${inputDict}=    utils.listToDict    ${instanceList}    ${listIndex}
    ${instanceName}=    utils.getFieldValueFromDict    ${inputDict}    name
    ${getJsonDict}=    utils.getDictFromListOfDict    ${json_result_list}    name    ${instanceName}
    ${test_result}=    utils.compare_dict    ${inputDict}    ${getJsonDict}
    Should Be True    ${json_result}

Test Delete Instances
    [Arguments]    ${listIndex}
    ${json_getresult}=    restApi.ApiGet    CH_CORE_INSTANCES
    ${json_getresult_list}=    Get From Dictionary    ${json_getresult}    items
    ${instanceList}=    Get Variable Value    ${iList}
    ${instanceDict}=    utils.listToDict    ${iList}    ${listIndex}
    ${instanceName}=    utils.getFieldValueFromDict    ${instanceDict}    name
    Log    ${instanceName}
    ${getInstanceDict}=    utils.getDictFromListofDict    ${json_getresult_list}    name    ${instanceName}
    Log    ${getInstanceDict}
    ${instanceID}=    utils.getFieldValueFromDict    ${getInstanceDict}    id
    Log    ${instanceID}
    ${test_result}=    restApi.ApiChameleonDelete    CH_CORE_INSTANCES    ${instanceID}
    Should be True    ${test_result}
