-- 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 memory_data_keeping = require 'basic_cooling.data_keeping.memory_data_keeping'
local MEMORY_INTERFACE <const> = "bmc.kepler.Systems.Memory"

TestMemoryDataKeeping = {}

function TestMemoryDataKeeping:test_action_after_obj_removed()
    local tmp = {
        memory_path_map = {
            ['/bmc/kepler/Systems/1/Memory/1'] = 'test1',
            ['/bmc/kepler/Systems/1/Memory/2'] = 'test1',
            ['/bmc/kepler/Systems/1/Memory/3'] = 'test1',
            ['/bmc/kepler/Systems/1/Memory/4'] = 'test1',
        },
        objs = {
            ['/bmc/kepler/Systems/1/Memory/1']= {
                [MEMORY_INTERFACE] = {
                    ['OriginalPartNumber'] = 'test1'
                }
            },
            ['/bmc/kepler/Systems/1/Memory/2'] = {
                [MEMORY_INTERFACE] = {
                    ['OriginalPartNumber'] = 'test1'
                }
            },
            ['/bmc/kepler/Systems/1/Memory/3'] = {
                [MEMORY_INTERFACE] = {
                    ['OriginalPartNumber'] = 'test1'
                }
            },
            ['/bmc/kepler/Systems/1/Memory/4'] = {
                [MEMORY_INTERFACE] = {
                    ['OriginalPartNumber'] = 'test2'
                }
            }
        },
        loaded_memory_part_number = {
            ['test1'] = 3,
            ['test2'] = 1
        }
    }
    local path = '/bmc/kepler/Systems/1/Memory/1'
    local ok = pcall(function()
        return memory_data_keeping.action_after_obj_removed(tmp, path, MEMORY_INTERFACE)
    end)
    lu.assertEquals(tmp.memory_path_map[path], nil)
    lu.assertEquals(tmp.loaded_memory_part_number['test1'], 2)
    lu.assertEquals(tmp.loaded_memory_part_number['test2'], 1)
    -- 不存在的path，则不会改变任何数据
    path = '/bmc/kepler/Systems/1/Memory/5'
    local ok = pcall(function()
        return memory_data_keeping.action_after_obj_removed(tmp, path, MEMORY_INTERFACE)
    end)
    lu.assertEquals(tmp.memory_path_map[path], nil)
    lu.assertEquals(tmp.loaded_memory_part_number['test1'], 2)
    lu.assertEquals(tmp.loaded_memory_part_number['test2'], 1)
end

function TestMemoryDataKeeping:test_action_after_property_changed()
    local tmp = {
        memory_path_map = {
            ['/bmc/kepler/Systems/1/Memory/1'] = 'test1',
            ['/bmc/kepler/Systems/1/Memory/2'] = 'test1',
            ['/bmc/kepler/Systems/1/Memory/3'] = 'test1',
            ['/bmc/kepler/Systems/1/Memory/4'] = 'test1',
        },
        objs = {
            ['/bmc/kepler/Systems/1/Memory/1']= {
                [MEMORY_INTERFACE] = {
                    ['OriginalPartNumber'] = 'test1'
                }
            },
            ['/bmc/kepler/Systems/1/Memory/2'] = {
                [MEMORY_INTERFACE] = {
                    ['OriginalPartNumber'] = 'test1'
                }
            },
            ['/bmc/kepler/Systems/1/Memory/3'] = {
                [MEMORY_INTERFACE] = {
                    ['OriginalPartNumber'] = 'test1'
                }
            },
            ['/bmc/kepler/Systems/1/Memory/4'] = {
                [MEMORY_INTERFACE] = {
                    ['OriginalPartNumber'] = 'test2'
                }
            }
        },
        loaded_memory_part_number = {
            ['test1'] = 3,
            ['test2'] = 1
        }
    }
    local path = '/bmc/kepler/Systems/1/Memory/1'
    local prop = 'OriginalPartNumber'
    local value = 'test3'
    local ok = pcall(function()
        return memory_data_keeping.action_after_property_changed(tmp, path, MEMORY_INTERFACE, prop, value)
    end)
    lu.assertEquals(ok, true)
    lu.assertEquals(tmp.loaded_memory_part_number['test1'], 2)
    lu.assertEquals(tmp.loaded_memory_part_number['test2'], 1)
    lu.assertEquals(tmp.loaded_memory_part_number['test3'], 1)
end