github.com/TIBCOSoftware/flogo-lib@v0.5.9/util/test/trigger.go (about) 1 package test 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/TIBCOSoftware/flogo-lib/core/action" 8 "github.com/TIBCOSoftware/flogo-lib/core/data" 9 "github.com/TIBCOSoftware/flogo-lib/core/trigger" 10 "github.com/TIBCOSoftware/flogo-lib/engine/runner" 11 ) 12 13 func InitTrigger(factory trigger.Factory, tConfig *trigger.Config, actions map[string]action.Action) (trigger.Trigger, error) { 14 15 r := runner.NewDirect() 16 17 if factory == nil { 18 return nil, fmt.Errorf("Trigger Factory not provided") 19 } 20 21 trg := factory.New(tConfig) 22 23 if trg == nil { 24 return nil, fmt.Errorf("cannot create Trigger for id '%s'", tConfig.Id) 25 } 26 27 tConfig.FixUp(trg.Metadata()) 28 29 initCtx := &initContext{handlers: make([]*trigger.Handler, 0, len(tConfig.Handlers))} 30 31 trgInit, ok := trg.(trigger.Initializable) 32 33 if !ok { 34 return nil, fmt.Errorf("Trigger does not implement trigger.Initializable interface") 35 } 36 37 //create handlers for that trigger and init 38 for _, hConfig := range tConfig.Handlers { 39 40 if hConfig.Action.Id == "" { 41 return nil, fmt.Errorf("Action not specified for handler") 42 } 43 44 act, exists := actions[hConfig.Action.Id] 45 if !exists { 46 return nil, fmt.Errorf("specified Action '%s' does not exists", hConfig.Action.Id) 47 } 48 49 handler := trigger.NewHandler(hConfig, act, trg.Metadata().Output, trg.Metadata().Reply, r) 50 initCtx.handlers = append(initCtx.handlers, handler) 51 } 52 53 err := trgInit.Initialize(initCtx) 54 if err != nil { 55 return nil, err 56 } 57 58 return trg, nil 59 } 60 61 ////////////////////////// 62 // Simple Init Context 63 64 type initContext struct { 65 handlers []*trigger.Handler 66 } 67 68 func (ctx *initContext) GetHandlers() []*trigger.Handler { 69 return ctx.handlers 70 } 71 72 ////////////////////////// 73 // Dummy Test Action 74 75 func NewDummyAction(f func()) action.Action { 76 return &testAction{f: f} 77 } 78 79 type testAction struct { 80 f func() 81 } 82 83 // Metadata get the Action's metadata 84 func (a *testAction) Metadata() *action.Metadata { 85 return nil 86 } 87 88 // IOMetadata get the Action's IO metadata 89 func (a *testAction) IOMetadata() *data.IOMetadata { 90 return nil 91 } 92 93 // Run implementation of action.SyncAction.Run 94 func (a *testAction) Run(ctx context.Context, inputs map[string]*data.Attribute) (map[string]*data.Attribute, error) { 95 a.f() 96 return nil, nil 97 } 98 99 // 100 //type testRunner struct { 101 //} 102 // 103 ////DEPRECATED 104 //func (tr *testRunner) Run(context context.Context, act action.Action, uri string, options interface{}) (code int, data interface{}, err error) { 105 // return 0, nil, nil 106 //} 107 // 108 ////DEPRECATED 109 //func (tr *testRunner) RunAction(ctx context.Context, act action.Action, options map[string]interface{}) (results map[string]*data.Attribute, err error) { 110 // return 0, nil, nil 111 //} 112 // 113 //func (tr *testRunner) Execute(ctx context.Context, act action.Action, inputs map[string]*data.Attribute) (results map[string]*data.Attribute, err error) { 114 // 115 // 116 // return nil, nil 117 //}