*** Settings ***
Documentation     This is a basic skeleton for a Robot Framework test suite.
Library           Collections
Library           RequestsLibrary



*** Variables ***
${BASE_URL}             http://localhost:8006/ogdhcp/v1
${headers}              Create Dictionary    Content-Type    application/json
${modified_subnet}      Create Dictionary    mask=255.255.192.0    address=192.168.1.0    nextServer=192.168.1.1   bootFileName=pxelinux.0
${wrong_subnet_data_netmask}    Create Dictionary    mask=333.333.333.333   address=192.168.1.0    nextServer=192.168.1.1   bootFileName=pxelinux.0
${wrong_subnet_data_address}    Create Dictionary    mask=255.255.255.0   address=444.168.1.0    nextServer=192.168.1.1   bootFileName=pxelinux.0
${wrong_subnet_data_server}     Create Dictionary    mask=255.255.255.0   address=192.168.1.0    nextServer=555.168.1.1   bootFileName=pxelinux.0
${invalid_net_id}       999
${valid_net_id}         2
${subnet_error}         0

*** Test Cases ***
Get Status of the DHCP server
    [Documentation]    Get status of the dhcp server and check services status
    [Tags]    status
    ${response}=    GET     ${BASE_URL}/status   
    Status Should Be  200   ${response}
    Log   ${response.json()}
    # Convertir la respuesta a JSON usando ${response.json()}
    ${json}=    Convert to Dictionary    ${response.json()}
    Dictionary Should Contain Key    ${json}    success
    Dictionary Should Contain Key    ${json}    message
    Should Contain    ${response.json()['message']}    disk_usage
    Should Contain    ${response.json()['message']}    subnets
    Should Contain    ${response.json()['message']['disk_usage']}    total


Get All Subnets
    [Documentation]    Este test verifica que la API retorna las subredes correctamente con el código 200.
    [Tags]    subnets
    ${response}=    GET    ${BASE_URL}/subnets
    # Verificar código de estado HTTP
    Should Be Equal As Numbers    ${response.status_code}    200
    ${json}=    Convert To Dictionary    ${response.json()}
    # Verificar que la respuesta contiene las claves 'success' y 'message'
    Dictionary Should Contain Key    ${json}    success
    Dictionary Should Contain Key    ${json}    message
    # Validar el mensaje de éxito
    Should Be Equal    ${json['success']}    Subredes obtenidas correctamente
    # Verificar que 'message' es una lista
    # Should Be True    ${json['message']} is list
    # Verificar que cada subred en 'message' tiene los campos esperados
    FOR    ${subred}    IN    @{json['message']}
        Dictionary Should Contain Key    ${subred}    id
        Dictionary Should Contain Key    ${subred}    subnet
    END

Post a new subnet
    [Documentation]    Post a new subnet
    [Tags]    subnets
    ${new_subnet}           Create Dictionary   
    ...    subnetId=${valid_net_id}
    ...    mask=255.255.255.0
    ...    address=192.168.2.0
    ...    nextServer=192.168.2.1
    ...    bootFileName=pxelinux.0
    ${headers}=    Create Dictionary    Content-Type=application/json
    ${response}=    POST  ${BASE_URL}/subnets   json=${new_subnet}    headers=${headers}   
    Status Should Be  200   ${response} 
    ${json}=    Convert to Dictionary    ${response.json()}
    Dictionary Should Contain Key    ${json}    success
    Dictionary Should Contain Key    ${json}    message 
    Should Contain    ${json["success"]}    Subred agregada correctamente
    Should Be Equal As Numbers   ${json["message"]["id"]}    ${valid_net_id}


Post a new subnet with existing id
    [Documentation]    Post a new subnet with invalid data
    [Tags]    subnets
    ${new_subnet}           Create Dictionary   
    ...    subnetId=2
    ...    mask=255.255.255.0
    ...    address=192.168.3.0
    ...    nextServer=192.168.3.1
    ...    bootFileName=pxelinux.0
    ${headers}=    Create Dictionary    Content-Type=application/json
    ${response}=   POST  ${BASE_URL}/subnets    json=${new_subnet}    headers=${headers}  expected_status=400
    Status Should Be  400   ${response}
    ${json}=    Convert to Dictionary    ${response.json()}
    Dictionary Should Contain Key    ${json}    error
    Should Contain    ${response.json()["error"]}   La subred con el id '${valid_net_id}' ya existe.

Post a new subnet with existing address 
    [Documentation]    Post a new subnet with invalid data
    [Tags]    subnets
    ${headers}=    Create Dictionary    Content-Type=application/json
    ${new_subnet_invalid}     Create Dictionary    
    ...     subnetId=3    
    ...     mask=255.255.255.0    
    ...     address=192.168.1.0    
    ...     nextServer=192.168.1.1    
    ...     bootFileName=pxelinux.0

    ${response}=   POST  ${BASE_URL}/subnets    json=${new_subnet_invalid}    headers=${headers}     expected_status=400
    Status Should Be  400   ${response}
    ${json}=    Convert to Dictionary    ${response.json()}
    Dictionary Should Contain Key    ${json}    error
    Should Contain    ${response.json()['error']}  La subred con la dirección '192.168.1.0/24' ya existe


