#
# Copyright (c) 2017 Orange.
#
#    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.
#
"""Test the configuration validator driver"""

import mock
from oslo_config import cfg
from oslo_config import types
from oslo_log import log as logging
import six
from testtools.content import text_content

from congress.datasources import cfgvalidator_driver
from congress.tests import base
from congress.tests import base_rpc
from congress.tests import helper

LOG = logging.getLogger(__name__)


# pylint: disable=protected-access
def _fake_conf():
    conf = mock.MagicMock()
    conf._namespace = 'ns'
    opt1 = mock.MagicMock()
    opt1.id_ = 'ho1'
    opt1.name = 'o1'
    opt1.type = types.String
    opt1.ns_id = 'ns'
    opt2 = mock.MagicMock()
    opt2.id_ = 'ho2'
    opt2.name = 'o2'
    opt2.type = types.String
    opt2.ns_id = 'ns'
    group = mock.MagicMock()
    group._opts = {'o2': {'opt': opt2}}
    conf._groups = {'g': group}
    conf._opts = {'o1': {'opt': opt1}}
    return conf


class TestCfgValidatorDriver(base.TestCase):
    """Test the configuration validator driver"""
    def setUp(self):
        super(TestCfgValidatorDriver, self).setUp()
        args = helper.datasource_openstack_args()
        with mock.patch('congress.datasources.cfgvalidator_driver.'
                        'ValidatorAgentClient',
                        spec=cfgvalidator_driver.ValidatorAgentClient) as agm:
            self.driver = cfgvalidator_driver.ValidatorDriver(args=args)
            self.agent_mock = agm
        self.driver.node = mock.MagicMock()

        for table in cfgvalidator_driver.ValidatorDriver.get_schema():
            self.driver.state[table] = set()

    def test_get_info(self):
        """Test info retrieval on datasource. Minimal requirements"""
        info = self.driver.get_datasource_info()
        self.assertIsNotNone(info['id'])
        self.assertIsNotNone(info['description'])
        self.assertIsNotNone(info['config'])

    def test_translate_type(self):
        """Test the translation of type"""
        cases = [
            {
                'inputs': ['lorem',
                           types.String(choices=['foo'], max_length=4)],
                'expected': {
                    cfgvalidator_driver.STR_TYPE:
                        (u'lorem', u'', 4, u'False', u'False', u'[\'foo\']')}
            },
            {
                'inputs': ['lorem', types.Integer(choices=[1], min=1, max=2)],
                'expected': {
                    cfgvalidator_driver.INT_TYPE: (u'lorem', 1, 2, u'[1]')}
            },
            {
                'inputs': ['lorem', types.Float(min=1, max=2)],
                'expected': {cfgvalidator_driver.FLOAT_TYPE: (u'lorem', 1, 2)}
            },
            {
                'inputs': ['lorem', types.List(item_type=types.Float(min=1))],
                'expected': {
                    cfgvalidator_driver.LIST_TYPE: (
                        u'lorem', u'Float', u'False'),
                    cfgvalidator_driver.FLOAT_TYPE: (u'lorem', 1, u''), }
            },
            {
                'inputs': ['lorem', types.URI(max_length=2, schemes=['HTTP'])],
                'expected': {
                    cfgvalidator_driver.URI_TYPE: (u'lorem', 2, u'[\'HTTP\']')}
            },
            {
                'inputs': ['lorem', types.Range(min=1, max=2)],