event-gateway/internal/config/config_test.go

137 lines
2.8 KiB
Go
Raw Permalink Normal View History

2024-04-20 20:31:06 +00:00
// Copyright © 2023 Roberto Hidalgo <event-gateway@un.rob.mx>
// SPDX-License-Identifier: Apache-2.0
package config_test
import (
"strings"
"testing"
"testing/fstest"
"git.rob.mx/nidito/event-gateway/internal/config"
"git.rob.mx/nidito/event-gateway/internal/sink/debug"
"git.rob.mx/nidito/event-gateway/internal/source/types"
)
func TestFileLoader(t *testing.T) {
config.FS = fstest.MapFS{
"empty-file.json": &fstest.MapFile{
Data: []byte(`{}`),
},
"bad-source.json": &fstest.MapFile{
Data: []byte(`{
"bad": {
"source": 42,
"sink": {
"kind": "debug"
}
}
}`),
},
"no-sink.json": &fstest.MapFile{
Data: []byte(`{
"bad": {
"source": {"kind": "http"}
}
}`),
},
"bad-sink.json": &fstest.MapFile{
Data: []byte(`{
"bad": {
"source": {"kind": "http"},
"sink": 42
}
}`),
},
"simple.json": &fstest.MapFile{
Data: []byte(`{
"simple": {
"source": {
"kind": "http",
"path": "simple"
},
"sink": {
"kind": "debug"
}
}
}`),
},
}
cases := []struct {
Name string
Path string
Error any
Expected []*config.Listener
}{
{
Name: "empty-file",
Path: "empty-file.json",
Expected: []*config.Listener{},
},
{
Name: "bad-source",
Path: "bad-source.json",
Error: "could not unserialize bad-source.json as json: unable to decode {",
},
{
Name: "no-sink",
Path: "no-sink.json",
Error: "could not unserialize no-sink.json as json: sink configuration not provided",
},
{
Name: "bad-sink",
Path: "bad-sink.json",
Error: "could not unserialize bad-sink.json as json: sink configuration for {",
},
{
Name: "simple",
Path: "simple.json",
Expected: []*config.Listener{
{
ID: "simple",
Source: &config.RawSource{Kind: types.HTTP},
Event: &debug.Event{},
Hash: "8SsF8PtGcr-5xE5iy8F1JES0ZoTQMdfmbw7iLLU3wik=",
},
},
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
l := &config.File{Path: c.Path}
res, err := l.Load()
if err != nil {
if c.Error != nil {
if errPrefix, ok := c.Error.(string); ok {
if !strings.HasPrefix(err.Error(), errPrefix) {
t.Fatalf("Unexpected error prefix, wanted %s, got %s", errPrefix, err)
}
} else if c.Error != err {
t.Fatalf("Unexpected error, wanted %s, got %s", c.Error, err)
}
return
}
}
if len(res) != len(c.Expected) {
t.Fatalf("Unexpected results, wanted %+v, got %+v", c.Expected, res)
}
for idx, l := range c.Expected {
m := res[idx]
if l.ID != m.ID {
t.Fatalf("Unexpected ID, wanted %s got %s", l.ID, m.ID)
}
if l.Hash != m.Hash {
t.Fatalf("Unexpected hash, wanted %s got %s", l.Hash, m.Hash)
}
}
})
}
}