Modify an existing subnet by id
    [Documentation]    Modify a subnet by id
    [Tags]    subnets 
    ${headers}=    Create Dictionary    Content-Type=application/json
    ${modified_subnet}=      Create Dictionary    
    ...     mask=255.255.192.0    
    ...     address=192.168.1.0    
    ...     nextServer=192.168.1.1   
    ...     bootFileName=pxelinux.0
    # Modificar la subred con id=2
    ${response}=        PUT     ${BASE_URL}/subnets/2      json=${modified_subnet}     headers=${headers}      expected_status=200
    Status Should Be  200   ${response}
    ${json}=    Convert to Dictionary    ${response.json()}
    # Verificar que la respuesta contiene clave 'success'
    Dictionary Should Contain Key    ${json}    success
    
    # Validar el contenido del mensaje de éxito
    Should Be Equal    ${json['success']}    Subred modificada correctamente
    Should Be Equal    ${json['message']['id']}    2

     
Modify a subnet with invalid id
    [Documentation]    Este test verifica que la modificación de una subred falla cuando el ID no existe.
    [Tags]   subnets
    ${headers}=    Create Dictionary    Content-Type=application/json
    ${modificar_subred}=    Create Dictionary    mask=255.255.192.0    address=192.168.1.0    nextServer=192.168.1.1   bootFileName=pxelinux.0
    ${response}=    PUT    ${BASE_URL}/ogdhcp/v1/subnets/${invalid_net_id}    json=${modificar_subred}    headers=${headers}  expected_status=404
    
    # Verificar código de estado HTTP
    Should Be Equal As Numbers    ${response.status_code}    404

    

Modify subnet with error to save configuration
    [Documentation]    Este test verifica que la modificación falla cuando hay un error al guardar la configuración en Kea DHCP.
    [Tags]    subnets
    ${headers}=    Create Dictionary    Content-Type=application/json
    ${modificar_subred}=    Create Dictionary    subnet="192.168.1.0" mask="255.255.255.0"   nextServer="192.168.1.1"    bootFileName="pxelinux.0"
    ${response}=    PUT    ${BASE_URL}/subnets/${subnet_error}    json=${modificar_subred}    headers=${headers}    expected_status=400
    
    # Verificar código de estado HTTP
    Should Be Equal As Numbers    ${response.status_code}    400
    
    ${json}=    Convert To Dictionary    ${response.json()}
    
    # Verificar que la respuesta contiene el error esperado
    Should Be Equal    ${json['error']}    Error al guardar la configuración en Kea DHCP: Unable to save configuration

Modify subnet with invalid configuration address without netmask 
    [Documentation]    Este test verifica que la modificación falla cuando hay un error en la configuración de Kea DHCP.
    [Tags]    subnets
    ${headers}=    Create Dictionary    Content-Type=application/json
    ${modificar_subred}=    Create Dictionary    address="192.168.1.0"    nextServer="192.168.1.1"    bootFileName="pxelinux.0"  

    ${response}=    PUT    ${BASE_URL}/subnets/2    json=${modificar_subred}    headers=${headers}  expected_status=400
    
    # Verificar código de estado HTTP
    Should Be Equal As Numbers    ${response.status_code}    400
    
    ${json}=    Convert To Dictionary    ${response.json()}
    
    # Verificar que la respuesta contiene el error esperado
    Should Be Equal    ${json['error']}    Falta un parámetro requerido: mask

Modify subnet with invalid configuration netmask without address 
    [Documentation]    Este test verifica que la modificación falla cuando hay un error en la configuración de Kea DHCP.
    [Tags]    subnets
    ${headers}=    Create Dictionary    Content-Type=application/json
    ${modificar_subred}=    Create Dictionary    mask="255.255.255.0"    nextServer="192.168.1.1"    bootFileName="pxelinux.0"  

    ${response}=    PUT    ${BASE_URL}/subnets/2    json=${modificar_subred}    headers=${headers}  expected_status=400
    
    # Verificar código de estado HTTP
    Should Be Equal As Numbers    ${response.status_code}    400
    
    ${json}=    Convert To Dictionary    ${response.json()}
    
    # Verificar que la respuesta contiene el error esperado
    Should Be Equal    ${json['error']}    Falta un parámetro requerido: address

Delete subnet by id 
    [Documentation]    Este test verifica que la subred se elimina correctamente con el código 200.
    [Tags]    subnets  
    ${response}=    DELETE    ${BASE_URL}/subnets/${valid_net_id}
    # Verificar código de estado HTTP
    Should Be Equal As Numbers    ${response.status_code}    200
    ${json}=    Convert To Dictionary    ${response.json()}
    # Verificar que la respuesta contiene la clave 'success'
    Dictionary Should Contain Key    ${json}    success
    # Validar el mensaje de éxito
    Should Be Equal    ${json['success']}    Subred eliminada correctamente

Delete subnet - Error: wrong subnet Id 
    [Documentation]    Este test verifica que la eliminación falla si la subred con el ID no existe.
    [Tags]    subnets
    ${response}=    DELETE    ${BASE_URL}/subnets/${invalid_net_id}   expected_status=404
    # Verificar código de estado HTTP
    Should Be Equal As Numbers    ${response.status_code}    404
    ${json}=    Convert To Dictionary    ${response.json()}
    # Verificar que la respuesta contiene la clave 'error'
    Dictionary Should Contain Key    ${json}    error
    # Validar el mensaje de error
    Should Be Equal    ${json['error']}    La subred con el id '999' no existe





