-
Notifications
You must be signed in to change notification settings - Fork 78
/
context.go
281 lines (213 loc) · 6.68 KB
/
context.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package test
import (
"github.com/TIBCOSoftware/flogo-contrib/action/flow/definition"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/core/data"
)
//todo needs to move to lib
// NewTestActivityContext creates a new TestActivityContext
func NewTestActivityContext(metadata *activity.Metadata) *TestActivityContext {
input := map[string]*data.Attribute{"Input1": data.NewZeroAttribute("Input1", data.TypeString)}
output := map[string]*data.Attribute{"Output1": data.NewZeroAttribute("Output1", data.TypeString)}
ac := &TestActivityHost{
HostId: "1",
HostRef: "github.com/TIBCOSoftware/flogo-contrib/action/flow",
IoMetadata: &data.IOMetadata{Input: input, Output: output},
HostData: data.NewSimpleScope(nil, nil),
}
return NewTestActivityContextWithAction(metadata, ac)
}
// NewTestActivityContextWithAction creates a new TestActivityContext
func NewTestActivityContextWithAction(metadata *activity.Metadata, activityHost *TestActivityHost) *TestActivityContext {
fd := &TestFlowDetails{
FlowIDVal: "1",
FlowNameVal: "Test Flow",
}
tc := &TestActivityContext{
metadata: metadata,
details: fd,
activityHost: activityHost,
TaskNameVal: "Test TaskOld",
Attrs: make(map[string]*data.Attribute),
inputs: make(map[string]*data.Attribute, len(metadata.Input)),
outputs: make(map[string]*data.Attribute, len(metadata.Output)),
settings: make(map[string]*data.Attribute, len(metadata.Settings)),
}
for _, element := range metadata.Input {
tc.inputs[element.Name()] = data.NewZeroAttribute(element.Name(), element.Type())
}
for _, element := range metadata.Output {
tc.outputs[element.Name()] = data.NewZeroAttribute(element.Name(), element.Type())
}
return tc
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestFlowDetails
// TestFlowDetails simple FlowDetails for use in testing
type TestFlowDetails struct {
FlowIDVal string
FlowNameVal string
}
// ID implements activity.FlowDetails.ID
func (fd *TestFlowDetails) ID() string {
return fd.FlowIDVal
}
// Name implements activity.FlowDetails.Name
func (fd *TestFlowDetails) Name() string {
return fd.FlowNameVal
}
// ReplyHandler implements activity.FlowDetails.ReplyHandler
func (fd *TestFlowDetails) ReplyHandler() activity.ReplyHandler {
return nil
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestActivityHost
type TestActivityHost struct {
HostId string
HostRef string
IoMetadata *data.IOMetadata
HostData data.Scope
ReplyData map[string]interface{}
ReplyDataAttr map[string]*data.Attribute
ReplyErr error
}
func (ac *TestActivityHost) Name() string {
return ""
}
func (ac *TestActivityHost) IOMetadata() *data.IOMetadata {
return nil
}
func (ac *TestActivityHost) ID() string {
return ac.HostId
}
func (ac *TestActivityHost) Ref() string {
return ac.HostRef
}
func (ac *TestActivityHost) Reply(data map[string]*data.Attribute, err error) {
//todo log reply
ac.ReplyDataAttr = data
ac.ReplyErr = err
}
func (ac *TestActivityHost) Return(data map[string]*data.Attribute, err error) {
//todo log reply
ac.ReplyDataAttr = data
ac.ReplyErr = err
}
func (ac *TestActivityHost) WorkingData() data.Scope {
return ac.HostData
}
func (ac *TestActivityHost) GetResolver() data.Resolver {
return definition.GetDataResolver()
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestActivityContext
// TestActivityContext is a dummy ActivityContext to assist in testing
type TestActivityContext struct {
details activity.FlowDetails
TaskNameVal string
Attrs map[string]*data.Attribute
activityHost activity.Host
metadata *activity.Metadata
settings map[string]*data.Attribute
inputs map[string]*data.Attribute
outputs map[string]*data.Attribute
shared map[string]interface{}
}
func (c *TestActivityContext) FlowDetails() activity.FlowDetails {
return c.details
}
func (c *TestActivityContext) ActivityHost() activity.Host {
return c.activityHost
}
func (c *TestActivityContext) Name() string {
return c.TaskNameVal
}
// GetSetting implements activity.Context.GetSetting
func (c *TestActivityContext) GetSetting(setting string) (value interface{}, exists bool) {
attr, found := c.settings[setting]
if found {
return attr.Value(), true
}
return nil, false
}
// GetInitValue implements activity.Context.GetInitValue
func (c *TestActivityContext) GetInitValue(key string) (value interface{}, exists bool) {
return nil, false
}
// TaskName implements activity.Context.TaskName
func (c *TestActivityContext) TaskName() string {
return c.TaskNameVal
}
// GetAttrType implements data.Scope.GetAttrType
func (c *TestActivityContext) GetAttrType(attrName string) (attrType data.Type, exists bool) {
attr, found := c.Attrs[attrName]
if found {
return attr.Type(), true
}
return 0, false
}
// GetAttrValue implements data.Scope.GetAttrValue
func (c *TestActivityContext) GetAttrValue(attrName string) (value string, exists bool) {
attr, found := c.Attrs[attrName]
if found {
return attr.Value().(string), true
}
return "", false
}
// SetAttrValue implements data.Scope.SetAttrValue
func (c *TestActivityContext) SetAttrValue(attrName string, value string) {
attr, found := c.Attrs[attrName]
if found {
attr.SetValue(value)
}
}
// SetInput implements activity.Context.SetInput
func (c *TestActivityContext) SetInput(name string, value interface{}) {
attr, found := c.inputs[name]
if found {
attr.SetValue(value)
} else {
//error?
}
}
// SetInput implements activity.Context.SetInput
func (c *TestActivityContext) SetSetting(name string, value interface{}) {
attr, found := c.metadata.Settings[name]
if found {
s, _ := data.NewAttribute(name, attr.Type(), value)
c.settings[name] = s
} else {
//error?
}
}
// GetInput implements activity.Context.GetInput
func (c *TestActivityContext) GetInput(name string) interface{} {
attr, found := c.inputs[name]
if found {
return attr.Value()
}
return nil
}
// SetOutput implements activity.Context.SetOutput
func (c *TestActivityContext) SetOutput(name string, value interface{}) {
attr, found := c.outputs[name]
if found {
attr.SetValue(value)
} else {
//error?
}
}
// GetOutput implements activity.Context.GetOutput
func (c *TestActivityContext) GetOutput(name string) interface{} {
attr, found := c.outputs[name]
if found {
return attr.Value()
}
return nil
}
func (c *TestActivityContext) GetSharedTempData() map[string]interface{} {
if c.shared == nil {
c.shared = make(map[string]interface{})
}
return c.shared
}