@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); }
@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()); }
@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 onlyStepWithRightActorInDifferentFlowReacts() { Model model = modelBuilder .useCase(USE_CASE) .basicFlow() .step(CUSTOMER_ENTERS_TEXT) .as(secondActor).user(EntersText.class).system(throwsRuntimeException()) .flow(ALTERNATIVE_FLOW).condition(this::textIsNotAvailable) .step(CUSTOMER_ENTERS_TEXT_AGAIN) .as(customer).user(EntersText.class).system(displaysEnteredText()) .build(); modelRunner.as(customer).run(model); Optional<Step> lastStepRun = modelRunner.reactTo(entersText()); assertEquals(CUSTOMER_ENTERS_TEXT_AGAIN, lastStepRun.get().getName()); }
@Test public void doesNotReactIfStepHasRightActorButFalseCondition() { Model model = modelBuilder .useCase(USE_CASE) .basicFlow() .step(CUSTOMER_ENTERS_TEXT) .as(secondActor).user(EntersText.class).system(displaysEnteredText()) .flow(ALTERNATIVE_FLOW).condition(() -> false) .step(THIS_STEP_SHOULD_BE_SKIPPED) .as(customer).user(EntersText.class).system(displaysEnteredText()) .build(); modelRunner.as(customer).run(model); Optional<Step> lastStepRun = modelRunner.reactTo(entersText()); assertFalse(lastStepRun.isPresent()); }
@Test public void reactsToSecondStepAlternativeWhen() { Model model = modelBuilder .useCase(USE_CASE) .basicFlow() .step(CUSTOMER_ENTERS_TEXT).user(EntersText.class).system(displaysEnteredText()) .step(THIS_STEP_SHOULD_BE_SKIPPED).user(EntersText.class).system(throwsRuntimeException()) .flow(ALTERNATIVE_FLOW).condition(() -> CUSTOMER_ENTERS_TEXT.equals(latestStepName())) .step(CUSTOMER_ENTERS_ALTERNATIVE_TEXT).user(EntersText.class).system(displaysEnteredText()) .build(); modelRunner.run(model).reactTo(entersText(), entersAlternativeText()); assertRecordedStepNames(CUSTOMER_ENTERS_TEXT, CUSTOMER_ENTERS_ALTERNATIVE_TEXT); }
@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 startsTwoUseCasesSequentially() { Model model = modelBuilder .useCase(USE_CASE) .basicFlow() .step(CUSTOMER_ENTERS_TEXT).user(EntersText.class).system(displaysEnteredText()) .useCase(USE_CASE_2) .basicFlow().condition(this::textIsAvailable) .step(CUSTOMER_ENTERS_NUMBER).user(EntersNumber.class).system(displaysEnteredNumber()) .build(); modelRunner.run(model); reactToAndAssertEvents(entersText(), entersNumber()); }
@Test public void reactsToFirstStepAlternativeWhen() { Model model = modelBuilder .useCase(USE_CASE) .basicFlow() .step(THIS_STEP_SHOULD_BE_SKIPPED).user(EntersText.class).system(throwsRuntimeException()) .step(THIS_STEP_SHOULD_BE_SKIPPED_AS_WELL).user(EntersText.class).system(throwsRuntimeException()) .flow(ALTERNATIVE_FLOW).condition(this::textIsNotAvailable) .step(CUSTOMER_ENTERS_ALTERNATIVE_TEXT).user(EntersText.class).system(displaysEnteredText()) .build(); modelRunner.run(model).reactTo(entersText()); assertRecordedStepNames(CUSTOMER_ENTERS_ALTERNATIVE_TEXT); }
@Test public void includesUseCaseWithBasicFlowAtFirstStep_withFalseCondition_cantReact() { Model model = modelBuilder .useCase(INCLUDED_USE_CASE) .basicFlow().condition(() -> false) .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); }
@Test public void includesUseCaseWithAlternativeFlowAtSecondStep_withFalseCondition() { Model model = modelBuilder .useCase(INCLUDED_USE_CASE) .basicFlow() .step(SYSTEM_HANDLES_EXCEPTION).user(Throwable.class).system(e -> e.printStackTrace()) .flow(ALTERNATIVE_FLOW).condition(() -> false) .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); }
@Test public void includesUseCaseWithAlternativeFlowAtFirstStep_withFalseCondition_cantReact() { Model model = modelBuilder .useCase(INCLUDED_USE_CASE) .basicFlow() .step(CUSTOMER_ENTERS_TEXT).user(EntersText.class).system(displaysEnteredText()) .flow(ALTERNATIVE_FLOW).condition(() -> false) .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); }
@Test public void includesUseCaseWithBasicFlowAtSecondStep_withFalseCondition_cantReact() { Model model = modelBuilder .useCase(INCLUDED_USE_CASE) .basicFlow().condition(() -> false) .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); }
.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())