@Test
 public void testLifecycle_DestroyedOnEnd() {
   manager.handle(new GenericEventMessage<StartingEvent>(new StartingEvent("12")));
   manager.handle(new GenericEventMessage<StartingEvent>(new StartingEvent("23")));
   manager.handle(new GenericEventMessage<MiddleEvent>(new MiddleEvent("12")));
   manager.handle(
       new GenericEventMessage<MiddleEvent>(new MiddleEvent("23"), singletonMap("catA", "value")));
   assertEquals(1, repositoryContents("12", MyTestSaga.class).size());
   assertEquals(1, repositoryContents("23", MyTestSaga.class).size());
   assertEquals(
       0,
       repositoryContents("12", MyTestSaga.class)
           .iterator()
           .next()
           .getSpecificHandlerInvocations());
   assertEquals(
       1,
       repositoryContents("23", MyTestSaga.class)
           .iterator()
           .next()
           .getSpecificHandlerInvocations());
   manager.handle(new GenericEventMessage<EndingEvent>(new EndingEvent("12")));
   assertEquals(1, repositoryContents("23", MyTestSaga.class).size());
   assertEquals(0, repositoryContents("12", MyTestSaga.class).size());
   manager.handle(new GenericEventMessage<EndingEvent>(new EndingEvent("23")));
   assertEquals(0, repositoryContents("23", MyTestSaga.class).size());
   assertEquals(0, repositoryContents("12", MyTestSaga.class).size());
 }
 @Test
 public void testCreationPolicy_CreationForced() {
   StartingEvent startingEvent = new StartingEvent("123");
   manager.handle(new GenericEventMessage<StartingEvent>(startingEvent));
   manager.handle(new GenericEventMessage<ForcingStartEvent>(new ForcingStartEvent("123")));
   Set<MyTestSaga> sagas = repositoryContents("123", MyTestSaga.class);
   assertEquals(2, sagas.size());
   for (MyTestSaga saga : sagas) {
     if (saga.getCapturedEvents().contains(startingEvent)) {
       assertEquals(2, saga.getCapturedEvents().size());
     }
     assertTrue(saga.getCapturedEvents().size() >= 1);
   }
 }
 @Test
 public void testLifeCycle_ExistingInstanceIgnoresEvent() {
   manager.handle(new GenericEventMessage<StartingEvent>(new StartingEvent("12")));
   manager.handle(new GenericEventMessage<StubDomainEvent>(new StubDomainEvent()));
   assertEquals(1, repositoryContents("12", MyTestSaga.class).size());
   assertEquals(
       1, repositoryContents("12", MyTestSaga.class).iterator().next().getCapturedEvents().size());
 }
  @Test
  public void testCorrelationDataReadFromProvider() throws Exception {
    CorrelationDataProvider<? super EventMessage> correlationDataProvider =
        mock(CorrelationDataProvider.class);
    manager.setCorrelationDataProvider(correlationDataProvider);

    manager.handle(
        asEventMessage(new StartingEvent("12")).withMetaData(singletonMap("key", "val")));

    verify(correlationDataProvider).correlationDataFor(isA(EventMessage.class));
  }
  @Test(timeout = 5000)
  public void testEventForSagaIsHandledWhenSagaIsBeingCreated() throws InterruptedException {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    final CountDownLatch awaitStart = new CountDownLatch(1);
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            manager.handle(
                new GenericEventMessage<StartingEvent>(
                    new SlowStartingEvent("12", awaitStart, 100)));
          }
        });
    awaitStart.await();
    manager.handle(asEventMessage(new MiddleEvent("12")));
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.SECONDS);

    assertEquals(1, repositoryContents("12", MyTestSaga.class).size());
    assertEquals(
        2, repositoryContents("12", MyTestSaga.class).iterator().next().getCapturedEvents().size());
  }
 @Test
 public void testCreationPolicy_SagaNotCreated() {
   manager.handle(new GenericEventMessage<MiddleEvent>(new MiddleEvent("123")));
   assertEquals(0, repositoryContents("123", MyTestSaga.class).size());
 }
 @Test
 public void testHandleUnrelatedEvent() {
   manager.handle(new GenericEventMessage("Unrelated"));
   verify(sagaRepository, never()).find(any(Class.class), (AssociationValue) isNull());
 }
 @Test
 public void testCreationPolicy_OneAlreadyExists() {
   manager.handle(new GenericEventMessage<StartingEvent>(new StartingEvent("123")));
   manager.handle(new GenericEventMessage<StartingEvent>(new StartingEvent("123")));
   assertEquals(1, repositoryContents("123", MyTestSaga.class).size());
 }
 @Test
 public void testLifeCycle_IgnoredEventDoesNotCreateInstance() {
   manager.handle(new GenericEventMessage<StubDomainEvent>(new StubDomainEvent()));
   assertEquals(0, repositoryContents("12", MyTestSaga.class).size());
 }
 @Test
 public void testNullAssociationValueDoesNotThrowNullPointer() {
   manager.handle(asEventMessage(new StartingEvent(null)));
 }