*** Settings ***
Documentation     Test suite for Instance creation for multiple subscribers with multiple "s_tag" - Chameleon APIs
Suite Setup       Read InputFile
Test Template     Verify Multiple Instance Creation 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_Subscriber_MultiInstance.json

*** Test Cases ***    TYPE                 LISTINDEX
Test Create Subscriber-1
                      SUBSCRIBER_CREATE    0

Test Create Subscriber-2
                      SUBSCRIBER_CREATE    1

Test Create Subscriber-3
                      SUBSCRIBER_CREATE    2

Test Instance creation validation
                      INSTANCE_VALIDATE    0

*** Keywords ***
Read InputFile
    ${subscriberList} =    utils.jsonToList    ${PATHFILE}    SubscriberInfo
    Set Suite Variable    ${slist}    ${subscriberList}
    @{instanceList}=    Create List
    ${Subscriber_Id}=    Set Variable
    Set Suite Variable    @{instanceList}
    Set Suite Variable    ${Subscriber_Id}

Verify Multiple Instance Creation Functionality
    [Arguments]    ${type}    ${listIndex}
    Run Keyword If    "${type}" == "SUBSCRIBER_CREATE"    Test Post Subscriber Create    ${listIndex}
    Run Keyword If    "${type}" == "INSTANCE_VALIDATE"    Test Instance Validation    ${listIndex}

Test Post Subscriber Create
    [Arguments]    ${listIndex}
    [Documentation]    Test posts subscribers and validates the end-end functionality until instance creations
    # Read input from data files and post subscriber
    ${subscriberList} =    Get Variable Value    ${slist}
    ${subscriberDict}=    utils.listToDict    ${subscriberList}    ${listIndex}
    ${api_result}=    restApi.ApiPost    VOLT_SUBSCRIBER    ${subscriberDict}
    Sleep    30
    ${service_specific_id}=    utils.getFieldValueFromDict    ${subscriberDict}    service_specific_id
    ${json_result}=    restApi.ApiGet    VOLT_SUBSCRIBER
    Log    ${json_result}
    # Verifying GET operation after POST and validating the contents posted match the input data
    ${json_result_list}=    Get From Dictionary    ${json_result}    items
    ${getJsonDict}=    utils.getDictFromListOfDict    ${json_result_list}    service_specific_id    ${service_specific_id}
    ${result}=    utils.compare_dict    ${subscriberDict}    ${getJsonDict}
    Should Be True    ${result}
    ${Subscriber_Id}=    utils.getFieldValueFromDict    ${getJsonDict}    id
    Log    ${Subscriber_Id}
    # Retrieve subscribed_links_id from GET result of the posted Subscriber
    ${subscribed_links_ids_list}=    utils.getFieldValueFromDict    ${getJsonDict}    subscribed_links_ids
    Log    ${subscribed_links_ids_list}
    ${subscribed_links_ids}=    Get From List    ${subscribed_links_ids_list}    0
    Log    ${subscribed_links_ids}
    # Retrieve provided_links_ids from core/serviceinstancelink
    ${getServiceLink}=    restApi.ApiChameleonGet    CH_CORE_SERVICELINK    ${subscribed_links_ids}
    ${provider_service_instance_id}=    Get From Dictionary    ${getServiceLink}    provider_service_instance_id
    # Verify created volt using the above provider_service_instance_id
    # Verifying GET operation for the Autocreated Volt
    ${get_result}=    restApi.ApiChameleonGet    VOLT_TENANT    ${provider_service_instance_id}
    Should Be True    ${get_result}
    # Get "subscriber_links_ids" from the GET result of /volt/volttenants
    ${subscribed_links_ids_list}=    Get From Dictionary    ${get_result}    subscribed_links_ids
    ${subscribed_links_ids}=    Get From List    ${subscribed_links_ids_list}    0
    # Validation of Instances
    Log    ${instanceList}
    ${serviceLinkDict}=    restApi.ApiChameleonGet    CH_CORE_SERVICELINK    ${subscribed_links_ids}
    Log    ${serviceLinkDict}
    ${VSGTenant}=    Get From Dictionary    ${serviceLinkDict}    provider_service_instance_id
    # Retrieve VSGTenant to retrieve instance_id
    ${getVSG_result}=    restApi.ApiChameleonGet    VSG_TENANT    ${VSGTenant}
    ${instance_id}=    Get From Dictionary    ${getVSG_result}    instance_id
    Append To List    ${instanceList}    ${instance_id}
    Log    ${instanceList}
    ${get_CoreInstanceresult}=    restApi.ApiChameleonGet    CH_CORE_INSTANCES    ${instance_id}
    Should Be True    ${get_CoreInstanceresult}

Test Instance Validation
    [Arguments]    ${listIndex}
    ${get_result}=    restApi.ApiGet    VSG_TENANT
    Should Be True    ${get_result}
    ${instanceFinalList}=    Remove Duplicates    ${instanceList}
    Log    ${instanceFinalList}
    ${instanceCount}=    Get Length    ${instanceFinalList}
    Log    ${instanceCount}
    Should Be True    ${instanceCount} == 3
