-- Copyright (c) 2024 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 link_service = require 'link_volume_array_drive_service'
local drive_collection = require 'drive.drive_collection'
local array_collection = require 'array.array_collection'

TestLinkVolumeArrayDriveServiceArrayId = {}

function TestLinkVolumeArrayDriveServiceArrayId:test_clear_refdiskarrayid_when_array_missing()
    local drive1 = { RefControllerId = 0, RefDiskArrayId = 1, Name = 'Disk0' }
    local drive2 = { RefControllerId = 0, RefDiskArrayId = 2, Name = 'Disk1' }
    array_collection.get_instance().get_ctrl_array_by_id = function(_, _)
        return nil
    end

    local inst = link_service.get_instance()
    inst:init()

    drive_collection.get_instance().on_update_array_id:emit({ drive1, drive2 })

    lu.assertEquals(drive1.RefDiskArrayId, require('common_def').INVALID_U16)
    lu.assertEquals(drive2.RefDiskArrayId, require('common_def').INVALID_U16)
end

function TestLinkVolumeArrayDriveServiceArrayId:test_clear_refdiskarrayid_when_not_in_array_refdrives()
    local common_def = require 'common_def'
    local drive1 = { RefControllerId = 0, RefDiskArrayId = 1, Name = 'Disk0' }
    local drive2 = { RefControllerId = 0, RefDiskArrayId = 1, Name = 'Disk1' }

    array_collection.get_instance().get_ctrl_array_by_id = function()
        return { RefDrives = { 'Disk0' } }
    end

    drive_collection.get_instance().on_update_array_id:emit({ drive1, drive2 })

    lu.assertEquals(drive1.RefDiskArrayId, 1)
    lu.assertEquals(drive2.RefDiskArrayId, common_def.INVALID_U16)
end

