-- 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 manage = require 'frudata_object.manage'
local lu = require 'luaunit'

function Test_Manage:test_is_leap_year()
    local year1 = 1900
    lu.assertEquals(manage.is_leap_year(year1), false)
    local year2 = 2000
    lu.assertEquals(manage.is_leap_year(year2), true)
    local year3 = 2028
    lu.assertEquals(manage.is_leap_year(year3), true)
    local year4 = 2027
    lu.assertEquals(manage.is_leap_year(year4), false)
end

function Test_Manage:test_get_days_in_month()
    local year1 = 1900
    local month1 = 2
    lu.assertEquals(manage.get_days_in_month(year1, month1), 28)
    local year2 = 2028
    local month2 = 2
    lu.assertEquals(manage.get_days_in_month(year2, month2), 29)
    local year3 = 2027
    local month3 = 10
    lu.assertEquals(manage.get_days_in_month(year3, month3), 31)
    local year4 = 2000
    local month4 = 4
    lu.assertEquals(manage.get_days_in_month(year4, month4), 30)
end

function Test_Manage:test_has_manufactured_field()
    local fru_id = 1
    local info1 = "Manufactured=2028-01-01"
    local timestamp1 = manage.has_manufactured_field(fru_id, info1)
    lu.assertEquals(timestamp1, 1830268800)
    local info2 = "Manufactured=01-012028"
    local timestamp2 = manage.has_manufactured_field(fru_id, info2)
    lu.assertEquals(timestamp2, nil)
    local info3 = "Manufactured=2020-00-12"
    local timestamp3 = manage.has_manufactured_field(fru_id, info3)
    lu.assertEquals(timestamp3, nil)
    local info4 = "Manufactured=2020-12-99"
    local timestamp4 = manage.has_manufactured_field(fru_id, info4)
    lu.assertEquals(timestamp4, nil)
end

function Test_Manage:test_update_mfg_date_by_manufactured()
    local fru_id = 1
    local manufactured_timestamp1 = 1830268800
    local obj1 = {["MfgDate"] = "", ["BoardCustomInfo"] = "Manufactured=2028-01-01"}
    manage.update_mfg_date_by_manufactured(manufactured_timestamp1, fru_id, obj1)
    lu.assertEquals(obj1["MfgDate"], '1996/01/01 Mon 00:00:00')
    local manufactured_timestamp2 = 1687881600
    local obj2 = {["MfgDate"] = "", ["BoardCustomInfo"] = "Manufactured=2023-06-28"}
    manage.update_mfg_date_by_manufactured(manufactured_timestamp2, fru_id, obj2)
    lu.assertEquals(obj2["MfgDate"], '2023/06/28 Wed 00:00:00')
end