Created
March 23, 2011 21:40
-
-
Save bendoerr/884076 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MealController { | |
def mealService | |
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) | |
} | |
if(meal.validate()) { | |
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() | |
} | |
} | |
class MealControllerBreakfastWebFlowTests extends WebFlowUnitTestCase { | |
void testEatBreakfast_EggsValidate() { | |
mockDomain(Meal) | |
def meal = new Meal() | |
meal.eggs = true | |
mockConversationScope.meal = meal | |
withMealServiceAddHotSauceMock { | |
controller.mealService = it | |
webFlow.eatBreakfast.action() | |
} | |
assert 'success' == stateTransition | |
assert mockConversationScope.meal.hashotSauce() | |
assert !mockConversationScope.meal.hasErrors() | |
} | |
void testEatBreakfast_EggsFailValidation() { | |
mockDomain(Meal) | |
def meal = new Meal() | |
meal.eggs = true | |
mockConversationScope.meal = meal | |
withMealServiceAddHotSauceMock(dontAddHotSauce: true) { | |
controller.mealService = it | |
webFlow.eatBreakfast.action() | |
} | |
assert 'unableToEatBreakfast' == stateTransition | |
assert !mockConversationScope.meal.hashotSauce() | |
assert mockConversationScope.meal.hasErrors() | |
} | |
void testChooseMainDish_TransitionNothingAction_ValidReason() { | |
mockDomain(Meal) | |
def meal = new Meal() | |
mockConversationScope.meal = meal | |
mockParams.reason = "Feeling Sick" | |
webFlow.chooseMainDish.on.nothing.action() | |
assert 'success' == stateTransition | |
assert mockParams.reason == mockConversationScope.meal.skipReason | |
assert mockConversationScope.meal.id | |
} | |
void testChooseMainDish_TransitionNothingAction_BadReason() { | |
mockDomain(Meal) | |
def meal = new Meal() | |
mockConversationScope.meal = meal | |
mockParams.reason = "" | |
webFlow.chooseMainDish.on.nothing.action() | |
assert 'error' == stateTransition | |
assert !mockConversationScope.meal.skipReason | |
assert !mockConversationScope.meal.id | |
} | |
void testGeneralTransitionStructure() { | |
assert 'chooseMainDish' == webFlow.init.on.success.to | |
assert 'prepareEggs' == webFlow.chooseMainDish.on.eggs.to | |
assert 'prepareToast' == webFlow.chooseMainDish.on.toast.to | |
assert 'end' == webFlow.chooseMainDish.on.nothing.to | |
assert controller.prepareEggsFlow == webFlow.prepareEggs.subflow | |
assert 'eatBreakfast' == webFlow.prepareEggs.on.success.to | |
assert 'chooseMainDish' == webFlow.prepareEggs.on.errors.to | |
assert controller.prepareToastFlow == webFlow.prepareToast.subflow | |
assert 'eatBreakfast' == webFlow.prepareToast.on.success.to | |
assert 'chooseMainDish' == webFlow.prepareToast.on.errors.to | |
assert 'beHappy' == webFlow.eatBreakfast.on.success.to | |
assert 'chooseMainDish' == webFlow.eatBreakfast.on.unableToEatBreakfast.to | |
assert 'done' == webFlow.beHappy.on.done.to | |
assert 'chooseMainDish' == webFlow.beHappy.on.eatMore.to | |
assert !webFlow.end.on | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment