update the file structure update Initialise function , update interface and update code and write table test cases
This commit is contained in:
31
shared/plugin/implementations/validator/cmd/plugin.go
Normal file
31
shared/plugin/implementations/validator/cmd/plugin.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"beckn-onix/shared/plugin/definition"
|
||||
"beckn-onix/shared/plugin/implementations/validator"
|
||||
)
|
||||
|
||||
// ValidatorProvider provides instances of Validator.
|
||||
type ValidatorProvider struct{}
|
||||
|
||||
// New initializes a new Verifier instance.
|
||||
func (vp ValidatorProvider) New(ctx context.Context, config map[string]string) (map[string]definition.Validator, error) {
|
||||
// Create a new Validator instance with the provided configuration
|
||||
validators, err := validator.New(ctx, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert the map to the expected type
|
||||
result := make(map[string]definition.Validator)
|
||||
for key, val := range validators {
|
||||
result[key] = val
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Provider is the exported symbol that the plugin manager will look for.
|
||||
var Provider definition.ValidatorProvider = ValidatorProvider{}
|
||||
85
shared/plugin/implementations/validator/cmd/plugin_test.go
Normal file
85
shared/plugin/implementations/validator/cmd/plugin_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"beckn-onix/shared/plugin/definition"
|
||||
)
|
||||
|
||||
// MockValidator is a mock implementation of the Validator interface for testing.
|
||||
type MockValidator struct{}
|
||||
|
||||
func (m *MockValidator) Validate(ctx context.Context, u url.URL, data []byte) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Mock New function for testing
|
||||
func MockNew(ctx context.Context, config map[string]string) (map[string]definition.Validator, error) {
|
||||
if config["error"] == "true" {
|
||||
return nil, errors.New("mock error")
|
||||
}
|
||||
|
||||
return map[string]definition.Validator{
|
||||
"validator1": &MockValidator{},
|
||||
"validator2": &MockValidator{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestValidatorProvider_New(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
config map[string]string
|
||||
expectedError string
|
||||
expectedCount int
|
||||
}{
|
||||
{
|
||||
name: "Successful initialization",
|
||||
config: map[string]string{"some_key": "some_value"},
|
||||
expectedError: "",
|
||||
expectedCount: 2, // Expecting 2 mock validators
|
||||
},
|
||||
{
|
||||
name: "Error during initialization (mock error)",
|
||||
config: map[string]string{"error": "true"},
|
||||
expectedError: "mock error",
|
||||
expectedCount: 0,
|
||||
},
|
||||
{
|
||||
name: "Empty config map",
|
||||
config: map[string]string{},
|
||||
expectedError: "",
|
||||
expectedCount: 2, // Expecting 2 mock validators
|
||||
},
|
||||
{
|
||||
name: "Non-empty config with invalid key",
|
||||
config: map[string]string{"invalid_key": "invalid_value"},
|
||||
expectedError: "",
|
||||
expectedCount: 2, // Expecting 2 mock validators
|
||||
},
|
||||
}
|
||||
|
||||
// Using the mock New function directly for testing
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Create a ValidatorProvider with the mock New function
|
||||
vp := &ValidatorProvider{}
|
||||
validators, err := vp.New(context.Background(), tt.config)
|
||||
|
||||
// Check for expected error
|
||||
if tt.expectedError != "" {
|
||||
if err == nil || err.Error() != tt.expectedError {
|
||||
t.Errorf("expected error %q, got %v", tt.expectedError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Check for expected number of validators
|
||||
if len(validators) != tt.expectedCount {
|
||||
t.Errorf("expected %d validators, got %d", tt.expectedCount, len(validators))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user