*** Settings ***
Documentation     Test suite for Slice API verification
Suite Setup       Read InputFile
Test Template     Verify Slice API functionality
Library           Collections
Library           String
Library           OperatingSystem
Library           XML
Library           RequestsLibrary
Library           ../Framework/utils/utils.py
Library           ../Framework/restApi.py
Variables         ../Properties/RestApiProperties.py

*** Variables ***
${PATHFILE}       ${CURDIR}/data/Ch_Slice.json
${IP}             ${SERVER_IP}
${PORT}           ${SERVER_PORT}

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

Test Get Slice-1      RETRIEVE    0

Test Delete Slice-1
                      DELETE      0

*** Keywords ***
Read InputFile
    ${sliceList} =    utils.jsonToList    ${PATHFILE}    SliceInfo
    Set Suite Variable    ${sList}    ${sliceList}

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

Test Post Slice API
    [Arguments]    ${listIndex}
    ${sliceList} =    Get Variable Value    ${sList}
    ${sliceDict}=    utils.listToDict    ${sliceList}    ${listIndex}
    ${api_result}=    restApi.ApiPost    CH_CORE_SLICES    ${sliceDict}
    Should Be True    ${api_result}

Test Get Slice API
    [Arguments]    ${listIndex}
    ${json_result}=    restApi.ApiGet    CH_CORE_SLICES
    ${json_result_list}=    Get From Dictionary    ${json_result}    items
    Log    ${json_result_list}
    ${sliceList}=    Get Variable Value    ${sList}
    ${sliceDict}=    utils.listToDict    ${sliceList}    ${listIndex}
    ${sliceName}=    Get From Dictionary    ${sliceDict}    name
    ${getJsonDict}=    utils.getDictFromListOfDict    ${json_result_list}    name    ${sliceName}
    ${test_result}=    utils.compare_dict    ${sliceDict}    ${getJsonDict}
    Should Be True    ${json_result}

Test Delete Slice API
    [Arguments]    ${listIndex}
    ${json_getresult}=    restApi.ApiGet    CH_CORE_SLICES
    ${json_getresult_list}=    Get From Dictionary    ${json_getresult}    items
    ${sliceList}=    Get Variable Value    ${sList}
    ${sliceDict}=    utils.listToDict    ${sList}    ${listIndex}
    ${sliceName}=    utils.getFieldValueFromDict    ${sliceDict}    name
    Log    ${sliceName}
    ${getSliceDict}=    utils.getDictFromListofDict    ${json_getresult_list}    name    ${sliceName}
    Log    ${getSliceDict}
    ${sliceID}=    utils.getFieldValueFromDict    ${getSliceDict}    id
    Log    ${sliceID}
    ${test_result}=    restApi.ApiChameleonDelete    CH_CORE_SLICES    ${sliceID}
    Should be True    ${test_result}
