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
This commit is contained in:
Steven van Beelen 2018-12-18 14:47:23 +01:00
parent cf56b33b9b
commit bd4881d478
2 changed files with 24 additions and 23 deletions

View File

@ -1,16 +1,18 @@
package com.baeldung.axon.aggregates; 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.CreateMessageCommand;
import com.baeldung.axon.commands.MarkReadMessageCommand; import com.baeldung.axon.commands.MarkReadMessageCommand;
import com.baeldung.axon.events.MessageCreatedEvent; import com.baeldung.axon.events.MessageCreatedEvent;
import com.baeldung.axon.events.MessageReadEvent; 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 { public class MessagesAggregate {
@AggregateIdentifier @AggregateIdentifier
@ -24,13 +26,13 @@ public class MessagesAggregate {
apply(new MessageCreatedEvent(command.getId(), command.getText())); apply(new MessageCreatedEvent(command.getId(), command.getText()));
} }
@EventHandler @CommandHandler
public void handle(MarkReadMessageCommand command) {
apply(new MessageReadEvent(id));
}
@EventSourcingHandler
public void on(MessageCreatedEvent event) { public void on(MessageCreatedEvent event) {
this.id = event.getId(); this.id = event.getId();
} }
@CommandHandler
public void markRead(MarkReadMessageCommand command) {
apply(new MessageReadEvent(id));
}
} }

View File

@ -1,29 +1,28 @@
package com.baeldung.axon; 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.aggregates.MessagesAggregate;
import com.baeldung.axon.commands.CreateMessageCommand; import com.baeldung.axon.commands.CreateMessageCommand;
import com.baeldung.axon.commands.MarkReadMessageCommand; import com.baeldung.axon.commands.MarkReadMessageCommand;
import com.baeldung.axon.events.MessageCreatedEvent; import com.baeldung.axon.events.MessageCreatedEvent;
import com.baeldung.axon.events.MessageReadEvent; 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 { public class MessagesAggregateIntegrationTest {
private FixtureConfiguration<MessagesAggregate> fixture; private FixtureConfiguration<MessagesAggregate> fixture;
@Before @Before
public void setUp() throws Exception { public void setUp() {
fixture = new AggregateTestFixture<MessagesAggregate>(MessagesAggregate.class); fixture = new AggregateTestFixture<>(MessagesAggregate.class);
} }
@Test @Test
public void giveAggregateRoot_whenCreateMessageCommand_thenShouldProduceMessageCreatedEvent() throws Exception { public void giveAggregateRoot_whenCreateMessageCommand_thenShouldProduceMessageCreatedEvent() {
String eventText = "Hello, how is your day?"; String eventText = "Hello, how is your day?";
String id = UUID.randomUUID().toString(); String id = UUID.randomUUID().toString();
fixture.given() fixture.given()
@ -32,7 +31,7 @@ public class MessagesAggregateIntegrationTest {
} }
@Test @Test
public void givenMessageCreatedEvent_whenReadMessageCommand_thenShouldProduceMessageReadEvent() throws Exception { public void givenMessageCreatedEvent_whenReadMessageCommand_thenShouldProduceMessageReadEvent() {
String id = UUID.randomUUID().toString(); String id = UUID.randomUUID().toString();
fixture.given(new MessageCreatedEvent(id, "Hello :-)")) fixture.given(new MessageCreatedEvent(id, "Hello :-)"))