mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-08-08 07:53:39 +00:00
156 lines
3.9 KiB
Go
156 lines
3.9 KiB
Go
package processor_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/jwetzell/showbridge-go/internal/common"
|
|
"github.com/jwetzell/showbridge-go/internal/config"
|
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
|
"github.com/jwetzell/showbridge-go/internal/test"
|
|
)
|
|
|
|
func TestRouterInputFromRegistry(t *testing.T) {
|
|
registration, ok := processor.GetProcessorRegistration("router.input")
|
|
if !ok {
|
|
t.Fatalf("router.input processor not registered")
|
|
}
|
|
|
|
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
Type: "router.input",
|
|
Params: config.Params{
|
|
"source": "test",
|
|
},
|
|
})
|
|
|
|
if err != nil {
|
|
t.Fatalf("failed to create router.input processor: %s", err)
|
|
}
|
|
|
|
if processorInstance.Type() != "router.input" {
|
|
t.Fatalf("router.input processor has wrong type: %s", processorInstance.Type())
|
|
}
|
|
|
|
payload := "test"
|
|
expected := "test"
|
|
|
|
got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
|
|
InputHandler: test.GetNewTestRouter().HandleInput,
|
|
Payload: payload,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("router.input processing failed: %s", err)
|
|
}
|
|
|
|
if got.Payload != expected {
|
|
t.Fatalf("router.input got %+v, expected %+v", got, expected)
|
|
}
|
|
}
|
|
|
|
func TestGoodRouterInput(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
name string
|
|
params map[string]any
|
|
payload any
|
|
expected any
|
|
}{}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
registration, ok := processor.GetProcessorRegistration("router.input")
|
|
if !ok {
|
|
t.Fatalf("router.input processor not registered")
|
|
}
|
|
|
|
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
Type: "router.input",
|
|
Params: testCase.params,
|
|
})
|
|
|
|
if err != nil {
|
|
t.Fatalf("router.input failed to create processor: %s", err)
|
|
}
|
|
|
|
got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: testCase.payload})
|
|
if err != nil {
|
|
t.Fatalf("router.input processing failed: %s", err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(got.Payload, testCase.expected) {
|
|
t.Fatalf("router.input got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, testCase.expected, testCase.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBadRouterInput(t *testing.T) {
|
|
router := test.GetNewTestRouter()
|
|
testCases := []struct {
|
|
name string
|
|
params map[string]any
|
|
payload any
|
|
inputHandler common.InputHandler
|
|
errorString string
|
|
}{
|
|
{
|
|
name: "no source param",
|
|
params: map[string]any{},
|
|
payload: "test",
|
|
inputHandler: router.HandleInput,
|
|
errorString: "router.input source error: not found",
|
|
},
|
|
{
|
|
name: "non-string source",
|
|
params: map[string]any{
|
|
"source": 123,
|
|
},
|
|
payload: "test",
|
|
inputHandler: router.HandleInput,
|
|
errorString: "router.input source error: not a string",
|
|
},
|
|
{
|
|
name: "router not found in context",
|
|
params: map[string]any{
|
|
"source": "test",
|
|
},
|
|
payload: "test",
|
|
inputHandler: nil,
|
|
errorString: "router.input no input handler found",
|
|
},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
registration, ok := processor.GetProcessorRegistration("router.input")
|
|
if !ok {
|
|
t.Fatalf("router.input processor not registered")
|
|
}
|
|
|
|
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
Type: "router.input",
|
|
Params: testCase.params,
|
|
})
|
|
|
|
if err != nil {
|
|
if testCase.errorString != err.Error() {
|
|
t.Fatalf("router.input got error '%s', expected '%s'", err.Error(), testCase.errorString)
|
|
}
|
|
return
|
|
}
|
|
|
|
got, err := processorInstance.Process(t.Context(), common.WrappedPayload{InputHandler: testCase.inputHandler, Payload: testCase.payload})
|
|
|
|
if err == nil {
|
|
t.Fatalf("router.input expected to fail but succeeded, got: %v", got)
|
|
}
|
|
|
|
if err.Error() != testCase.errorString {
|
|
t.Fatalf("router.input got error '%s', expected '%s'", err.Error(), testCase.errorString)
|
|
}
|
|
})
|
|
}
|
|
}
|