2024-05-14 16:53:47 +00:00
|
|
|
// 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"
|
2024-08-15 15:12:42 +00:00
|
|
|
"io"
|
|
|
|
"bytes"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2024-05-14 16:53:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestUser struct {
|
2024-08-15 15:12:42 +00:00
|
|
|
Name string `json:"name" yaml:"name" protobuf:"bytes,1,opt,name=name"`
|
|
|
|
Uid string `json:"uid" yaml:"uid" protobuf:"bytes,2,opt,name=uid"`
|
|
|
|
Group string `json:"group" yaml:"group" protobuf:"bytes,3,opt,name=group"`
|
|
|
|
Home string `json:"home" yaml:"home" protobuf:"bytes,4,opt,name=home"`
|
|
|
|
State string `json:"state" yaml:"state" protobuf:"bytes,5,opt,name=state"`
|
2024-05-14 16:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewYAMLDecoder(t *testing.T) {
|
|
|
|
e := NewYAMLDecoder(strings.NewReader(""))
|
|
|
|
assert.NotNil(t, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDecoderDecodeJSON(t *testing.T) {
|
|
|
|
schema:=`
|
|
|
|
{
|
|
|
|
"$id": "user",
|
|
|
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
|
|
"title": "user",
|
|
|
|
"type": "object",
|
|
|
|
"required": [ "name" ],
|
|
|
|
"properties": {
|
|
|
|
"path": {
|
|
|
|
"type": "string",
|
|
|
|
"description": "user name",
|
|
|
|
"minLength": 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
decl := `{
|
|
|
|
"name": "testuser",
|
|
|
|
"uid": "12001",
|
|
|
|
"group": "12001",
|
|
|
|
"home": "/home/testuser",
|
|
|
|
"state": "present"
|
|
|
|
}`
|
|
|
|
|
|
|
|
jsonReader := strings.NewReader(decl)
|
|
|
|
user := &TestUser{}
|
|
|
|
|
|
|
|
e := NewJSONDecoder(jsonReader)
|
|
|
|
assert.NotNil(t, e)
|
|
|
|
docErr := e.Decode(user)
|
|
|
|
assert.Nil(t, docErr)
|
|
|
|
|
|
|
|
schemaLoader := gojsonschema.NewStringLoader(schema)
|
|
|
|
loader := gojsonschema.NewStringLoader(decl)
|
|
|
|
result, validateErr := gojsonschema.Validate(schemaLoader, loader)
|
|
|
|
assert.True(t, result.Valid())
|
|
|
|
assert.Nil(t, validateErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewJSONStringDecoder(t *testing.T) {
|
|
|
|
decl := `{
|
|
|
|
"name": "testuser",
|
|
|
|
"uid": "12001",
|
|
|
|
"group": "12001",
|
|
|
|
"home": "/home/testuser",
|
|
|
|
"state": "present"
|
|
|
|
}`
|
|
|
|
|
|
|
|
e := NewJSONStringDecoder(decl)
|
|
|
|
assert.NotNil(t, e)
|
|
|
|
docErr := e.Decode(&TestUser{})
|
|
|
|
assert.Nil(t, docErr)
|
|
|
|
}
|
2024-08-15 15:12:42 +00:00
|
|
|
|
|
|
|
func TestNewDecoder(t *testing.T) {
|
2024-08-18 01:16:02 +00:00
|
|
|
pbData, err := proto.Marshal(&TestPBUser{ Name: "pb", Uid: "15001", Group: "15005", Home: "/home/pb", State: "present" })
|
2024-08-15 15:12:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
for _, v := range []struct{ reader io.Reader; format Format; expectedhome string } {
|
|
|
|
{ reader: strings.NewReader(`{
|
|
|
|
"name": "testuser",
|
|
|
|
"uid": "12001",
|
|
|
|
"group": "12001",
|
|
|
|
"home": "/home/testuser",
|
|
|
|
"state": "present" }`), format: FormatJson, expectedhome: "/home/testuser" },
|
|
|
|
{ reader: strings.NewReader(`
|
|
|
|
name: "testuser"
|
|
|
|
uid: "12001"
|
|
|
|
group: "12001"
|
|
|
|
home: "/home/test"
|
|
|
|
state: "present"
|
|
|
|
`), format: FormatYaml, expectedhome: "/home/test" },
|
|
|
|
{ reader: bytes.NewReader(pbData), format: FormatProtoBuf, expectedhome: "/home/pb" },
|
|
|
|
} {
|
|
|
|
|
|
|
|
decoder := NewDecoder(v.reader, v.format)
|
|
|
|
assert.NotNil(t, decoder)
|
2024-08-18 01:16:02 +00:00
|
|
|
u := &TestPBUser{}
|
2024-08-15 15:12:42 +00:00
|
|
|
assert.Nil(t, decoder.Decode(u))
|
|
|
|
assert.Equal(t, v.expectedhome, u.Home )
|
|
|
|
}
|
|
|
|
}
|
2024-08-18 01:16:02 +00:00
|
|
|
|
|
|
|
func TestNewDecoderError(t *testing.T) {
|
|
|
|
pbData, err := proto.Marshal(&TestPBUser{ Name: "pb", Uid: "15001", Group: "15005", Home: "/home/pb", State: "present" })
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
decoder := NewDecoder(bytes.NewReader(pbData), Format("foo"))
|
|
|
|
assert.Nil(t, decoder)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewStringDecoder(t *testing.T) {
|
|
|
|
jsonDoc := `{
|
|
|
|
"name": "testuser",
|
|
|
|
"uid": "12001",
|
|
|
|
"group": "12001",
|
|
|
|
"home": "/home/testuser",
|
|
|
|
"state": "present" }`
|
|
|
|
decoder := NewStringDecoder(jsonDoc, FormatJson)
|
|
|
|
assert.NotNil(t, decoder)
|
|
|
|
u := &TestUser{}
|
|
|
|
assert.Nil(t, decoder.Decode(u))
|
|
|
|
assert.Equal(t, "testuser", u.Name)
|
|
|
|
|
|
|
|
}
|