-- 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 fw_info_collector = require 'upgrade_subject.fw_info_collector'

TestFwInfoCollector = {}

function TestFwInfoCollector:test_insert_fw_info()
    fw_info_collector:clean('test_firmware')
    fw_info_collector:insert_fw_info('test_firmware', '1', '2', '3')
    lu.assertEquals(fw_info_collector.test_firmware.fw_info_list[1].component_id, '1')
    lu.assertEquals(fw_info_collector.test_firmware.fw_info_list[1].component_id_ex, '2')
    lu.assertEquals(fw_info_collector.test_firmware.fw_info_list[1].uid, '3')

    fw_info_collector:clean('test_firmware')
end

function TestFwInfoCollector:test_insert_fw_info_without_clean()
    fw_info_collector:insert_fw_info('test_firmware_fw_info', '1', '2', '3')
    lu.assertEquals(fw_info_collector.test_firmware_fw_info.fw_info_list[1].component_id, '1')
    lu.assertEquals(fw_info_collector.test_firmware_fw_info.fw_info_list[1].component_id_ex, '2')
    lu.assertEquals(fw_info_collector.test_firmware_fw_info.fw_info_list[1].uid, '3')

    fw_info_collector:clean('test_firmware')
end

function TestFwInfoCollector:test_insert_hpm_version()
    fw_info_collector:clean('test_firmware')
    fw_info_collector:insert_hpm_version('test_firmware', '1')
    lu.assertEquals(fw_info_collector.test_firmware.hpm_version, '1')

    fw_info_collector:clean('test_firmware')
end

function TestFwInfoCollector:test_insert_hpm_version_without_clean()
    fw_info_collector:insert_hpm_version('test_firmware_hpm_version', '1')
    lu.assertEquals(fw_info_collector.test_firmware_hpm_version.hpm_version, '1')

    fw_info_collector:clean('test_firmware')
end

function TestFwInfoCollector:test_get_all_value_from_fw_info_list()
    fw_info_collector:clean('test_firmware1')
    fw_info_collector:clean('test_firmware2')

    local component_id, component_id_ex, uid = fw_info_collector:get_register_info_from_fw_info_list('test_firmware1')
    lu.assertEquals(component_id, '')
    lu.assertEquals(component_id_ex, '')
    lu.assertEquals(uid, '')

    fw_info_collector:insert_fw_info('test_firmware1', '1', '2', '3')
    fw_info_collector:insert_fw_info('test_firmware1', '4', '5', '6')
    fw_info_collector:insert_fw_info('test_firmware1', '1', '1', '2')
    fw_info_collector:insert_fw_info('test_firmware1', '1', '2', '3')
    fw_info_collector:insert_fw_info('test_firmware2', '1', '2', '3')

    local component_id1, component_id_ex1, uid1 = fw_info_collector:get_register_info_from_fw_info_list('test_firmware1')
    local component_id2, component_id_ex2, uid2 = fw_info_collector:get_register_info_from_fw_info_list('test_firmware2')
    lu.assertEquals(component_id1, '1,4,1')
    lu.assertEquals(component_id_ex1, '2,5,1')
    lu.assertEquals(uid1, '3,6,2')
    lu.assertEquals(component_id2, '1')
    lu.assertEquals(component_id_ex2, '2')
    lu.assertEquals(uid2, '3')

    fw_info_collector:clean('test_firmware1')
    fw_info_collector:clean('test_firmware2')
end

function TestFwInfoCollector:test_get_hpm_version()
    fw_info_collector:clean('test_firmware')

    local result = fw_info_collector:get_hpm_version('test_firmware')
    lu.assertEquals(result, '')

    fw_info_collector:insert_hpm_version('test_firmware', '1.2.3')
    result = fw_info_collector:get_hpm_version('test_firmware')
    lu.assertEquals(result, '1.2.3')

    fw_info_collector:clean('test_firmware')
end