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

Added an option to create a fixture for a state stored aggregate #1772

Merged
merged 9 commits into from
May 12, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public class AggregateTestFixture<T> implements FixtureConfiguration<T>, TestExe
private static final Logger logger = LoggerFactory.getLogger(AggregateTestFixture.class);

private final Class<T> aggregateType;
private final boolean useStateStorage;
private final Set<Class<? extends T>> subtypes = new HashSet<>();
private final SimpleCommandBus commandBus;
private final EventStore eventStore;
Expand All @@ -145,24 +144,13 @@ public class AggregateTestFixture<T> implements FixtureConfiguration<T>, TestExe
* @param aggregateType the aggregate to initialize the test fixture for
*/
public AggregateTestFixture(Class<T> aggregateType) {
this(aggregateType, false);
}

/**
* Initializes a new given-when-then style test fixture for the given {@code aggregateType}.
*
* @param aggregateType the aggregate to initialize the test fixture for
* @param useStateStorage whether to use state storage or event sourcing
*/
public AggregateTestFixture(Class<T> aggregateType, boolean useStateStorage) {
deadlineManager = new StubDeadlineManager();
commandBus = SimpleCommandBus.builder().build();
eventStore = new RecordingEventStore();
resources.add(commandBus);
resources.add(eventStore);
resources.add(deadlineManager);
this.aggregateType = aggregateType;
this.useStateStorage = useStateStorage;
clearGivenWhenState();

registeredParameterResolverFactories.add(new SimpleResourceParameterResolverFactory(resources));
Expand All @@ -178,6 +166,16 @@ public final FixtureConfiguration<T> withSubtypes(Class<? extends T>... subtypes
return this;
}

@Override
public FixtureConfiguration<T> useStateStorage() {
return this.registerRepository(new InMemoryRepository<>(aggregateType,
smcvb marked this conversation as resolved.
Show resolved Hide resolved
subtypes,
eventStore,
getParameterResolverFactory(),
getHandlerDefinition(),
getRepositoryProvider()));
}

@Override
public FixtureConfiguration<T> registerRepository(Repository<T> repository) {
this.repository = new IdentifierValidatingRepository<>(repository);
Expand Down Expand Up @@ -323,9 +321,11 @@ public TestExecutor<T> givenNoPriorActivity() {

@Override
public TestExecutor<T> givenState(Supplier<T> aggregate) {
ensureRepositoryConfiguration();
clearGivenWhenState();
DefaultUnitOfWork.startAndGet(null).execute(() -> {
if (repository == null) {
this.useStateStorage();
}
try {
repository.newInstance(aggregate::get);
} catch (Exception e) {
Expand Down Expand Up @@ -511,28 +511,16 @@ private void registerAggregateCommandHandlers() {
}

private void ensureRepositoryConfiguration() {
if(repository != null) {
return;
}

if(this.useStateStorage) {
this.registerRepository(new InMemoryRepository<>(
aggregateType,
subtypes,
eventStore,
getParameterResolverFactory(),
getHandlerDefinition(),
getRepositoryProvider()));
} else {
if (repository == null) {
AggregateModel<T> aggregateModel = aggregateModel();
this.registerRepository(EventSourcingRepository.builder(aggregateType)
.aggregateModel(aggregateModel)
.aggregateFactory(new GenericAggregateFactory<>(aggregateModel))
.eventStore(eventStore)
.parameterResolverFactory(getParameterResolverFactory())
.handlerDefinition(getHandlerDefinition())
.repositoryProvider(getRepositoryProvider())
.build());
registerRepository(EventSourcingRepository.builder(aggregateType)
.aggregateModel(aggregateModel)
.aggregateFactory(new GenericAggregateFactory<>(aggregateModel))
.eventStore(eventStore)
.parameterResolverFactory(getParameterResolverFactory())
.handlerDefinition(getHandlerDefinition())
.repositoryProvider(getRepositoryProvider())
.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public interface FixtureConfiguration<T> {
*/
FixtureConfiguration<T> withSubtypes(Class<? extends T>... subtypes);

/**
* Configures the fixture for state stored aggregates.
sascha-eisenmann marked this conversation as resolved.
Show resolved Hide resolved
* This will register an {@link org.axonframework.test.aggregate.AggregateTestFixture.InMemoryRepository} with the fixture
*
* @return the current FixtureConfiguration, for fluent interfacing
*/
FixtureConfiguration<T> useStateStorage();

/**
* Registers an arbitrary {@code repository} with the fixture. The repository must be wired
* with the Event Store of this test fixture.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class FixtureTest_StateStorage {

@BeforeEach
void setUp() {
fixture = new AggregateTestFixture<>(StateStoredAggregate.class, true);
fixture = new AggregateTestFixture<>(StateStoredAggregate.class);
}

@AfterEach
Expand All @@ -63,7 +63,9 @@ void testCreateStateStoredAggregate() {

@Test
void testCreateStateStoredAggregateWithCommand() {
fixture.givenCommands(new InitializeCommand(AGGREGATE_ID, "message"))
fixture
smcvb marked this conversation as resolved.
Show resolved Hide resolved
.useStateStorage()
.givenCommands(new InitializeCommand(AGGREGATE_ID, "message"))
.when(new SetMessageCommand(AGGREGATE_ID, "message2"))
.expectEvents(new StubDomainEvent())
.expectState(aggregate -> assertEquals("message2", aggregate.getMessage()));
Expand Down