forked from cloudfoundry/bosh-softlayer-cpi-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_factory.go
executable file
·50 lines (41 loc) · 1.2 KB
/
fake_factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package fakes
import (
"errors"
"fmt"
bslcaction "github.com/maximilien/bosh-softlayer-cpi/action"
)
type FakeFactory struct {
registeredActions map[string]*FakeAction
registeredActionErrs map[string]error
}
func NewFakeFactory() *FakeFactory {
return &FakeFactory{
registeredActions: make(map[string]*FakeAction),
registeredActionErrs: make(map[string]error),
}
}
func (f *FakeFactory) Create(method string) (bslcaction.Action, error) {
if err := f.registeredActionErrs[method]; err != nil {
return nil, err
}
if action := f.registeredActions[method]; action != nil {
return action, nil
}
return nil, errors.New("Action not found")
}
func (f *FakeFactory) RegisterAction(method string, action *FakeAction) {
if a := f.registeredActions[method]; a != nil {
panic(fmt.Sprintf("Action is already registered: %v", a))
}
f.registeredActions[method] = action
}
func (f *FakeFactory) RegisterActionErr(method string, err error) {
if e := f.registeredActionErrs[method]; e != nil {
panic(fmt.Sprintf("Action err is already registered: %v", e))
}
f.registeredActionErrs[method] = err
}
type FakeAction struct{}
func (a *FakeAction) Run(payload []byte) (interface{}, error) {
return nil, nil
}