### Question:
CompensationMessageHandler implements MessageHandler { @Override public void onReceive(String globalTxId, String localTxId, String parentTxId, String compensationMethod, Object... payloads) { context.apply(globalTxId, localTxId, parentTxId, compensationMethod, payloads); if (!context.getOmegaContext().getAlphaMetas().isAkkaEnabled()) { sender.send(new TxCompensatedEvent(globalTxId, localTxId, parentTxId, compensationMethod)); } } CompensationMessageHandler(SagaMessageSender sender, CallbackContext context); @Override void onReceive(String globalTxId, String localTxId, String parentTxId, String compensationMethod,
      Object... payloads);  }### Answer:
@Test public void sendsCompensatedEventOnCompensationCompleted() { final CallbackContext context = mock(CallbackContext.class); final CompensationMessageHandler handler = new CompensationMessageHandler(sender, context); IdGenerator<String> idGenerator = new UniqueIdGenerator(); OmegaContext omegaContext = new OmegaContext(idGenerator,AlphaMetas.builder().akkaEnabled(false).build()); when(context.getOmegaContext()).thenReturn(omegaContext); handler.onReceive(globalTxId, localTxId, parentTxId, compensationMethod, payload); assertThat(events.size(), is(1)); TxEvent event = events.get(0); assertThat(event.globalTxId(), is(globalTxId)); assertThat(event.localTxId(), is(localTxId)); assertThat(event.parentTxId(), is(parentTxId)); assertThat(event.type(), is(EventType.TxCompensatedEvent)); assertThat(event.compensationMethod(), is(getClass().getCanonicalName())); assertThat(event.payloads().length, is(0)); verify(context).apply(globalTxId, localTxId, parentTxId, compensationMethod, payload); }
@Test public void sendsCompensateAckSucceedEventOnCompensationCompletedWithFSM() throws NoSuchMethodException { IdGenerator<String> idGenerator = new UniqueIdGenerator(); OmegaContext omegaContext = new OmegaContext(idGenerator,AlphaMetas.builder().akkaEnabled(true).build()); CallbackContext context = new CallbackContext(omegaContext, sender); Method mockMethod = this.getClass().getMethod("mockCompensationSucceedMethod",String.class); context.addCallbackContext(compensationMethod, mockMethod, this); CompensationMessageHandler handler = new CompensationMessageHandler(sender, context); handler.onReceive(globalTxId, localTxId, parentTxId, compensationMethod, payload); assertThat(events.size(), is(1)); TxEvent event = events.get(0); assertThat(event.globalTxId(), is(globalTxId)); assertThat(event.localTxId(), is(localTxId)); assertThat(event.parentTxId(), is(parentTxId)); assertThat(event.type(), is(EventType.TxCompensateAckSucceedEvent)); assertThat(event.compensationMethod(), is(getClass().getCanonicalName())); assertThat(event.payloads().length, is(0)); }
@Test public void sendsCompensateAckFailedEventOnCompensationFailedWithFSM() throws NoSuchMethodException { IdGenerator<String> idGenerator = new UniqueIdGenerator(); OmegaContext omegaContext = new OmegaContext(idGenerator,AlphaMetas.builder().akkaEnabled(true).build()); CallbackContext context = new CallbackContext(omegaContext, sender); Method mockMethod = this.getClass().getMethod("mockCompensationFailedMethod",String.class); context.addCallbackContext(compensationMethod, mockMethod, this); CompensationMessageHandler handler = new CompensationMessageHandler(sender, context); handler.onReceive(globalTxId, localTxId, parentTxId, compensationMethod, payload); assertThat(events.size(), is(1)); TxEvent event = events.get(0); assertThat(event.globalTxId(), is(globalTxId)); assertThat(event.localTxId(), is(localTxId)); assertThat(event.parentTxId(), is(parentTxId)); assertThat(event.type(), is(EventType.TxCompensateAckFailedEvent)); assertThat(event.compensationMethod(), is(getClass().getCanonicalName())); assertThat(event.payloads().length, greaterThan(0)); }