151 lines
4.3 KiB
Go
151 lines
4.3 KiB
Go
package testutil
|
|
|
|
import (
|
|
"platform/pkg/sdks/baiyin"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
// MockCloudClient 是CloudClient接口的测试实现
|
|
type MockCloudClient struct {
|
|
// 存储预期结果的字段
|
|
EdgesMock func(param baiyin.CloudEdgesReq) (*baiyin.CloudEdgesResp, error)
|
|
ConnectMock func(param baiyin.CloudConnectReq) error
|
|
DisconnectMock func(param baiyin.CloudDisconnectReq) (int, error)
|
|
AutoQueryMock func() (baiyin.CloudConnectResp, error)
|
|
|
|
// 记录调用历史
|
|
EdgesCalls []baiyin.CloudEdgesReq
|
|
ConnectCalls []baiyin.CloudConnectReq
|
|
DisconnectCalls []baiyin.CloudDisconnectReq
|
|
AutoQueryCalls int
|
|
|
|
// 用于并发安全
|
|
mu sync.Mutex
|
|
}
|
|
|
|
// 确保MockCloudClient实现了CloudClient接口
|
|
var _ baiyin.CloudClient = (*MockCloudClient)(nil)
|
|
|
|
func (m *MockCloudClient) CloudEdges(param baiyin.CloudEdgesReq) (*baiyin.CloudEdgesResp, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.EdgesCalls = append(m.EdgesCalls, param)
|
|
if m.EdgesMock != nil {
|
|
return m.EdgesMock(param)
|
|
}
|
|
return &baiyin.CloudEdgesResp{}, nil
|
|
}
|
|
|
|
func (m *MockCloudClient) CloudConnect(param baiyin.CloudConnectReq) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.ConnectCalls = append(m.ConnectCalls, param)
|
|
if m.ConnectMock != nil {
|
|
return m.ConnectMock(param)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MockCloudClient) CloudDisconnect(param baiyin.CloudDisconnectReq) (int, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.DisconnectCalls = append(m.DisconnectCalls, param)
|
|
if m.DisconnectMock != nil {
|
|
return m.DisconnectMock(param)
|
|
}
|
|
return 0, nil
|
|
}
|
|
|
|
func (m *MockCloudClient) CloudAutoQuery() (baiyin.CloudConnectResp, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.AutoQueryCalls++
|
|
if m.AutoQueryMock != nil {
|
|
return m.AutoQueryMock()
|
|
}
|
|
return baiyin.CloudConnectResp{}, nil
|
|
}
|
|
|
|
// SetupCloudClientMock 替换全局CloudClient为测试实现并在测试完成后恢复
|
|
func SetupCloudClientMock(t *testing.T) *MockCloudClient {
|
|
mock := &MockCloudClient{
|
|
EdgesMock: func(param baiyin.CloudEdgesReq) (*baiyin.CloudEdgesResp, error) {
|
|
panic("not implemented")
|
|
},
|
|
ConnectMock: func(param baiyin.CloudConnectReq) error {
|
|
panic("not implemented")
|
|
},
|
|
DisconnectMock: func(param baiyin.CloudDisconnectReq) (int, error) {
|
|
panic("not implemented")
|
|
},
|
|
AutoQueryMock: func() (baiyin.CloudConnectResp, error) {
|
|
panic("not implemented")
|
|
},
|
|
}
|
|
baiyin.Cloud = mock
|
|
|
|
return mock
|
|
}
|
|
|
|
// MockGatewayClient 是GatewayClient接口的测试实现
|
|
type MockGatewayClient struct {
|
|
Host string
|
|
}
|
|
|
|
// 确保MockGatewayClient实现了GatewayClient接口
|
|
var _ baiyin.GatewayClient = (*MockGatewayClient)(nil)
|
|
|
|
func (m *MockGatewayClient) GatewayPortConfigs(params []baiyin.PortConfigsReq) error {
|
|
testGatewayBase.mu.Lock()
|
|
defer testGatewayBase.mu.Unlock()
|
|
testGatewayBase.PortConfigsCalls = append(testGatewayBase.PortConfigsCalls, params)
|
|
if testGatewayBase.PortConfigsMock != nil {
|
|
return testGatewayBase.PortConfigsMock(m, params)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MockGatewayClient) GatewayPortActive(param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error) {
|
|
testGatewayBase.mu.Lock()
|
|
defer testGatewayBase.mu.Unlock()
|
|
testGatewayBase.PortActiveCalls = append(testGatewayBase.PortActiveCalls, param)
|
|
if testGatewayBase.PortActiveMock != nil {
|
|
return testGatewayBase.PortActiveMock(m, param...)
|
|
}
|
|
return map[string]baiyin.PortData{}, nil
|
|
}
|
|
|
|
type GatewayClientIns struct {
|
|
|
|
// 存储预期结果的字段
|
|
PortConfigsMock func(c *MockGatewayClient, params []baiyin.PortConfigsReq) error
|
|
PortActiveMock func(c *MockGatewayClient, param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error)
|
|
|
|
// 记录调用历史
|
|
PortConfigsCalls [][]baiyin.PortConfigsReq
|
|
PortActiveCalls [][]baiyin.PortActiveReq
|
|
|
|
// 用于并发安全
|
|
mu sync.Mutex
|
|
}
|
|
|
|
var testGatewayBase = &GatewayClientIns{
|
|
PortConfigsMock: func(c *MockGatewayClient, params []baiyin.PortConfigsReq) error {
|
|
panic("not implemented")
|
|
},
|
|
PortActiveMock: func(c *MockGatewayClient, param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error) {
|
|
panic("not implemented")
|
|
},
|
|
}
|
|
|
|
// SetupGatewayClientMock 创建一个MockGatewayClient并提供替换函数
|
|
func SetupGatewayClientMock(t *testing.T) *GatewayClientIns {
|
|
baiyin.GatewayInitializer = func(url, username, password string) baiyin.GatewayClient {
|
|
return &MockGatewayClient{
|
|
Host: url,
|
|
}
|
|
}
|
|
return testGatewayBase
|
|
}
|