Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade dependencies #2

Merged
merged 4 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.2/apache-maven-3.8.2-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Therefore, a custom configuration is required to resolve the artifacts, please r
<dependency>
<groupId>io.morin.faggregate</groupId>
<artifactId>faggregate-core-simple</artifactId>
<version>1.0.0</version>
<version>VERSION</version>
</dependency>
```

Expand All @@ -76,17 +76,19 @@ Therefore, a custom configuration is required to resolve the artifacts, please r
<dependency>
<groupId>io.morin.faggregate</groupId>
<artifactId>faggregate-spi-simple</artifactId>
<version>1.0.0</version>
<version>VERSION</version>
</dependency>
```

### The validation framework

[The GitHub Package](https://github.com/tmorin/faggregate/packages/1842497)

```xml
<dependency>
<groupId>io.morin.faggregate</groupId>
<artifactId>faggregate-core-validation</artifactId>
<version>1.0.0</version>
<artifactId>faggregate-core-scenario</artifactId>
<version>VERSION</version>
</dependency>
```

Expand Down
1 change: 1 addition & 0 deletions core-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<artifactId>faggregate</artifactId>
<groupId>io.morin.faggregate</groupId>
<version>1.6.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
1 change: 1 addition & 0 deletions core-scenario/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<artifactId>faggregate</artifactId>
<groupId>io.morin.faggregate</groupId>
<version>1.6.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package io.morin.faggregate.core.validation;

import io.morin.faggregate.api.Context;
import io.morin.faggregate.api.Initializer;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;

/**
* An in-memory repository is a repository that stores the state and the events in memory.
* <p>
* The purpose of this repository is to be used for testing and prototyping.
* Especially in conjunction with the scenario execution.
* So that, the scenario can be validated by the core business logic before to be used by the side effect implementations.
* <p>
* The initialization of the state can be handled by an initializer on load.
* The <code>initializedWhenNoFound</code> flag must be set to true.
* So that, the initializer will be called when the state is not found.
*
* @param <I> the type of the aggregate identifier
* @param <S> the type of the aggregate state
*/
@Value
@Builder
public class InMemoryRepository<I, S> implements Suite.Repository<I, S> {

/**
* The map to store the states.
*/
@Builder.Default
Map<I, S> statesMap = new HashMap<>();

/**
* The map to store the events.
*/
@Builder.Default
Map<I, List<Object>> eventsMap = new HashMap<>();

/**
* The flag to initialize the state when the state is not found.
* <p>
* The default value is false.
*/
boolean initializedWhenNoFound;

/**
* The initializer is called when the state is not found and the <code>initializedWhenNoFound</code> flag is set to true.
* <p>
* The default initializer throws an UnsupportedOperationException.
*/
@Builder.Default
Initializer<I, S> initializer = context -> {
throw new UnsupportedOperationException("Not implemented");
};

@Override
public <E> CompletableFuture<Void> persist(Context<I, ?> context, S state, List<E> events) {
// Delegate the store method
return this.storeState(context.getIdentifier(), state, events).toCompletableFuture();
}

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<Optional<S>> load(Context<I, ?> context) {
// Load the state
return this.loadState(context.getIdentifier())
.thenApply(d -> (Optional<S>) Optional.ofNullable(d))
.thenCompose(optional -> {
// If the state is not found and the initializedWhenNoFound flag is set to true, initialize the state
if (optional.isEmpty() && initializedWhenNoFound) {
return initializer.initialize(context).thenApply(Optional::of);
}
return CompletableFuture.completedFuture(optional);
})
.toCompletableFuture();
}

@Override
public <E> CompletableFuture<Void> destroy(Context<I, ?> context, S state, List<E> events) {
// Remove the state
statesMap.remove(context.getIdentifier());

// Remove the events
eventsMap.remove(context.getIdentifier());

// Return a completed future
return CompletableFuture.completedFuture(null);
}

@Override
@SuppressWarnings("unchecked")
public CompletionStage<Void> storeState(
@NonNull Object identifier,
@NonNull Object state,
@NonNull List<?> events
) {
// Persist the state
statesMap.put((I) identifier, (S) state);

// Persist the events
eventsMap.computeIfAbsent((I) identifier, k -> new ArrayList<>()).addAll(events);

// Return a completed future
return CompletableFuture.completedStage(null);
}

@Override
public CompletionStage<Object> loadState(@NonNull Object identifier) {
// Load the state
return CompletableFuture.completedStage(statesMap.get(identifier));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public CompletionStage<Void> execute() {
return Optional
// GIVEN STEP - initialize the state of the aggregate with a given state
.ofNullable(scenario.getGiven().getState())
.map(state -> before.store(scenario.getGiven().getIdentifier(), state, scenario.getGiven().getEvents()))
.map(state -> before.storeState(scenario.getGiven().getIdentifier(), state, scenario.getGiven().getEvents())
)
.orElseGet(() -> CompletableFuture.completedStage(null))
// GIVEN STEP - mutate the aggregate with given commands
.thenAccept(unused -> {
Expand All @@ -94,7 +95,7 @@ public CompletionStage<Void> execute() {
// WHEN - create the outcome based on the fetch aggregate state and the output of the command
.thenCompose(output ->
after
.load(scenario.getGiven().getIdentifier())
.loadState(scenario.getGiven().getIdentifier())
.thenApply(currentState -> new Outcome(output, currentState))
)
// THEN - assert the outcome
Expand Down Expand Up @@ -139,7 +140,7 @@ public interface Before {
* @param events a set of initial domain events
* @return a completion stage
*/
CompletionStage<Void> store(@NonNull Object identifier, @NonNull Object state, @NonNull List<?> events);
CompletionStage<Void> storeState(@NonNull Object identifier, @NonNull Object state, @NonNull List<?> events);
}

/**
Expand All @@ -153,7 +154,7 @@ public interface After {
* @param identifier the identifier of the aggregate
* @return the state of the aggregate as a completion stage
*/
CompletionStage<Object> load(@NonNull Object identifier);
CompletionStage<Object> loadState(@NonNull Object identifier);
}

/**
Expand All @@ -168,7 +169,7 @@ static class Outcome {
final Output<?> output;

/**
* The state fetched using {@link After#load(Object)}.
* The state fetched using {@link After#loadState(Object)}.
*/
final Object state;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.morin.faggregate.core.validation;

import io.morin.faggregate.api.AggregateManager;
import io.morin.faggregate.api.*;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import lombok.*;

Expand All @@ -14,14 +15,76 @@
@Builder
public class Suite {

/**
* The repository is a set of interfaces to store, load and destroy the state of the aggregate.
*
* @param <I> the type of the identifier
* @param <S> the type of the state
*/
public interface Repository<I, S>
extends Loader<I, S>, Persister<I, S>, Destroyer<I, S>, ScenarioExecutor.Before, ScenarioExecutor.After {
/**
* Apply the in-memory repository to the aggregate manager builder.
*
* @param builder the aggregate manager builder
* @return the aggregate manager builder
*/
default AggregateManagerBuilder<I, S> applyTo(@NonNull AggregateManagerBuilder<I, S> builder) {
builder.set((Loader<I, S>) this);
builder.set((Persister<I, S>) this);
builder.set((Destroyer<I, S>) this);
return builder;
}
}

/**
* The set of scenarios.
*/
@Singular
List<Scenario> scenarios;

/**
* Execute the scenarios sequentially.
* The supplier of the in-memory repository.
* <p>
* The default value is a supplier that creates a new instance of the in-memory repository.
*/
@Builder.Default
Supplier<Repository<?, ?>> repositorySupplier = () -> InMemoryRepository.builder().build();

/**
* Execute the scenarios sequentially based on the given Aggregate Manager Builder.
* <p>
* The method build the artifact manager associating side effect implementations to an in-memory repository.
* Moreover, the <i>before</i> and <i>after</i> lambda are implemented to store and load the state of the aggregate.
* This method is useful to validate the scenarios before to be used by the side effect implementations.
* That means to perform integration test on the core business logic, i.e. the {@link io.morin.faggregate.api.Handler}
* and the {@link io.morin.faggregate.api.Mutator}.
* <p>
* The in-memory repository implements the following interfaces:
* <ul>
* <li>{@link io.morin.faggregate.api.Persister}</li>
* <li>{@link io.morin.faggregate.api.Loader}</li>
* <li>{@link io.morin.faggregate.api.Destroyer}</li>
* <li>{@link io.morin.faggregate.core.validation.ScenarioExecutor.Before#storeState(Object, Object, List)}</li>
* <li>{@link io.morin.faggregate.core.validation.ScenarioExecutor.After#loadState(Object)}</li>
* </ul>
*
* @param amBuilder the Aggregate Manager Builder
* @param <I> The type of the identifier
* @param <S> The type of the state
* @return a completion stage
*/
@SuppressWarnings("unchecked")
public <I, S> CompletableFuture<Void> execute(@NonNull AggregateManagerBuilder<I, S> amBuilder) {
val repository = (Repository<I, S>) repositorySupplier.get();
return execute(repository.applyTo(amBuilder).build(), repository, repository);
}

/**
* Execute the scenarios sequentially based on the given Aggregate Manager.
* <p>
* This method is useful to validate the side effect implementations when the scenarios don't rely on both an initial
* state and the validation of the final state.
*
* @param am the Aggregate Manager
* @param <I> The type of the identifier
Expand All @@ -33,15 +96,20 @@ public <I> CompletableFuture<Void> execute(@NonNull AggregateManager<I> am) {
}

/**
* Execute the scenarios sequentially.
* Execute the scenarios sequentially based on the given Aggregate Manager.
* <p>
* This method is useful to validate the side effect implementations when the scenarios rely on either an initial
* state or the validation of the final state.
* <p>
* The before lambda is executed before the _Given_ phase.
* Its purpose is to store the state of the aggregate.
* As long as the state of the artifact is not provided during the _Given_ phase, the before lambda is not mandatory.
* As long as the state of the artifact is not provided during the <i>Given</i> phase (i.e. {@link io.morin.faggregate.core.validation.Scenario.Given#state}),
* the before lambda is not mandatory.
* <p>
* The after lambda is executed after the _Then_ phase.
* Its purpose is to load the state of the aggregate.
* As long as the state of the artifact is not validated during the _Then_ phase, the after lambda is not mandatory.
* As long as the state of the artifact is not validated during the <i>Then</i> phase (i.e. {@link io.morin.faggregate.core.validation.Scenario.Then#state}),
* the after lambda is not mandatory.
*
* @param am the Aggregate Manager
* @param before the optional before lambda
Expand Down
Loading
Loading