-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathactivity_test.go
120 lines (94 loc) · 2.54 KB
/
activity_test.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
package jsexec
import (
"testing"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/mapper"
"github.com/project-flogo/core/data/metadata"
logger "github.com/project-flogo/core/support/log"
"github.com/stretchr/testify/assert"
)
type initContext struct {
settings map[string]interface{}
}
func newInitContext(values map[string]interface{}) *initContext {
if values == nil {
values = make(map[string]interface{})
}
return &initContext{
settings: values,
}
}
func (i *initContext) Settings() map[string]interface{} {
return i.settings
}
func (i *initContext) MapperFactory() mapper.Factory {
return nil
}
func (i *initContext) Logger() logger.Logger {
return logger.RootLogger()
}
type activityContext struct {
input map[string]interface{}
output map[string]interface{}
}
func newActivityContext(values map[string]interface{}) *activityContext {
if values == nil {
values = make(map[string]interface{})
}
return &activityContext{
input: values,
output: make(map[string]interface{}),
}
}
func (a *activityContext) ActivityHost() activity.Host {
return a
}
func (a *activityContext) Name() string {
return "test"
}
func (a *activityContext) GetInput(name string) interface{} {
return a.input[name]
}
func (a *activityContext) SetOutput(name string, value interface{}) error {
a.output[name] = value
return nil
}
func (a *activityContext) GetInputObject(input data.StructValue) error {
return input.FromMap(a.input)
}
func (a *activityContext) SetOutputObject(output data.StructValue) error {
a.output = output.ToMap()
return nil
}
func (a *activityContext) GetSharedTempData() map[string]interface{} {
return nil
}
func (a *activityContext) ID() string {
return "test"
}
func (a *activityContext) IOMetadata() *metadata.IOMetadata {
return nil
}
func (a *activityContext) Reply(replyData map[string]interface{}, err error) {
}
func (a *activityContext) Return(returnData map[string]interface{}, err error) {
}
func (a *activityContext) Scope() data.Scope {
return nil
}
func (a *activityContext) Logger() logger.Logger {
return logger.RootLogger()
}
func TestJS(t *testing.T) {
activity, err := New(newInitContext(map[string]interface{}{
"script": "result.sum = parameters.a + parameters.b",
}))
assert.Nil(t, err)
ctx := newActivityContext(map[string]interface{}{
"parameters": map[string]interface{}{"a": 1.0, "b": 2.0},
})
_, err = activity.Eval(ctx)
assert.Nil(t, err)
assert.Equal(t, 3.0, ctx.output["result"].(map[string]interface{})["sum"].(float64))
}