-
Notifications
You must be signed in to change notification settings - Fork 21
/
model.go
264 lines (227 loc) · 7.22 KB
/
model.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
package openmock
import (
"fmt"
"time"
"github.com/fatih/structs"
"github.com/goombaio/orderedmap"
yaml "gopkg.in/yaml.v2"
)
const (
KindBehavior = "Behavior"
KindAbstractBehavior = "AbstractBehavior"
KindTemplate = "Template"
)
// Mock represents a mock struct
// swagger:model
type Mock struct {
// Common fields
Kind string `yaml:"kind,omitempty"`
Key string `yaml:"key,omitempty"`
Extend string `yaml:"extend,omitempty"`
// KindBehavior fields
Expect Expect `yaml:"expect,omitempty"`
Actions []ActionDispatcher `yaml:"actions,omitempty"`
Values map[string]interface{} `yaml:"values,omitempty"`
// KindTemplate fields
Template string `yaml:"template,omitempty"`
}
// Validate validates the mock
func (m *Mock) Validate() error {
if m.Kind == "" {
m.Kind = KindBehavior
}
if m.Key == "" {
return fmt.Errorf("key cannot be empty")
}
switch m.Kind {
case KindTemplate:
if !structs.IsZero(m.Expect) || len(m.Actions) != 0 {
return fmt.Errorf("kind template is only permitted to have `key` and `template` fields. found in: %s", m.Key)
}
case KindBehavior:
if len(m.Template) != 0 {
return fmt.Errorf("kind behavior is only permitted to have `key`, `expect` and `actions` fields. found in: %s", m.Key)
}
foundReply := false
for _, a := range m.Actions {
if !structs.IsZero(a.ActionReplyHTTP) {
if foundReply {
return fmt.Errorf("Only one reply_http action allowed per behavior")
}
foundReply = true
}
}
case KindAbstractBehavior:
default:
return fmt.Errorf(
"invalid kind: %s with key: %s. only supported kinds are %v",
m.Kind, m.Key,
[]string{KindBehavior, KindAbstractBehavior, KindTemplate},
)
}
return nil
}
type (
// MocksArray represents an array of Mocks
MocksArray []*Mock
// HTTPMocks ...
HTTPMocks map[ExpectHTTP]MocksArray
// KafkaMocks is keyed by Topic
KafkaMocks map[ExpectKafka]MocksArray
// GRPCMocks is keyed by service/method
GRPCMocks map[ExpectGRPC]MocksArray
// AMQPMocks is keyed by Queue name
AMQPMocks map[ExpectAMQP]MocksArray
// MockRepo stores a repository of Mocks
MockRepo struct {
HTTPMocks HTTPMocks
GRPCMocks GRPCMocks
KafkaMocks KafkaMocks
AMQPMocks AMQPMocks
Templates MocksArray
Behaviors *orderedmap.OrderedMap
}
)
type (
// Expect represents what to expect from a mock
Expect struct {
Condition string `yaml:"condition,omitempty"`
HTTP ExpectHTTP `yaml:"http,omitempty"`
Kafka ExpectKafka `yaml:"kafka,omitempty"`
AMQP ExpectAMQP `yaml:"amqp,omitempty"`
GRPC ExpectGRPC `yaml:"grpc,omitempty"`
}
// ExpectKafka represents kafka expectation
ExpectKafka struct {
Topic string `yaml:"topic,omitempty"`
}
// ExpectAMQP represents amqp expectation
ExpectAMQP struct {
Exchange string `yaml:"exchange,omitempty"`
RoutingKey string `yaml:"routing_key,omitempty"`
Queue string `yaml:"queue,omitempty"`
}
// ExpectHTTP represents http expectation
ExpectHTTP struct {
Method string `yaml:"method,omitempty"`
Path string `yaml:"path,omitempty"`
}
// ExpectGRPC represents grpc expectation
ExpectGRPC struct {
Service string `yaml:"service,omitempty"`
Method string `yaml:"method,omitempty"`
}
)
// Action represents actions
type ActionDispatcher struct {
Order int `yaml:"order,omitempty"`
ActionPublishAMQP ActionPublishAMQP `yaml:"publish_amqp,omitempty"`
ActionPublishKafka ActionPublishKafka `yaml:"publish_kafka,omitempty"`
ActionRedis ActionRedis `yaml:"redis,omitempty"`
ActionReplyHTTP ActionReplyHTTP `yaml:"reply_http,omitempty"`
ActionSendHTTP ActionSendHTTP `yaml:"send_http,omitempty"`
ActionReplyGRPC ActionReplyGRPC `yaml:"reply_grpc,omitempty"`
ActionSleep ActionSleep `yaml:"sleep,omitempty"`
}
type Action interface {
Perform(context Context) error
}
// ActionRedis represents a list of redis commands
type ActionRedis []string
// ActionSendHTTP represents the send http action
type ActionSendHTTP struct {
URL string `yaml:"url,omitempty"`
Method string `yaml:"method,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
Body string `yaml:"body,omitempty"`
BodyFromFile string `yaml:"body_from_file,omitempty"`
BodyFromBinaryFile string `yaml:"body_from_binary_file,omitempty"`
BinaryFileName string `yaml:"binary_file_name,omitempty"`
BinaryFile []byte // Just for internal use
}
// ActionReplyHTTP represents reply http action
type ActionReplyHTTP struct {
StatusCode int `yaml:"status_code,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
Body string `yaml:"body,omitempty"`
BodyFromFile string `yaml:"body_from_file,omitempty"`
BodyFromBinaryFile string `yaml:"body_from_binary_file,omitempty"`
BinaryFileName string `yaml:"binary_file_name,omitempty"`
BinaryFile []byte // Just for internal use
}
// ActionReplyGRPC represents reply grpc action
type ActionReplyGRPC struct {
Headers map[string]string `yaml:"headers,omitempty"`
Payload string `yaml:"payload,omitempty"`
PayloadFromFile string `yaml:"payload_from_file,omitempty"`
}
// ActionPublishAMQP represents publish AMQP action
type ActionPublishAMQP struct {
Exchange string `yaml:"exchange,omitempty"`
RoutingKey string `yaml:"routing_key,omitempty"`
Payload string `yaml:"payload,omitempty"`
PayloadFromFile string `yaml:"payload_from_file,omitempty"`
}
// ActionPublishKafka represents publish kafka action
type ActionPublishKafka struct {
Topic string `yaml:"topic,omitempty"`
Payload string `yaml:"payload,omitempty"`
PayloadFromFile string `yaml:"payload_from_file,omitempty"`
}
// ActionSleep represents the sleep action
type ActionSleep struct {
Duration time.Duration `yaml:"duration,omitempty"`
}
func (repo *MockRepo) AsArray() (ret []*Mock) {
for _, item := range repo.Templates {
ret = append(ret, item)
}
for _, arr := range repo.HTTPMocks {
for _, m := range arr {
ret = append(ret, m)
}
}
for _, arr := range repo.AMQPMocks {
for _, m := range arr {
ret = append(ret, m)
}
}
for _, arr := range repo.KafkaMocks {
for _, m := range arr {
ret = append(ret, m)
}
}
for _, arr := range repo.GRPCMocks {
for _, m := range arr {
ret = append(ret, m)
}
}
return ret
}
// ToYAML outputs MockRepo to yaml bytes
func (repo *MockRepo) ToYAML() []byte {
ret := repo.AsArray()
b, _ := yaml.Marshal(ret)
return b
}
var GetActualAction = func(action ActionDispatcher) Action {
if !structs.IsZero(action.ActionPublishAMQP) {
return action.ActionPublishAMQP
}
if !structs.IsZero(action.ActionSendHTTP) {
return action.ActionSendHTTP
}
if !structs.IsZero(action.ActionReplyHTTP) {
return action.ActionReplyHTTP
}
if !structs.IsZero(action.ActionReplyGRPC) {
return action.ActionReplyGRPC
}
if len(action.ActionRedis) > 0 {
return action.ActionRedis
}
if !structs.IsZero(action.ActionPublishKafka) {
return action.ActionPublishKafka
}
return action.ActionSleep
}