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

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

Test Get Service-1    RETRIEVE    0

Test Edit Service-1
                      PUT         0

Test Delete Service-1
                      DELETE      0

Test Post Service-2
                      CREATE      1

Test Get Service-2    RETRIEVE    1

Test Edit Service-2
                      PUT         1

Test Delete Service-2
                      DELETE      1

*** Keywords ***
Read InputFile
    ${serviceList}=    utils.jsonToList    ${PATHFILE}    ServiceInfo
    Set Suite Variable    ${slist}    ${serviceList}
    ${putServiceList}=    utils.jsonToList    ${PATHFILE2}    ServiceInfo
    Set Suite Variable    ${putList}    ${putServiceList}

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

Test Post Service API
    [Arguments]    ${listIndex}
    ${serviceList} =    Get Variable Value    ${slist}
    ${serviceDict}=    utils.listToDict    ${serviceList}    ${listIndex}
    ${api_result}=    restApi.ApiPost    CH_CORE_SERVICES    ${serviceDict}
    Should Be True    ${api_result}

Test Get Service API
    [Arguments]    ${listIndex}
    ${json_result}=    restApi.ApiGet    CH_CORE_SERVICES
    ${json_result_list}=    Get From Dictionary    ${json_result}    items
    Log    ${json_result_list}
    Log    ${json_result}
    ${serviceList}=    Get Variable Value    ${slist}
    ${serviceDict}=    utils.listToDict    ${serviceList}    ${listIndex}
    ${name}=    utils.getFieldValueFromDict    ${serviceDict}    name
    ${getJsonDict}=    utils.getDictFromListOfDict    ${json_result_list}    name    ${name}
    ${test_result}=    utils.compare_dict    ${serviceDict}    ${getJsonDict}
    Should Be True    ${test_result}

Test Edit Service API
    [Arguments]    ${listIndex}
    ${get_result}=    restApi.ApiGet    CH_CORE_SERVICES
    ${get_result_list}=    Get From Dictionary    ${get_result}    items
    ${putServiceList}=    Get Variable Value    ${putList}
    ${putServiceDict}=    utils.listToDict    ${putServiceList}    ${listIndex}
    ${name}=    utils.getFieldValueFromDict    ${putServiceDict}    name
    ${serviceDict}=    utils.getDictFromListofDict    ${get_result_list}    name    ${name}
    ${serviceID}=    utils.getFieldValueFromDict    ${serviceDict}    id
    ${api_result}=    restApi.ApiChameleonPut    CH_CORE_SERVICES    ${putServiceDict}    ${serviceID}
    Should Be True    ${api_result}
    ${getResultAfterPut}=    restApi.ApiChameleonGet    CH_CORE_SERVICES    ${serviceID}
    ${test_result}=    utils.compare_dict    ${putServiceDict}    ${getResultAfterPut}
    Should Be True    ${test_result}

Test Delete Service API
    [Arguments]    ${listIndex}
    ${json_result}=    restApi.ApiGet    CH_CORE_SERVICES
    ${json_result_list}=    Get From Dictionary    ${json_result}    items
    ${serviceList}=    Get Variable Value    ${slist}
    ${serviceDict}=    utils.listToDict    ${serviceList}    ${listIndex}
    ${name}=    utils.getFieldValueFromDict    ${serviceDict}    name
    Log    ${name}
    ${serviceDict}=    utils.getDictFromListofDict    ${json_result_list}    name    ${name}
    Log    ${serviceDict}
    ${serviceId}=    utils.getFieldValueFromDict    ${serviceDict}    id
    Log    ${serviceId}
    ${test_result}=    restApi.ApiChameleonDelete    CH_CORE_SERVICES    ${serviceId}
    Should Be True    ${test_result}
