/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
 * CANN Open Software License Agreement Version 2.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * 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 FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#include <gtest/gtest.h>
#include <mockcpp/mokc.h>
#include <mockcpp/mockcpp.hpp>
#include "inner_net_dev_manager.h"

using namespace testing;
using namespace Hccl;

// Test suite class
class InnerNetDevManagerTest : public ::testing::Test {
protected:
    static void SetUpTestCase()
    {
        std::cout << "InnerNetDevTest SetUP" << std::endl;
    }

    static void TearDownTestCase()
    {
        std::cout << "InnerNetDevTest TearDown" << std::endl;
    }

    virtual void SetUp()
    {
        std::cout << "A Test case in InnerNetDevTest SetUP" << std::endl;
    }

    virtual void TearDown()
    {    
        GlobalMockObject::verify();
        std::cout << "A Test case in InnerNetDevTest TearDown" << std::endl;
    }
};

/**
* @tc.name  : AddDevice_ShouldReturnSuccess_WhenDeviceCreated
* @tc.number: InnerNetDevManager_Test_001
* @tc.desc  : 测试 AddDevice 成功创建设备并添加到管理器
*/
TEST_F(InnerNetDevManagerTest, AddDevice_ShouldReturnSuccess_WhenDeviceCreated)
{
    MOCKER(Hccl::HrtRaRdmaInit).stubs().will(returnValue(Hccl::RdmaHandle()));
    Hccl::NetDevInfo info = {0, Hccl::PortDeploymentType::DEV_NET, Hccl::LinkProtoType::RDMA, 0, Hccl::IpAddress("1.0.0.0")};

    Hccl::HcclNetDevice* hcclNetDev = nullptr;
    HcclResult result = Hccl::InnerNetDevManager::GetInstance().AddDevice(info, hcclNetDev);

    EXPECT_EQ(result, HCCL_SUCCESS);
    EXPECT_NE(hcclNetDev, nullptr);
    delete hcclNetDev;
}

/**
* @tc.name  : AddDevice_ShouldReturnError_WhenDeviceCreationFails
* @tc.number: InnerNetDevManager_Test_002
* @tc.desc  : 测试 AddDevice 创建设备失败时返回错误
*/
TEST_F(InnerNetDevManagerTest, AddDevice_ShouldReturnError_WhenDeviceCreationFails)
{
    // 模拟 new HcclNetDev 返回 nullptr
    // 由于无法 mock new,此处无法直接测试,但可以模拟失败场景
    // 例如:通过修改内存限制或使用其他方式,但此处无法实现,因此跳过
    // 本测试用例仅用于说明逻辑
    SUCCEED();
}

/**
* @tc.name  : RemoveDevice_ShouldReturnSuccess_WhenDeviceExists
* @tc.number: InnerNetDevManager_Test_003
* @tc.desc  : 测试 RemoveDevice 成功移除设备
*/
TEST_F(InnerNetDevManagerTest, RemoveDevice_ShouldReturnSuccess_WhenDeviceExists)
{
    MOCKER(Hccl::HrtRaRdmaInit).stubs().will(returnValue(Hccl::RdmaHandle()));
    Hccl::NetDevInfo info = {0, Hccl::PortDeploymentType::DEV_NET, Hccl::LinkProtoType::RDMA, 0, Hccl::IpAddress("1.0.0.0")};
    
    Hccl::HcclNetDevice* hcclNetDev = nullptr;
    Hccl::InnerNetDevManager::GetInstance().AddDevice(info, hcclNetDev);
    HcclResult result = Hccl::InnerNetDevManager::GetInstance().DeleteDevice(hcclNetDev);

    EXPECT_EQ(result, HCCL_SUCCESS);
}

/**
* @tc.name  : RemoveDevice_ShouldReturnError_WhenDeviceDoesNotExist
* @tc.number: InnerNetDevManager_Test_004
* @tc.desc  : 测试 RemoveDevice 设备不存在时返回错误
*/
TEST_F(InnerNetDevManagerTest, RemoveDevice_ShouldReturnError_WhenDeviceDoesNotExist)
{
    MOCKER(Hccl::HrtRaRdmaInit).stubs().will(returnValue(Hccl::RdmaHandle()));
    Hccl::NetDevInfo info = {0, Hccl::PortDeploymentType::DEV_NET, Hccl::LinkProtoType::RDMA, 0, Hccl::IpAddress("1.0.0.0")};
    HcclResult result = Hccl::InnerNetDevManager::GetInstance().RemoveDevice(info);

    EXPECT_EQ(result, HCCL_SUCCESS);
}