fix: added comments
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Error represents an error response.
|
||||
// Error represents a standard error response.
|
||||
type Error struct {
|
||||
Code string `json:"code"`
|
||||
Paths string `json:"paths,omitempty"`
|
||||
@@ -18,7 +18,7 @@ func (e *Error) Error() string {
|
||||
return fmt.Sprintf("Error: Code=%s, Path=%s, Message=%s", e.Code, e.Paths, e.Message)
|
||||
}
|
||||
|
||||
// SchemaValidationErr represents a collection of schema validation failures.
|
||||
// SchemaValidationErr occurs when schema validation errors are encountered.
|
||||
type SchemaValidationErr struct {
|
||||
Errors []Error
|
||||
}
|
||||
@@ -32,6 +32,7 @@ func (e *SchemaValidationErr) Error() string {
|
||||
return strings.Join(errorMessages, "; ")
|
||||
}
|
||||
|
||||
// BecknError converts the SchemaValidationErr to an instance of Error.
|
||||
func (e *SchemaValidationErr) BecknError() *Error {
|
||||
if len(e.Errors) == 0 {
|
||||
return &Error{
|
||||
@@ -57,7 +58,7 @@ func (e *SchemaValidationErr) BecknError() *Error {
|
||||
}
|
||||
}
|
||||
|
||||
// SignValidationErr represents a collection of schema validation failures.
|
||||
// SignValidationErr occurs when signature validation fails.
|
||||
type SignValidationErr struct {
|
||||
error
|
||||
}
|
||||
@@ -75,7 +76,7 @@ func (e *SignValidationErr) BecknError() *Error {
|
||||
}
|
||||
}
|
||||
|
||||
// SignValidationErr represents a collection of schema validation failures.
|
||||
// BadReqErr occurs when a bad request is encountered.
|
||||
type BadReqErr struct {
|
||||
error
|
||||
}
|
||||
@@ -93,7 +94,7 @@ func (e *BadReqErr) BecknError() *Error {
|
||||
}
|
||||
}
|
||||
|
||||
// SignValidationErr represents a collection of schema validation failures.
|
||||
// NotFoundErr occurs when a requested endpoint is not found.
|
||||
type NotFoundErr struct {
|
||||
error
|
||||
}
|
||||
|
||||
@@ -10,17 +10,17 @@ import (
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// NewSignValidationErrf formats an error message according to a format specifier and arguments,and returns a new instance of SignValidationErr.
|
||||
// NewSignValidationErrf creates a new SignValidationErr with a formatted error message.
|
||||
func NewSignValidationErrf(format string, a ...any) *SignValidationErr {
|
||||
return &SignValidationErr{fmt.Errorf(format, a...)}
|
||||
}
|
||||
|
||||
// NewNotFoundErrf formats an error message according to a format specifier and arguments, and returns a new instance of NotFoundErr.
|
||||
// NewNotFoundErrf creates a new NotFoundErr with a formatted error message.
|
||||
func NewNotFoundErrf(format string, a ...any) *NotFoundErr {
|
||||
return &NotFoundErr{fmt.Errorf(format, a...)}
|
||||
}
|
||||
|
||||
// NewBadReqErrf formats an error message according to a format specifier and arguments, and returns a new instance of BadReqErr.
|
||||
// NewBadReqErrf creates a new BadReqErr with a formatted error message.
|
||||
func NewBadReqErrf(format string, a ...any) *BadReqErr {
|
||||
return &BadReqErr{fmt.Errorf(format, a...)}
|
||||
}
|
||||
|
||||
24
pkg/plugin/definition/router.go
Normal file
24
pkg/plugin/definition/router.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package definition
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// Route defines the structure for the Route returned.
|
||||
type Route struct {
|
||||
TargetType string // "url" or "msgq" or "bap" or "bpp"
|
||||
PublisherID string // For message queues
|
||||
URL *url.URL // For API calls
|
||||
}
|
||||
|
||||
// RouterProvider initializes the a new Router instance with the given config.
|
||||
type RouterProvider interface {
|
||||
New(ctx context.Context, config map[string]string) (Router, func() error, error)
|
||||
}
|
||||
|
||||
// Router defines the interface for routing requests.
|
||||
type Router interface {
|
||||
// Route determines the routing destination based on the request context.
|
||||
Route(ctx context.Context, url *url.URL, body []byte) (*Route, error)
|
||||
}
|
||||
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 routingConfig key from the config map
|
||||
routingConfig, ok := config["routingConfig"]
|
||||
if !ok {
|
||||
return nil, nil, errors.New("routingConfig 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 = RouterProvider{}
|
||||
101
pkg/plugin/implementation/router/cmd/plugin_test.go
Normal file
101
pkg/plugin/implementation/router/cmd/plugin_test.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// setupTestConfig creates a temporary directory and writes a sample routing rules file.
|
||||
func setupTestConfig(t *testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
// Get project root (assuming testData is in project root)
|
||||
_, filename, _, _ := runtime.Caller(0) // Path to plugin_test.go
|
||||
projectRoot := filepath.Dir(filepath.Dir(filename)) // Move up from cmd/
|
||||
yamlPath := filepath.Join(projectRoot, "testData", "bap_receiver.yaml")
|
||||
|
||||
// Copy to temp file (to test file loading logic)
|
||||
tempDir := t.TempDir()
|
||||
tempPath := filepath.Join(tempDir, "routingRules.yaml")
|
||||
content, err := os.ReadFile(yamlPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read test file: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(tempPath, content, 0644); err != nil {
|
||||
t.Fatalf("Failed to create temp config: %v", err)
|
||||
}
|
||||
|
||||
return tempPath
|
||||
}
|
||||
|
||||
// TestRouterProviderSuccess tests successful router creation.
|
||||
func TestRouterProviderSuccess(t *testing.T) {
|
||||
rulesFilePath := setupTestConfig(t)
|
||||
defer os.RemoveAll(filepath.Dir(rulesFilePath))
|
||||
|
||||
provider := RouterProvider{}
|
||||
router, _, err := provider.New(context.Background(), map[string]string{
|
||||
"routingConfig": rulesFilePath,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("New() unexpected error: %v", err)
|
||||
}
|
||||
if router == nil {
|
||||
t.Error("New() returned nil router, want non-nil")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouterProviderFailure tests the RouterProvider implementation for failure cases.
|
||||
func TestRouterProviderFailure(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
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "Empty routing config path",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{
|
||||
"routingConfig": "",
|
||||
},
|
||||
wantErr: "failed to load routing rules: routingConfig path is empty",
|
||||
},
|
||||
{
|
||||
name: "Missing routing config key",
|
||||
ctx: context.Background(),
|
||||
config: map[string]string{},
|
||||
wantErr: "routingConfig is required in the configuration",
|
||||
},
|
||||
{
|
||||
name: "Nil context",
|
||||
ctx: nil,
|
||||
config: map[string]string{"routingConfig": rulesFilePath},
|
||||
wantErr: "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 {
|
||||
t.Fatalf("New(%v, %v) = nil error, want error containing %q", tt.ctx, tt.config, tt.wantErr)
|
||||
}
|
||||
if !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Errorf("New(%v, %v) = %v, want error containing %q", tt.ctx, tt.config, err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
275
pkg/plugin/implementation/router/router.go
Normal file
275
pkg/plugin/implementation/router/router.go
Normal file
@@ -0,0 +1,275 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
definition "github.com/beckn/beckn-onix/pkg/plugin/definition"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Config holds the configuration for the Router plugin.
|
||||
type Config struct {
|
||||
RoutingConfig string `json:"routingConfig"`
|
||||
}
|
||||
|
||||
// RoutingConfig represents the structure of the routing configuration file.
|
||||
type routingConfig struct {
|
||||
RoutingRules []routingRule `yaml:"routingRules"`
|
||||
}
|
||||
|
||||
// Router implements Router interface.
|
||||
type Router struct {
|
||||
rules map[string]map[string]map[string]*definition.Route // domain -> version -> endpoint -> route
|
||||
}
|
||||
|
||||
// RoutingRule represents a single routing rule.
|
||||
type routingRule struct {
|
||||
Domain string `yaml:"domain"`
|
||||
Version string `yaml:"version"`
|
||||
TargetType string `yaml:"targetType"` // "url", "publisher", "bpp", or "bap"
|
||||
Target target `yaml:"target,omitempty"`
|
||||
Endpoints []string `yaml:"endpoints"`
|
||||
}
|
||||
|
||||
// Target contains destination-specific details.
|
||||
type target struct {
|
||||
URL string `yaml:"url,omitempty"` // URL for "url" or gateway endpoint for "bpp"/"bap"
|
||||
PublisherID string `yaml:"publisherId,omitempty"` // For "msgq" type
|
||||
}
|
||||
|
||||
// TargetType defines possible target destinations.
|
||||
const (
|
||||
targetTypeURL = "url" // Route to a specific URL
|
||||
targetTypePublisher = "publisher" // Route to a publisher
|
||||
targetTypeBPP = "bpp" // Route to a BPP endpoint
|
||||
targetTypeBAP = "bap" // Route to a BAP endpoint
|
||||
)
|
||||
|
||||
// New initializes a new Router instance with the provided configuration.
|
||||
// It loads and validates the routing rules from the specified YAML file.
|
||||
// Returns an error if the configuration is invalid or the rules cannot be loaded.
|
||||
func New(ctx context.Context, config *Config) (*Router, func() error, error) {
|
||||
// Check if config is nil
|
||||
if config == nil {
|
||||
return nil, nil, fmt.Errorf("config cannot be nil")
|
||||
}
|
||||
router := &Router{
|
||||
rules: make(map[string]map[string]map[string]*definition.Route),
|
||||
}
|
||||
|
||||
// Load rules at bootup
|
||||
if err := router.loadRules(config.RoutingConfig); err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to load routing rules: %w", err)
|
||||
}
|
||||
return router, nil, nil
|
||||
}
|
||||
|
||||
// parseTargetURL parses a URL string into a url.URL object with strict validation
|
||||
func parseTargetURL(urlStr string) (*url.URL, error) {
|
||||
if urlStr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
parsed, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid URL '%s': %w", urlStr, err)
|
||||
}
|
||||
|
||||
// Enforce scheme requirement
|
||||
if parsed.Scheme == "" {
|
||||
return nil, fmt.Errorf("URL '%s' must include a scheme (http/https)", urlStr)
|
||||
}
|
||||
|
||||
// Optionally validate scheme is http or https
|
||||
if parsed.Scheme != "https" {
|
||||
return nil, fmt.Errorf("URL '%s' must use https scheme", urlStr)
|
||||
}
|
||||
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
// LoadRules reads and parses routing rules from the YAML configuration file.
|
||||
func (r *Router) loadRules(configPath string) error {
|
||||
if configPath == "" {
|
||||
return fmt.Errorf("routingConfig path is empty")
|
||||
}
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading config file at %s: %w", configPath, err)
|
||||
}
|
||||
var config routingConfig
|
||||
if err := yaml.Unmarshal(data, &config); err != nil {
|
||||
return fmt.Errorf("error parsing YAML: %w", err)
|
||||
}
|
||||
|
||||
// Validate rules
|
||||
if err := validateRules(config.RoutingRules); err != nil {
|
||||
return fmt.Errorf("invalid routing rules: %w", err)
|
||||
}
|
||||
// Build the optimized rule map
|
||||
for _, rule := range config.RoutingRules {
|
||||
// Initialize domain map if not exists
|
||||
if _, ok := r.rules[rule.Domain]; !ok {
|
||||
r.rules[rule.Domain] = make(map[string]map[string]*definition.Route)
|
||||
}
|
||||
|
||||
// Initialize version map if not exists
|
||||
if _, ok := r.rules[rule.Domain][rule.Version]; !ok {
|
||||
r.rules[rule.Domain][rule.Version] = make(map[string]*definition.Route)
|
||||
}
|
||||
|
||||
// Add all endpoints for this rule
|
||||
for _, endpoint := range rule.Endpoints {
|
||||
var route *definition.Route
|
||||
switch rule.TargetType {
|
||||
case targetTypePublisher:
|
||||
route = &definition.Route{
|
||||
TargetType: rule.TargetType,
|
||||
PublisherID: rule.Target.PublisherID,
|
||||
}
|
||||
case targetTypeURL:
|
||||
parsedURL, err := parseTargetURL(rule.Target.URL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid URL in rule: %w", err)
|
||||
}
|
||||
route = &definition.Route{
|
||||
TargetType: rule.TargetType,
|
||||
URL: parsedURL,
|
||||
}
|
||||
case targetTypeBPP, targetTypeBAP:
|
||||
var parsedURL *url.URL
|
||||
if rule.Target.URL != "" {
|
||||
parsedURL, err = parseTargetURL(rule.Target.URL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid URL in rule: %w", err)
|
||||
}
|
||||
}
|
||||
route = &definition.Route{
|
||||
TargetType: rule.TargetType,
|
||||
URL: parsedURL,
|
||||
}
|
||||
}
|
||||
r.rules[rule.Domain][rule.Version][endpoint] = route
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateRules performs basic validation on the loaded routing rules.
|
||||
func validateRules(rules []routingRule) error {
|
||||
for _, rule := range rules {
|
||||
// Ensure domain, version, and TargetType are present
|
||||
if rule.Domain == "" || rule.Version == "" || rule.TargetType == "" {
|
||||
return fmt.Errorf("invalid rule: domain, version, and targetType are required")
|
||||
}
|
||||
|
||||
// Validate based on TargetType
|
||||
switch rule.TargetType {
|
||||
case targetTypeURL:
|
||||
if rule.Target.URL == "" {
|
||||
return fmt.Errorf("invalid rule: url is required for targetType 'url'")
|
||||
}
|
||||
if _, err := parseTargetURL(rule.Target.URL); err != nil {
|
||||
return fmt.Errorf("invalid URL - %s: %w", rule.Target.URL, err)
|
||||
}
|
||||
case targetTypePublisher:
|
||||
if rule.Target.PublisherID == "" {
|
||||
return fmt.Errorf("invalid rule: publisherID is required for targetType 'publisher'")
|
||||
}
|
||||
case targetTypeBPP, targetTypeBAP:
|
||||
if rule.Target.URL != "" {
|
||||
if _, err := parseTargetURL(rule.Target.URL); err != nil {
|
||||
return fmt.Errorf("invalid URL - %s defined in routing config for target type %s: %w", rule.Target.URL, rule.TargetType, err)
|
||||
}
|
||||
}
|
||||
continue
|
||||
default:
|
||||
return fmt.Errorf("invalid rule: unknown targetType '%s'", rule.TargetType)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Route determines the routing destination based on the request context.
|
||||
func (r *Router) Route(ctx context.Context, url *url.URL, body []byte) (*definition.Route, error) {
|
||||
|
||||
// Parse the body to extract domain and version
|
||||
var requestBody struct {
|
||||
Context struct {
|
||||
Domain string `json:"domain"`
|
||||
Version string `json:"version"`
|
||||
BPPURI string `json:"bpp_uri,omitempty"`
|
||||
BAPURI string `json:"bap_uri,omitempty"`
|
||||
} `json:"context"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &requestBody); err != nil {
|
||||
return nil, fmt.Errorf("error parsing request body: %w", err)
|
||||
}
|
||||
|
||||
// Extract the endpoint from the URL
|
||||
endpoint := path.Base(url.Path)
|
||||
|
||||
// Lookup route in the optimized map
|
||||
domainRules, ok := r.rules[requestBody.Context.Domain]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no routing rules found for domain %s", requestBody.Context.Domain)
|
||||
}
|
||||
|
||||
versionRules, ok := domainRules[requestBody.Context.Version]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no routing rules found for domain %s version %s", requestBody.Context.Domain, requestBody.Context.Version)
|
||||
}
|
||||
|
||||
route, ok := versionRules[endpoint]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("endpoint '%s' is not supported for domain %s and version %s in routing config",
|
||||
endpoint, requestBody.Context.Domain, requestBody.Context.Version)
|
||||
}
|
||||
// Handle BPP/BAP routing with request URIs
|
||||
switch route.TargetType {
|
||||
case targetTypeBPP:
|
||||
return handleProtocolMapping(route, requestBody.Context.BPPURI, endpoint)
|
||||
case targetTypeBAP:
|
||||
return handleProtocolMapping(route, requestBody.Context.BAPURI, endpoint)
|
||||
}
|
||||
return route, nil
|
||||
}
|
||||
|
||||
// handleProtocolMapping handles both BPP and BAP routing with proper URL construction
|
||||
func handleProtocolMapping(route *definition.Route, requestURI, endpoint string) (*definition.Route, error) {
|
||||
uri := strings.TrimSpace(requestURI)
|
||||
var targetURL *url.URL
|
||||
if len(uri) != 0 {
|
||||
parsedURL, err := parseTargetURL(uri)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid %s URI - %s in request body for %s: %w", strings.ToUpper(route.TargetType), uri, endpoint, err)
|
||||
}
|
||||
targetURL = parsedURL
|
||||
}
|
||||
|
||||
// If no request URI, fall back to configured URL with endpoint appended
|
||||
if targetURL == nil {
|
||||
if route.URL == nil {
|
||||
return nil, fmt.Errorf("could not determine destination for endpoint '%s': neither request contained a %s URI nor was a default URL configured in routing rules", endpoint, strings.ToUpper(route.TargetType))
|
||||
}
|
||||
|
||||
targetURL = &url.URL{
|
||||
Scheme: route.URL.Scheme,
|
||||
Host: route.URL.Host,
|
||||
Path: path.Join(route.URL.Path, endpoint),
|
||||
}
|
||||
}
|
||||
|
||||
return &definition.Route{
|
||||
TargetType: targetTypeURL,
|
||||
URL: targetURL,
|
||||
}, nil
|
||||
}
|
||||
486
pkg/plugin/implementation/router/router_test.go
Normal file
486
pkg/plugin/implementation/router/router_test.go
Normal file
@@ -0,0 +1,486 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:embed testData/*
|
||||
var testData embed.FS
|
||||
|
||||
func setupTestConfig(t *testing.T, yamlFileName string) string {
|
||||
t.Helper()
|
||||
configDir := t.TempDir()
|
||||
|
||||
content, err := testData.ReadFile("testData/" + yamlFileName)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile() err = %v, want nil", err)
|
||||
}
|
||||
|
||||
rulesPath := filepath.Join(configDir, "routing_rules.yaml")
|
||||
if err := os.WriteFile(rulesPath, content, 0644); err != nil {
|
||||
t.Fatalf("WriteFile() err = %v, want nil", err)
|
||||
}
|
||||
|
||||
return rulesPath
|
||||
}
|
||||
|
||||
// setupRouter is a helper function to create router instance.
|
||||
func setupRouter(t *testing.T, configFile string) (*Router, func() error, string) {
|
||||
rulesFilePath := setupTestConfig(t, configFile)
|
||||
config := &Config{
|
||||
RoutingConfig: rulesFilePath,
|
||||
}
|
||||
router, _, err := New(context.Background(), config)
|
||||
if err != nil {
|
||||
t.Fatalf("New failed: %v", err)
|
||||
}
|
||||
return router, nil, rulesFilePath
|
||||
}
|
||||
|
||||
// TestNew tests the New function.
|
||||
func TestNew(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// List of YAML files in the testData directory
|
||||
yamlFiles := []string{
|
||||
"bap_caller.yaml",
|
||||
"bap_receiver.yaml",
|
||||
"bpp_caller.yaml",
|
||||
"bpp_receiver.yaml",
|
||||
}
|
||||
|
||||
for _, yamlFile := range yamlFiles {
|
||||
t.Run(yamlFile, func(t *testing.T) {
|
||||
rulesFilePath := setupTestConfig(t, yamlFile)
|
||||
defer os.RemoveAll(filepath.Dir(rulesFilePath))
|
||||
|
||||
// Define test cases
|
||||
tests := []struct {
|
||||
name string
|
||||
config *Config
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "Valid configuration",
|
||||
config: &Config{
|
||||
RoutingConfig: rulesFilePath,
|
||||
},
|
||||
wantErr: "",
|
||||
},
|
||||
{
|
||||
name: "Empty config",
|
||||
config: nil,
|
||||
wantErr: "config cannot be nil",
|
||||
},
|
||||
{
|
||||
name: "Empty routing config path",
|
||||
config: &Config{
|
||||
RoutingConfig: "",
|
||||
},
|
||||
wantErr: "routingConfig path is empty",
|
||||
},
|
||||
{
|
||||
name: "Routing config file does not exist",
|
||||
config: &Config{
|
||||
RoutingConfig: "/nonexistent/path/to/rules.yaml",
|
||||
},
|
||||
wantErr: "error reading config file",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
router, _, err := New(ctx, tt.config)
|
||||
|
||||
// Check for expected error
|
||||
if tt.wantErr != "" {
|
||||
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Errorf("New(%v) = %v, want error containing %q", tt.config, err, tt.wantErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure no error occurred
|
||||
if err != nil {
|
||||
t.Errorf("New(%v) = %v, want nil error", tt.config, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure the router and close function are not nil
|
||||
if router == nil {
|
||||
t.Errorf("New(%v, %v) = nil router, want non-nil", ctx, tt.config)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateRulesSuccess tests the validate function for success cases.
|
||||
func TestValidateRulesSuccess(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
rules []routingRule
|
||||
}{
|
||||
{
|
||||
name: "Valid rules with url routing",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
TargetType: "url",
|
||||
Target: target{
|
||||
URL: "https://example.com/api",
|
||||
},
|
||||
Endpoints: []string{"on_search", "on_select"},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Valid rules with publisher routing",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
TargetType: "publisher",
|
||||
Target: target{
|
||||
PublisherID: "example_topic",
|
||||
},
|
||||
Endpoints: []string{"on_search", "on_select"},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Valid rules with bpp routing to gateway",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
TargetType: "bpp",
|
||||
Target: target{
|
||||
URL: "https://mock_gateway.com/api",
|
||||
},
|
||||
Endpoints: []string{"search"},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Valid rules with bpp routing",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
TargetType: "bpp",
|
||||
Endpoints: []string{"select"},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Valid rules with bap routing",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
TargetType: "bap",
|
||||
Endpoints: []string{"select"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := validateRules(tt.rules)
|
||||
if err != nil {
|
||||
t.Errorf("validateRules(%v) = %v, want nil error", tt.rules, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateRulesFailure tests the validate function for failure cases.
|
||||
func TestValidateRulesFailure(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
rules []routingRule
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "Missing domain",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Version: "1.0.0",
|
||||
TargetType: "url",
|
||||
Target: target{
|
||||
URL: "https://example.com/api",
|
||||
},
|
||||
Endpoints: []string{"search", "select"},
|
||||
},
|
||||
},
|
||||
wantErr: "invalid rule: domain, version, and targetType are required",
|
||||
},
|
||||
{
|
||||
name: "Missing version",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
TargetType: "url",
|
||||
Target: target{
|
||||
URL: "https://example.com/api",
|
||||
},
|
||||
Endpoints: []string{"search", "select"},
|
||||
},
|
||||
},
|
||||
wantErr: "invalid rule: domain, version, and targetType are required",
|
||||
},
|
||||
{
|
||||
name: "Missing targetType",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
Target: target{
|
||||
URL: "https://example.com/api",
|
||||
},
|
||||
Endpoints: []string{"search", "select"},
|
||||
},
|
||||
},
|
||||
wantErr: "invalid rule: domain, version, and targetType are required",
|
||||
},
|
||||
{
|
||||
name: "Invalid targetType",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
TargetType: "invalid",
|
||||
Target: target{
|
||||
URL: "https://example.com/api",
|
||||
},
|
||||
Endpoints: []string{"search", "select"},
|
||||
},
|
||||
},
|
||||
wantErr: "invalid rule: unknown targetType 'invalid'",
|
||||
},
|
||||
{
|
||||
name: "Missing url for targetType: url",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
TargetType: "url",
|
||||
Target: target{
|
||||
// URL is missing
|
||||
},
|
||||
Endpoints: []string{"search", "select"},
|
||||
},
|
||||
},
|
||||
wantErr: "invalid rule: url is required for targetType 'url'",
|
||||
},
|
||||
{
|
||||
name: "Invalid URL format for targetType: url",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
TargetType: "url",
|
||||
Target: target{
|
||||
URL: "htp://invalid-url.com", // Invalid scheme
|
||||
},
|
||||
Endpoints: []string{"search"},
|
||||
},
|
||||
},
|
||||
wantErr: "invalid URL - htp://invalid-url.com: URL 'htp://invalid-url.com' must use https scheme",
|
||||
},
|
||||
{
|
||||
name: "Missing topic_id for targetType: publisher",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
TargetType: "publisher",
|
||||
Target: target{
|
||||
// PublisherID is missing
|
||||
},
|
||||
Endpoints: []string{"search", "select"},
|
||||
},
|
||||
},
|
||||
wantErr: "invalid rule: publisherID is required for targetType 'publisher'",
|
||||
},
|
||||
{
|
||||
name: "Invalid URL for BPP targetType",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
TargetType: "bpp",
|
||||
Target: target{
|
||||
URL: "htp://invalid-url.com", // Invalid URL
|
||||
},
|
||||
Endpoints: []string{"search"},
|
||||
},
|
||||
},
|
||||
wantErr: "invalid URL - htp://invalid-url.com defined in routing config for target type bpp",
|
||||
},
|
||||
{
|
||||
name: "Invalid URL for BAP targetType",
|
||||
rules: []routingRule{
|
||||
{
|
||||
Domain: "retail",
|
||||
Version: "1.0.0",
|
||||
TargetType: "bap",
|
||||
Target: target{
|
||||
URL: "http://[invalid].com", // Invalid host
|
||||
},
|
||||
Endpoints: []string{"search"},
|
||||
},
|
||||
},
|
||||
wantErr: "invalid URL - http://[invalid].com defined in routing config for target type bap",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := validateRules(tt.rules)
|
||||
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Errorf("validateRules(%v) = %v, want error containing %q", tt.rules, err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouteSuccess tests the Route function for success cases.
|
||||
func TestRouteSuccess(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Define success test cases
|
||||
tests := []struct {
|
||||
name string
|
||||
configFile string
|
||||
url string
|
||||
body string
|
||||
}{
|
||||
{
|
||||
name: "Valid domain, version, and endpoint (bpp routing with gateway URL)",
|
||||
configFile: "bap_caller.yaml",
|
||||
url: "https://example.com/v1/ondc/search",
|
||||
body: `{"context": {"domain": "ONDC:TRV10", "version": "2.0.0"}}`,
|
||||
},
|
||||
{
|
||||
name: "Valid domain, version, and endpoint (bpp routing with bpp_uri)",
|
||||
configFile: "bap_caller.yaml",
|
||||
url: "https://example.com/v1/ondc/select",
|
||||
body: `{"context": {"domain": "ONDC:TRV10", "version": "2.0.0", "bpp_uri": "https://bpp1.example.com"}}`,
|
||||
},
|
||||
{
|
||||
name: "Valid domain, version, and endpoint (url routing)",
|
||||
configFile: "bpp_receiver.yaml",
|
||||
url: "https://example.com/v1/ondc/select",
|
||||
body: `{"context": {"domain": "ONDC:TRV10", "version": "2.0.0"}}`,
|
||||
},
|
||||
{
|
||||
name: "Valid domain, version, and endpoint (publisher routing)",
|
||||
configFile: "bpp_receiver.yaml",
|
||||
url: "https://example.com/v1/ondc/search",
|
||||
body: `{"context": {"domain": "ONDC:TRV10", "version": "2.0.0"}}`,
|
||||
},
|
||||
{
|
||||
name: "Valid domain, version, and endpoint (bap routing with bap_uri)",
|
||||
configFile: "bpp_caller.yaml",
|
||||
url: "https://example.com/v1/ondc/on_select",
|
||||
body: `{"context": {"domain": "ONDC:TRV10", "version": "2.0.0", "bap_uri": "https://bap1.example.com"}}`,
|
||||
},
|
||||
{
|
||||
name: "Valid domain, version, and endpoint (bpp routing with bpp_uri)",
|
||||
configFile: "bap_receiver.yaml",
|
||||
url: "https://example.com/v1/ondc/on_select",
|
||||
body: `{"context": {"domain": "ONDC:TRV10", "version": "2.0.0", "bpp_uri": "https://bpp1.example.com"}}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
router, _, rulesFilePath := setupRouter(t, tt.configFile)
|
||||
defer os.RemoveAll(filepath.Dir(rulesFilePath))
|
||||
|
||||
parsedURL, _ := url.Parse(tt.url)
|
||||
_, err := router.Route(ctx, parsedURL, []byte(tt.body))
|
||||
|
||||
// Ensure no error occurred
|
||||
if err != nil {
|
||||
t.Errorf("router.Route(%v, %v, %v) = %v, want nil error", ctx, parsedURL, []byte(tt.body), err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouteFailure tests the Route function for failure cases.
|
||||
func TestRouteFailure(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Define failure test cases
|
||||
tests := []struct {
|
||||
name string
|
||||
configFile string
|
||||
url string
|
||||
body string
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "Unsupported endpoint",
|
||||
configFile: "bpp_receiver.yaml",
|
||||
url: "https://example.com/v1/ondc/unsupported",
|
||||
body: `{"context": {"domain": "ONDC:TRV11", "version": "2.0.0"}}`,
|
||||
wantErr: "endpoint 'unsupported' is not supported for domain ONDC:TRV11 and version 2.0.0",
|
||||
},
|
||||
{
|
||||
name: "No matching rule",
|
||||
configFile: "bpp_receiver.yaml",
|
||||
url: "https://example.com/v1/ondc/select",
|
||||
body: `{"context": {"domain": "ONDC:SRV11", "version": "2.0.0"}}`,
|
||||
wantErr: "no routing rules found for domain ONDC:SRV11",
|
||||
},
|
||||
{
|
||||
name: "Missing bap_uri for bap routing",
|
||||
configFile: "bpp_caller.yaml",
|
||||
url: "https://example.com/v1/ondc/on_search",
|
||||
body: `{"context": {"domain": "ONDC:TRV10", "version": "2.0.0"}}`,
|
||||
wantErr: "could not determine destination for endpoint 'on_search': neither request contained a BAP URI nor was a default URL configured in routing rules",
|
||||
},
|
||||
{
|
||||
name: "Missing bpp_uri for bpp routing",
|
||||
configFile: "bap_caller.yaml",
|
||||
url: "https://example.com/v1/ondc/select",
|
||||
body: `{"context": {"domain": "ONDC:TRV10", "version": "2.0.0"}}`,
|
||||
wantErr: "could not determine destination for endpoint 'select': neither request contained a BPP URI nor was a default URL configured in routing rules",
|
||||
},
|
||||
{
|
||||
name: "Invalid bpp_uri format in request",
|
||||
configFile: "bap_caller.yaml",
|
||||
url: "https://example.com/v1/ondc/select",
|
||||
body: `{"context": {"domain": "ONDC:TRV10", "version": "2.0.0", "bpp_uri": "htp://invalid-url"}}`, // Invalid scheme (htp instead of http)
|
||||
wantErr: "invalid BPP URI - htp://invalid-url in request body for select: URL 'htp://invalid-url' must use https scheme",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
router, _, rulesFilePath := setupRouter(t, tt.configFile)
|
||||
defer os.RemoveAll(filepath.Dir(rulesFilePath))
|
||||
|
||||
parsedURL, _ := url.Parse(tt.url)
|
||||
_, err := router.Route(ctx, parsedURL, []byte(tt.body))
|
||||
|
||||
// Check for expected error
|
||||
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Errorf("Route(%q, %q) = %v, want error containing %q", tt.url, tt.body, err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
25
pkg/plugin/implementation/router/testData/bap_caller.yaml
Normal file
25
pkg/plugin/implementation/router/testData/bap_caller.yaml
Normal file
@@ -0,0 +1,25 @@
|
||||
routingRules:
|
||||
- domain: ONDC:TRV10
|
||||
version: 2.0.0
|
||||
targetType: bpp
|
||||
target:
|
||||
url: https://gateway.example.com
|
||||
endpoints:
|
||||
- search
|
||||
- domain: ONDC:TRV10
|
||||
version: 2.0.0
|
||||
targetType: bpp
|
||||
endpoints:
|
||||
- select
|
||||
- init
|
||||
- confirm
|
||||
- status
|
||||
- cancel
|
||||
- domain: ONDC:TRV12
|
||||
version: 2.0.0
|
||||
targetType: bpp
|
||||
endpoints:
|
||||
- select
|
||||
- init
|
||||
- confirm
|
||||
- status
|
||||
20
pkg/plugin/implementation/router/testData/bap_receiver.yaml
Normal file
20
pkg/plugin/implementation/router/testData/bap_receiver.yaml
Normal file
@@ -0,0 +1,20 @@
|
||||
routingRules:
|
||||
- domain: ONDC:TRV10
|
||||
version: 2.0.0
|
||||
targetType: url
|
||||
target:
|
||||
url: https://services-backend/trv/v1
|
||||
endpoints:
|
||||
- on_select
|
||||
- on_init
|
||||
- on_confirm
|
||||
- on_status
|
||||
- on_update
|
||||
- on_cancel
|
||||
- domain: ONDC:TRV10
|
||||
version: 2.0.0
|
||||
targetType: publisher
|
||||
target:
|
||||
publisherId: trv_topic_id1
|
||||
endpoints:
|
||||
- on_search
|
||||
23
pkg/plugin/implementation/router/testData/bpp_caller.yaml
Normal file
23
pkg/plugin/implementation/router/testData/bpp_caller.yaml
Normal file
@@ -0,0 +1,23 @@
|
||||
routingRules:
|
||||
- domain: ONDC:TRV10
|
||||
version: 2.0.0
|
||||
targetType: bap
|
||||
endpoints:
|
||||
- on_search
|
||||
- on_select
|
||||
- on_init
|
||||
- on_confirm
|
||||
- on_status
|
||||
- on_update
|
||||
- on_cancel
|
||||
- domain: ONDC:TRV11
|
||||
version: 2.0.0
|
||||
targetType: bap
|
||||
endpoints:
|
||||
- on_search
|
||||
- on_select
|
||||
- on_init
|
||||
- on_confirm
|
||||
- on_status
|
||||
- on_update
|
||||
- on_cancel
|
||||
28
pkg/plugin/implementation/router/testData/bpp_receiver.yaml
Normal file
28
pkg/plugin/implementation/router/testData/bpp_receiver.yaml
Normal file
@@ -0,0 +1,28 @@
|
||||
routingRules:
|
||||
- domain: ONDC:TRV10
|
||||
version: 2.0.0
|
||||
targetType: url
|
||||
target:
|
||||
url: https://services-backend/trv/v1
|
||||
endpoints:
|
||||
- select
|
||||
- init
|
||||
- confirm
|
||||
- status
|
||||
- cancel
|
||||
- domain: ONDC:TRV10
|
||||
version: 2.0.0
|
||||
targetType: publisher
|
||||
target:
|
||||
publisherId: trv_topic_id1
|
||||
endpoints:
|
||||
- search
|
||||
- domain: ONDC:TRV11
|
||||
version: 2.0.0
|
||||
targetType: url
|
||||
target:
|
||||
url: https://services-backend/trv/v1
|
||||
endpoints:
|
||||
- select
|
||||
- init
|
||||
- confirm
|
||||
Reference in New Issue
Block a user