90 lines
2.9 KiB
Go
90 lines
2.9 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
)
|
|
|
|
func TestValidatorRegistryBehavior(t *testing.T) {
|
|
runRegistryBehaviorTests(t, registryBehaviorCase[contracts.Validator]{
|
|
name: "ValidatorRegistry",
|
|
key: "generic-validator",
|
|
stage: StageValidate,
|
|
wrongStage: StageExtract,
|
|
newRegistry: func() any {
|
|
return NewValidatorRegistry()
|
|
},
|
|
register: func(registry any, key string, constructor func() (contracts.Validator, error)) error {
|
|
return registry.(*ValidatorRegistry).Register(key, constructor)
|
|
},
|
|
registerWithSpec: func(registry any, spec ModuleSpec, constructor func() (contracts.Validator, error)) error {
|
|
return registry.(*ValidatorRegistry).RegisterWithSpec(spec, constructor)
|
|
},
|
|
build: func(registry any, key string) (contracts.Validator, error) {
|
|
return registry.(*ValidatorRegistry).Build(key)
|
|
},
|
|
spec: func(registry any, key string) (ModuleSpec, bool) {
|
|
return registry.(*ValidatorRegistry).Spec(key)
|
|
},
|
|
registeredKeys: func(registry any) []string {
|
|
return registry.(*ValidatorRegistry).RegisteredKeys()
|
|
},
|
|
nilRegister: func(key string, constructor func() (contracts.Validator, error)) error {
|
|
var registry *ValidatorRegistry
|
|
return registry.Register(key, constructor)
|
|
},
|
|
nilBuild: func(key string) (contracts.Validator, error) {
|
|
var registry *ValidatorRegistry
|
|
return registry.Build(key)
|
|
},
|
|
nilSpec: func(key string) (ModuleSpec, bool) {
|
|
var registry *ValidatorRegistry
|
|
return registry.Spec(key)
|
|
},
|
|
nilRegisteredKey: func() []string {
|
|
var registry *ValidatorRegistry
|
|
return registry.RegisteredKeys()
|
|
},
|
|
constructor: func(key string) func() (contracts.Validator, error) {
|
|
return func() (contracts.Validator, error) {
|
|
return registryValidator{name: key}, nil
|
|
}
|
|
},
|
|
moduleKey: func(module contracts.Validator) string {
|
|
return module.Name()
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestValidatorRegistryRejectsUnsupportedExecutionClass(t *testing.T) {
|
|
registry := NewValidatorRegistry()
|
|
if err := registry.Register("invalid-validator", func() (contracts.Validator, error) {
|
|
return invalidExecutionClassValidator{name: "invalid-validator"}, nil
|
|
}); err != nil {
|
|
t.Fatalf("Register() error = %v, want nil", err)
|
|
}
|
|
|
|
_, err := registry.Build("invalid-validator")
|
|
if err == nil {
|
|
t.Fatal("Build() error = nil, want unsupported execution class error")
|
|
}
|
|
}
|
|
|
|
type invalidExecutionClassValidator struct {
|
|
name string
|
|
}
|
|
|
|
func (validator invalidExecutionClassValidator) Name() string {
|
|
return validator.name
|
|
}
|
|
|
|
func (validator invalidExecutionClassValidator) ExecutionClass() contracts.ExecutionClass {
|
|
return contracts.ExecutionClass("unsupported")
|
|
}
|
|
|
|
func (validator invalidExecutionClassValidator) Validate(ctx context.Context, req contracts.ValidationRequest) (contracts.ValidationResult, error) {
|
|
return contracts.ValidationResult{Approved: true}, nil
|
|
}
|