-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#85 Support conditional sending of notifications (non-reactive code)
- Loading branch information
Showing
8 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ build/ | |
*.iml | ||
*.log | ||
out | ||
**/bin/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...tuate/tram/sagas/simpledsl/notifications/ConditionalNotificationBasedCreateOrderSaga.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.eventuate.tram.sagas.simpledsl.notifications; | ||
|
||
import io.eventuate.tram.sagas.orchestration.SagaDefinition; | ||
import io.eventuate.tram.sagas.simpledsl.SimpleSaga; | ||
|
||
public class ConditionalNotificationBasedCreateOrderSaga implements SimpleSaga<ConditionalNotificationBasedCreateOrderSagaData> { | ||
|
||
private final SagaDefinition<ConditionalNotificationBasedCreateOrderSagaData> sagaDefinition; | ||
|
||
public ConditionalNotificationBasedCreateOrderSaga(ConditionalNotificationBasedCreateOrderSagaSteps steps) { | ||
this.sagaDefinition = | ||
step() | ||
.invokeLocal(steps::createOrder) | ||
.withCompensation(steps::rejectOrder) | ||
.step() | ||
.invokeParticipant(ConditionalNotificationBasedCreateOrderSagaData::reserveCredit) | ||
.withCompensation(ConditionalNotificationBasedCreateOrderSagaData::releaseCredit) | ||
.step() | ||
.invokeParticipant(ConditionalNotificationBasedCreateOrderSagaData::reserveInventory) | ||
.withCompensationNotification(ConditionalNotificationBasedCreateOrderSagaData::isReleaseInventory, ConditionalNotificationBasedCreateOrderSagaData::releaseInventory) | ||
.step() | ||
.invokeLocal(steps::approveOrder) | ||
.step() | ||
.notifyParticipant(ConditionalNotificationBasedCreateOrderSagaData::isFulfillOrder, ConditionalNotificationBasedCreateOrderSagaData::fulfillOrder) | ||
.build(); | ||
} | ||
|
||
|
||
@Override | ||
public SagaDefinition<ConditionalNotificationBasedCreateOrderSagaData> getSagaDefinition() { | ||
return this.sagaDefinition; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...e/tram/sagas/simpledsl/notifications/ConditionalNotificationBasedCreateOrderSagaData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.eventuate.tram.sagas.simpledsl.notifications; | ||
|
||
import io.eventuate.tram.commands.consumer.CommandWithDestination; | ||
import io.eventuate.tram.sagas.simpledsl.ReleaseCreditCommand; | ||
import io.eventuate.tram.sagas.simpledsl.ReserveCreditCommand; | ||
|
||
public class ConditionalNotificationBasedCreateOrderSagaData { | ||
|
||
private boolean fulfillOrder = true; | ||
private boolean releaseInventory = true; | ||
|
||
public CommandWithDestination reserveCredit() { | ||
return new CommandWithDestination("customerService", null, new ReserveCreditCommand()); | ||
} | ||
|
||
public CommandWithDestination releaseCredit() { | ||
return new CommandWithDestination("customerService", null, new ReleaseCreditCommand()); | ||
} | ||
|
||
public CommandWithDestination reserveInventory() { | ||
return new CommandWithDestination("inventoryService", null, new ReserveInventory()); | ||
} | ||
|
||
public CommandWithDestination releaseInventory() { | ||
return new CommandWithDestination("inventoryService", null, new ReleaseInventory()); | ||
} | ||
|
||
public CommandWithDestination fulfillOrder() { | ||
return new CommandWithDestination("fulfillmentService", null, new FulfillOrder()); | ||
} | ||
|
||
public ConditionalNotificationBasedCreateOrderSagaData skipFulfillOrder() { | ||
this.fulfillOrder = false; | ||
return this; | ||
} | ||
|
||
public ConditionalNotificationBasedCreateOrderSagaData skipReleaseInventory() { | ||
this.releaseInventory = false; | ||
return this; | ||
} | ||
|
||
public boolean isFulfillOrder() { | ||
return fulfillOrder; | ||
} | ||
|
||
public boolean isReleaseInventory() { | ||
return releaseInventory; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
.../tram/sagas/simpledsl/notifications/ConditionalNotificationBasedCreateOrderSagaSteps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.eventuate.tram.sagas.simpledsl.notifications; | ||
|
||
public interface ConditionalNotificationBasedCreateOrderSagaSteps { | ||
|
||
void createOrder(ConditionalNotificationBasedCreateOrderSagaData data); | ||
void rejectOrder(ConditionalNotificationBasedCreateOrderSagaData data); | ||
void approveOrder(ConditionalNotificationBasedCreateOrderSagaData data); | ||
|
||
} |
120 changes: 120 additions & 0 deletions
120
...e/tram/sagas/simpledsl/notifications/ConditionalNotificationBasedCreateOrderSagaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package io.eventuate.tram.sagas.simpledsl.notifications; | ||
|
||
import io.eventuate.tram.sagas.simpledsl.ReleaseCreditCommand; | ||
import io.eventuate.tram.sagas.simpledsl.ReserveCreditCommand; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static io.eventuate.tram.sagas.testing.SagaUnitTestSupport.given; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class ConditionalNotificationBasedCreateOrderSagaTest { | ||
|
||
private ConditionalNotificationBasedCreateOrderSagaSteps steps; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
steps = mock(ConditionalNotificationBasedCreateOrderSagaSteps.class); | ||
} | ||
|
||
@Test | ||
public void shouldExecuteAllStepsSuccessfully() { | ||
given(). | ||
saga(new ConditionalNotificationBasedCreateOrderSaga(steps), new ConditionalNotificationBasedCreateOrderSagaData()). | ||
expect(). | ||
command(new ReserveCreditCommand()). | ||
to("customerService"). | ||
andGiven(). | ||
successReply(). | ||
expect(). | ||
command(new ReserveInventory()). | ||
to("inventoryService"). | ||
andGiven(). | ||
successReply(). | ||
expect(). | ||
notification(new FulfillOrder()). | ||
to("fulfillmentService"). | ||
expectCompletedSuccessfully() | ||
; | ||
} | ||
|
||
@Test | ||
public void shouldExecuteAllStepsSuccessfullyWithSkippedFulfilOrder() { | ||
given(). | ||
saga(new ConditionalNotificationBasedCreateOrderSaga(steps), | ||
new ConditionalNotificationBasedCreateOrderSagaData().skipFulfillOrder()). | ||
expect(). | ||
command(new ReserveCreditCommand()). | ||
to("customerService"). | ||
andGiven(). | ||
successReply(). | ||
expect(). | ||
command(new ReserveInventory()). | ||
to("inventoryService"). | ||
andGiven(). | ||
successReply(). | ||
expectCompletedSuccessfully() | ||
; | ||
} | ||
|
||
@Test | ||
public void shouldHandleFailureOfLastLocalStep() { | ||
doThrow(new RuntimeException()).when(steps).approveOrder(any()); | ||
given(). | ||
saga(new ConditionalNotificationBasedCreateOrderSaga(steps), new ConditionalNotificationBasedCreateOrderSagaData()). | ||
expect(). | ||
command(new ReserveCreditCommand()). | ||
to("customerService"). | ||
andGiven(). | ||
successReply(). | ||
expect(). | ||
command(new ReserveInventory()). | ||
to("inventoryService"). | ||
andGiven(). | ||
successReply(). | ||
expect(). | ||
multiple(). | ||
notification(new ReleaseInventory()). | ||
to("inventoryService"). | ||
command(new ReleaseCreditCommand()). | ||
to("customerService"). | ||
verify(). | ||
andGiven(). | ||
successReply(). | ||
expectRolledBack() | ||
; | ||
} | ||
|
||
|
||
@Test | ||
public void shouldHandleFailureOfLastLocalStepWithSkippedReleaseInventory() { | ||
doThrow(new RuntimeException()).when(steps).approveOrder(any()); | ||
given(). | ||
saga(new ConditionalNotificationBasedCreateOrderSaga(steps), | ||
new ConditionalNotificationBasedCreateOrderSagaData() | ||
.skipReleaseInventory()). | ||
expect(). | ||
command(new ReserveCreditCommand()). | ||
to("customerService"). | ||
andGiven(). | ||
successReply(). | ||
expect(). | ||
command(new ReserveInventory()). | ||
to("inventoryService"). | ||
andGiven(). | ||
successReply(). | ||
expect(). | ||
multiple(). | ||
command(new ReleaseCreditCommand()). | ||
to("customerService"). | ||
verify(). | ||
andGiven(). | ||
successReply(). | ||
expectRolledBack() | ||
; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters