-- Copyright (c) 2025 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 c_ubport_service = require 'unit_manager.class.unit.ubinterface.ubport_service'
local unit_manager = require 'unit_manager.unit_manager'
local c_uninterface_card = require 'unit_manager.class.unit.ubinterface.ub_interface_card'

TestUBInterface = {}

function TestUBInterface:test_add_and_delete_ubcard_obj()
    unit_manager.ubinterface_cards = {}
    unit_manager.unit_collection = {}
    local ubinterface_card_obj = {
        Description = 'UB_Board',
        BoardType = 'UBInterfaceCard',
        DeviceName = 'UB_Board',
        position = '0101',
        Position = 'PCIeRiser1',
        get_prop = function ()
            return 'UB_Board'
        end,
        set_prop = function (str, obj)
            return obj[str]
        end
    }
    local riser_card_obj = {
        DeviceName = 'PCIeRiser1',
        get_prop = function ()
            return 'PCIeRiser1'
        end
    }
    c_uninterface_card.new = function ()
        return ubinterface_card_obj
    end
    unit_manager.unit_collection['01'] = riser_card_obj
    unit_manager:on_add_object('UBInterfaceCard', ubinterface_card_obj, '0101')
    lu.assertEquals(unit_manager.ubinterface_cards[1], ubinterface_card_obj)
    
    unit_manager:on_delete_object('UBInterfaceCard', ubinterface_card_obj, '0101')
    unit_manager.unit_collection['01'] = nil
    lu.assertEquals(#unit_manager.ubinterface_cards, 0)
end

function TestUBInterface:test_add_and_delete_ubport_obj()
    local ubport_service = c_ubport_service.new()
    local ubport_obj = {
        LinkStatus = 'nolink',
        PortId = 1,
        CableDetection = 0,
        LinkStatusFromSMC = 1,
        property_changed = {
            on = function (self, cb)
                cb('LinkStatusFromSMC', 0)
                cb('LinkStatusFromSMC', 1)
                cb('LinkStatusFromSMC', 255)
            end
        }
    }
    ubport_service:on_add_object('UBPort', ubport_obj, 1)
    lu.assertEquals(ubport_service.ubport_obj_table[1].mds_obj, ubport_obj)
    lu.assertEquals(ubport_service.ubport_obj_table[1].position, 1)
    
    ubport_service:on_delete_object('UBPort', ubport_obj, 1)
    lu.assertEquals(#ubport_service.ubport_obj_table, 0)
end