Hi all. I’m new to this framework, so apologies if I’m missing something really basic.
This is for a new open source project, the full code exhibiting this issue can be found here:
https://github.com/ddouglascarr/liquidcanon-api/tree/unit-cqrs
The rule I’m trying to implement is: “a member can create a new member only if they are an admin”. My problem is that I’m getting an EventStoreException in a test.
Both admin and non-admin members use the same aggregate, the only difference is that admins have an admin attribute. (accessed from MemberAggregate.getAdmin())
I have written different commands and events for creating admins and non-admins, but they both operate on the same aggreagate (MemberAggregate).
The above rule is implemented in this command handler (https://github.com/ddouglascarr/liquidcanon-api/blob/unit-cqrs/src/main/java/org/ddouglascarr/command/member/MemberCommandHandler.java):
@CommandHandler
public void handleCreateMember(CreateMemberCommand command)
throws MemberUnprivilegedException
{
MemberAggregate requester = repository.load(command.getRequestingMemberId());
if (!requester.getAdmin()) {
throw new MemberUnprivilegedException();
}
MemberAggregate memberAggregate = new MemberAggregate(
command.getId(), command.getLogin(), command.getPassword(),
command.getNotifyEmail(), false );
repository.add(memberAggregate);
}
This test is failing (https://github.com/ddouglascarr/liquidcanon-api/blob/unit-cqrs/src/test/java/org/ddouglascarr/integration/command/MemberCommandComponentTests.java):
@Test
public void createMemberShouldCreateMember() throws Exception
{
fixture.given(new AdminMemberCreatedEvent(ADMIN_MEMBER_ID, ADMIN_LOGIN, PASSWORD))
.when(new CreateMemberCommand(ADMIN_MEMBER_ID, MEMBER_ID, LOGIN, PASSWORD, EMAIL))
.expectEvents(new MemberCreatedEvent(MEMBER_ID, LOGIN, PASSWORD, EMAIL));
}
It fails with:
org.axonframework.eventstore.EventStoreException: Writing events for an unexpected aggregate. This could indicate that a wrong aggregate is being triggered.
If I remove the load command and validation logic from the command handler, the test fails with:
java.lang.AssertionError: The aggregate used in this fixture was initialized with an identifier different than the one used to load it. Loaded [a4a031fd-57a4-4666-b1fd-afcb75811111], but actual identifier is [a4a031fd-57a4-4666-b1fd-afcb75800000].
Make sure the identifier passed in the Command matches that of the given Events.
The UUID in the message equal MEMBER_ID and ADMIN_MEMBER_ID respectively
What am I doing wrong here?
Is it not possible to load an aggregate of the same type within a command handler?
If so, should I be using the query system to validate the rule, not the command system?
Thanks for your help,
Daniel