优化结构
This commit is contained in:
@@ -3,6 +3,7 @@ package integration
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -27,17 +28,19 @@ func (*integrationConfigRepoTestDouble) DeleteIntegrationConfig(context.Context,
|
||||
type integrationConnectionTesterDouble struct {
|
||||
calls int
|
||||
config *IntegrationConfig
|
||||
err error
|
||||
}
|
||||
|
||||
func (t *integrationConnectionTesterDouble) TestIntegration(_ context.Context, config *IntegrationConfig) error {
|
||||
t.calls++
|
||||
t.config = config
|
||||
return nil
|
||||
return t.err
|
||||
}
|
||||
|
||||
func TestCommunicationIntegrationDefinitionsAndValidation(t *testing.T) {
|
||||
for _, target := range []struct{ kind, provider string }{
|
||||
{IntegrationKindMQ, "emqx"},
|
||||
{IntegrationKindMQ, "kafka"},
|
||||
{IntegrationKindMQ, "rabbitmq"},
|
||||
{IntegrationKindWebSocket, "melody"},
|
||||
} {
|
||||
@@ -51,12 +54,49 @@ func TestCommunicationIntegrationDefinitionsAndValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestKafkaDefaultConfigSurvivesJSONRoundTrip(t *testing.T) {
|
||||
raw, err := json.Marshal(DefaultIntegrationConfig(IntegrationKindMQ, "kafka"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
values, err := decodeIntegrationObject(raw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := integrationInt64(values, "max_bytes", 0); got != 10485760 {
|
||||
t.Fatalf("max_bytes = %d, want 10485760; decoded=%#v", got, values["max_bytes"])
|
||||
}
|
||||
if err = ValidateIntegrationConfig(IntegrationKindMQ, "kafka", values); err != nil {
|
||||
t.Fatalf("JSON-decoded kafka defaults are invalid: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegrationInt64RejectsFractionalAndOverflowValues(t *testing.T) {
|
||||
values := map[string]any{"fractional": 1.5, "overflow": json.Number("9223372036854775808")}
|
||||
if got := integrationInt64(values, "fractional", -1); got != -1 {
|
||||
t.Fatalf("fractional value = %d, want fallback", got)
|
||||
}
|
||||
if got := integrationInt64(values, "overflow", -1); got != -1 {
|
||||
t.Fatalf("overflow value = %d, want fallback", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommunicationIntegrationValidationRejectsInvalidValues(t *testing.T) {
|
||||
rabbit := DefaultIntegrationConfig(IntegrationKindMQ, "rabbitmq")
|
||||
rabbit["port"] = 0
|
||||
if err := ValidateIntegrationConfig(IntegrationKindMQ, "rabbitmq", rabbit); err == nil {
|
||||
t.Fatal("invalid rabbitmq port was accepted")
|
||||
}
|
||||
kafka := DefaultIntegrationConfig(IntegrationKindMQ, "kafka")
|
||||
kafka["brokers"] = []string{"missing-port"}
|
||||
if err := ValidateIntegrationConfig(IntegrationKindMQ, "kafka", kafka); err == nil {
|
||||
t.Fatal("invalid kafka broker was accepted")
|
||||
}
|
||||
kafka = DefaultIntegrationConfig(IntegrationKindMQ, "kafka")
|
||||
kafka["max_bytes"] = 0
|
||||
if err := ValidateIntegrationConfig(IntegrationKindMQ, "kafka", kafka); err == nil {
|
||||
t.Fatal("invalid kafka byte limits were accepted")
|
||||
}
|
||||
websocket := DefaultIntegrationConfig(IntegrationKindWebSocket, "melody")
|
||||
websocket["path"] = "ws"
|
||||
if err := ValidateIntegrationConfig(IntegrationKindWebSocket, "melody", websocket); err == nil {
|
||||
@@ -97,6 +137,31 @@ func TestIntegrationConfigTestDoesNotPersistCandidate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegrationConfigTestReturnsSafeProviderFailure(t *testing.T) {
|
||||
underlying := errors.New("dial tcp 127.0.0.1:9092: connection refused")
|
||||
for _, test := range []struct {
|
||||
provider string
|
||||
name string
|
||||
}{
|
||||
{provider: "kafka", name: "Kafka"},
|
||||
{provider: "emqx", name: "EMQX"},
|
||||
{provider: "rabbitmq", name: "RabbitMQ"},
|
||||
} {
|
||||
t.Run(test.provider, func(t *testing.T) {
|
||||
values := DefaultIntegrationConfig(IntegrationKindMQ, test.provider)
|
||||
raw, _ := json.Marshal(values)
|
||||
usecase := NewIntegrationConfigUsecase(&integrationConfigRepoTestDouble{}, &integrationConnectionTesterDouble{err: underlying})
|
||||
err := usecase.Test(context.Background(), &IntegrationConfig{Kind: IntegrationKindMQ, Provider: test.provider, Values: raw})
|
||||
if err == nil || err.Error() != test.name+" 连接测试失败" {
|
||||
t.Fatalf("Test() error = %v", err)
|
||||
}
|
||||
if !errors.Is(err, underlying) {
|
||||
t.Fatalf("Test() error does not wrap the provider failure: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegrationConfigRejectsJSONNull(t *testing.T) {
|
||||
repo := &integrationConfigRepoTestDouble{}
|
||||
usecase := NewIntegrationConfigUsecase(repo, &integrationConnectionTesterDouble{})
|
||||
|
||||
Reference in New Issue
Block a user