Skip to content

Commit

Permalink
Enable including use cases recursively (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bertilmuth committed May 25, 2017
1 parent 8f3c86d commit 8d61f18
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Expand Up @@ -3,6 +3,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -35,6 +36,8 @@ public class UseCaseModelRunner {
private SystemReactionTrigger systemReactionTrigger;
private Consumer<SystemReactionTrigger> systemReaction;
private Optional<Predicate<Step>> stepWithoutAlternativePredicate;
private LinkedList<UseCase> includedUseCases;
private LinkedList<Step> includeSteps;
private Optional<UseCase> optionalIncludedUseCase;
private Optional<Step> optionalIncludeStep;

Expand Down Expand Up @@ -67,6 +70,8 @@ public void adaptSystemReaction(Consumer<SystemReactionTrigger> adaptedSystemRea
*/
public void restart() {
setLatestStep(Optional.empty());
includedUseCases = new LinkedList<>();
includeSteps = new LinkedList<>();
optionalIncludedUseCase = Optional.empty();
optionalIncludeStep = Optional.empty();
}
Expand Down Expand Up @@ -216,10 +221,22 @@ private void continueAfterIncludeStepWhenEndOfIncludedFlowIsReached() {
&& optionalIncludeStep.isPresent()
&& isAtEndOfIncludedFlow()) {
setLatestStep(optionalIncludeStep);
optionalIncludedUseCase = Optional.empty();
optionalIncludeStep = Optional.empty();
optionalIncludedUseCase = getUseCaseIncludedBefore();
optionalIncludeStep = getIncludeStepBefore();
}
}

private Optional<UseCase> getUseCaseIncludedBefore(){
includedUseCases.pop();
UseCase includedUseCase = includedUseCases.peek();
return includedUseCase != null? Optional.of(includedUseCase) : Optional.empty();
}

private Optional<Step> getIncludeStepBefore(){
includeSteps.pop();
Step includeStep = includeSteps.peek();
return includeStep != null? Optional.of(includeStep) : Optional.empty();
}


/**
Expand Down Expand Up @@ -350,13 +367,15 @@ public Optional<Flow> getLatestFlow() {
return latestStep.map(step -> step.getFlow());
}

public void setStepWithoutAlternativePredicate(Predicate<Step> stepWithoutAlternativePredicate) {
this.stepWithoutAlternativePredicate = Optional.of(stepWithoutAlternativePredicate);
public void setStepWithoutAlternativePredicate(Predicate<Step> predicate) {
stepWithoutAlternativePredicate = Optional.of(predicate);
}

public void includeUseCase(UseCase includedUseCase, Step includeStep) {
this.optionalIncludedUseCase = Optional.of(includedUseCase);
this.optionalIncludeStep = Optional.of(includeStep);
includedUseCases.push(includedUseCase);
includeSteps.push(includeStep);
optionalIncludedUseCase = Optional.of(includedUseCase);
optionalIncludeStep = Optional.of(includeStep);
for(Flow includedFlow : includedUseCase.getFlows()){
includeFlowAfterStep(includedFlow, includeStep);
}
Expand Down
Expand Up @@ -443,4 +443,33 @@ public void includedUseCaseCanBeRunOnItsOwn() {

assertEquals(CUSTOMER_ENTERS_NUMBER + ";", runStepNames());
}

@Test
public void includesUseCaseThatIncludesUseCase_withoutPredicate() {
UseCaseModel useCaseModel = useCaseModelBuilder
.useCase(USE_CASE_2)
.basicFlow()
.step(CUSTOMER_ENTERS_NUMBER).user(EnterNumber.class).system(displayEnteredNumber())
.useCase(INCLUDED_USE_CASE)
.basicFlow()
.step(CUSTOMER_ENTERS_TEXT).user(EnterText.class).system(displayEnteredText())
.step(SYSTEM_INCLUDES_USE_CASE_2).includeUseCase(USE_CASE_2)
.step(CUSTOMER_ENTERS_TEXT_AGAIN).user(EnterText.class).system(displayEnteredText())
.useCase(USE_CASE)
.basicFlow()
.step(SYSTEM_INCLUDES_USE_CASE).includeUseCase(INCLUDED_USE_CASE)
.step(SYSTEM_DISPLAYS_TEXT).system(displayConstantText())
.build();

useCaseModelRunner.run(useCaseModel);
useCaseModelRunner.reactTo(enterText(), enterNumber(), enterText());
String expectedSteps =
SYSTEM_INCLUDES_USE_CASE + ";"
+ CUSTOMER_ENTERS_TEXT + ";"
+ SYSTEM_INCLUDES_USE_CASE_2 + ";"
+ CUSTOMER_ENTERS_NUMBER + ";"
+ CUSTOMER_ENTERS_TEXT_AGAIN + ";"
+ SYSTEM_DISPLAYS_TEXT + ";";
assertEquals(expectedSteps, runStepNames());
}
}

0 comments on commit 8d61f18

Please sign in to comment.