jx/internal/folio/mock_foo_resource_test.go
Matthew Rich 55fd39f09d
Some checks are pending
Lint / golangci-lint (push) Waiting to run
Declarative Tests / test (push) Waiting to run
Declarative Tests / build-fedora (push) Waiting to run
Declarative Tests / build-ubuntu-focal (push) Waiting to run
add document imports
2024-09-24 19:22:49 +00:00

121 lines
3.5 KiB
Go

// 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()
f.Name = filepath.Join(u.Hostname(), u.Path)
return f
})
TestResourceTypes.Register([]string{"bar"}, func(u *url.URL) data.Resource {
f := NewBarResource()
f.Name = filepath.Join(u.Hostname(), u.Path)
return f
})
TestResourceTypes.Register([]string{"testuser"}, func(u *url.URL) data.Resource {
f := NewTestuserResource()
f.Name = filepath.Join(u.Hostname(), u.Path)
return f
})
TestResourceTypes.Register([]string{"file"}, func(u *url.URL) data.Resource {
f := NewFileResource()
f.Name = filepath.Join(u.Hostname(), u.Path)
return f
})
}
type MockFoo struct {
stater machine.Stater `json:"-" yaml:"-"`
*MockResource `json:"-" yaml:"-"`
Name string `json:"name" yaml:"name"`
Size int `json:"size" yaml:"size"`
}
type MockBar struct {
stater machine.Stater `json:"-" yaml:"-"`
*MockResource `json:"-" yaml:"-"`
Name string `json:"name" yaml:"name"`
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:"-" yaml:"-"`
Name string `json:"name" yaml:"name"`
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:"-" yaml:"-"`
Name string `json:"name" yaml:"name"`
Size int `json:"size" yaml:"size"`
}
func NewMockResource(typename string, stater machine.Stater) *MockResource {
return &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) {},
}
}
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
}