-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathworkflow_test.go
214 lines (162 loc) · 7.23 KB
/
workflow_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
package subscription
import (
"github.com/golang/mock/gomock"
"github.com/indeedeng/iwf-golang-samples/workflows/service"
"github.com/indeedeng/iwf-golang-sdk/gen/iwfidl"
"github.com/indeedeng/iwf-golang-sdk/iwf"
"github.com/indeedeng/iwf-golang-sdk/iwftest"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
// mockgen -source=workflows/subscription/my_service.go -destination=workflows/subscription/my_service_mock.go --package=subscription
var testCustomer = Customer{
FirstName: "Quanzheng",
LastName: "Long",
Id: "123",
Email: "[email protected]",
Subscription: Subscription{
BillingPeriod: time.Second,
MaxBillingPeriods: 10,
TrialPeriod: time.Second * 2,
BillingPeriodCharge: 100,
},
}
var testCustomerObj = iwftest.NewTestObject(testCustomer)
var mockWfCtx *iwftest.MockWorkflowContext
var mockPersistence *iwftest.MockPersistence
var mockCommunication *iwftest.MockCommunication
var emptyCmdResults = iwf.CommandResults{}
var emptyObj = iwftest.NewTestObject(nil)
var mockSvc *service.MockMyService
func beforeEach(t *testing.T) {
ctrl := gomock.NewController(t)
mockSvc = service.NewMockMyService(ctrl)
mockWfCtx = iwftest.NewMockWorkflowContext(ctrl)
mockPersistence = iwftest.NewMockPersistence(ctrl)
mockCommunication = iwftest.NewMockCommunication(ctrl)
}
func TestInitState_WaitUntil(t *testing.T) {
beforeEach(t)
state := NewInitState()
mockPersistence.EXPECT().SetDataAttribute(keyCustomer, testCustomer)
cmdReq, err := state.WaitUntil(mockWfCtx, testCustomerObj, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.EmptyCommandRequest(), cmdReq)
}
func TestInitState_Execute(t *testing.T) {
beforeEach(t)
state := NewInitState()
input := iwftest.NewTestObject(testCustomer)
decision, err := state.Execute(mockWfCtx, input, emptyCmdResults, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.MultiNextStates(
trialState{}, cancelState{}, updateChargeAmountState{},
), decision)
}
func TestTrialState_WaitUntil(t *testing.T) {
beforeEach(t)
state := NewTrialState(mockSvc)
mockSvc.EXPECT().SendEmail(testCustomer.Email, gomock.Any(), gomock.Any())
mockPersistence.EXPECT().GetDataAttribute(keyCustomer, gomock.Any()).SetArg(1, testCustomer)
cmdReq, err := state.WaitUntil(mockWfCtx, emptyObj, mockPersistence, mockCommunication)
assert.Nil(t, err)
firingTime := cmdReq.Commands[0].TimerCommand.FiringUnixTimestampSeconds
assert.Equal(t, iwf.AllCommandsCompletedRequest(
iwf.NewTimerCommand("", time.Unix(firingTime, 0)),
), cmdReq)
}
func TestTrialState_Execute(t *testing.T) {
beforeEach(t)
state := NewTrialState(mockSvc)
mockPersistence.EXPECT().SetDataAttribute(keyBillingPeriodNum, 0)
decision, err := state.Execute(mockWfCtx, emptyObj, emptyCmdResults, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.SingleNextState(
chargeCurrentBillState{}, nil,
), decision)
}
func TestChargeCurrentBillStateStart_waitForDuration(t *testing.T) {
beforeEach(t)
state := NewChargeCurrentBillState(mockSvc)
mockPersistence.EXPECT().GetDataAttribute(keyCustomer, gomock.Any()).SetArg(1, testCustomer)
mockPersistence.EXPECT().GetDataAttribute(keyBillingPeriodNum, gomock.Any()).SetArg(1, 0)
mockPersistence.EXPECT().SetDataAttribute(keyBillingPeriodNum, 1)
cmdReq, err := state.WaitUntil(mockWfCtx, emptyObj, mockPersistence, mockCommunication)
assert.Nil(t, err)
cmd := cmdReq.Commands[0]
assert.Equal(t, iwf.AllCommandsCompletedRequest(iwf.NewTimerCommand("", time.Unix(cmd.TimerCommand.FiringUnixTimestampSeconds, 0))), cmdReq)
}
func TestChargeCurrentBillStateStart_subscriptionOver(t *testing.T) {
beforeEach(t)
state := NewChargeCurrentBillState(mockSvc)
mockPersistence.EXPECT().GetDataAttribute(keyCustomer, gomock.Any()).SetArg(1, testCustomer)
mockPersistence.EXPECT().GetDataAttribute(keyBillingPeriodNum, gomock.Any()).SetArg(1, testCustomer.Subscription.MaxBillingPeriods)
mockPersistence.EXPECT().SetStateExecutionLocal(subscriptionOverKey, true)
cmdReq, err := state.WaitUntil(mockWfCtx, emptyObj, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.EmptyCommandRequest(), cmdReq)
}
func TestChargeCurrentBillStateDecide_subscriptionNotOver(t *testing.T) {
beforeEach(t)
state := NewChargeCurrentBillState(mockSvc)
mockPersistence.EXPECT().GetDataAttribute(keyCustomer, gomock.Any()).SetArg(1, testCustomer)
mockPersistence.EXPECT().GetStateExecutionLocal(subscriptionOverKey, gomock.Any())
mockSvc.EXPECT().ChargeUser(testCustomer.Email, testCustomer.Id, testCustomer.Subscription.BillingPeriodCharge)
decision, err := state.Execute(mockWfCtx, emptyObj, emptyCmdResults, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.SingleNextState(&chargeCurrentBillState{}, nil), decision)
}
func TestChargeCurrentBillStateDecide_subscriptionOver(t *testing.T) {
beforeEach(t)
state := NewChargeCurrentBillState(mockSvc)
mockPersistence.EXPECT().GetDataAttribute(keyCustomer, gomock.Any()).SetArg(1, testCustomer)
mockPersistence.EXPECT().GetStateExecutionLocal(subscriptionOverKey, gomock.Any()).SetArg(1, true)
mockSvc.EXPECT().SendEmail(testCustomer.Email, gomock.Any(), gomock.Any())
decision, err := state.Execute(mockWfCtx, emptyObj, emptyCmdResults, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.ForceCompletingWorkflow, decision)
}
func TestUpdateChargeAmountState_WaitUntil(t *testing.T) {
beforeEach(t)
state := NewUpdateChargeAmountState()
cmdReq, err := state.WaitUntil(mockWfCtx, emptyObj, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.AllCommandsCompletedRequest(iwf.NewSignalCommand("", SignalUpdateBillingPeriodChargeAmount)), cmdReq)
}
func TestUpdateChargeAmountState_Execute(t *testing.T) {
beforeEach(t)
state := NewUpdateChargeAmountState()
cmdResults := iwf.CommandResults{
Signals: []iwf.SignalCommandResult{
{
ChannelName: SignalUpdateBillingPeriodChargeAmount,
SignalValue: iwftest.NewTestObject(200),
Status: iwfidl.RECEIVED,
},
},
}
updatedCustomer := testCustomer
updatedCustomer.Subscription.BillingPeriodCharge = 200
mockPersistence.EXPECT().GetDataAttribute(keyCustomer, gomock.Any()).SetArg(1, testCustomer)
mockPersistence.EXPECT().SetDataAttribute(keyCustomer, updatedCustomer)
decision, err := state.Execute(mockWfCtx, emptyObj, cmdResults, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.SingleNextState(&updateChargeAmountState{}, nil), decision)
}
func TestCancelState_WaitUntil(t *testing.T) {
beforeEach(t)
state := NewCancelState(mockSvc)
cmdReq, err := state.WaitUntil(mockWfCtx, emptyObj, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.AllCommandsCompletedRequest(iwf.NewSignalCommand("", SignalCancelSubscription)), cmdReq)
}
func TestCancelState_Execute(t *testing.T) {
beforeEach(t)
state := NewCancelState(mockSvc)
mockPersistence.EXPECT().GetDataAttribute(keyCustomer, gomock.Any()).SetArg(1, testCustomer)
mockSvc.EXPECT().SendEmail(testCustomer.Email, gomock.Any(), gomock.Any())
decision, err := state.Execute(mockWfCtx, emptyObj, emptyCmdResults, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.ForceCompletingWorkflow, decision)
}