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

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

Test Get Site-1         RETRIEVE    0

Test Edit Site-1        PUT         0

Test Delete Site-1      DELETE      0

Test Post Site-2        CREATE      1

Test Get Site-2         RETRIEVE    1

Test Edit Site-2        PUT         1

Test Delete Site-2      DELETE      1

*** Keywords ***
Read InputFile
    ${siteList}=    utils.jsonToList    ${PATHFILE}    SiteInfo
    Set Suite Variable    ${slist}    ${siteList}
    ${putSiteList}=    utils.jsonToList    ${PATHFILE2}    SiteInfo
    Set Suite Variable    ${putList}    ${putSiteList}

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

Test Post Site API
    [Arguments]    ${listIndex}
    ${siteList} =    Get Variable Value    ${slist}
    ${siteDict}=    utils.listToDict    ${siteList}    ${listIndex}
    ${api_result}=    restApi.ApiPost    CORE_SITES    ${siteDict}
    Should Be True    ${api_result}

Test Get Site API
    [Arguments]    ${listIndex}
    ${json_result}=    restApi.ApiGet   CORE_SITES
    Log    ${json_result}
    ${siteList}=    Get Variable Value    ${slist}
    ${siteDict}=    utils.listToDict    ${siteList}    ${listIndex}
    ${name}=    utils.getFieldValueFromDict    ${siteDict}   name
    ${getJsonDict}=    utils.getDictFromListOfDict    ${json_result}    name    ${name}
    ${test_result}=    utils.compare_dict    ${siteDict}    ${getJsonDict}
    Should Be True    ${test_result}

Test Edit Site API
    [Arguments]    ${listIndex}
    ${get_result}=    restApi.ApiGet    CORE_SITES
    ${putSiteList}=    Get Variable Value    ${putList}
    ${putSiteDict}=    utils.listToDict    ${putSiteList}    ${listIndex}
    ${name}=    utils.getFieldValueFromDict    ${putSiteDict}    name
    ${siteDict}=    utils.getDictFromListofDict    ${get_result}    name    ${name}
    ${siteID}=    utils.getFieldValueFromDict    ${siteDict}    id
    ${api_result}=    restApi.ApiPut    CORE_SITES    ${putSiteDict}    ${siteID}
    Should Be True    ${api_result}
    ${getResultAfterPut}=    restApi.ApiGet    CORE_SITES    ${siteID}
    ${test_result}=    utils.compare_dict    ${putSiteDict}    ${getResultAfterPut}
    Should Be True    ${test_result}

Test Delete Site API
    [Arguments]    ${listIndex}
    ${json_result}=    restApi.ApiGet    CORE_SITES
    ${siteList}=    Get Variable Value    ${slist}
    ${siteDict}=    utils.listToDict    ${siteList}    ${listIndex}
    ${name}=    utils.getFieldValueFromDict    ${siteDict}    name
    Log    ${name}
    ${siteDict}=    utils.getDictFromListofDict    ${json_result}    name  ${name}
    Log    ${siteDict}
    ${siteId}=    utils.getFieldValueFromDict    ${siteDict}    id
    Log    ${siteId}
    ${test_result}=    restApi.ApiDelete    CORE_SITES    ${siteId}
    Should Be True    ${test_result}
