*** Settings ***
Documentation     Test suite for Users API
Suite Setup       Read InputFile
Test Template     Verify User 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_Users.json

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

Test Get User-1       RETRIEVE    0

Test Delete User-1    DELETE      0

Test Add User-2       CREATE      1

Test Get User-2       RETRIEVE    1

Test Delete User-2    DELETE      1

Test Add User-3       CREATE      2

Test Get User-3       RETRIEVE    2

Test Delete User-3    DELETE      2

*** Keywords ***
Read InputFile
    ${userList} =    utils.jsonToList    ${PATHFILE}    UserInfo
    Set Suite Variable    ${uList}    ${userList}

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

Test Post Users API
    [Arguments]    ${listIndex}
    ${userList} =    Get Variable Value    ${uList}
    ${userDict}=    utils.listToDict    ${userList}    ${listIndex}
    ${api_result}=    restApi.ApiPost    CH_CORE_USERS    ${userDict}
    Should Be True    ${api_result}

Test Get Users API
    [Arguments]    ${listIndex}
    ${json_result}=    restApi.ApiGet    CH_CORE_USERS
    ${json_result_list}=    Get From Dictionary    ${json_result}    items
    Log    ${json_result_list}
    ${userList}=    Get Variable Value    ${uList}
    ${inputDict}=    utils.listToDict    ${userList}    ${listIndex}
    ${email}=    utils.getFieldValueFromDict    ${inputDict}    email
    ${getJsonDict}=    utils.getDictFromListOfDict    ${json_result_list}    email    ${email}
    ${test_result}=    utils.compare_dict    ${inputDict}    ${getJsonDict}
    Should Be True    ${json_result}

Test Delete Users API
    [Arguments]    ${listIndex}
    ${json_getresult}=    restApi.ApiGet    CH_CORE_USERS
    ${json_getresult_list}=    Get From Dictionary    ${json_getresult}    items
    ${userList}=    Get Variable Value    ${uList}
    ${userDict}=    utils.listToDict    ${uList}    ${listIndex}
    ${email}=    utils.getFieldValueFromDict    ${userDict}    email
    Log    ${email}
    ${getUserDict}=    utils.getDictFromListofDict    ${json_getresult_list}    email    ${email}
    Log    ${getUserDict}
    ${userID}=    utils.getFieldValueFromDict    ${getUserDict}    id
    Log    ${userID}
    ${test_result}=    restApi.ApiChameleonDelete    CH_CORE_USERS    ${userID}
    Should be True    ${test_result}
