jx/internal/folio/mock_foo_resource_test.go

132 lines
3.7 KiB
Go
Raw Permalink Normal View History

2024-08-18 01:19:56 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. 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)
}
2024-08-18 01:19:56 +00:00
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)
}
2024-08-18 01:19:56 +00:00
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)
}
2024-08-18 01:19:56 +00:00
return f
})
2024-09-24 19:22:49 +00:00
TestResourceTypes.Register([]string{"file"}, func(u *url.URL) data.Resource {
f := NewFileResource()
if u != nil {
f.Name = filepath.Join(u.Hostname(), u.Path)
}
2024-09-24 19:22:49 +00:00
return f
})
2024-08-18 01:19:56 +00:00
}
type MockFoo struct {
stater machine.Stater `json:"-" yaml:"-"`
*MockResource `json:",inline" yaml:",inline"`
2024-08-18 01:19:56 +00:00
Size int `json:"size" yaml:"size"`
}
type MockBar struct {
stater machine.Stater `json:"-" yaml:"-"`
*MockResource `json:",inline" yaml:",inline"`
2024-08-18 01:19:56 +00:00
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"`
2024-08-18 01:19:56 +00:00
Uid string `json:"uid" yaml:"uid"`
Group string `json:"group" yaml:"group"`
Home string `json:"home" yaml:"home"`
}
2024-09-24 19:22:49 +00:00
type MockFile struct {
stater machine.Stater `json:"-" yaml:"-"`
*MockResource `json:",inline" yaml:",inline"`
2024-09-24 19:22:49 +00:00
Size int `json:"size" yaml:"size"`
}
func NewMockResource(typename string, stater machine.Stater) (m *MockResource) {
m = &MockResource {
2024-08-18 01:19:56 +00:00
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
2024-08-18 01:19:56 +00:00
}
func NewFooResource() *MockFoo {
2024-09-24 19:22:49 +00:00
f := &MockFoo {}
2024-08-18 01:19:56 +00:00
f.stater = data.StorageMachine(f)
f.MockResource = NewMockResource("foo", f.stater)
return f
}
func NewBarResource() *MockBar {
2024-09-24 19:22:49 +00:00
b := &MockBar {}
2024-08-18 01:19:56 +00:00
b.stater = data.StorageMachine(b)
b.MockResource = NewMockResource("bar", b.stater)
return b
}
func NewTestuserResource() *MockTestuser {
2024-09-24 19:22:49 +00:00
u := &MockTestuser {}
2024-08-18 01:19:56 +00:00
u.stater = data.StorageMachine(u)
u.MockResource = NewMockResource("testuser", u.stater)
return u
}
2024-09-24 19:22:49 +00:00
func NewFileResource() *MockFile {
f := &MockFile {}
f.stater = data.StorageMachine(f)
f.MockResource = NewMockResource("file", f.stater)
return f
}