# -*- coding: utf-8 -*-
# 测试用例
# 作者: 陈磊
# 时间: 2019-11-18


from __future__ import division

from WorkCase import CaseBase
from WorkUtils.UtilsLog import UtilsLog
from WorkUtils.UtilsResponse import UtilsResponse
from WorkUtils.UtilsDataBase import UtilsDataBase
from WorkApi.APP.Experience.experience_get import ExperienceGet

from WorkData.Argus.hosts import DataHosts

import allure


@allure.feature("测试模块：experience.get")
class TestExperienceGet(object):
    log = UtilsLog()
    env = CaseBase().app_environment
    host = env["host"]
    db_url = env["db_url"]
    db_port = env["db_port"]
    db_user = env["db_user"]
    db_pw = env["db_pw"]
    db_base = env["db_base"]

    base_host_name = "SS测试主机名"
    base_asset_code = "SS测试主机资产编号"
    base_manage_ip = "10.0.28."
    base_parent_host = "SS测试所属主机"

    @classmethod
    def setup_class(cls):
        cls.log.debug("开始执行测试套件.......")
        session = UtilsDataBase().conn_mysql(db_url=cls.db_url, db_port=cls.db_port, db_base=cls.db_base, db_user=cls.db_user, db_pw=cls.db_pw)
        DataHosts().delete_like_host_name(session=session, host_name=cls.base_host_name)

    @classmethod
    def teardown_class(cls):
        cls.log.debug("结束执行测试套件.......")

    def setup_method(self):
        self.log.debug("测试用例执行开始...")

    def teardown_method(self):
        self.log.debug("测试用例执行结束...")

    @allure.step("调用接口：experience.get")
    def experience_get(self, hostType=None, hostName=None, manageLevel=None, manageIp=None, businessid=None, alarmRuleid=None,
                       title=None, tagid=None, description=None):
        api = ExperienceGet(_host=self.host)
        api.hostType = hostType
        api.hostName = hostName
        api.manageLevel = manageLevel
        api.manageIp = manageIp
        api.businessid = businessid
        api.alarmRuleid = alarmRuleid
        api.title = title
        api.tagid = tagid
        api.description = description
        api.get_response()
        return api.response

    @allure.step("断言返回结果")
    def check_code(self, response, code):
        _code = UtilsResponse().get_code(response=response)
        assert _code == code

    @allure.step("连接数据库：Argus")
    def db_session(self):
        session = UtilsDataBase().conn_mysql(db_url=self.db_url, db_port=self.db_port, db_base=self.db_base, db_user=self.db_user, db_pw=self.db_pw)
        return session

    @allure.step("查询表：hosts")
    def select_hosts(self, hostid=None, host_name=None, available=None, monitor_status=None):
        session = self.db_session()
        sql = DataHosts().select_all_from_allKeys(session=session, hostid=hostid, host_name=host_name, available=available, monitor_status=monitor_status)
        return sql

    @allure.step("新增表数据：hosts")
    def insert_hosts(self, host_name=None, asset_code=None, manage_ip=None, parent_host=None, available=None, monitor_status=None):
        session = self.db_session()
        hostid = DataHosts().insert_one(session=session, host_name=host_name, asset_code=asset_code, manage_ip=manage_ip,
                                        parent_host=parent_host, available=available, monitor_status=monitor_status)
        return hostid

    @allure.title("experience.get：无参数调用")
    @allure.story("查询主机监控：无参数调用")
    @allure.severity("blocker")
    def test_case_01(self):
        host_name = self.base_host_name + "01"
        asset_code = self.base_asset_code + "01"
        manage_ip = self.base_manage_ip + "1"
        parent_host = self.base_parent_host + "01"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=1, monitor_status=1)
        response = self.experience_get()
        self.check_code(response=response, code=0)

    @allure.title("experience.get：模糊查询hostName")
    @allure.story("查询主机监控：模糊查询hostName")
    def test_case_02(self):
        host_name = self.base_host_name + "02"
        asset_code = self.base_asset_code + "02"
        manage_ip = self.base_manage_ip + "2"
        parent_host = self.base_parent_host + "02"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=1, monitor_status=1)
        response = self.experience_get(hostName=host_name)
        self.check_code(response=response, code=0)
        data = UtilsResponse().get_data(response=response)

    @allure.title("experience.get：查询assetCode")
    @allure.story("查询主机监控：查询assetCode")
    def test_case_03(self):
        host_name = self.base_host_name + "03"
        asset_code = self.base_asset_code + "03"
        manage_ip = self.base_manage_ip + "3"
        parent_host = self.base_parent_host + "03"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=1, monitor_status=1)
        response = self.experience_get(assetCode=asset_code)
        self.check_code(response=response, code=0)
        data = UtilsResponse().get_data(response=response)

    @allure.title("experience.get：模糊查询manageIp")
    @allure.story("查询主机监控：模糊查询manageIp")
    def test_case_04(self):
        host_name = self.base_host_name + "04"
        asset_code = self.base_asset_code + "04"
        manage_ip = self.base_manage_ip + "4"
        parent_host = self.base_parent_host + "04"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=1, monitor_status=1)
        response = self.experience_get(manageIp=manage_ip)
        self.check_code(response=response, code=0)
        data = UtilsResponse().get_data(response=response)

    @allure.title("experience.get：模糊查询parentHost")
    @allure.story("查询主机监控：模糊查询parentHost")
    def test_case_05(self):
        host_name = self.base_host_name + "05"
        asset_code = self.base_asset_code + "05"
        manage_ip = self.base_manage_ip + "5"
        parent_host = self.base_parent_host + "05"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=1, monitor_status=1)
        response = self.experience_get(parentHost=parent_host)
        self.check_code(response=response, code=0)
        data = UtilsResponse().get_data(response=response)

    @allure.title("experience.get：模糊查询available")
    @allure.story("查询主机监控：模糊查询available")
    def test_case_06(self):
        host_name = self.base_host_name + "06"
        asset_code = self.base_asset_code + "06"
        manage_ip = self.base_manage_ip + "6"
        parent_host = self.base_parent_host + "06"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=0, monitor_status=1)
        response = self.experience_get(available=0)
        self.check_code(response=response, code=0)
        data = UtilsResponse().get_data(response=response)

    @allure.title("experience.get：模糊查询monitor_status")
    @allure.story("查询主机监控：模糊查询monitor_status")
    def test_case_07(self):
        host_name = self.base_host_name + "07"
        asset_code = self.base_asset_code + "07"
        manage_ip = self.base_manage_ip + "7"
        parent_host = self.base_parent_host + "07"
        self.insert_hosts(host_name=host_name, asset_code=asset_code, manage_ip=manage_ip, parent_host=parent_host, available=1, monitor_status=0)
        response = self.experience_get(status=0)
        self.check_code(response=response, code=0)
        data = UtilsResponse().get_data(response=response)


if __name__ == "__main__":
    from WorkUtils.UtilsPyTest import UtilsPyTest
    from WorkUtils.UtilsCmd import UtilsCmd

    import os

    # 执行自动化测试用例
    case_info = os.path.split(__file__)
    case = UtilsCmd().pytest_cmd()
    r = UtilsPyTest(case=case, case_info=case_info)
    r.run_main()

    # a = TestExperienceGet()
    # a.setup_class()
    # a.test_case_04()
