public Model buildWith(ModelBuilder modelBuilder) { Model model = modelBuilder.useCase("Get greeted") .basicFlow() .step("S1").system(this::promptsUserToEnterFirstName) .step("S2").user(ENTERS_FIRST_NAME).system(this::savesFirstName) .step("S3").system(this::promptsUserToEnterAge) .step("S4").user(ENTERS_AGE).system(this::savesAge) .step("S5").system(this::greetsUserWithFirstNameAndAge) .step("S6").system(this::stops) .flow("Handle out-of-bounds age").insteadOf("S5").condition(this::ageIsOutOfBounds) .step("S5a_1").system(this::informsUserAboutOutOfBoundsAge) .step("S5a_2").continuesAt("S3") .flow("Handle non-numerical age").insteadOf("S5") .step("S5b_1").on(NON_NUMERICAL_AGE).system(this::informsUserAboutNonNumericalAge) .step("S5b_2").continuesAt("S3") .build(); return model; }
@Test public void doesNotHandleExceptionIfSystemReactionDoesNotThrowException() { Model model = modelBuilder.useCase(USE_CASE) .basicFlow() .step(SYSTEM_DISPLAYS_TEXT).system(displaysConstantText()) .flow(ALTERNATIVE_FLOW).after(SYSTEM_DISPLAYS_TEXT) .step(SYSTEM_HANDLES_EXCEPTION).on(ArrayIndexOutOfBoundsException.class).system(e -> {}) .build(); modelRunner.run(model); assertRecordedStepNames(SYSTEM_DISPLAYS_TEXT); }
@Test public void handlesExceptionAtAnyTime() { Model model = modelBuilder.useCase(USE_CASE) .basicFlow() .step(SYSTEM_DISPLAYS_TEXT).system(displaysConstantText()) .flow(ALTERNATIVE_FLOW).after(SYSTEM_DISPLAYS_TEXT) .step(SYSTEM_THROWS_EXCEPTION).system(throwsArrayIndexOutOfBoundsException()) .flow(ALTERNATIVE_FLOW_2).anytime() .step(SYSTEM_HANDLES_EXCEPTION).on(ArrayIndexOutOfBoundsException.class).system(e -> {}) .build(); modelRunner.run(model); assertRecordedStepNames(SYSTEM_DISPLAYS_TEXT, SYSTEM_THROWS_EXCEPTION, SYSTEM_HANDLES_EXCEPTION); } }
@Test public void eventTypesReactedOnlyIfConditionFulfilled() { Model model = modelBuilder.useCase(USE_CASE) .basicFlow().condition(() -> false) .step(CUSTOMER_ENTERS_TEXT).user(EntersText.class).system(displaysEnteredText()) .flow("Alternative Flow: Could react as well").anytime() .step(CUSTOMER_ENTERS_NUMBER).user(EntersNumber.class).system(displaysEnteredNumber()) .build(); modelRunner.run(model); Set<Class<?>> reactToTypes = modelRunner.getReactToTypes(); assertEquals(1, reactToTypes.size()); Class<?> eventTypeReactedTo = reactToTypes.iterator().next(); assertEquals(EntersNumber.class, eventTypeReactedTo); }
public Model buildWith(ModelBuilder modelBuilder) { Model model = modelBuilder.useCase("Get greeted") .basicFlow() .step("S1").system(this::greetsUser) .build(); return model; }
@Test public void stepThasHasTrueConditionReactsEvenIfOtherStepWouldBePerformedBySystem() { Model model = modelBuilder .useCase(USE_CASE) .basicFlow() .step(CUSTOMER_ENTERS_TEXT).user(EntersText.class).system(displaysEnteredText()) .flow("Alternative Flow: Skipped").condition(() -> false) .step(THIS_STEP_SHOULD_BE_SKIPPED).system(() -> System.out.println("You should not see this!")) .build(); modelRunner.run(model).reactTo(entersText(), entersText()); assertRecordedStepNames(CUSTOMER_ENTERS_TEXT); }
@Test public void includesUseCaseWithAlternativeFlowAtFirstStep_witAnytimeCondition() { Model model = modelBuilder .useCase(INCLUDED_USE_CASE) .basicFlow() .step(CUSTOMER_ENTERS_TEXT).user(EntersText.class).system(displaysEnteredText()) .flow(ALTERNATIVE_FLOW).anytime() .step(CUSTOMER_ENTERS_NUMBER).user(EntersNumber.class).system(displaysEnteredNumber()) .useCase(USE_CASE) .basicFlow() .step(SYSTEM_INCLUDES_USE_CASE).includesUseCase(INCLUDED_USE_CASE) .step(SYSTEM_DISPLAYS_TEXT).system(displaysConstantText()) .build(); modelRunner.run(model).reactTo(entersNumber(), entersNumber()); assertRecordedStepNames(SYSTEM_INCLUDES_USE_CASE, CUSTOMER_ENTERS_NUMBER, SYSTEM_DISPLAYS_TEXT, CUSTOMER_ENTERS_NUMBER); }
.useCase("Included use case") .basicFlow() .step("Included step").system(promptsUserToEnterName()) .useCase("Get greeted") .basicFlow() .step("S1").system(promptsUserToEnterName()) .step("S2").user(entersName()).system(greetsUser()) .step("S3").as(firstActor).user(entersName()).system(greetsUser()).reactWhile(someConditionIsFulfilled()) .step("S5").as(firstActor, secondActor).system(promptsUserToEnterName()) .step("S6").system(quits()) .flow("Alternative flow A").insteadOf("S4") .step("S4a_1").system(blowsUp()) .step("S4a_2").continuesAt("S1") .flow("Alternative flow B").after("S3") .step("S4b_1").continuesAfter("S2") .flow("Alternative flow C").condition(thereIsNoAlternative()) .step("S5a").continuesWithoutAlternativeAt("S4") .flow("Alternative flow D").insteadOf("S4").condition(thereIsNoAlternative()) .step("S4c_1").includesUseCase("Included use case") .step("S4c_2").continuesAt("S1") .flow("EX").anytime() .step("EX1").on(Exception.class).system(logsException()) .build();
@Test public void throwsExceptionIfInsteadOfStepNotExistsInSameUseCase() { thrown.expect(NoSuchElementInModel.class); thrown.expectMessage(CUSTOMER_ENTERS_TEXT); modelBuilder.useCase(USE_CASE).basicFlow().insteadOf(CUSTOMER_ENTERS_TEXT); }
@Test public void sameEventTypeReactedTo() { Model model = modelBuilder.useCase(USE_CASE) .basicFlow().anytime() .step(CUSTOMER_ENTERS_TEXT).user(EntersText.class).system(displaysEnteredText()) .flow("Alternative Flow: Could react as well").anytime() .step(CUSTOMER_ENTERS_ALTERNATIVE_TEXT).user(EntersText.class).system(displaysEnteredText()) .build(); modelRunner.run(model); Set<Class<?>> reactToTypes = modelRunner.getReactToTypes(); assertEquals(1, reactToTypes.size()); Class<?> eventTypeReactedTo = reactToTypes.iterator().next(); assertEquals(EntersText.class, eventTypeReactedTo); }
@Test public void throwsExceptionIfAfterStepNotExistsInSameUseCase() { thrown.expect(NoSuchElementInModel.class); thrown.expectMessage(CUSTOMER_ENTERS_TEXT); modelBuilder.useCase(USE_CASE).basicFlow().after(CUSTOMER_ENTERS_TEXT); }
@Test public void createsSingleStepThatHandlesUserCommandWithCondition() { UseCasePart useCasePart = modelBuilder.useCase(USE_CASE); useCasePart.basicFlow().condition(() -> textIsAvailable()) .step(CUSTOMER_ENTERS_TEXT).user(EntersText.class).system(displaysEnteredText()) .build(); UseCase useCase = useCasePart.getUseCase(); assertTrue(useCase.getBasicFlow().getCondition().isPresent()); }
/** * Start the "happy day scenario" where all is fine and dandy. * * @return the flow part to create the steps of the basic flow. */ public FlowPart basicFlow() { Flow useCaseFlow = getUseCase().getBasicFlow(); return new FlowPart(useCaseFlow, this); }
public Model buildWith(ModelBuilder modelBuilder) { Model model = modelBuilder.useCase("Get greeted") .basicFlow() .step("S1").system(this::greetsUser) .step("S2").system(this::printsHooray).reactWhile(this::lessThanThreeHooraysHaveBeenPrinted) .build(); return model; }
@Test public void onlyStepWithTrueConditionReacts() { Model model = modelBuilder .useCase(USE_CASE) .basicFlow() .step(CUSTOMER_ENTERS_TEXT).user(EntersText.class).system(displaysEnteredText()) .flow("Alternative Flow: Skipped").condition(() -> false) .step(THIS_STEP_SHOULD_BE_SKIPPED).user(EntersText.class).system(throwsRuntimeException()) .build(); modelRunner.run(model).reactTo(entersText(), entersText()); assertRecordedStepNames(CUSTOMER_ENTERS_TEXT); }
@Test public void includesUseCaseWithAlternativeFlowAtSecondStep_withAnytimeCondition() { Model model = modelBuilder .useCase(INCLUDED_USE_CASE) .basicFlow() .step(SYSTEM_HANDLES_EXCEPTION).user(Throwable.class).system(e -> e.printStackTrace()) .flow(ALTERNATIVE_FLOW).anytime() .step(CUSTOMER_ENTERS_NUMBER).user(EntersNumber.class).system(displaysEnteredNumber()) .useCase(USE_CASE) .basicFlow() .step(CUSTOMER_ENTERS_TEXT).user(EntersText.class).system(displaysEnteredText()) .step(SYSTEM_INCLUDES_USE_CASE).includesUseCase(INCLUDED_USE_CASE) .step(SYSTEM_DISPLAYS_TEXT).system(displaysConstantText()) .build(); modelRunner.run(model).reactTo(entersText(), entersNumber(), entersNumber()); assertRecordedStepNames(CUSTOMER_ENTERS_TEXT, SYSTEM_INCLUDES_USE_CASE, CUSTOMER_ENTERS_NUMBER, SYSTEM_DISPLAYS_TEXT, CUSTOMER_ENTERS_NUMBER); }
@Test public void differentEventTypesReactedTo() { Model model = modelBuilder.useCase(USE_CASE) .basicFlow().anytime() .step(CUSTOMER_ENTERS_TEXT).user(EntersText.class).system(displaysEnteredText()) .flow("Alternative Flow: Could react as well").anytime() .step(CUSTOMER_ENTERS_NUMBER).user(EntersNumber.class).system(displaysEnteredNumber()) .build(); modelRunner.run(model); Set<Class<?>> reactToTypes = modelRunner.getReactToTypes(); assertEquals(2, reactToTypes.size()); assertTrue(reactToTypes.contains(EntersText.class)); assertTrue(reactToTypes.contains(EntersNumber.class)); }
/** * Start a flow with the specified name. * * @param flowName * the name of the flow. * * @return the flow part to create the steps of the flow. */ public FlowPart flow(String flowName) { Flow useCaseFlow = getUseCase().newFlow(flowName); return new FlowPart(useCaseFlow, this); }
public Model buildWith(ModelBuilder modelBuilder) { normalUser = modelBuilder.actor("Normal User"); anonymousUser = modelBuilder.actor("Anonymous User"); Model model = modelBuilder.useCase("Get greeted") .basicFlow() .step("S1").as(normalUser).system(this::promptsUserToEnterFirstName) .step("S2").as(normalUser).user(ENTERS_FIRST_NAME).system(this::savesFirstName) .step("S3").as(normalUser, anonymousUser).system(this::promptsUserToEnterAge) .step("S4").as(normalUser, anonymousUser).user(ENTERS_AGE).system(this::savesAge) .step("S5").as(normalUser).system(this::greetsUserWithFirstName) .step("S6").as(normalUser, anonymousUser).system(this::greetsUserWithAge) .step("S7").as(normalUser, anonymousUser).system(this::stops) .flow("Handle out-of-bounds age").insteadOf("S5").condition(this::ageIsOutOfBounds) .step("S5a_1").system(this::informsUserAboutOutOfBoundsAge) .step("S5a_2").continuesAt("S3") .flow("Handle non-numerical age").insteadOf("S5") .step("S5b_1").on(NON_NUMERICAL_AGE).system(this::informsUserAboutNonNumericalAge) .step("S5b_2").continuesAt("S3") .flow("Anonymous greeted with age only").insteadOf("S5").condition(this::ageIsOk) .step("S5c_1").as(anonymousUser).continuesAt("S6") .flow("Anonymous does not enter name").insteadOf("S1") .step("S1a_1").as(anonymousUser).continuesAt("S3") .build(); return model; }
public Model buildWith(ModelBuilder modelBuilder) { Model model = modelBuilder.useCase("Get greeted") .basicFlow() .step("S1").system(this::promptsUserToEnterFirstName) .step("S2").user(ENTERS_FIRST_NAME).system(this::greetsUserWithFirstName) .build(); return model; }