/
WebFlowUnitTestMixinTests.groovy
201 lines (159 loc) · 5.17 KB
/
WebFlowUnitTestMixinTests.groovy
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
package grails.test.mixin
import grails.test.mixin.webflow.WebFlowUnitTestMixin
import org.junit.Test
import grails.test.mixin.domain.DomainClassUnitTestMixin
import grails.persistence.Entity
/**
* Created by IntelliJ IDEA.
* User: graemerocher
* Date: 7/12/11
* Time: 3:32 PM
* To change this template use File | Settings | File Templates.
*/
@TestMixin([WebFlowUnitTestMixin, DomainClassUnitTestMixin])
class WebFlowUnitTestMixinTests {
@Test
void testEatBreakfast_EggsFailValidation() {
mockDomain(Meal)
mockController(MealController)
def meal = new Meal()
meal.eggs = true
conversation.meal = meal
breakfastFlow.eatBreakfast.action()
assert 'unableToEatBreakfast' == stateTransition
assert !conversation.meal.hotSauce
assert conversation.meal.hasErrors()
}
@Test
void testFlowExecution() {
mockController(MealController)
breakfastFlow.init.action()
assert conversation.meal != null
}
@Test
void testExecuteTransitionAction() {
mockDomain(Meal)
mockController(MealController)
def meal = new Meal()
conversation.meal = meal
params.reason = "Feeling Sick"
def event = breakfastFlow.chooseMainDish.on.nothing.action()
assert event == 'success'
assert 'end' == stateTransition
assert params.reason == conversation.meal.skipReason
assert conversation.meal.id
}
@Test
void testChooseMainDish_TransitionNothingAction_BadReason() {
mockDomain(Meal)
mockController(MealController)
def meal = new Meal()
conversation.meal = meal
params.reason = ""
def event = breakfastFlow.chooseMainDish.on.nothing.action()
assert 'error' == event
assert 'chooseMainDish' == stateTransition
assert !conversation.meal.skipReason
assert !conversation.meal.id
}
@Test
void testGeneralTransitionStructure() {
mockController(MealController)
assert 'chooseMainDish' == breakfastFlow.init.on.success.to
assert 'prepareEggs' == breakfastFlow.chooseMainDish.on.eggs.to
assert 'prepareToast' == breakfastFlow.chooseMainDish.on.toast.to
assert 'end' == breakfastFlow.chooseMainDish.on.nothing.to
assert 'eatBreakfast' == breakfastFlow.prepareEggs.on.success.to
assert 'chooseMainDish' == breakfastFlow.prepareEggs.on.errors.to
assert 'eatBreakfast' == breakfastFlow.prepareToast.on.success.to
assert 'chooseMainDish' == breakfastFlow.prepareToast.on.errors.to
assert 'beHappy' == breakfastFlow.eatBreakfast.on.success.to
assert 'chooseMainDish' == breakfastFlow.eatBreakfast.on.unableToEatBreakfast.to
assert 'end' == breakfastFlow.beHappy.on.done.to
assert 'chooseMainDish' == breakfastFlow.beHappy.on.eatMore.to
assert !breakfastFlow.end.on
}
}
@Entity
class Meal {
String skipReason
boolean eggs
boolean hotSauce
void skip(String reason) {
skipReason = reason
}
static constraints = {
skipReason blank:false
hotSauce validator: { val, obj ->
return !(val == false && obj.eggs == true)
}
}
}
class MealController {
def mealService = [prepareForBreakfast:{ new Meal() }, addHotSauce:{ },addButter:{} ]
def prepareEggsFlow = {
crackEggs {
on 'sucess' to 'end'
}
end()
}
def prepareToastFlow = {
sliceBread {
on 'sucess' to 'end'
}
end()
}
final breakfastFlow = {
init {
action {
conversation.meal = mealService.prepareForBreakfast()
}
on('success').to('chooseMainDish')
}
chooseMainDish {
on('eggs').to('prepareEggs')
on('toast').to('prepareToast')
on('nothing'){
conversation.meal.skip(params.reason)
if(!conversation.meal.save()) {
return error()
}
return success()
}.to('end')
}
prepareEggs {
subflow(prepareEggsFlow)
on('success').to('eatBreakfast')
on('errors').to('chooseMainDish')
}
prepareToast{
subflow(prepareToastFlow)
on('success').to('eatBreakfast')
on('errors').to('chooseMainDish')
}
eatBreakfast {
action {
def meal = conversation.meal
if(meal.isEggs()) {
mealService.addHotSauce(meal)
} else if(meal.isToast()) {
mealService.addButter(meal)
}
def valid = meal.validate()
if(!valid) {
return unableToEatBreakfast()
}
return success()
}
on('success').to('beHappy')
on('unableToEatBreakfast').to('chooseMainDish')
}
beHappy {
on('done') {
conversation.meal.save()
}.to('end')
on('eatMore').to('chooseMainDish')
}
end()
}
}