重构远程接口,替换为Baiyin SDK & 添加实名认证处理逻辑
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"platform/pkg/remote"
|
||||
"platform/pkg/sdks/baiyin"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
@@ -9,15 +9,15 @@ import (
|
||||
// MockCloudClient 是CloudClient接口的测试实现
|
||||
type MockCloudClient struct {
|
||||
// 存储预期结果的字段
|
||||
EdgesMock func(param remote.CloudEdgesReq) (*remote.CloudEdgesResp, error)
|
||||
ConnectMock func(param remote.CloudConnectReq) error
|
||||
DisconnectMock func(param remote.CloudDisconnectReq) (int, error)
|
||||
AutoQueryMock func() (remote.CloudConnectResp, error)
|
||||
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 []remote.CloudEdgesReq
|
||||
ConnectCalls []remote.CloudConnectReq
|
||||
DisconnectCalls []remote.CloudDisconnectReq
|
||||
EdgesCalls []baiyin.CloudEdgesReq
|
||||
ConnectCalls []baiyin.CloudConnectReq
|
||||
DisconnectCalls []baiyin.CloudDisconnectReq
|
||||
AutoQueryCalls int
|
||||
|
||||
// 用于并发安全
|
||||
@@ -25,19 +25,19 @@ type MockCloudClient struct {
|
||||
}
|
||||
|
||||
// 确保MockCloudClient实现了CloudClient接口
|
||||
var _ remote.CloudClient = (*MockCloudClient)(nil)
|
||||
var _ baiyin.CloudClient = (*MockCloudClient)(nil)
|
||||
|
||||
func (m *MockCloudClient) CloudEdges(param remote.CloudEdgesReq) (*remote.CloudEdgesResp, error) {
|
||||
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 &remote.CloudEdgesResp{}, nil
|
||||
return &baiyin.CloudEdgesResp{}, nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudConnect(param remote.CloudConnectReq) error {
|
||||
func (m *MockCloudClient) CloudConnect(param baiyin.CloudConnectReq) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ConnectCalls = append(m.ConnectCalls, param)
|
||||
@@ -47,7 +47,7 @@ func (m *MockCloudClient) CloudConnect(param remote.CloudConnectReq) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudDisconnect(param remote.CloudDisconnectReq) (int, error) {
|
||||
func (m *MockCloudClient) CloudDisconnect(param baiyin.CloudDisconnectReq) (int, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.DisconnectCalls = append(m.DisconnectCalls, param)
|
||||
@@ -57,33 +57,33 @@ func (m *MockCloudClient) CloudDisconnect(param remote.CloudDisconnectReq) (int,
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudAutoQuery() (remote.CloudConnectResp, error) {
|
||||
func (m *MockCloudClient) CloudAutoQuery() (baiyin.CloudConnectResp, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.AutoQueryCalls++
|
||||
if m.AutoQueryMock != nil {
|
||||
return m.AutoQueryMock()
|
||||
}
|
||||
return remote.CloudConnectResp{}, nil
|
||||
return baiyin.CloudConnectResp{}, nil
|
||||
}
|
||||
|
||||
// SetupCloudClientMock 替换全局CloudClient为测试实现并在测试完成后恢复
|
||||
func SetupCloudClientMock(t *testing.T) *MockCloudClient {
|
||||
mock := &MockCloudClient{
|
||||
EdgesMock: func(param remote.CloudEdgesReq) (*remote.CloudEdgesResp, error) {
|
||||
EdgesMock: func(param baiyin.CloudEdgesReq) (*baiyin.CloudEdgesResp, error) {
|
||||
panic("not implemented")
|
||||
},
|
||||
ConnectMock: func(param remote.CloudConnectReq) error {
|
||||
ConnectMock: func(param baiyin.CloudConnectReq) error {
|
||||
panic("not implemented")
|
||||
},
|
||||
DisconnectMock: func(param remote.CloudDisconnectReq) (int, error) {
|
||||
DisconnectMock: func(param baiyin.CloudDisconnectReq) (int, error) {
|
||||
panic("not implemented")
|
||||
},
|
||||
AutoQueryMock: func() (remote.CloudConnectResp, error) {
|
||||
AutoQueryMock: func() (baiyin.CloudConnectResp, error) {
|
||||
panic("not implemented")
|
||||
},
|
||||
}
|
||||
remote.Cloud = mock
|
||||
baiyin.Cloud = mock
|
||||
|
||||
return mock
|
||||
}
|
||||
@@ -94,9 +94,9 @@ type MockGatewayClient struct {
|
||||
}
|
||||
|
||||
// 确保MockGatewayClient实现了GatewayClient接口
|
||||
var _ remote.GatewayClient = (*MockGatewayClient)(nil)
|
||||
var _ baiyin.GatewayClient = (*MockGatewayClient)(nil)
|
||||
|
||||
func (m *MockGatewayClient) GatewayPortConfigs(params []remote.PortConfigsReq) error {
|
||||
func (m *MockGatewayClient) GatewayPortConfigs(params []baiyin.PortConfigsReq) error {
|
||||
testGatewayBase.mu.Lock()
|
||||
defer testGatewayBase.mu.Unlock()
|
||||
testGatewayBase.PortConfigsCalls = append(testGatewayBase.PortConfigsCalls, params)
|
||||
@@ -106,42 +106,42 @@ func (m *MockGatewayClient) GatewayPortConfigs(params []remote.PortConfigsReq) e
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockGatewayClient) GatewayPortActive(param ...remote.PortActiveReq) (map[string]remote.PortData, error) {
|
||||
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]remote.PortData{}, nil
|
||||
return map[string]baiyin.PortData{}, nil
|
||||
}
|
||||
|
||||
type GatewayClientIns struct {
|
||||
|
||||
// 存储预期结果的字段
|
||||
PortConfigsMock func(c *MockGatewayClient, params []remote.PortConfigsReq) error
|
||||
PortActiveMock func(c *MockGatewayClient, param ...remote.PortActiveReq) (map[string]remote.PortData, error)
|
||||
PortConfigsMock func(c *MockGatewayClient, params []baiyin.PortConfigsReq) error
|
||||
PortActiveMock func(c *MockGatewayClient, param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error)
|
||||
|
||||
// 记录调用历史
|
||||
PortConfigsCalls [][]remote.PortConfigsReq
|
||||
PortActiveCalls [][]remote.PortActiveReq
|
||||
PortConfigsCalls [][]baiyin.PortConfigsReq
|
||||
PortActiveCalls [][]baiyin.PortActiveReq
|
||||
|
||||
// 用于并发安全
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
var testGatewayBase = &GatewayClientIns{
|
||||
PortConfigsMock: func(c *MockGatewayClient, params []remote.PortConfigsReq) error {
|
||||
PortConfigsMock: func(c *MockGatewayClient, params []baiyin.PortConfigsReq) error {
|
||||
panic("not implemented")
|
||||
},
|
||||
PortActiveMock: func(c *MockGatewayClient, param ...remote.PortActiveReq) (map[string]remote.PortData, error) {
|
||||
PortActiveMock: func(c *MockGatewayClient, param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error) {
|
||||
panic("not implemented")
|
||||
},
|
||||
}
|
||||
|
||||
// SetupGatewayClientMock 创建一个MockGatewayClient并提供替换函数
|
||||
func SetupGatewayClientMock(t *testing.T) *GatewayClientIns {
|
||||
remote.GatewayInitializer = func(url, username, password string) remote.GatewayClient {
|
||||
baiyin.GatewayInitializer = func(url, username, password string) baiyin.GatewayClient {
|
||||
return &MockGatewayClient{
|
||||
Host: url,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user