#######################################################################################
# Copyright ETSI Contributors and Others.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import asynctest
from juju.errors import JujuError
from osm_common.dataclasses.temporal_dataclasses import ModelInfo
from osm_common.dbbase import DbException
from osm_lcm.temporal.juju_paas_activities import JujuPaasConnector
from n2vc.temporal_libjuju import ConnectionInfo
from temporalio.testing import ActivityEnvironment
from unittest.mock import Mock

vim_id = "some-vim-uuid"
namespace = "some-namespace"
model_info = ModelInfo(vim_id, namespace)
connection_info = ConnectionInfo(
    "1.2.3.4:17070", "user", "password", "cacert", "cloud_name", "cloud_credentials"
)


class TestJujuPaasConnector(asynctest.TestCase):
    def setUp(self):
        self.db = Mock()
        self.env = ActivityEnvironment()
        self.juju_paas_connector = JujuPaasConnector(self.db)

    @asynctest.mock.patch("osm_lcm.temporal.juju_paas_activities.Libjuju.add_model")
    @asynctest.mock.patch(
        "osm_lcm.temporal.juju_paas_activities.JujuPaasConnector._get_connection_info"
    )
    async def test_create_model_nominal_case(
        self, mock_get_connection_info, mock_add_model
    ):
        mock_get_connection_info.return_value = connection_info
        await self.env.run(self.juju_paas_connector.create_model, model_info)
        mock_get_connection_info.assert_called_once_with(vim_id)
        mock_add_model.assert_called_once_with(namespace)

    @asynctest.mock.patch("osm_lcm.temporal.juju_paas_activities.Libjuju.add_model")
    @asynctest.mock.patch(
        "osm_lcm.temporal.juju_paas_activities.JujuPaasConnector._get_connection_info"
    )
    async def test_create_model_raises_juju_exception(
        self, mock_get_connection_info, mock_add_model
    ):
        mock_get_connection_info.return_value = connection_info
        mock_add_model.side_effect = JujuError()
        with self.assertRaises(JujuError):
            await self.env.run(self.juju_paas_connector.create_model, model_info)
        mock_get_connection_info.assert_called_once_with(vim_id)
        mock_add_model.assert_called_once_with(namespace)

    @asynctest.mock.patch("osm_lcm.temporal.juju_paas_activities.Libjuju.add_model")
    @asynctest.mock.patch(
        "osm_lcm.temporal.juju_paas_activities.JujuPaasConnector._get_connection_info"
    )
    async def test_create_model_raises_db_exception(
        self, mock_get_connection_info, mock_add_model
    ):
        mock_get_connection_info.side_effect = DbException("not found")
        with self.assertRaises(DbException):
            await self.env.run(self.juju_paas_connector.create_model, model_info)
        mock_get_connection_info.assert_called_once_with(vim_id)
        mock_add_model.assert_not_called()
