// Copyright 2024 Matthew Rich . 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 TestUser struct { 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"` } 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) } func TestNewDecoder(t *testing.T) { pbData, err := proto.Marshal(&TestPBUser{ Name: "pb", Uid: "15001", Group: "15005", Home: "/home/pb", State: "present" }) 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) u := &TestPBUser{} assert.Nil(t, decoder.Decode(u)) assert.Equal(t, v.expectedhome, u.Home ) } } 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) }