From bd4881d4787cea8ee7f78309e956a7363d14c1b3 Mon Sep 17 00:00:00 2001 From: Steven van Beelen Date: Tue, 18 Dec 2018 14:47:23 +0100 Subject: [PATCH] BAEL-2435 Update Aggregate in regards to latest axon version -Replace old import statements for the new locations -Mark the aggregate as @Aggregate to make it autowired through the spring boot starter -Move all command handlers/decision making functions to the top of the file -Rename the command handler to `handle` as a best practice for command handlers -Use @EventSourcingHandler i.o. @EventHandler as a best practice for event handlers in Aggregates, as those only are used to 'source' the aggregate -Update test case according to latest standards --- .../axon/aggregates/MessagesAggregate.java | 26 ++++++++++--------- .../MessagesAggregateIntegrationTest.java | 21 +++++++-------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/axon/src/main/java/com/baeldung/axon/aggregates/MessagesAggregate.java b/axon/src/main/java/com/baeldung/axon/aggregates/MessagesAggregate.java index e762604b74..d95dd81df9 100644 --- a/axon/src/main/java/com/baeldung/axon/aggregates/MessagesAggregate.java +++ b/axon/src/main/java/com/baeldung/axon/aggregates/MessagesAggregate.java @@ -1,16 +1,18 @@ package com.baeldung.axon.aggregates; +import static org.axonframework.modelling.command.AggregateLifecycle.apply; + +import org.axonframework.commandhandling.CommandHandler; +import org.axonframework.eventsourcing.EventSourcingHandler; +import org.axonframework.modelling.command.AggregateIdentifier; +import org.axonframework.spring.stereotype.Aggregate; + import com.baeldung.axon.commands.CreateMessageCommand; import com.baeldung.axon.commands.MarkReadMessageCommand; import com.baeldung.axon.events.MessageCreatedEvent; import com.baeldung.axon.events.MessageReadEvent; -import org.axonframework.commandhandling.CommandHandler; -import org.axonframework.commandhandling.model.AggregateIdentifier; -import org.axonframework.eventhandling.EventHandler; - -import static org.axonframework.commandhandling.model.AggregateLifecycle.apply; - +@Aggregate public class MessagesAggregate { @AggregateIdentifier @@ -24,13 +26,13 @@ public class MessagesAggregate { apply(new MessageCreatedEvent(command.getId(), command.getText())); } - @EventHandler + @CommandHandler + public void handle(MarkReadMessageCommand command) { + apply(new MessageReadEvent(id)); + } + + @EventSourcingHandler public void on(MessageCreatedEvent event) { this.id = event.getId(); } - - @CommandHandler - public void markRead(MarkReadMessageCommand command) { - apply(new MessageReadEvent(id)); - } } \ No newline at end of file diff --git a/axon/src/test/java/com/baeldung/axon/MessagesAggregateIntegrationTest.java b/axon/src/test/java/com/baeldung/axon/MessagesAggregateIntegrationTest.java index ad099d2c2b..dc008dcd78 100644 --- a/axon/src/test/java/com/baeldung/axon/MessagesAggregateIntegrationTest.java +++ b/axon/src/test/java/com/baeldung/axon/MessagesAggregateIntegrationTest.java @@ -1,29 +1,28 @@ package com.baeldung.axon; +import java.util.UUID; + +import org.axonframework.test.aggregate.AggregateTestFixture; +import org.axonframework.test.aggregate.FixtureConfiguration; +import org.junit.*; + import com.baeldung.axon.aggregates.MessagesAggregate; import com.baeldung.axon.commands.CreateMessageCommand; import com.baeldung.axon.commands.MarkReadMessageCommand; import com.baeldung.axon.events.MessageCreatedEvent; import com.baeldung.axon.events.MessageReadEvent; -import org.axonframework.test.aggregate.AggregateTestFixture; -import org.axonframework.test.aggregate.FixtureConfiguration; -import org.junit.Before; -import org.junit.Test; - -import java.util.UUID; public class MessagesAggregateIntegrationTest { private FixtureConfiguration fixture; @Before - public void setUp() throws Exception { - fixture = new AggregateTestFixture(MessagesAggregate.class); - + public void setUp() { + fixture = new AggregateTestFixture<>(MessagesAggregate.class); } @Test - public void giveAggregateRoot_whenCreateMessageCommand_thenShouldProduceMessageCreatedEvent() throws Exception { + public void giveAggregateRoot_whenCreateMessageCommand_thenShouldProduceMessageCreatedEvent() { String eventText = "Hello, how is your day?"; String id = UUID.randomUUID().toString(); fixture.given() @@ -32,7 +31,7 @@ public class MessagesAggregateIntegrationTest { } @Test - public void givenMessageCreatedEvent_whenReadMessageCommand_thenShouldProduceMessageReadEvent() throws Exception { + public void givenMessageCreatedEvent_whenReadMessageCommand_thenShouldProduceMessageReadEvent() { String id = UUID.randomUUID().toString(); fixture.given(new MessageCreatedEvent(id, "Hello :-)"))