# Copyright (c) 2016 Servionica
#
# 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 oslo_utils import uuidutils
from tempest.lib import decorators

from watcher_tempest_plugin.tests.client_functional.v1 import base


class AuditTemplateTests(base.TestCase):
    """Functional tests for audit template."""

    dummy_name = 'dummy'
    list_fields = ['UUID', 'Name', 'Goal', 'Strategy']
    detailed_list_fields = list_fields + ['Created At', 'Updated At',
                                          'Deleted At', 'Description',
                                          'Audit Scope']
    audit_template_name = 'a' + uuidutils.generate_uuid()

    @classmethod
    def setUpClass(cls):
        cls.watcher('audittemplate create %s dummy -s dummy'
                    % cls.audit_template_name)

    @classmethod
    def tearDownClass(cls):
        cls.watcher('audittemplate delete %s' % cls.audit_template_name)

    @decorators.idempotent_id('b5065a27-9757-4d65-8e9c-9c6f3677eb10')
    def test_audit_template_list(self):
        raw_output = self.watcher('audittemplate list')
        self.assert_table_structure([raw_output], self.list_fields)

    @decorators.idempotent_id('a26146b4-86fd-42ff-8af5-23567a8570e5')
    def test_audit_template_detailed_list(self):
        raw_output = self.watcher('audittemplate list --detail')
        self.assert_table_structure([raw_output], self.detailed_list_fields)

    @decorators.idempotent_id('fa8a1e4b-e5b5-4643-b298-99f80e909dc3')
    def test_audit_template_show(self):
        audit_template = self.watcher(
            'audittemplate show %s' % self.audit_template_name)
        self.assertIn(self.audit_template_name, audit_template)
        self.assert_table_structure([audit_template],
                                    self.detailed_list_fields)

    @decorators.idempotent_id('b632e1f5-4ab6-4338-91b3-abf4d257fa28')
    def test_audit_template_update(self):
        raw_output = self.watcher('audittemplate update %s replace '
                                  'description="Updated Desc"'
                                  % self.audit_template_name)
        audit_template_output = self.parse_show_as_object(raw_output)
        assert audit_template_output['Description'] == 'Updated Desc'


class AuditTemplateActiveTests(base.TestCase):

    audit_template_name = 'b' + uuidutils.generate_uuid()
    list_fields = ['UUID', 'Name', 'Goal', 'Strategy']
    detailed_list_fields = list_fields + ['Created At', 'Updated At',
                                          'Deleted At', 'Description',
                                          'Audit Scope']

    def _create_audit_template(self):
        self.watcher('audittemplate create %s dummy -s dummy '
                     '-d "Test Audit Template"' % self.audit_template_name)

    def _delete_audit_template(self):
        self.watcher('audittemplate delete %s' % self.audit_template_name)

    @decorators.idempotent_id('8c88f312-0e27-44c5-bd9b-c171086bfc55')
    def test_create_audit_template(self):
        raw_output = self.watcher('audittemplate create %s dummy '
                                  '-s dummy -d "Test Audit Template"'
                                  % self.audit_template_name)
        self.assert_table_structure([raw_output], self.detailed_list_fields)
        self._delete_audit_template()

    @decorators.idempotent_id('c1980ccc-8194-4b18-bcdc-964395de3cf9')
    def test_delete_audit_template(self):
        self._create_audit_template()
        raw_output = self.watcher('audittemplate delete %s'
                                  % self.audit_template_name)
        self.assertOutput('', raw_output)
