# -*- 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.NetworkMonitor.networkMonitor_get import NetworkMonitorGet

from WorkData.Argus.hosts import DataHosts

import allure


@allure.feature("测试模块：networkmonitor.get")
class TestNetworkMonitorGet(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("调用接口：networkmonitor.get")
    def networkmonitor_get(self, hostName=None, assetCode=None, manageIp=None, parentHost=None, available=None, status=None,
                           operationsPeopleId=None, page=None, size=None):
        api = NetworkMonitorGet(_host=self.host)
        api.hostName = hostName
        api.assetCode = assetCode
        api.manageIp = manageIp
        api.parentHost = parentHost
        api.available = available
        api.status = status
        api.operationsPeopleId = operationsPeopleId
        api.page = page
        api.size = size
        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("断言返回结果")
    def check_data(self, response, key, value, is_default):
        data = UtilsResponse().get_data(response=response)
        for x, y in enumerate(data["list"]):
            if is_default == 1:
                assert y[key] == value
            else:
                assert value in y[key]

    @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("networkmonitor.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.networkmonitor_get()
        self.check_code(response=response, code=0)

    @allure.title("networkmonitor.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.networkmonitor_get(hostName=self.base_host_name)
        self.check_code(response=response, code=0)
        self.check_data(response=response, key="assetName", value=self.base_host_name, is_default=0)

    @allure.title("networkmonitor.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.networkmonitor_get(assetCode=self.base_asset_code)
        self.check_code(response=response, code=0)
        self.check_data(response=response, key="assetCode", value=self.base_asset_code, is_default=0)

    @allure.title("networkmonitor.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.networkmonitor_get(manageIp=self.base_manage_ip)
        self.check_code(response=response, code=0)
        self.check_data(response=response, key="manageIp", value=self.base_manage_ip, is_default=0)

    @allure.title("networkmonitor.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.networkmonitor_get(parentHost=self.base_parent_host)
        self.check_code(response=response, code=0)
        self.check_data(response=response, key="parentHost", value=self.base_parent_host, is_default=0)

    @allure.title("networkmonitor.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.networkmonitor_get(available=0)
        self.check_code(response=response, code=0)
        self.check_data(response=response, key="available", value=0, is_default=1)

    @allure.title("networkmonitor.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.networkmonitor_get(status=0)
        self.check_code(response=response, code=0)
        self.check_data(response=response, key="monitorStatus", value=0, is_default=1)


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 = TestNetworkMonitorGet()
    # a.setup_class()
    # a.test_case_03()
