-
Notifications
You must be signed in to change notification settings - Fork 35
/
service_test.go
197 lines (176 loc) · 4.11 KB
/
service_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
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
package endly_test
import (
"fmt"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/viant/assertly"
"github.com/viant/endly"
"github.com/viant/endly/model/location"
"testing"
)
func TestAbstractService_Run(t *testing.T) {
manager := endly.New()
srv := newService()
var useCases = []struct {
Description string
Request interface{}
Expected interface{}
HasError bool
}{
{
Description: "Init error",
Request: &TestRequest{
InitError: true,
},
HasError: true,
},
{
Description: "Validation error",
Request: &TestRequest{
ValidationError: true,
},
HasError: true,
},
{
Description: "Run error",
Request: &TestRequest{
RunError: true,
},
HasError: true,
},
{
Description: "Validation error",
Request: &TestRequest{
ValidationError: true,
},
HasError: true,
},
{
Description: "Simple request",
Request: &TestRequest{
Data: "test",
},
Expected: `{"Request":{"Data":"test","RunError":false,"InitError":false,"ValidationError":false,"Inited":true,"Validated":true}}`,
},
{
Description: "Assignable request",
Request: &TwinTestRequest{
Data: "test",
},
Expected: `{"Request":{"Data":"test","RunError":false,"InitError":false,"ValidationError":false,"Inited":false,"Validated":false}}`,
},
{
Description: "Unsupported request",
Request: &struct{}{},
HasError: true,
},
}
for _, useCase := range useCases {
context := manager.NewContext(nil)
response := srv.Run(context, useCase.Request)
if useCase.HasError {
assert.Equal(t, "error", response.Status, useCase.Description)
continue
}
assertly.AssertValues(t, useCase.Expected, response.Response, useCase.Description)
}
}
func TestAbstractService_Route(t *testing.T) {
srv := newService()
route, err := srv.Route("test")
if assert.Nil(t, err) {
assert.NotNil(t, route)
}
_, err = srv.Route("test2")
assert.NotNil(t, err)
}
func TestAbstractService_GetHostAndSSHPort(t *testing.T) {
srv := newService()
{
host, port := srv.GetHostname(nil)
assert.Equal(t, host, "")
assert.Equal(t, port, 0)
}
{
host, port := srv.GetHostname(location.NewResource("scp://127.0.0.1:22"))
assert.Equal(t, host, "127.0.0.1")
assert.Equal(t, port, 22)
}
{
host, port := srv.GetHostname(location.NewResource("file:///avc"))
assert.Equal(t, host, "127.0.0.1")
assert.Equal(t, port, 22)
}
}
func TestAbstractService_Sleep(t *testing.T) {
manager := endly.New()
context := manager.NewContext(nil)
srv := newService()
srv.Sleep(context, 1)
}
func TestAbstractService_Actions(t *testing.T) {
_ = endly.Registry.Register(func() endly.Service {
return newService()
})
}
type TestRequest struct {
Data string
RunError bool
InitError bool
ValidationError bool
Inited bool
Validated bool
}
func (r *TestRequest) Init() error {
r.Inited = true
if r.InitError {
return errors.New(r.Data)
}
return nil
}
func (r *TestRequest) Validate() error {
r.Validated = true
if r.ValidationError {
return errors.New(r.Data)
}
return nil
}
type TwinTestRequest TestRequest
type TestResponse struct {
Request interface{}
}
type service struct {
*endly.AbstractService
}
func (s *service) registerRoutes() {
s.Register(&endly.Route{
Action: "test",
RequestInfo: &endly.ActionInfo{
Description: "no operation action, helper for separating action.Init as self descriptive steps",
},
RequestProvider: func() interface{} {
return &TestRequest{}
},
ResponseProvider: func() interface{} {
return struct{}{}
},
Handler: func(context *endly.Context, request interface{}) (interface{}, error) {
if req, ok := request.(*TestRequest); ok {
if req.RunError {
return nil, errors.New(req.Data)
}
return &TestResponse{req}, nil
}
return nil, fmt.Errorf("unsupported request type: %T", request)
},
})
}
// newNopService creates a new NoOperation nopService.
func newService() *service {
var result = &service{
AbstractService: endly.NewAbstractService("test"),
}
result.AbstractService.Service = result
result.registerRoutes()
return result
}