-
Notifications
You must be signed in to change notification settings - Fork 21
/
load_test.go
405 lines (363 loc) · 9.79 KB
/
load_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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package openmock
import (
"testing"
"github.com/goombaio/orderedmap"
"github.com/stretchr/testify/assert"
)
func getMockFromMap(m *orderedmap.OrderedMap, key string) *Mock {
item, ok := m.Get(key)
if !ok {
return &Mock{}
}
return item.(*Mock)
}
func TestLoadRedis(t *testing.T) {
om := &OpenMock{
RedisType: "",
}
om.SetRedis()
redis := om.redis
t.Run("load redis happy", func(t *testing.T) {
_, err := om.redis.Do("HSET", redisTemplatesStore, "123", "stuff")
assert.NoError(t, err)
bytes, err := loadRedis(redis)
assert.Empty(t, err)
assert.Equal(t, "stuff", string(bytes))
_, err = om.redis.Do("HDEL", redisTemplatesStore, "123")
assert.NoError(t, err)
})
t.Run("load redis post keys", func(t *testing.T) {
postKey := "foobar"
_, err := om.redis.Do("HSET", redisTemplatesStore, "123", "stuff")
assert.NoError(t, err)
_, err = om.redis.Do("HSET", redisTemplatesStore+"_"+postKey, "123", "things")
assert.NoError(t, err)
bytes, err := loadRedis(redis)
assert.Empty(t, err)
assert.Equal(t, "stuff\nthings", string(bytes))
_, err = om.redis.Do("HDEL", redisTemplatesStore, "123")
assert.NoError(t, err)
_, err = om.redis.Do("HDEL", redisTemplatesStore+"_"+postKey, "123")
assert.NoError(t, err)
})
}
func TestLoadFile(t *testing.T) {
t.Run("kafka payload", func(t *testing.T) {
m := &Mock{
Actions: []ActionDispatcher{
{
ActionPublishKafka: ActionPublishKafka{
PayloadFromFile: "./files/colors.json",
},
},
},
}
m.loadFile("demo_templates")
assert.NotZero(t, m.Actions[0].ActionPublishKafka.Payload)
})
t.Run("amqp payload", func(t *testing.T) {
m := &Mock{
Actions: []ActionDispatcher{
{
ActionPublishAMQP: ActionPublishAMQP{
PayloadFromFile: "./files/colors.json",
},
},
},
}
m.loadFile("demo_templates")
assert.NotZero(t, m.Actions[0].ActionPublishAMQP.Payload)
})
t.Run("reply_http body", func(t *testing.T) {
m := &Mock{
Actions: []ActionDispatcher{
{
ActionReplyHTTP: ActionReplyHTTP{
BodyFromFile: "./files/colors.json",
},
},
},
}
m.loadFile("demo_templates")
assert.NotZero(t, m.Actions[0].ActionReplyHTTP.Body)
})
t.Run("reply_http body with binary file", func(t *testing.T) {
m := &Mock{
Actions: []ActionDispatcher{
{
ActionReplyHTTP: ActionReplyHTTP{
BodyFromBinaryFile: "./files/file-sample_150kB.pdf",
},
},
},
}
m.loadFile("demo_templates")
assert.NotZero(t, len(m.Actions[0].ActionReplyHTTP.BinaryFile))
})
t.Run("reply_grpc body", func(t *testing.T) {
m := &Mock{
Actions: []ActionDispatcher{
{
ActionReplyGRPC: ActionReplyGRPC{
PayloadFromFile: "./files/example_grpc_response.json",
},
},
},
}
m.loadFile("demo_templates")
assert.NotZero(t, m.Actions[0].ActionReplyGRPC.Payload)
})
t.Run("send_http body", func(t *testing.T) {
m := &Mock{
Actions: []ActionDispatcher{
{
ActionSendHTTP: ActionSendHTTP{
BodyFromFile: "./files/colors.json",
},
},
},
}
m.loadFile("demo_templates")
assert.NotZero(t, m.Actions[0].ActionSendHTTP.Body)
})
t.Run("send_http body with binary file", func(t *testing.T) {
m := &Mock{
Actions: []ActionDispatcher{
{
ActionSendHTTP: ActionSendHTTP{
BodyFromBinaryFile: "./files/file-sample_150kB.pdf",
},
},
},
}
m.loadFile("demo_templates")
assert.NotZero(t, len(m.Actions[0].ActionSendHTTP.BinaryFile))
})
t.Run("file not found", func(t *testing.T) {
m := &Mock{
Actions: []ActionDispatcher{
{
ActionReplyHTTP: ActionReplyHTTP{
BodyFromFile: "./files/not_exists.json",
},
},
},
}
m.loadFile("demo_templates")
assert.Zero(t, m.Actions[0].ActionReplyHTTP.Body)
})
t.Run("binary file not found", func(t *testing.T) {
m := &Mock{
Actions: []ActionDispatcher{
{
ActionReplyHTTP: ActionReplyHTTP{
BodyFromFile: "./files/not_exists.pdf",
},
},
},
}
m.loadFile("demo_templates")
assert.Zero(t, len(m.Actions[0].ActionReplyHTTP.BinaryFile))
})
}
func TestLoadYAML(t *testing.T) {
t.Run("happy code path", func(t *testing.T) {
bytes, err := loadYAML("demo_templates")
assert.NoError(t, err)
assert.NotZero(t, bytes)
})
t.Run("path not exists", func(t *testing.T) {
_, err := loadYAML("")
assert.Error(t, err)
assert.Equal(t, "lstat : no such file or directory", err.Error())
})
}
func TestLoad(t *testing.T) {
t.Run("happy code path", func(t *testing.T) {
om := &OpenMock{
TemplatesDir: "demo_templates",
}
om.SetupRepo()
err := om.Load()
assert.NoError(t, err)
assert.NotZero(t, len(om.repo.AMQPMocks))
assert.NotZero(t, len(om.repo.HTTPMocks))
assert.NotZero(t, len(om.repo.GRPCMocks))
assert.NotZero(t, len(om.repo.KafkaMocks))
assert.NotZero(t, len(om.repo.Templates))
assert.NotZero(t, om.repo.Behaviors.Size())
})
}
func TestLoadBehaviors(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
om := &OpenMock{}
om.SetupRepo()
ping := &Mock{
Key: "ping",
}
om.populateBehaviors(MocksArray{ping})
assert.Equal(t, ping, getMockFromMap(om.repo.Behaviors, "ping"))
})
t.Run("actions are ordered if order specified", func(t *testing.T) {
om := &OpenMock{}
om.SetupRepo()
expectedActions := []ActionDispatcher{
{
Order: 0,
ActionRedis: ActionRedis{"potato", "potato"},
},
{
Order: 1,
ActionReplyHTTP: ActionReplyHTTP{Body: "banana"},
},
}
ping := &Mock{
Key: "ping",
Actions: []ActionDispatcher{
{
Order: 1,
ActionReplyHTTP: ActionReplyHTTP{Body: "banana"},
},
{
Order: 0,
ActionRedis: ActionRedis{"potato", "potato"},
},
},
}
om.populateBehaviors(MocksArray{ping})
assert.Equal(t, expectedActions, getMockFromMap(om.repo.Behaviors, "ping").Actions)
})
}
func TestLoadExtendedBehaviors(t *testing.T) {
expectHTTP := ExpectHTTP{
Method: "GET",
Path: "/health-check",
}
abstract := &Mock{
Key: "abstract",
Kind: KindAbstractBehavior,
Expect: Expect{HTTP: expectHTTP},
Actions: []ActionDispatcher{{
ActionReplyHTTP: ActionReplyHTTP{Body: "banana"},
}},
}
concrete := &Mock{
Key: "concrete",
Expect: Expect{HTTP: expectHTTP},
Extend: "abstract",
Values: map[string]interface{}{
"foo": "bar",
},
}
t.Run("inherits actions", func(t *testing.T) {
om := &OpenMock{}
om.SetupRepo()
om.populateBehaviors(MocksArray{abstract, concrete})
assert.NotZero(t, getMockFromMap(om.repo.Behaviors, "concrete").Actions)
assert.Equal(t, "banana", getMockFromMap(om.repo.Behaviors, "concrete").Actions[0].ActionReplyHTTP.Body)
})
t.Run("persists values", func(t *testing.T) {
om := &OpenMock{}
om.SetupRepo()
om.populateBehaviors(MocksArray{abstract, concrete})
assert.Equal(t, "bar", getMockFromMap(om.repo.Behaviors, "concrete").Values["foo"])
})
t.Run("extended behavior defined after concrete behavior", func(t *testing.T) {
om := &OpenMock{}
om.SetupRepo()
om.populateBehaviors(MocksArray{concrete, abstract})
assert.Equal(t, "banana", getMockFromMap(om.repo.Behaviors, "concrete").Actions[0].ActionReplyHTTP.Body)
})
t.Run("abstract behavior not exposed as actionable", func(t *testing.T) {
om := &OpenMock{}
om.SetupRepo()
om.populateBehaviors(MocksArray{concrete, abstract})
assert.Equal(t, 1, len(om.repo.HTTPMocks[expectHTTP]))
})
concreteWithActions := &Mock{
Key: "concreteWithActions",
Expect: Expect{HTTP: expectHTTP},
Extend: "abstract",
Values: map[string]interface{}{
"foo": "bar",
},
Actions: []ActionDispatcher{{
Order: 1,
ActionRedis: ActionRedis{"hi", "bye"},
}},
}
t.Run("combines actions", func(t *testing.T) {
om := &OpenMock{}
om.SetupRepo()
om.populateBehaviors(MocksArray{abstract, concreteWithActions})
assert.NotZero(t, getMockFromMap(om.repo.Behaviors, "concreteWithActions").Actions)
assert.Equal(t, "banana", getMockFromMap(om.repo.Behaviors, "concreteWithActions").Actions[0].ActionReplyHTTP.Body)
assert.Equal(t, "hi", getMockFromMap(om.repo.Behaviors, "concreteWithActions").Actions[1].ActionRedis[0])
})
}
func TestMockPatching(t *testing.T) {
parentMock := Mock{
Key: "parent",
Expect: Expect{
Condition: "foo",
},
Values: map[string]interface{}{
"foo": "bar",
"value": "not-nana",
},
}
childMock := Mock{
Key: "child",
Values: map[string]interface{}{
"value": "banana",
},
}
patchedMock := parentMock.patchedWith(childMock)
t.Run("patched mock has values from the parent", func(t *testing.T) {
assert.Equal(t, "foo", patchedMock.Expect.Condition)
})
t.Run("patched mock overrides values from the patch", func(t *testing.T) {
assert.Equal(t, "banana", patchedMock.Values["value"])
assert.Equal(t, "child", patchedMock.Key)
})
t.Run("patched mock has values from the parent", func(t *testing.T) {
assert.Equal(t, "bar", patchedMock.Values["foo"])
})
t.Run("the original mocks are unchanged", func(t *testing.T) {
assert.Equal(t, "not-nana", parentMock.Values["value"])
assert.Zero(t, childMock.Expect)
})
}
func TestOverrideBehaviorByKey(t *testing.T) {
expectHTTP := ExpectHTTP{
Method: "GET",
Path: "/health-check",
}
nothealthy := &Mock{
Key: "health-check",
Expect: Expect{
HTTP: expectHTTP,
},
Values: map[string]interface{}{
"value": "not-healthy-banana",
},
}
healthy := &Mock{
Key: "health-check",
Expect: Expect{
HTTP: expectHTTP,
},
Values: map[string]interface{}{
"value": "healthy-banana",
},
}
om := &OpenMock{}
om.SetupRepo()
om.populateBehaviors(MocksArray{nothealthy, healthy})
t.Run("should only have one behavior with the same key", func(t *testing.T) {
assert.Equal(t, 1, len(om.repo.HTTPMocks[expectHTTP]))
})
t.Run("should take values with latest behavior", func(t *testing.T) {
assert.Equal(t, "healthy-banana", om.repo.HTTPMocks[expectHTTP][0].Values["value"])
})
}