From e029ebe1f263a6b8558f69638685484d57079870 Mon Sep 17 00:00:00 2001 From: Nirmal N R Date: Wed, 24 Sep 2025 12:16:20 +0530 Subject: [PATCH] Unit tests for excludeAction Added unit tests for the following: - For target type URL: - ExcludeAction explicitly set true - ExcludeAction explicitly set to false - ExcludeAction omitted - ExcludeAction when target type is BPP - ExcludeAction when target type is BAP - ExcludeAction when target type is Publisher --- .../implementation/router/router_test.go | 130 ++++++++++++++++++ .../router/testData/exclude_action_bap.yaml | 10 ++ .../router/testData/exclude_action_bpp.yaml | 10 ++ .../testData/exclude_action_default.yaml | 9 ++ .../router/testData/exclude_action_false.yaml | 10 ++ .../testData/exclude_action_publisher.yaml | 10 ++ .../router/testData/exclude_action_true.yaml | 10 ++ 7 files changed, 189 insertions(+) create mode 100644 pkg/plugin/implementation/router/testData/exclude_action_bap.yaml create mode 100644 pkg/plugin/implementation/router/testData/exclude_action_bpp.yaml create mode 100644 pkg/plugin/implementation/router/testData/exclude_action_default.yaml create mode 100644 pkg/plugin/implementation/router/testData/exclude_action_false.yaml create mode 100644 pkg/plugin/implementation/router/testData/exclude_action_publisher.yaml create mode 100644 pkg/plugin/implementation/router/testData/exclude_action_true.yaml diff --git a/pkg/plugin/implementation/router/router_test.go b/pkg/plugin/implementation/router/router_test.go index 1f685aa..9fae926 100644 --- a/pkg/plugin/implementation/router/router_test.go +++ b/pkg/plugin/implementation/router/router_test.go @@ -551,4 +551,134 @@ func TestRouteFailure(t *testing.T) { } }) } +} + +// TestExcludeAction tests the excludeAction feature for URL routing +func TestExcludeAction(t *testing.T) { + tests := []struct { + name string + configFile string + expectedRoutes map[string]map[string]map[string]*model.Route + }{ + { + name: "excludeAction true - action not appended to URL", + configFile: "exclude_action_true.yaml", + expectedRoutes: map[string]map[string]map[string]*model.Route{ + "ONDC:TRV10": { + "2.0.0": { + "search": {TargetType: targetTypeURL, URL: parseURL(t, "https://services-backend.com/v2/ondc")}, + "init": {TargetType: targetTypeURL, URL: parseURL(t, "https://services-backend.com/v2/ondc")}, + }, + }, + }, + }, + { + name: "excludeAction false - action appended to URL", + configFile: "exclude_action_false.yaml", + expectedRoutes: map[string]map[string]map[string]*model.Route{ + "ONDC:TRV10": { + "2.0.0": { + "search": {TargetType: targetTypeURL, URL: parseURL(t, "https://services-backend.com/v2/ondc/search")}, + "init": {TargetType: targetTypeURL, URL: parseURL(t, "https://services-backend.com/v2/ondc/init")}, + }, + }, + }, + }, + { + name: "excludeAction not specified - defaults to false (action appended)", + configFile: "exclude_action_default.yaml", + expectedRoutes: map[string]map[string]map[string]*model.Route{ + "ONDC:TRV10": { + "2.0.0": { + "search": {TargetType: targetTypeURL, URL: parseURL(t, "https://services-backend.com/v2/ondc/search")}, + "init": {TargetType: targetTypeURL, URL: parseURL(t, "https://services-backend.com/v2/ondc/init")}, + }, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + router := &Router{ + rules: make(map[string]map[string]map[string]*model.Route), + } + rulesFilePath := setupTestConfig(t, tt.configFile) + defer os.RemoveAll(filepath.Dir(rulesFilePath)) + + err := router.loadRules(rulesFilePath) + if err != nil { + t.Fatalf("loadRules() err = %v, want nil", err) + } + + if !reflect.DeepEqual(router.rules, tt.expectedRoutes) { + t.Errorf("Loaded rules mismatch for %s.\nGot:\n%#v\nWant:\n%#v", tt.name, router.rules, tt.expectedRoutes) + } + }) + } +} + +// TestExcludeActionWithNonURLTargetTypes tests that excludeAction is ignored for non-URL target types +func TestExcludeActionWithNonURLTargetTypes(t *testing.T) { + tests := []struct { + name string + configFile string + expectedRoutes map[string]map[string]map[string]*model.Route + }{ + { + name: "BPP target type - excludeAction should be ignored", + configFile: "exclude_action_bpp.yaml", + expectedRoutes: map[string]map[string]map[string]*model.Route{ + "ONDC:TRV10": { + "2.0.0": { + "search": {TargetType: targetTypeBPP, URL: parseURL(t, "https://mock_bpp.com/v2/ondc/search")}, + "init": {TargetType: targetTypeBPP, URL: parseURL(t, "https://mock_bpp.com/v2/ondc/init")}, + }, + }, + }, + }, + { + name: "BAP target type - excludeAction should be ignored", + configFile: "exclude_action_bap.yaml", + expectedRoutes: map[string]map[string]map[string]*model.Route{ + "ONDC:TRV10": { + "2.0.0": { + "search": {TargetType: targetTypeBAP, URL: parseURL(t, "https://mock_bap.com/v2/ondc/search")}, + "init": {TargetType: targetTypeBAP, URL: parseURL(t, "https://mock_bap.com/v2/ondc/init")}, + }, + }, + }, + }, + { + name: "Publisher target type - excludeAction should be ignored", + configFile: "exclude_action_publisher.yaml", + expectedRoutes: map[string]map[string]map[string]*model.Route{ + "ONDC:TRV10": { + "2.0.0": { + "search": {TargetType: targetTypePublisher, PublisherID: "test_topic", URL: nil}, + "init": {TargetType: targetTypePublisher, PublisherID: "test_topic", URL: nil}, + }, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + router := &Router{ + rules: make(map[string]map[string]map[string]*model.Route), + } + rulesFilePath := setupTestConfig(t, tt.configFile) + defer os.RemoveAll(filepath.Dir(rulesFilePath)) + + err := router.loadRules(rulesFilePath) + if err != nil { + t.Fatalf("loadRules() err = %v, want nil", err) + } + + if !reflect.DeepEqual(router.rules, tt.expectedRoutes) { + t.Errorf("Loaded rules mismatch for %s.\nGot:\n%#v\nWant:\n%#v", tt.name, router.rules, tt.expectedRoutes) + } + }) + } } \ No newline at end of file diff --git a/pkg/plugin/implementation/router/testData/exclude_action_bap.yaml b/pkg/plugin/implementation/router/testData/exclude_action_bap.yaml new file mode 100644 index 0000000..be4a724 --- /dev/null +++ b/pkg/plugin/implementation/router/testData/exclude_action_bap.yaml @@ -0,0 +1,10 @@ +routingRules: + - domain: ONDC:TRV10 + version: 2.0.0 + targetType: bap + target: + url: https://mock_bap.com/v2/ondc + excludeAction: true + endpoints: + - search + - init diff --git a/pkg/plugin/implementation/router/testData/exclude_action_bpp.yaml b/pkg/plugin/implementation/router/testData/exclude_action_bpp.yaml new file mode 100644 index 0000000..f3a515b --- /dev/null +++ b/pkg/plugin/implementation/router/testData/exclude_action_bpp.yaml @@ -0,0 +1,10 @@ +routingRules: + - domain: ONDC:TRV10 + version: 2.0.0 + targetType: bpp + target: + url: https://mock_bpp.com/v2/ondc + excludeAction: true + endpoints: + - search + - init diff --git a/pkg/plugin/implementation/router/testData/exclude_action_default.yaml b/pkg/plugin/implementation/router/testData/exclude_action_default.yaml new file mode 100644 index 0000000..a3da856 --- /dev/null +++ b/pkg/plugin/implementation/router/testData/exclude_action_default.yaml @@ -0,0 +1,9 @@ +routingRules: + - domain: ONDC:TRV10 + version: 2.0.0 + targetType: url + target: + url: https://services-backend.com/v2/ondc + endpoints: + - search + - init diff --git a/pkg/plugin/implementation/router/testData/exclude_action_false.yaml b/pkg/plugin/implementation/router/testData/exclude_action_false.yaml new file mode 100644 index 0000000..3e130e3 --- /dev/null +++ b/pkg/plugin/implementation/router/testData/exclude_action_false.yaml @@ -0,0 +1,10 @@ +routingRules: + - domain: ONDC:TRV10 + version: 2.0.0 + targetType: url + target: + url: https://services-backend.com/v2/ondc + excludeAction: false + endpoints: + - search + - init diff --git a/pkg/plugin/implementation/router/testData/exclude_action_publisher.yaml b/pkg/plugin/implementation/router/testData/exclude_action_publisher.yaml new file mode 100644 index 0000000..1032a4c --- /dev/null +++ b/pkg/plugin/implementation/router/testData/exclude_action_publisher.yaml @@ -0,0 +1,10 @@ +routingRules: + - domain: ONDC:TRV10 + version: 2.0.0 + targetType: publisher + target: + publisherId: test_topic + excludeAction: true + endpoints: + - search + - init diff --git a/pkg/plugin/implementation/router/testData/exclude_action_true.yaml b/pkg/plugin/implementation/router/testData/exclude_action_true.yaml new file mode 100644 index 0000000..3a75ebd --- /dev/null +++ b/pkg/plugin/implementation/router/testData/exclude_action_true.yaml @@ -0,0 +1,10 @@ +routingRules: + - domain: ONDC:TRV10 + version: 2.0.0 + targetType: url + target: + url: https://services-backend.com/v2/ondc + excludeAction: true + endpoints: + - search + - init