# (c) Copyright IBM Corp. 2010, 2018. All Rights Reserved.
# -*- coding: utf-8 -*-
"""Tests using pytest_resilient_circuits"""

from __future__ import print_function
import pytest
from resilient_circuits.util import get_config_data, get_function_definition
from resilient_circuits import SubmitTestFunction, FunctionResult
from fn_thug.util.thug_utils import ThugUtils

try:
    from unittest.mock import patch
except ImportError:
    from mock import patch

PACKAGE_NAME = "fn_thug"
FUNCTION_NAME = "thug_analysis"

# Read the default configuration-data section from the package
config_data = get_config_data(PACKAGE_NAME)

# Provide a simulation of the Resilient REST API (uncomment to connect to a real appliance)
resilient_mock = "pytest_resilient_circuits.BasicResilientMock"


def call_thug_analysis_function(circuits, function_params, timeout=10):
    # Fire a message to the function
    evt = SubmitTestFunction("thug_analysis", function_params)
    circuits.manager.fire(evt)
    event = circuits.watcher.wait("thug_analysis_result", parent=evt, timeout=timeout)
    assert event
    assert isinstance(event.kwargs["result"], FunctionResult)
    pytest.wait_for(event, "complete", True)
    return event.kwargs["result"].value


class TestThugAnalysis:
    """ Tests for the thug_analysis function"""

    def test_function_definition(self):
        """ Test that the package provides customization_data that defines the function """
        func = get_function_definition(PACKAGE_NAME, FUNCTION_NAME)
        assert func is not None

    @patch("fn_thug.components.thug_analysis.ThugUtils")
    @pytest.mark.parametrize("thug_args, thug_url, expected_results", [
        (None, "https://www.google.com", {"value": "xyz"})
    ])
    def test_success(self, mock_utils, circuits_app, thug_args, thug_url, expected_results):
        """ Test calling with sample values for the parameters """
        function_params = { 
            "thug_args": thug_args,
            "thug_url": thug_url
        }
        mock_utils.return_value = ThugUtils({'thug_dir': '/tmp'})
        results = call_thug_analysis_function(circuits_app, function_params)
        assert results.get('url') == 'https://www.google.com'
        assert results.get('report_json') is not None
        assert results.get('graph_svg') is not None
        assert results.get('report_xml') is not None