-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scenario): enhance the tutorial by adding a section on scenario
- Loading branch information
Showing
7 changed files
with
169 additions
and
9 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
64 changes: 64 additions & 0 deletions
64
examples/tutorial/src/main/java/faggregate/tutorial/CounterSuiteFactory.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,64 @@ | ||
package faggregate.tutorial; | ||
|
||
import io.morin.faggregate.core.validation.Scenario; | ||
import io.morin.faggregate.core.validation.Suite; | ||
import lombok.experimental.UtilityClass; | ||
import lombok.val; | ||
|
||
/** | ||
* The {@link CounterSuiteFactory} class is a factory class that creates a suite of scenarios to validate the | ||
* behavior of the {@link Counter} Aggregate. | ||
*/ | ||
@UtilityClass | ||
public class CounterSuiteFactory { | ||
|
||
/** | ||
* Creates a suite of scenarios to validate the behavior of the {@link Counter} Aggregate. | ||
* | ||
* @return the suite of scenarios | ||
*/ | ||
public Suite create() { | ||
// Define a scenario validating the creation of a counter | ||
val scenarioAId = "counter#a"; | ||
val scenarioA = Scenario | ||
.builder() | ||
.name("Counter Creation") | ||
// no state initialization is required for the Counter Creation scenario | ||
.given(Scenario.Given.builder().identifier(scenarioAId).build()) | ||
// trigger the increment of the counter | ||
.when(Scenario.When.builder().command(new IncrementCounter(scenarioAId)).build()) | ||
.then( | ||
Scenario.Then | ||
.builder() | ||
// validate the state of the aggregate has been incremented | ||
.state(Counter.create(scenarioAId).updateValue(1)) | ||
// validate the right event has been published | ||
.event(new CounterChanged(scenarioAId, 0, 1)) | ||
.build() | ||
) | ||
.build(); | ||
|
||
// Define a scenario validating the increment of a counter | ||
val scenarioBId = "counter#b"; | ||
val scenarioB = Scenario | ||
.builder() | ||
.name("Counter Creation") | ||
// initialize the state of the aggregate with a value of 1 for the Counter Increment scenario | ||
.given(Scenario.Given.builder().identifier(scenarioBId).command(new IncrementCounter(scenarioBId)).build()) | ||
// trigger the increment of the counter | ||
.when(Scenario.When.builder().command(new IncrementCounter(scenarioBId)).build()) | ||
.then( | ||
Scenario.Then | ||
.builder() | ||
// validate the state of the aggregate has been incremented | ||
.state(Counter.create(scenarioBId).updateValue(2)) | ||
// validate the right event has been published | ||
.event(new CounterChanged(scenarioBId, 1, 2)) | ||
.build() | ||
) | ||
.build(); | ||
|
||
// Create and return a suite containing the two scenarios | ||
return Suite.builder().scenario(scenarioA).scenario(scenarioB).build(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
examples/tutorial/src/test/java/faggregate/tutorial/CounterSuiteTest.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,41 @@ | ||
package faggregate.tutorial; | ||
|
||
import io.morin.faggregate.simple.core.SimpleAggregateManagerBuilder; | ||
import java.util.concurrent.CompletableFuture; | ||
import lombok.val; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* This JUnit test validates the side effect implementation executing the CounterSuite. | ||
*/ | ||
class CounterSuiteTest { | ||
|
||
@Test | ||
void shouldValidateSideEffects() { | ||
// create the repository | ||
val counterRepository = CounterRepository.create(); | ||
|
||
// create the builder for the aggregate manager | ||
val counterManagerBuilder = SimpleAggregateManagerBuilder.<String, Counter>get(); | ||
|
||
// create the configurer and configure the aggregate manager | ||
// i.e. register the handlers, mutators and side effects | ||
CounterConfigurer.create(counterRepository).configure(counterManagerBuilder); | ||
|
||
// build the aggregate manager | ||
val counterManager = counterManagerBuilder.build(); | ||
|
||
// execute the suite providing the aggregate manager | ||
CounterSuiteFactory | ||
.create() | ||
.execute( | ||
// the aggregate manager to test | ||
counterManager, | ||
// an optional lambda to store the initialize state of the aggregate before the _Given_ phase | ||
// this functionality is not required for the CounterSuite, so the implementation is null | ||
null, | ||
// an optional lambda to load the state of the aggregate before the _Then_ phase | ||
identifier -> CompletableFuture.completedFuture(counterRepository.statesByIdentifier.get(identifier)) | ||
); | ||
} | ||
} |