-
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.
#76 Send one-way commands/notifications to saga participants - reacti…
…ve saga
- Loading branch information
Showing
23 changed files
with
758 additions
and
110 deletions.
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
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
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
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
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
51 changes: 51 additions & 0 deletions
51
...c/test/java/io/eventuate/tram/sagas/reactive/simpledsl/AbstractReactiveLocalSagaTest.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,51 @@ | ||
package io.eventuate.tram.sagas.reactive.simpledsl; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import reactor.core.publisher.Mono; | ||
|
||
import static io.eventuate.tram.sagas.reactive.simpledsl.framework.ReactiveSagaUnitTestSupport.given; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
public abstract class AbstractReactiveLocalSagaTest { | ||
protected LocalSagaSteps steps; | ||
|
||
@Before | ||
public void setUp() { | ||
steps = mock(LocalSagaSteps.class); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldRollbackFromStep2() { | ||
doReturn(Mono.empty()).when(steps).localStep1(any()); | ||
doReturn(Mono.empty()).when(steps).localStep1Compensation(any()); | ||
doReturn(Mono.empty()).when(steps).localStep3(any()); | ||
|
||
given(). | ||
saga(makeSaga(), new LocalSagaData()). | ||
expect(). | ||
command(new ReserveCreditCommand()). | ||
to("participant2"). | ||
andGiven(). | ||
failureReply(). | ||
andGiven(). | ||
expectRolledBack() | ||
; | ||
} | ||
|
||
@Test | ||
public void shouldHandleFailureOfFirstLocalStep() { | ||
LocalSagaData data = new LocalSagaData(); | ||
RuntimeException expectedCreateException = new RuntimeException("Failed local step"); | ||
doThrow(expectedCreateException).when(steps).localStep1(data); | ||
given(). | ||
saga(makeSaga(), data). | ||
expectException(expectedCreateException) | ||
; | ||
} | ||
|
||
protected abstract SimpleReactiveSaga<LocalSagaData> makeSaga(); | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...on-simple-dsl/src/test/java/io/eventuate/tram/sagas/reactive/simpledsl/LocalSagaData.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,20 @@ | ||
package io.eventuate.tram.sagas.reactive.simpledsl; | ||
|
||
import io.eventuate.tram.commands.consumer.CommandWithDestination; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class LocalSagaData { | ||
|
||
public Mono<CommandWithDestination> do2() { | ||
return Mono.just(new CommandWithDestination("participant2", null, new ReserveCreditCommand())); | ||
} | ||
|
||
public Mono<CommandWithDestination> undo2() { | ||
return Mono.just(new CommandWithDestination("participant2", null, new ReleaseCreditCommand())); | ||
} | ||
|
||
public Mono<CommandWithDestination> notify3() { | ||
return Mono.just(new CommandWithDestination("participant3", null, new NotifyCommand())); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...n-simple-dsl/src/test/java/io/eventuate/tram/sagas/reactive/simpledsl/LocalSagaSteps.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,11 @@ | ||
package io.eventuate.tram.sagas.reactive.simpledsl; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
public interface LocalSagaSteps { | ||
|
||
Mono<Void> localStep1(LocalSagaData data); | ||
Mono<Void> localStep1Compensation(LocalSagaData data); | ||
Mono<Void> localStep3(LocalSagaData data); | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
...on-simple-dsl/src/test/java/io/eventuate/tram/sagas/reactive/simpledsl/NotifyCommand.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,6 @@ | ||
package io.eventuate.tram.sagas.reactive.simpledsl; | ||
|
||
import io.eventuate.tram.commands.common.Command; | ||
|
||
public class NotifyCommand implements Command { | ||
} |
28 changes: 28 additions & 0 deletions
28
...imple-dsl/src/test/java/io/eventuate/tram/sagas/reactive/simpledsl/ReactiveLocalSaga.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,28 @@ | ||
package io.eventuate.tram.sagas.reactive.simpledsl; | ||
|
||
import io.eventuate.tram.sagas.reactive.orchestration.ReactiveSagaDefinition; | ||
|
||
public class ReactiveLocalSaga implements SimpleReactiveSaga<LocalSagaData> { | ||
|
||
private ReactiveSagaDefinition<LocalSagaData> sagaDefinition; | ||
|
||
public ReactiveLocalSaga(LocalSagaSteps steps) { | ||
this.sagaDefinition = | ||
step() | ||
.invokeLocal(steps::localStep1) | ||
.withCompensation(steps::localStep1Compensation) | ||
.step() | ||
.invokeParticipant(LocalSagaData::do2) | ||
.withCompensation(LocalSagaData::undo2) | ||
.step() | ||
.invokeLocal(steps::localStep3) | ||
.build(); | ||
} | ||
|
||
|
||
@Override | ||
public ReactiveSagaDefinition<LocalSagaData> getSagaDefinition() { | ||
return this.sagaDefinition; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...e-dsl/src/test/java/io/eventuate/tram/sagas/reactive/simpledsl/ReactiveLocalSagaTest.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,59 @@ | ||
package io.eventuate.tram.sagas.reactive.simpledsl; | ||
|
||
import org.junit.Test; | ||
import reactor.core.publisher.Mono; | ||
|
||
import static io.eventuate.tram.sagas.reactive.simpledsl.framework.ReactiveSagaUnitTestSupport.given; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class ReactiveLocalSagaTest extends AbstractReactiveLocalSagaTest { | ||
|
||
@Override | ||
protected SimpleReactiveSaga<LocalSagaData> makeSaga() { | ||
return new ReactiveLocalSaga(steps); | ||
} | ||
|
||
|
||
@Test | ||
public void shouldExecuteAllStepsSuccessfully() { | ||
doReturn(Mono.empty()).when(steps).localStep1(any()); | ||
doReturn(Mono.empty()).when(steps).localStep1Compensation(any()); | ||
doReturn(Mono.empty()).when(steps).localStep3(any()); | ||
|
||
given(). | ||
saga(makeSaga(), new LocalSagaData()). | ||
expect(). | ||
command(new ReserveCreditCommand()). | ||
to("participant2"). | ||
andGiven(). | ||
successReply() | ||
.expectCompletedSuccessfully() | ||
; | ||
|
||
verify(steps).localStep3(any()); | ||
} | ||
|
||
@Test | ||
public void shouldHandleFailureOfLastLocalStep() { | ||
doReturn(Mono.empty()).when(steps).localStep1(any()); | ||
doReturn(Mono.empty()).when(steps).localStep1Compensation(any()); | ||
doReturn(Mono.error(new RuntimeException())).when(steps).localStep3(any()); | ||
|
||
given(). | ||
saga(makeSaga(), new LocalSagaData()). | ||
expect(). | ||
command(new ReserveCreditCommand()). | ||
to("participant2"). | ||
andGiven(). | ||
successReply(). | ||
expect(). | ||
command(new ReleaseCreditCommand()). | ||
to("participant2"). | ||
andGiven(). | ||
successReply(). | ||
expectRolledBack() | ||
; | ||
} | ||
} |
Oops, something went wrong.