From e72703ad209f5fc2f3e2133282df35b939d3709e Mon Sep 17 00:00:00 2001 From: ARYANPATEL-BIT Date: Wed, 6 May 2026 01:13:30 +0530 Subject: [PATCH] fix(fxconfig): add validation for NotificationsConfig and QueriesConfig Signed-off-by: ARYANPATEL-BIT --- tools/fxconfig/internal/config/validate.go | 15 ++ .../config/validate_notifications_test.go | 138 ++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 tools/fxconfig/internal/config/validate_notifications_test.go diff --git a/tools/fxconfig/internal/config/validate.go b/tools/fxconfig/internal/config/validate.go index 71a06de1..1a8d7eb5 100644 --- a/tools/fxconfig/internal/config/validate.go +++ b/tools/fxconfig/internal/config/validate.go @@ -40,6 +40,21 @@ func (c *OrdererConfig) Validate(vctx validation.Context) error { return c.EndpointServiceConfig.Validate(vctx) } +// Validate validates Queries configuration. +// Check endpoint service configuration. +func (c *QueriesConfig) Validate(vctx validation.Context) error { + return c.EndpointServiceConfig.Validate(vctx) +} + +// Validate validates Notifications configuration. +// Check waiting timeout and endpoint service configuration. +func (c *NotificationsConfig) Validate(vctx validation.Context) error { + if c.WaitingTimeout <= 0 { + return errors.New("waiting timeout must be greater than zero") + } + return c.EndpointServiceConfig.Validate(vctx) +} + // Validate validates service endpoint configuration. // Checks address, timeout, and TLS settings for a given service. func (c *EndpointServiceConfig) Validate(vctx validation.Context) error { diff --git a/tools/fxconfig/internal/config/validate_notifications_test.go b/tools/fxconfig/internal/config/validate_notifications_test.go new file mode 100644 index 00000000..9dee7c77 --- /dev/null +++ b/tools/fxconfig/internal/config/validate_notifications_test.go @@ -0,0 +1,138 @@ +package config + +import ( + "testing" + "time" + + "github.com/hyperledger/fabric-x/tools/fxconfig/internal/validation" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNotificationsConfig_Validate(t *testing.T) { + t.Parallel() + + validTLS := &TLSConfig{Enabled: new(bool)} + *validTLS.Enabled = false + + vctx := validation.Context{} + + tests := []struct { + name string + cfg NotificationsConfig + expectedError string + }{ + { + name: "valid config", + cfg: NotificationsConfig{ + EndpointServiceConfig: EndpointServiceConfig{ + Address: "localhost:1234", + ConnectionTimeout: 5 * time.Second, + TLS: validTLS, + }, + WaitingTimeout: 30 * time.Second, + }, + expectedError: "", + }, + { + name: "zero waiting timeout", + cfg: NotificationsConfig{ + EndpointServiceConfig: EndpointServiceConfig{ + Address: "localhost:1234", + ConnectionTimeout: 5 * time.Second, + TLS: validTLS, + }, + WaitingTimeout: 0, + }, + expectedError: "waiting timeout must be greater than zero", + }, + { + name: "negative waiting timeout", + cfg: NotificationsConfig{ + EndpointServiceConfig: EndpointServiceConfig{ + Address: "localhost:1234", + ConnectionTimeout: 5 * time.Second, + TLS: validTLS, + }, + WaitingTimeout: -1 * time.Second, + }, + expectedError: "waiting timeout must be greater than zero", + }, + { + name: "invalid embedded config", + cfg: NotificationsConfig{ + EndpointServiceConfig: EndpointServiceConfig{ + Address: "invalid-address", + ConnectionTimeout: 5 * time.Second, + TLS: validTLS, + }, + WaitingTimeout: 30 * time.Second, + }, + expectedError: "invalid address", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + err := tt.cfg.Validate(vctx) + if tt.expectedError != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.expectedError) + } else { + require.NoError(t, err) + } + }) + } +} + +func TestQueriesConfig_Validate(t *testing.T) { + t.Parallel() + + validTLS := &TLSConfig{Enabled: new(bool)} + *validTLS.Enabled = false + + vctx := validation.Context{} + + tests := []struct { + name string + cfg QueriesConfig + expectedError string + }{ + { + name: "valid config", + cfg: QueriesConfig{ + EndpointServiceConfig: EndpointServiceConfig{ + Address: "localhost:1234", + ConnectionTimeout: 5 * time.Second, + TLS: validTLS, + }, + }, + expectedError: "", + }, + { + name: "invalid embedded config", + cfg: QueriesConfig{ + EndpointServiceConfig: EndpointServiceConfig{ + Address: "invalid-address", + ConnectionTimeout: 5 * time.Second, + TLS: validTLS, + }, + }, + expectedError: "invalid address", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + err := tt.cfg.Validate(vctx) + if tt.expectedError != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.expectedError) + } else { + require.NoError(t, err) + } + }) + } +}