ISSUE 509 - feat: Add DeDi registry plugin implementation for lookup

This commit is contained in:
ameersohel45
2025-09-22 14:53:10 +05:30
parent de98c115b8
commit b385c18ee2
7 changed files with 871 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
package main
import (
"context"
"errors"
"strconv"
"github.com/beckn-one/beckn-onix/pkg/log"
"github.com/beckn-one/beckn-onix/pkg/plugin/definition"
"github.com/beckn-one/beckn-onix/pkg/plugin/implementation/dediregistry"
)
// dediRegistryProvider implements the RegistryLookupProvider interface for the DeDi registry plugin.
type dediRegistryProvider struct{}
// New creates a new DeDi registry plugin instance.
func (d dediRegistryProvider) New(ctx context.Context, config map[string]string) (definition.RegistryLookup, func() error, error) {
if ctx == nil {
return nil, nil, errors.New("context cannot be nil")
}
// Create dediregistry.Config directly from map - validation is handled by dediregistry.New
dediConfig := &dediregistry.Config{
BaseURL: config["baseURL"],
ApiKey: config["apiKey"],
NamespaceID: config["namespaceID"],
RegistryName: config["registryName"],
RecordName: config["recordName"],
}
// Parse timeout if provided
if timeoutStr, exists := config["timeout"]; exists && timeoutStr != "" {
if timeout, err := strconv.Atoi(timeoutStr); err == nil {
dediConfig.Timeout = timeout
}
}
log.Debugf(ctx, "DeDi Registry config mapped: %+v", dediConfig)
dediClient, closer, err := dediregistry.New(ctx, dediConfig)
if err != nil {
log.Errorf(ctx, err, "Failed to create DeDi registry instance")
return nil, nil, err
}
log.Infof(ctx, "DeDi Registry instance created successfully")
return dediClient, closer, nil
}
// Provider is the exported plugin instance
var Provider = dediRegistryProvider{}

View File

@@ -0,0 +1,97 @@
package main
import (
"context"
"testing"
)
func TestDediRegistryProvider_New(t *testing.T) {
ctx := context.Background()
provider := dediRegistryProvider{}
config := map[string]string{
"baseURL": "https://test.com",
"apiKey": "test-key",
"namespaceID": "test-namespace",
"registryName": "test-registry",
"recordName": "test-record",
"timeout": "30",
}
dediRegistry, closer, err := provider.New(ctx, config)
if err != nil {
t.Errorf("New() error = %v", err)
return
}
if dediRegistry == nil {
t.Error("New() returned nil dediRegistry")
}
if closer == nil {
t.Error("New() returned nil closer")
}
// Test cleanup
if err := closer(); err != nil {
t.Errorf("closer() error = %v", err)
}
}
func TestDediRegistryProvider_New_InvalidConfig(t *testing.T) {
ctx := context.Background()
provider := dediRegistryProvider{}
tests := []struct {
name string
config map[string]string
}{
{
name: "missing baseURL",
config: map[string]string{"apiKey": "test-key"},
},
{
name: "missing apiKey",
config: map[string]string{"baseURL": "https://test.com"},
},
{
name: "empty config",
config: map[string]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, _, err := provider.New(ctx, tt.config)
if err == nil {
t.Errorf("New() with %s should return error", tt.name)
}
})
}
}
func TestDediRegistryProvider_New_InvalidTimeout(t *testing.T) {
ctx := context.Background()
provider := dediRegistryProvider{}
config := map[string]string{
"baseURL": "https://test.com",
"apiKey": "test-key",
"namespaceID": "test-namespace",
"registryName": "test-registry",
"recordName": "test-record",
"timeout": "invalid",
}
// Invalid timeout should be ignored, not cause error
dediRegistry, closer, err := provider.New(ctx, config)
if err != nil {
t.Errorf("New() with invalid timeout should not return error, got: %v", err)
}
if dediRegistry == nil {
t.Error("New() should return valid registry even with invalid timeout")
}
if closer != nil {
closer()
}
}