Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tools/fxconfig/internal/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
138 changes: 138 additions & 0 deletions tools/fxconfig/internal/config/validate_notifications_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}