#    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 mock
from neutron_lib import context
from neutron_lib import fixture
from oslo_utils import uuidutils

from neutron.db.quota import api as quota_db_api
from neutron.tests.unit.api import test_extensions
from neutron.tests.unit.extensions import test_l3
from neutron.tests.unit.extensions import test_securitygroup
from neutron.tests.unit.plugins.ml2 import base as ml2_base
from neutron.tests.unit.plugins.ml2 import test_plugin


class SgTestCaseWrapper(test_securitygroup.SecurityGroupDBTestCase):
    # This wrapper class enables Ml2PluginV2TestCase to correctly call the
    # setup method in SecurityGroupDBTestCase which does not accept the
    # service_plugins keyword parameter.

    def setUp(self, plugin, **kwargs):
        super(SgTestCaseWrapper, self).setUp(plugin)


class BaseTestTrackedResources(test_plugin.Ml2PluginV2TestCase,
                               SgTestCaseWrapper):

    def setUp(self):
        self.ctx = context.get_admin_context()
        super(BaseTestTrackedResources, self).setUp()
        self._tenant_id = uuidutils.generate_uuid()

    def _test_init(self, resource_name):
        quota_db_api.set_quota_usage(
            self.ctx, resource_name, self._tenant_id)


class BaseTestEventHandler(object):

    def setUp(self):
        # Prevent noise from default security group operations
        def_sec_group_patch = mock.patch(
            'neutron.db.securitygroups_db.SecurityGroupDbMixin.'
            '_ensure_default_security_group')
        def_sec_group_patch.start()
        get_sec_group_port_patch = mock.patch(