-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqs_test.go
216 lines (178 loc) · 5.38 KB
/
sqs_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
package theta_test
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"time"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go/aws"
"github.com/phogolabs/theta"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/phogolabs/theta/fake"
)
var _ = Describe("SQSHandler", func() {
var (
reactor *theta.SQSHandler
event *theta.EventArgs
handler *FakeEventHandler
)
BeforeEach(func() {
event = &theta.EventArgs{
Event: &theta.Event{
ID: "event-001",
Source: "twilio",
Sender: "members-api",
Name: "member_created",
Timestamp: time.Now(),
},
Meta: theta.Metadata{},
Body: []byte("{}"),
}
handler = &FakeEventHandler{}
reactor = &theta.SQSHandler{
EventHandler: handler,
}
})
NewSQSEvent := func(data interface{}) events.SQSEvent {
buffer := &bytes.Buffer{}
writer := base64.NewEncoder(
base64.StdEncoding,
buffer,
)
Expect(json.NewEncoder(writer).Encode(data)).To(Succeed())
Expect(writer.Close()).To(Succeed())
return events.SQSEvent{
Records: []events.SQSMessage{
{Body: buffer.String()},
},
}
}
It("reacts on event successfully", func() {
sqsevent := NewSQSEvent(event)
Expect(reactor.HandleContext(context.TODO(), sqsevent)).To(Succeed())
Expect(handler.HandleContextCallCount()).To(Equal(1))
_, args := handler.HandleContextArgsForCall(0)
Expect(args.Event.ID).To(Equal(event.Event.ID))
})
Context("when the event cannot be unmarshalled", func() {
It("returns an error", func() {
sqsevent := NewSQSEvent("")
Expect(reactor.HandleContext(context.TODO(), sqsevent)).To(MatchError("json: cannot unmarshal string into Go value of type theta.EventArgs"))
})
})
Context("when the event validation fails", func() {
BeforeEach(func() {
event.Event.Name = ""
})
It("returns an error", func() {
sqsevent := NewSQSEvent(event)
Expect(reactor.HandleContext(context.TODO(), sqsevent)).To(HaveOccurred())
})
})
Context("when the event handler fails", func() {
BeforeEach(func() {
handler.HandleContextReturns(fmt.Errorf("oh no"))
})
It("returns an error", func() {
sqsevent := NewSQSEvent(event)
Expect(reactor.HandleContext(context.TODO(), sqsevent)).To(MatchError("oh no"))
})
})
})
// var _ = Describe("SQSCommandDispatcher", func() {
// var (
// command *domain.CommandArgs
// dispatcher *domain.SQSCommandDispatcher
// client *FakeSQSClient
// )
// BeforeEach(func() {
// client = &FakeSQSClient{}
// dispatcher = &domain.SQSCommandDispatcher{
// QueueURL: "http://example.com",
// Client: client,
// }
// command = &domain.CommandArgs{
// Command: &domain.Command{
// ID: "cmd-001",
// Name: "send",
// Sender: "notification-api",
// Source: "test",
// Timestamp: time.Now(),
// },
// Param: []byte("{}"),
// }
// })
// It("dispatches the command successfully", func() {
// Expect(dispatcher.ExecuteContext(context.TODO(), command)).To(Succeed())
// Expect(client.SendMessageWithContextCallCount()).To(Equal(1))
// ctx, input, _ := client.SendMessageWithContextArgsForCall(0)
// Expect(ctx).To(Equal(context.TODO()))
// Expect(input.QueueUrl).To(Equal(aws.String("http://example.com")))
// reader := base64.NewDecoder(
// base64.StdEncoding,
// bytes.NewBufferString(aws.StringValue(input.MessageBody)),
// )
// executed := &domain.CommandArgs{}
// Expect(json.NewDecoder(reader).Decode(executed)).To(Succeed())
// Expect(executed.Command.Name).To(Equal(command.Command.Name))
// })
// Context("when the command is not valid", func() {
// BeforeEach(func() {
// command.Command.Name = ""
// })
// It("returns an error", func() {
// err := dispatcher.ExecuteContext(context.TODO(), command)
// Expect(err).To(HaveOccurred())
// })
// })
// Context("when the client fails", func() {
// BeforeEach(func() {
// client.SendMessageWithContextReturns(nil, fmt.Errorf("oh no"))
// })
// It("returns an error", func() {
// Expect(dispatcher.ExecuteContext(context.TODO(), command)).To(MatchError("oh no"))
// })
// })
// })
var _ = Describe("SQSDispatcher", func() {
var (
eventArgs *theta.EventArgs
handler *theta.SQSDispatcher
client *FakeSQSClient
)
BeforeEach(func() {
eventArgs = &theta.EventArgs{
Event: &theta.Event{ID: "event-001"},
}
client = &FakeSQSClient{}
handler = &theta.SQSDispatcher{
QueueURL: "http://example.com",
Client: client,
}
})
It("streams the event successfully", func() {
Expect(handler.HandleContext(context.TODO(), eventArgs)).To(Succeed())
Expect(client.SendMessageWithContextCallCount()).To(Equal(1))
ctx, input, _ := client.SendMessageWithContextArgsForCall(0)
Expect(ctx).To(Equal(context.TODO()))
Expect(input.QueueUrl).To(Equal(aws.String("http://example.com")))
reader := base64.NewDecoder(
base64.StdEncoding,
bytes.NewBufferString(aws.StringValue(input.MessageBody)),
)
outboundEventArgs := &theta.EventArgs{}
Expect(json.NewDecoder(reader).Decode(outboundEventArgs)).To(Succeed())
Expect(outboundEventArgs.Event.ID).To(Equal(eventArgs.Event.ID))
})
Context("when the client fails", func() {
BeforeEach(func() {
client.SendMessageWithContextReturns(nil, fmt.Errorf("oh no"))
})
It("returns an error", func() {
Expect(handler.HandleContext(context.TODO(), eventArgs)).To(MatchError("oh no"))
})
})
})