111 lines
2.7 KiB
Go
111 lines
2.7 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package codec
|
|
|
|
import (
|
|
_ "fmt"
|
|
"github.com/stretchr/testify/assert"
|
|
_ "log"
|
|
"strings"
|
|
"testing"
|
|
"github.com/xeipuuv/gojsonschema"
|
|
"io"
|
|
"bytes"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type TestFile struct {
|
|
Path string `json:"path" yaml:"path"`
|
|
}
|
|
|
|
func TestNewYAMLEncoder(t *testing.T) {
|
|
var yamlDoc strings.Builder
|
|
e := NewYAMLEncoder(&yamlDoc)
|
|
assert.NotNil(t, e)
|
|
}
|
|
|
|
func TestNewEncoderEncodeJSON(t *testing.T) {
|
|
schema:=`
|
|
{
|
|
"$id": "file",
|
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
"title": "file",
|
|
"type": "object",
|
|
"required": [ "path" ],
|
|
"properties": {
|
|
"path": {
|
|
"type": "string",
|
|
"description": "file path",
|
|
"minLength": 1
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
var jsonDoc strings.Builder
|
|
file := &TestFile{}
|
|
file.Path = "foo"
|
|
|
|
e := NewJSONEncoder(&jsonDoc)
|
|
assert.NotNil(t, e)
|
|
docErr := e.Encode(file)
|
|
assert.Nil(t, docErr)
|
|
|
|
schemaLoader := gojsonschema.NewStringLoader(schema)
|
|
loader := gojsonschema.NewStringLoader(jsonDoc.String())
|
|
result, err := gojsonschema.Validate(schemaLoader, loader)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, result.Valid())
|
|
}
|
|
|
|
func TestNewEncoder(t *testing.T) {
|
|
|
|
pb := &TestPBUser{ Name: "pb", Uid: "15001", Group: "15005", Home: "/home/pb", State: "present" }
|
|
jx := &TestUser{ Name: "jx", Uid: "17001", Group: "17005", Home: "/home/jx", State: "present" }
|
|
|
|
pbData, pbErr := proto.Marshal(pb)
|
|
assert.Nil(t, pbErr)
|
|
|
|
for _, v := range []struct{ writer io.Writer; testuser any; format Format; expected []byte} {
|
|
{ writer: &bytes.Buffer{}, testuser: jx, expected: []byte(`{"name":"jx","uid":"17001","group":"17005","home":"/home/jx","state":"present"}
|
|
`), format: FormatJson },
|
|
{ writer: &bytes.Buffer{}, testuser: jx, expected: []byte(`name: jx
|
|
uid: "17001"
|
|
group: "17005"
|
|
home: /home/jx
|
|
state: present
|
|
`), format: FormatYaml },
|
|
{ writer: &bytes.Buffer{}, testuser: pb, expected: pbData , format: FormatProtoBuf },
|
|
} {
|
|
encoder := NewEncoder(v.writer, v.format)
|
|
assert.NotNil(t, encoder)
|
|
assert.Nil(t, encoder.Encode(v.testuser))
|
|
assert.Equal(t, string(v.expected), v.writer.(*bytes.Buffer).String())
|
|
assert.Equal(t, v.expected, v.writer.(*bytes.Buffer).Bytes())
|
|
assert.Nil(t, encoder.Close())
|
|
}
|
|
}
|
|
|
|
func TestNewEncoderError(t *testing.T) {
|
|
encoder := NewEncoder(&strings.Builder{}, Format("foo"))
|
|
assert.Nil(t, encoder)
|
|
}
|
|
|
|
func TestNewProtobufError(t *testing.T) {
|
|
encoder := NewProtoBufEncoder(nil)
|
|
assert.Nil(t, encoder)
|
|
}
|
|
|
|
/*
|
|
func TestProtobufEncodeError(t *testing.T) {
|
|
buf := &bytes.Buffer{}
|
|
buf.Write([]byte("broken input"))
|
|
|
|
encoder := NewProtoBufEncoder(buf)
|
|
assert.NotNil(t, encoder)
|
|
assert.NotNil(t, encoder.Encode(&TestPBUser{}))
|
|
}
|
|
*/
|