*** 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           CORDRobot
Library           ImportResource  resources=CORDRobot
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} =    CORDRobot.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}=    CORDRobot.listToDict    ${sliceList}    ${listIndex}
    ${api_result}=    CORDRobot.ApiPost    CH_CORE_SLICES    ${sliceDict}
    Should Be True    ${api_result}

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

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