From 5b32a51682caeb91a9c36e3048f5bcb13923f37c Mon Sep 17 00:00:00 2001 From: MohitKatare-protean Date: Wed, 9 Apr 2025 11:23:38 +0530 Subject: [PATCH 1/2] changed adapter test case created test folder to add test config and use it in adapter test cases. --- cmd/adapter/main_test.go | 56 ++++----------------------------------- cmd/test/validConfig.yaml | 41 ++++++++++++++++++++++++++++ go.mod | 5 +--- go.sum | 2 -- 4 files changed, 47 insertions(+), 57 deletions(-) create mode 100644 cmd/test/validConfig.yaml diff --git a/cmd/adapter/main_test.go b/cmd/adapter/main_test.go index 8bfd24e..1ba1667 100644 --- a/cmd/adapter/main_test.go +++ b/cmd/adapter/main_test.go @@ -6,6 +6,7 @@ import ( "flag" "net/http" "os" + "path/filepath" "strings" "testing" "time" @@ -99,18 +100,17 @@ func TestMainFunction(t *testing.T) { main() } -// TestRunSuccess tests the successful execution of the run function with different configurations. func TestRunSuccess(t *testing.T) { tests := []struct { name string - configData string + configPath string mockMgr func() (*plugin.Manager, func(), error) mockLogger func(cfg *Config) error mockServer func(ctx context.Context, mgr handler.PluginManager, cfg *Config) (http.Handler, error) }{ { name: "Valid Config", - configData: "valid_config.yaml", + configPath: "../test/validConfig.yaml", mockMgr: func() (*plugin.Manager, func(), error) { return &plugin.Manager{}, func() {}, nil }, @@ -128,51 +128,6 @@ func TestRunSuccess(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() - testFilePath := tt.configData - mockConfig := `appName: "testAdapter" -log: - level: debug - destinations: - - type: stdout - context_keys: - - transaction_id - - message_id -http: - port: 8080 - timeout: - read: 30 - write: 30 - idle: 30 -plugin: - root: "/mock/plugins" - pluginZipPath: "/mock/plugins/plugins_bundle.zip" - plugins: - - testPlugin1 - - testPlugin2 -modules: - - name: testModule - type: transaction - path: /testPath - targetType: msgQ - plugin: - schemaValidator: - id: testValidator - publisher: - id: testPublisher - config: - project: test-project - topic: test-topic - router: - id: testRouter - config: - routingConfigPath: "/mock/configs/testRouting-config.yaml"` - - err := os.WriteFile(testFilePath, []byte(mockConfig), 0644) - if err != nil { - t.Errorf("Failed to create test config file: %v", err) - } - defer os.Remove(testFilePath) - // Mock dependencies originalNewManager := newManagerFunc newManagerFunc = func(ctx context.Context, cfg *plugin.ManagerConfig) (*plugin.Manager, func(), error) { @@ -186,9 +141,8 @@ modules: } defer func() { newServerFunc = originalNewServer }() - // Run function - err = run(ctx, testFilePath) - if err != nil { + // Run the app using static config file + if err := run(ctx, filepath.Clean(tt.configPath)); err != nil { t.Errorf("Expected no error, but got: %v", err) } }) diff --git a/cmd/test/validConfig.yaml b/cmd/test/validConfig.yaml new file mode 100644 index 0000000..450e09f --- /dev/null +++ b/cmd/test/validConfig.yaml @@ -0,0 +1,41 @@ +appName: "testAdapter" + +log: + level: debug + destinations: + - type: stdout + context_keys: + - transaction_id + - message_id + +http: + port: 8080 + timeout: + read: 30 + write: 30 + idle: 30 + +plugin: + root: "/mock/plugins" + pluginZipPath: "/mock/plugins/plugins_bundle.zip" + plugins: + - testPlugin1 + - testPlugin2 + +modules: + - name: testModule + type: transaction + path: /testPath + targetType: msgQ + plugin: + schemaValidator: + id: testValidator + publisher: + id: testPublisher + config: + project: test-project + topic: test-topic + router: + id: testRouter + config: + routingConfigPath: "/mock/configs/testRouting-config.yaml" diff --git a/go.mod b/go.mod index 3ec4911..12fad60 100644 --- a/go.mod +++ b/go.mod @@ -22,10 +22,7 @@ require ( require github.com/zenazn/pkcs7pad v0.0.0-20170308005700-253a5b1f0e03 -require ( - github.com/google/uuid v1.6.0 - golang.org/x/text v0.23.0 // indirect -) +require golang.org/x/text v0.23.0 // indirect require ( github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/go.sum b/go.sum index b00b8d5..821e117 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,6 @@ github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cn github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= -github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k= From 2f033d28ad48124625ce26286924bcaf91b42662 Mon Sep 17 00:00:00 2001 From: MohitKatare-protean Date: Fri, 11 Apr 2025 15:46:06 +0530 Subject: [PATCH 2/2] Review comments resolved --- cmd/adapter/main_test.go | 59 +++++++++++---------------------------- cmd/test/validConfig.yaml | 4 --- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/cmd/adapter/main_test.go b/cmd/adapter/main_test.go index 1ba1667..e8b6658 100644 --- a/cmd/adapter/main_test.go +++ b/cmd/adapter/main_test.go @@ -101,51 +101,26 @@ func TestMainFunction(t *testing.T) { } func TestRunSuccess(t *testing.T) { - tests := []struct { - name string - configPath string - mockMgr func() (*plugin.Manager, func(), error) - mockLogger func(cfg *Config) error - mockServer func(ctx context.Context, mgr handler.PluginManager, cfg *Config) (http.Handler, error) - }{ - { - name: "Valid Config", - configPath: "../test/validConfig.yaml", - mockMgr: func() (*plugin.Manager, func(), error) { - return &plugin.Manager{}, func() {}, nil - }, - mockLogger: func(cfg *Config) error { - return nil - }, - mockServer: func(ctx context.Context, mgr handler.PluginManager, cfg *Config) (http.Handler, error) { - return http.NewServeMux(), nil - }, - }, + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + + configPath := "../test/validConfig.yaml" + + // Mock dependencies + originalNewManager := newManagerFunc + newManagerFunc = func(ctx context.Context, cfg *plugin.ManagerConfig) (*plugin.Manager, func(), error) { + return &plugin.Manager{}, func() {}, nil } + defer func() { newManagerFunc = originalNewManager }() - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() + originalNewServer := newServerFunc + newServerFunc = func(ctx context.Context, mgr handler.PluginManager, cfg *Config) (http.Handler, error) { + return http.NewServeMux(), nil + } + defer func() { newServerFunc = originalNewServer }() - // Mock dependencies - originalNewManager := newManagerFunc - newManagerFunc = func(ctx context.Context, cfg *plugin.ManagerConfig) (*plugin.Manager, func(), error) { - return tt.mockMgr() - } - defer func() { newManagerFunc = originalNewManager }() - - originalNewServer := newServerFunc - newServerFunc = func(ctx context.Context, mgr handler.PluginManager, cfg *Config) (http.Handler, error) { - return tt.mockServer(ctx, mgr, cfg) - } - defer func() { newServerFunc = originalNewServer }() - - // Run the app using static config file - if err := run(ctx, filepath.Clean(tt.configPath)); err != nil { - t.Errorf("Expected no error, but got: %v", err) - } - }) + if err := run(ctx, filepath.Clean(configPath)); err != nil { + t.Errorf("Expected no error, but got: %v", err) } } diff --git a/cmd/test/validConfig.yaml b/cmd/test/validConfig.yaml index 450e09f..5497753 100644 --- a/cmd/test/validConfig.yaml +++ b/cmd/test/validConfig.yaml @@ -1,5 +1,4 @@ appName: "testAdapter" - log: level: debug destinations: @@ -7,21 +6,18 @@ log: context_keys: - transaction_id - message_id - http: port: 8080 timeout: read: 30 write: 30 idle: 30 - plugin: root: "/mock/plugins" pluginZipPath: "/mock/plugins/plugins_bundle.zip" plugins: - testPlugin1 - testPlugin2 - modules: - name: testModule type: transaction