Router plugin
This commit is contained in:
31
pkg/plugin/implementation/router/cmd/plugin.go
Normal file
31
pkg/plugin/implementation/router/cmd/plugin.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
definition "github.com/beckn/beckn-onix/pkg/plugin/definition"
|
||||
router "github.com/beckn/beckn-onix/pkg/plugin/implementation/router"
|
||||
)
|
||||
|
||||
// RouterProvider provides instances of Router.
|
||||
type RouterProvider struct{}
|
||||
|
||||
// New initializes a new Router instance.
|
||||
func (rp RouterProvider) New(ctx context.Context, config map[string]string) (definition.Router, func() error, error) {
|
||||
if ctx == nil {
|
||||
return nil, nil, errors.New("context cannot be nil")
|
||||
}
|
||||
|
||||
// Parse the routing_config key from the config map
|
||||
routingConfig, ok := config["routing_config"]
|
||||
if !ok {
|
||||
return nil, nil, errors.New("routing_config is required in the configuration")
|
||||
}
|
||||
return router.New(ctx, &router.Config{
|
||||
RoutingConfig: routingConfig,
|
||||
})
|
||||
}
|
||||
|
||||
// Provider is the exported symbol that the plugin manager will look for.
|
||||
var Provider definition.RouterProvider = RouterProvider{}
|
||||
145
pkg/plugin/implementation/router/cmd/plugin_test.go
Normal file
145
pkg/plugin/implementation/router/cmd/plugin_test.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// setupTestConfig creates a temporary directory and writes a sample routing rules file.
|
||||
func setupTestConfig(t *testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
// Create a temporary directory for the routing rules
|
||||
configDir, err := os.MkdirTemp("", "routing_rules")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp directory: %v", err)
|
||||
}
|
||||
|
||||
// Define sample routing rules
|
||||
rulesContent := `
|
||||
routing_rules:
|
||||
- domain: "ONDC:TRV11"
|
||||
version: "2.0.0"
|
||||
routing_type: "url"
|
||||
target:
|
||||
url: "https://services-backend/trv/v1"
|
||||
endpoints:
|
||||
- select
|
||||
- init
|
||||
- confirm
|
||||
- status
|
||||
|
||||
- domain: "ONDC:TRV11"
|
||||
version: "2.0.0"
|
||||
routing_type: "msgq"
|
||||
target:
|
||||
topic_id: "trv_topic_id1"
|
||||
endpoints:
|
||||
- search
|
||||
`
|
||||
|
||||
// Write the routing rules to a file
|
||||
rulesFilePath := filepath.Join(configDir, "routing_rules.yaml")
|
||||
if err := os.WriteFile(rulesFilePath, []byte(rulesContent), 0644); err != nil {
|
||||
t.Fatalf("Failed to write routing rules file: %v", err)
|
||||
}
|
||||
|
||||
return rulesFilePath
|
||||
}
|
||||
|
||||
// TestRouterProvider_Success tests the RouterProvider implementation for success cases.
|
||||
func TestRouterProvider_Success(t *testing.T) {
|
||||
rulesFilePath := setupTestConfig(t)
|
||||
defer os.RemoveAll(filepath.Dir(rulesFilePath))
|
||||
|
||||
// Define test cases
|
||||
tests := []struct {
|
||||
name string
|
||||
ctx context.Context
|
||||
config map[string]string
|
||||
}{
|
||||
{
|
||||
name: "Valid configuration",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{
|
||||
"routing_config": rulesFilePath,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
provider := RouterProvider{}
|
||||
router, closeFunc, err := provider.New(tt.ctx, tt.config)
|
||||
|
||||
// Ensure no error occurred
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure the router and close function are not nil
|
||||
if router == nil {
|
||||
t.Error("expected a non-nil Router instance, got nil")
|
||||
}
|
||||
if closeFunc == nil {
|
||||
t.Error("expected a non-nil close function, got nil")
|
||||
}
|
||||
|
||||
// Test the close function
|
||||
if err := closeFunc(); err != nil {
|
||||
t.Errorf("close function returned an error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouterProvider_Failure tests the RouterProvider implementation for failure cases.
|
||||
func TestRouterProvider_Failure(t *testing.T) {
|
||||
rulesFilePath := setupTestConfig(t)
|
||||
defer os.RemoveAll(filepath.Dir(rulesFilePath))
|
||||
|
||||
// Define test cases
|
||||
tests := []struct {
|
||||
name string
|
||||
ctx context.Context
|
||||
config map[string]string
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
name: "Empty routing config path",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{
|
||||
"routing_config": "",
|
||||
},
|
||||
expectedError: "failed to load routing rules: routing_config path is empty",
|
||||
},
|
||||
{
|
||||
name: "Missing routing config key",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{},
|
||||
expectedError: "routing_config is required in the configuration",
|
||||
},
|
||||
{
|
||||
name: "Nil context",
|
||||
ctx: nil,
|
||||
config: map[string]string{"routing_config": rulesFilePath},
|
||||
expectedError: "context cannot be nil",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
provider := RouterProvider{}
|
||||
_, _, err := provider.New(tt.ctx, tt.config)
|
||||
|
||||
// Check for expected error
|
||||
if err == nil || !strings.Contains(err.Error(), tt.expectedError) {
|
||||
t.Errorf("expected error %q, got %v", tt.expectedError, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user