*** Settings ***
Documentation     Test suite for Node API verification
Suite Setup       Read InputFile
Test Template     Verify Node 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_Node.json
${IP}             ${SERVER_IP}
${PORT}           ${SERVER_PORT}

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

Test Get Node-1       RETRIEVE    0

Test Delete Node-1    DELETE      0

Test Post Node-2      CREATE      1

Test Get Node-2       RETRIEVE    1

Test Delete Node-2    DELETE      1

*** Keywords ***
Read InputFile
    ${nodeList}=    CORDRobot.jsonToList    ${PATHFILE}    NodeInfo
    Set Suite Variable    ${nlist}    ${nodeList}
    ${siteDeployment}=    Catenate    SEPARATOR=    http://    ${IP}    :    ${PORT}
    ...    /api/core/sitedeployments/1/
    Set Suite Variable    ${siteDeployment}    ${siteDeployment}

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

Test Post Node API
    [Arguments]    ${listIndex}
    ${nodeList}=    Get Variable Value    ${nlist}
    ${nodeDict}=    CORDRobot.listToDict    ${nodeList}    ${listIndex}
    ${api_result}=    CORDRobot.ApiPost    CH_CORE_NODES    ${nodeDict}
    Should Be True    ${api_result}

Test Get Node API
    [Arguments]    ${listIndex}
    ${json_result}=    CORDRobot.ApiGet    CH_CORE_NODES
    ${json_result_list}=    Get From Dictionary    ${json_result}    items
    Log    ${json_result_list}
    ${nodeList}=    Get Variable Value    ${nlist}
    ${nodeDict}=    CORDRobot.listToDict    ${nodeList}    ${listIndex}
    ${name}=    CORDRobot.getFieldValueFromDict    ${nodeDict}    name
    Log    ${name}
    ${getJsonDict}=    CORDRobot.getDictFromListOfDict    ${json_result_list}    name    ${name}
    ${test_result}=    CORDRobot.compare_dict    ${nodeDict}    ${getJsonDict}
    Should Be True    ${test_result}

Test Delete Node API
    [Arguments]    ${listIndex}
    ${json_result}=    CORDRobot.ApiGet    CH_CORE_NODES
    ${json_result_list}=    Get From Dictionary    ${json_result}    items
    ${nodeList}=    Get Variable Value    ${nlist}
    ${nodeDict}=    CORDRobot.listToDict    ${nodeList}    ${listIndex}
    ${name}=    CORDRobot.getFieldValueFromDict    ${nodeDict}    name
    Log    ${name}
    ${nodeDict}=    CORDRobot.getDictFromListofDict    ${json_result_list}    name    ${name}
    Log    ${nodeDict}
    ${nodeId}=    CORDRobot.getFieldValueFromDict    ${nodeDict}    id
    Log    ${nodeId}
    ${test_result}=    CORDRobot.ApiChameleonDelete    CH_CORE_NODES    ${nodeId}
    Should Be True    ${test_result}
