// Copyright 2024 Matthew Rich . All rights reserved. package folio import ( "context" _ "gopkg.in/yaml.v3" "gitea.rosskeen.house/rosskeen.house/machine" "decl/internal/codec" "decl/internal/data" "io" "net/url" "path/filepath" "fmt" ) func RegisterMocks() { TestResourceTypes.Register([]string{"foo"}, func(u *url.URL) data.Resource { f := NewFooResource() if u != nil { f.Name = filepath.Join(u.Hostname(), u.Path) } return f }) TestResourceTypes.Register([]string{"bar"}, func(u *url.URL) data.Resource { f := NewBarResource() if u != nil { f.Name = filepath.Join(u.Hostname(), u.Path) } return f }) TestResourceTypes.Register([]string{"testuser"}, func(u *url.URL) data.Resource { f := NewTestuserResource() if u != nil { f.Name = filepath.Join(u.Hostname(), u.Path) } return f }) TestResourceTypes.Register([]string{"file"}, func(u *url.URL) data.Resource { f := NewFileResource() if u != nil { f.Name = filepath.Join(u.Hostname(), u.Path) } return f }) } type MockFoo struct { stater machine.Stater `json:"-" yaml:"-"` *MockResource `json:",inline" yaml:",inline"` Size int `json:"size" yaml:"size"` } type MockBar struct { stater machine.Stater `json:"-" yaml:"-"` *MockResource `json:",inline" yaml:",inline"` Size int `json:"size,omitempty" yaml:"size,omitempty"` Owner string `json:"owner,omitempty" yaml:"owner,omitempty"` } type MockTestuser struct { stater machine.Stater `json:"-" yaml:"-"` *MockResource `json:",inline" yaml:",inline"` Uid string `json:"uid" yaml:"uid"` Group string `json:"group" yaml:"group"` Home string `json:"home" yaml:"home"` } type MockFile struct { stater machine.Stater `json:"-" yaml:"-"` *MockResource `json:",inline" yaml:",inline"` Size int `json:"size" yaml:"size"` } func NewMockResource(typename string, stater machine.Stater) (m *MockResource) { m = &MockResource { InjectType: func() string { return typename }, InjectResolveId: func(ctx context.Context) string { return "bar" }, InjectLoadDecl: func(string) error { return nil }, InjectLoadString: func(string, codec.Format) (error) { return nil }, InjectLoad: func([]byte, codec.Format) (error) { return nil }, InjectLoadReader: func(io.ReadCloser, codec.Format) (error) { return nil }, InjectApply: func() error { return nil }, InjectStateMachine: func() machine.Stater { return stater }, InjectValidate: func() error { return nil }, InjectCreate: func(context.Context) error { return nil }, InjectRead: func(context.Context) ([]byte, error) { return nil, nil }, InjectUpdate: func(context.Context) error { return nil }, InjectDelete: func(context.Context) error { return nil }, InjectUseConfig: func(data.ConfigurationValueGetter) {}, InjectSetResourceMapper: func(data.ResourceMapper) {}, InjectURI: func() string { return fmt.Sprintf("%s://bar", typename) }, InjectNotify: func(*machine.EventMessage) {}, } m.InjectInit = func(u data.URIParser) error { if u != nil { m.Name = filepath.Join(u.URL().Hostname(), u.URL().Path) } return nil } return m } func NewFooResource() *MockFoo { f := &MockFoo {} f.stater = data.StorageMachine(f) f.MockResource = NewMockResource("foo", f.stater) return f } func NewBarResource() *MockBar { b := &MockBar {} b.stater = data.StorageMachine(b) b.MockResource = NewMockResource("bar", b.stater) return b } func NewTestuserResource() *MockTestuser { u := &MockTestuser {} u.stater = data.StorageMachine(u) u.MockResource = NewMockResource("testuser", u.stater) return u } func NewFileResource() *MockFile { f := &MockFile {} f.stater = data.StorageMachine(f) f.MockResource = NewMockResource("file", f.stater) return f }