-- Copyright (c) 2026 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
--         http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local lu = require 'luaunit'
local cjson = require 'cjson'

TestUBRuleCableMonitorPlugin = {}

function TestUBRuleCableMonitorPlugin:test_make_unnecessary_cable_info()
    local mock_client = {
        GetSystemsBoardObjects = function()
            return {
                {
                    Slot = 1
                }
            }
        end
    }
    local back_mock_client = package.loaded['pcie_device.client']
    package.loaded['pcie_device.client'] = mock_client
    package.loaded['biz_topo.class.ub_rule_cable_monitor_plugin'] = nil
    local c_ub_rule_cable_monitor_plugin = require 'biz_topo.class.ub_rule_cable_monitor_plugin'
    local biz_connector_list = {
        {
            is_pcie_biz_connector = function()
                return true
            end,
            ref_pcie_addr_info = {
                get_prop = function(_self, item)
                    if item == 'ComponentType' then
                        return 8
                    elseif item == 'SlotID' then
                        return 1
                    elseif item == 'ContainerSlot' then
                        return 2
                    elseif item == 'ContainerUID' then
                        return "00150"
                    elseif item == 'ContainerUnitType' then
                        return "IEU"
                    end
                end
            },
            res_port_ids = {17}
        }
    }

    c_ub_rule_cable_monitor_plugin:ctor(biz_connector_list)
    c_ub_rule_cable_monitor_plugin:ub_rules_get_unnecessary_cable()

    local config = {
        uid = "00150",
        connected_target_ports_id = {},
        target_ports_id = {17}
    }

    lu.assertEquals(c_ub_rule_cable_monitor_plugin:is_match_ub_rule(config, 'IEU', 2), true)
    lu.assertEquals(c_ub_rule_cable_monitor_plugin:is_match_ub_rule_refine_alarm("00150", 'IEU', 2, 17), true)

    package.loaded['pcie_device.client'] = back_mock_client
end