HandlerDescriptionAnalyzer implements SagaAnalyzer { @Override public Map<Class<? extends Saga>, SagaHandlersMap> scanHandledMessageTypes() { Collection<Class<? extends Saga>> sagaClasses = typeScanner.scanForSagas(); Stream<DescribesHandlers> directDescriptionSagas = sagaClasses.stream() .filter(DescribesHandlers.class::isAssignableFrom) .filter(c -> !Modifier.isAbstract(c.getModifiers())) .flatMap(this::createDirectDescriptionInstance); return createHandlersMap(directDescriptionSagas); } @Inject  HandlerDescriptionAnalyzer(final TypeScanner typeScanner, final SagaInstanceCreator instanceCreator); @Override Map<Class<? extends Saga>, SagaHandlersMap> scanHandledMessageTypes();  }
@Test public void scanHandledMessageTypes_sagaIsAbstract_returnsEmpty() throws ExecutionException { when(typeScanner.scanForSagas()).thenReturn(Collections.singleton(AbstractHandler.class)); Map<Class<? extends Saga>, SagaHandlersMap> classSagaHandlersMapMap = sut.scanHandledMessageTypes(); assertThat("Expected map to be empty.", classSagaHandlersMapMap.isEmpty(), is(true)); }
@Test public void scanHandledMessageTypes_sagaWithHandler_returnsHandler() throws ExecutionException { mockSaga(String.class); Map<Class<? extends Saga>, SagaHandlersMap> classSagaHandlersMapMap = sut.scanHandledMessageTypes(); assertThat("Expected saga type to be present.", classSagaHandlersMapMap.containsKey(TestSaga.class), is(true)); Collection<MessageHandler> handlers = classSagaHandlersMapMap.get(TestSaga.class).messageHandlers(); assertThat("Expected handlers map to contain handler type.", handlers.iterator().next().getMessageType(), equalTo(String.class)); assertThat("Expected handlers map to to be starting type.", handlers.iterator().next().getStartsSaga(), is(true)); }
@Test public void scanHandledMessageTypes_sagaWithContinueHandler_returnsHandler() throws ExecutionException { mockSaga(String.class, Integer.class); Map<Class<? extends Saga>, SagaHandlersMap> classSagaHandlersMapMap = sut.scanHandledMessageTypes(); assertThat("Expected saga type to be present.", classSagaHandlersMapMap.containsKey(TestSaga.class), is(true)); Collection<MessageHandler> handlers = classSagaHandlersMapMap.get(TestSaga.class).messageHandlers(); Optional<MessageHandler> continueHandler = handlers.stream().filter(h -> h.getMessageType().equals(Integer.class)).findFirst(); assertThat("Expected handlers map to contain handler type", continueHandler.isPresent(), is(true)); assertThat("Expected handlers map to to be starting type.", continueHandler.get().getStartsSaga(), is(false)); }
@Test public void scanHandledMessageTypes_sagaNotImplementingDescription_returnsEmpty() throws ExecutionException { when(typeScanner.scanForSagas()).thenReturn(Collections.singleton(com.codebullets.sagalib.TestSaga.class)); Map<Class<? extends Saga>, SagaHandlersMap> classSagaHandlersMapMap = sut.scanHandledMessageTypes(); assertThat("Expected map to be empty.", classSagaHandlersMapMap.isEmpty(), is(true)); }