#
# Aura Engine (https://gitlab.servus.at/aura/engine)
#
# Copyright (C) 2017-now() - The Aura Engine Team.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import unittest
from unittest import mock

from aura_engine.base.config import AuraConfig
from aura_engine.plugins.monitor import AuraMonitor, MonitorResponseCode


class TestLogger(unittest.TestCase):
    """
    Testing the Logger.
    """

    monitor = None

    #
    # Mock
    #

    # Mock `requests.get` in `SimpleRestApi`
    def mocked_requests_get(*args, **kwargs):
        class MockResponse:
            def __init__(self, json_data, status_code):
                self.json_data = json_data
                self.status_code = status_code

            def json(self):
                return self.json_data

        print(f"Calling mocked 'requests.get' with '{args[0]}'")
        if args[0] == "http://aura.test.available":
            return MockResponse({"foo": "bar"}, 200)

        return MockResponse(None, 404)

    def setUp(self):
        self.config = AuraConfig.instance.config
        self.monitor = AuraMonitor(None)

    @mock.patch("aura_engine.base.api.requests.get", side_effect=mocked_requests_get)
    def test_get_url_response_success(self, mock_get):
        print(self._testMethodName)

        json = self.monitor.get_url_response("http://aura.test.available")

        # Success
        self.assertEqual("bar", json["foo"])

    @mock.patch("aura_engine.base.api.requests.get", side_effect=mocked_requests_get)
    def test_get_url_response_fail(self, mock_get):
        print(self._testMethodName)

        invalid_status = self.monitor.get_url_response("http://aura.test.not.available")

        # Success
        self.assertEqual(MonitorResponseCode.INVALID_STATE.value, invalid_status)


if __name__ == "__main__":
    unittest.main()