#
#    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.

from unittest import mock
import yaml

from osc_lib import exceptions

from heat.common import exception
from heat.common.i18n import _
from heat.common import template_format
from heat.engine.resources.openstack.octavia import l7policy
from heat.tests import common
from heat.tests.openstack.octavia import inline_templates
from heat.tests import utils


class L7PolicyTest(common.HeatTestCase):

    def test_resource_mapping(self):
        mapping = l7policy.resource_mapping()
        self.assertEqual(mapping['OS::Octavia::L7Policy'],
                         l7policy.L7Policy)

    def _create_stack(self, tmpl=inline_templates.L7POLICY_TEMPLATE):
        self.t = template_format.parse(tmpl)
        self.stack = utils.parse_stack(self.t)
        self.l7policy = self.stack['l7policy']

        self.octavia_client = mock.MagicMock()
        self.l7policy.client = mock.MagicMock(
            return_value=self.octavia_client)
        self.l7policy.client_plugin().client = mock.MagicMock(
            return_value=self.octavia_client)

    def test_validate_reject_action_with_conflicting_props(self):
        tmpl = yaml.safe_load(inline_templates.L7POLICY_TEMPLATE)
        props = tmpl['resources']['l7policy']['properties']
        props['action'] = 'REJECT'
        self._create_stack(tmpl=yaml.safe_dump(tmpl))

        msg = _('Properties redirect_pool and redirect_url are not '
                'required when action type is set to REJECT.')
        with mock.patch('heat.engine.clients.os.neutron.NeutronClientPlugin.'
                        'has_extension', return_value=True):
            self.assertRaisesRegex(exception.StackValidationFailed,
                                   msg, self.l7policy.validate)

    def test_validate_redirect_pool_action_with_url(self):
        tmpl = yaml.safe_load(inline_templates.L7POLICY_TEMPLATE)
        props = tmpl['resources']['l7policy']['properties']
        props['action'] = 'REDIRECT_TO_POOL'
        props['redirect_pool'] = '123'
        self._create_stack(tmpl=yaml.safe_dump(tmpl))

        msg = _('redirect_url property should only be specified '
                'for action with value REDIRECT_TO_URL.')
        with mock.patch('heat.engine.clients.os.neutron.NeutronClientPlugin.'
                        'has_extension', return_value=True):
            self.assertRaisesRegex(exception.ResourcePropertyValueDependency,
                                   msg, self.l7policy.validate)

    def test_validate_redirect_pool_action_without_pool(self):
        tmpl = yaml.safe_load(inline_templates.L7POLICY_TEMPLATE)
        props = tmpl['resources']['l7policy']['properties']
        props['action'] = 'REDIRECT_TO_POOL'
        del props['redirect_url']
        self._create_stack(tmpl=yaml.safe_dump(tmpl))

        msg = _('Property redirect_pool is required when action type '
                'is set to REDIRECT_TO_POOL.')
        with mock.patch('heat.engine.clients.os.neutron.NeutronClientPlugin.'
                        'has_extension', return_value=True):
            self.assertRaisesRegex(exception.StackValidationFailed,
                                   msg, self.l7policy.validate)

    def test_validate_redirect_url_action_with_pool(self):
        tmpl = yaml.safe_load(inline_templates.L7POLICY_TEMPLATE)
        props = tmpl['resources']['l7policy']['properties']
        props['redirect_pool'] = '123'
        self._create_stack(tmpl=yaml.safe_dump(tmpl))

        msg = _('redirect_pool property should only be specified '
                'for action with value REDIRECT_TO_POOL.')
        with mock.patch('heat.engine.clients.os.neutron.NeutronClientPlugin.'
                        'has_extension', return_value=True):
            self.assertRaisesRegex(exception.ResourcePropertyValueDependency,
                                   msg, self.l7policy.validate)

    def test_validate_redirect_url_action_without_url(self):
        tmpl = yaml.safe_load(inline_templates.L7POLICY_TEMPLATE)
        props = tmpl['resources']['l7policy']['properties']
        del props['redirect_url']
        self._create_stack(tmpl=yaml.safe_dump(tmpl))

        msg = _('Property redirect_url is required when action type '
                'is set to REDIRECT_TO_URL.')
        with mock.patch('heat.engine.clients.os.neutron.NeutronClientPlugin.'
                        'has_extension', return_value=True):
            self.assertRaisesRegex(exception.StackValidationFailed,
                                   msg, self.l7policy.validate)

    def test_create(self):