Merge pull request #454 from beckn/feature/adapter_testcase
changed adapter test case
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -99,99 +100,27 @@ func TestMainFunction(t *testing.T) {
|
|||||||
main()
|
main()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestRunSuccess tests the successful execution of the run function with different configurations.
|
|
||||||
func TestRunSuccess(t *testing.T) {
|
func TestRunSuccess(t *testing.T) {
|
||||||
tests := []struct {
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||||
name string
|
defer cancel()
|
||||||
configData string
|
|
||||||
mockMgr func() (*plugin.Manager, func(), error)
|
configPath := "../test/validConfig.yaml"
|
||||||
mockLogger func(cfg *Config) error
|
|
||||||
mockServer func(ctx context.Context, mgr handler.PluginManager, cfg *Config) (http.Handler, error)
|
// Mock dependencies
|
||||||
}{
|
originalNewManager := newManagerFunc
|
||||||
{
|
newManagerFunc = func(ctx context.Context, cfg *plugin.ManagerConfig) (*plugin.Manager, func(), error) {
|
||||||
name: "Valid Config",
|
return &plugin.Manager{}, func() {}, nil
|
||||||
configData: "valid_config.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
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
defer func() { newManagerFunc = originalNewManager }()
|
||||||
|
|
||||||
for _, tt := range tests {
|
originalNewServer := newServerFunc
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
newServerFunc = func(ctx context.Context, mgr handler.PluginManager, cfg *Config) (http.Handler, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
return http.NewServeMux(), nil
|
||||||
defer cancel()
|
}
|
||||||
|
defer func() { newServerFunc = originalNewServer }()
|
||||||
|
|
||||||
testFilePath := tt.configData
|
if err := run(ctx, filepath.Clean(configPath)); err != nil {
|
||||||
mockConfig := `appName: "testAdapter"
|
t.Errorf("Expected no error, but got: %v", err)
|
||||||
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) {
|
|
||||||
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 function
|
|
||||||
err = run(ctx, testFilePath)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Expected no error, but got: %v", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
37
cmd/test/validConfig.yaml
Normal file
37
cmd/test/validConfig.yaml
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
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"
|
||||||
5
go.mod
5
go.mod
@@ -22,10 +22,7 @@ require (
|
|||||||
|
|
||||||
require github.com/zenazn/pkcs7pad v0.0.0-20170308005700-253a5b1f0e03
|
require github.com/zenazn/pkcs7pad v0.0.0-20170308005700-253a5b1f0e03
|
||||||
|
|
||||||
require (
|
require golang.org/x/text v0.23.0 // indirect
|
||||||
github.com/google/uuid v1.6.0
|
|
||||||
golang.org/x/text v0.23.0 // indirect
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
|||||||
2
go.sum
2
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 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
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/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 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
|
||||||
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
|
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=
|
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
|
||||||
|
|||||||
Reference in New Issue
Block a user