-- 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 drive_obj = require 'drive.drive_object'
local sml = require 'sml'

TestSataHwDefinedSmart = {}

local pre_sml_func = sml.pd_get_sata_hwdefined_smart_info

function TestSataHwDefinedSmart:setUp()
    sml.pd_get_sata_hwdefined_smart_info = pre_sml_func
end

function TestSataHwDefinedSmart:tearDown()
    sml.pd_get_sata_hwdefined_smart_info = pre_sml_func
end

function TestSataHwDefinedSmart:test_collect_sata_hw_defined_smart_success()
    local mock_drive = {
        Name = "Disk1",
        RefControllerId = 0,
        device_id = 1,
        sata_hw_defined_smart_info = {},
        collect_sata_hw_defined_smart = drive_obj.collect_sata_hw_defined_smart
    }

    local test_data = "ABCD"
    sml.pd_get_sata_hwdefined_smart_info = function(ref_controller_id, device_id)
        lu.assertEquals(ref_controller_id, 0)
        lu.assertEquals(device_id, 1)
        return test_data
    end

    local ctx = {}
    local result = mock_drive:collect_sata_hw_defined_smart(ctx)
    lu.assertIsTrue(result)
    lu.assertEquals(mock_drive.sata_hw_defined_smart_info["HwDefinedSmartData"], "41424344")
end

function TestSataHwDefinedSmart:test_collect_sata_hw_defined_smart_empty_string()
    local mock_drive = {
        Name = "Disk2",
        RefControllerId = 1,
        device_id = 2,
        sata_hw_defined_smart_info = {},
        collect_sata_hw_defined_smart = drive_obj.collect_sata_hw_defined_smart
    }

    sml.pd_get_sata_hwdefined_smart_info = function(ref_controller_id, device_id)
        return ""
    end

    local ctx = {}
    local result = mock_drive:collect_sata_hw_defined_smart(ctx)
    lu.assertIsTrue(result)
    lu.assertEquals(mock_drive.sata_hw_defined_smart_info["HwDefinedSmartData"], "")
end

function TestSataHwDefinedSmart:test_collect_sata_hw_defined_smart_fail()
    local mock_drive = {
        Name = "Disk3",
        RefControllerId = 0,
        device_id = 3,
        sata_hw_defined_smart_info = {},
        collect_sata_hw_defined_smart = drive_obj.collect_sata_hw_defined_smart
    }

    sml.pd_get_sata_hwdefined_smart_info = function(ref_controller_id, device_id)
        error("mock error")
    end

    local ctx = {}
    local result = mock_drive:collect_sata_hw_defined_smart(ctx)
    lu.assertIsFalse(result)
    lu.assertEquals(mock_drive.sata_hw_defined_smart_info["HwDefinedSmartData"], "")
end

return TestSataHwDefinedSmart
