-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtrigger_test.go
93 lines (80 loc) · 1.93 KB
/
trigger_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
package main
import (
"context"
"testing"
"github.com/project-flogo/core/support/log"
"github.com/project-flogo/core/trigger"
"github.com/project-flogo/grpc/proto/grpc2grpc"
_ "github.com/project-flogo/grpc/trigger/grpc"
"github.com/project-flogo/grpc/util"
"github.com/stretchr/testify/assert"
)
type handler struct {
handled bool
}
func (h *handler) Name() string {
return "test"
}
func (h *handler) Settings() map[string]interface{} {
return map[string]interface{}{
"serviceName": "PetStoreService",
}
}
func (h *handler) Handle(ctx context.Context, triggerData interface{}) (map[string]interface{}, error) {
h.handled = true
return map[string]interface{}{
"code": 200,
"data": map[string]interface{}{
"pet": map[string]interface{}{
"id": 2,
"name": "pet2",
},
},
}, nil
}
type triggerInitContext struct {
handlers []trigger.Handler
}
func (i *triggerInitContext) Logger() log.Logger {
return log.RootLogger()
}
func (i *triggerInitContext) GetHandlers() []trigger.Handler {
return i.handlers
}
func TestGRPCTrigger(t *testing.T) {
factory := trigger.GetFactory("github.com/project-flogo/grpc/trigger/grpc")
assert.NotNil(t, factory)
hndlrConfigs := []*trigger.HandlerConfig{}
hc := &trigger.HandlerConfig{
Settings: map[string]interface{}{
"serviceName": "PetStoreService",
},
}
hndlrConfigs = append(hndlrConfigs, hc)
config := trigger.Config{
Id: "test",
Settings: map[string]interface{}{
"port": 9096,
"protoName": "petstore",
},
Handlers: hndlrConfigs,
}
instance, err := factory.New(&config)
assert.Nil(t, err)
h := handler{}
context := triggerInitContext{
handlers: []trigger.Handler{
&h,
},
}
err = instance.Initialize(&context)
assert.Nil(t, err)
util.Drain("9096")
instance.Start()
util.Pour("9096")
defer instance.Stop()
port, method := "9096", "pet"
_, err = grpc2grpc.CallClient(&port, &method, "2", nil)
assert.Nil(t, err)
assert.True(t, h.handled)
}