From bd0a1efe707ba65b04f21a21f621110b7769ab0a Mon Sep 17 00:00:00 2001 From: Steven van Beelen Date: Thu, 27 Dec 2018 09:51:08 +0100 Subject: [PATCH] BAEL-2435 Switch domain around Change the domain from 'Messages' to 'Order' as that domain is better suited for an example project like this. To that end the MessagesAggregate should be replaced by an OrderAggregate. Additionally, add some decision making logic which denies the shipping of an order if it hasn't been confirmed yet. Adjust and update thee unit test accordingly --- .../axon/commandmodel/MessagesAggregate.java | 38 ------------ .../axon/commandmodel/OrderAggregate.java | 58 +++++++++++++++++++ .../MessagesAggregateUnitTest.java | 40 ------------- .../commandmodel/OrderAggregateUnitTest.java | 48 +++++++++++++++ 4 files changed, 106 insertions(+), 78 deletions(-) delete mode 100644 axon/src/main/java/com/baeldung/axon/commandmodel/MessagesAggregate.java create mode 100644 axon/src/main/java/com/baeldung/axon/commandmodel/OrderAggregate.java delete mode 100644 axon/src/test/java/com/baeldung/axon/commandmodel/MessagesAggregateUnitTest.java create mode 100644 axon/src/test/java/com/baeldung/axon/commandmodel/OrderAggregateUnitTest.java diff --git a/axon/src/main/java/com/baeldung/axon/commandmodel/MessagesAggregate.java b/axon/src/main/java/com/baeldung/axon/commandmodel/MessagesAggregate.java deleted file mode 100644 index 1ab9501617..0000000000 --- a/axon/src/main/java/com/baeldung/axon/commandmodel/MessagesAggregate.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.axon.commandmodel; - -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.coreapi.commands.CreateMessageCommand; -import com.baeldung.axon.coreapi.commands.MarkReadMessageCommand; -import com.baeldung.axon.coreapi.events.MessageCreatedEvent; -import com.baeldung.axon.coreapi.events.MessageReadEvent; - -@Aggregate -public class MessagesAggregate { - - @AggregateIdentifier - private String id; - - public MessagesAggregate() { - } - - @CommandHandler - public MessagesAggregate(CreateMessageCommand command) { - apply(new MessageCreatedEvent(command.getId(), command.getText())); - } - - @CommandHandler - public void handle(MarkReadMessageCommand command) { - apply(new MessageReadEvent(id)); - } - - @EventSourcingHandler - public void on(MessageCreatedEvent event) { - this.id = event.getId(); - } -} \ No newline at end of file diff --git a/axon/src/main/java/com/baeldung/axon/commandmodel/OrderAggregate.java b/axon/src/main/java/com/baeldung/axon/commandmodel/OrderAggregate.java new file mode 100644 index 0000000000..fd4f901032 --- /dev/null +++ b/axon/src/main/java/com/baeldung/axon/commandmodel/OrderAggregate.java @@ -0,0 +1,58 @@ +package com.baeldung.axon.commandmodel; + +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.coreapi.commands.ConfirmOrderCommand; +import com.baeldung.axon.coreapi.commands.PlaceOrderCommand; +import com.baeldung.axon.coreapi.commands.ShipOrderCommand; +import com.baeldung.axon.coreapi.events.OrderConfirmedEvent; +import com.baeldung.axon.coreapi.events.OrderPlacedEvent; +import com.baeldung.axon.coreapi.events.OrderShippedEvent; + +@Aggregate +public class OrderAggregate { + + @AggregateIdentifier + private String orderId; + private boolean orderConfirmed; + + @CommandHandler + public OrderAggregate(PlaceOrderCommand command) { + apply(new OrderPlacedEvent(command.getOrderId(), command.getProduct())); + } + + @CommandHandler + public void handle(ConfirmOrderCommand command) { + apply(new OrderConfirmedEvent(orderId)); + } + + @CommandHandler + public void handle(ShipOrderCommand command) { + if (!orderConfirmed) { + throw new IllegalStateException("Cannot ship an order which has not ben confirmed yet."); + } + + apply(new OrderShippedEvent(orderId)); + } + + @EventSourcingHandler + public void on(OrderPlacedEvent event) { + this.orderId = event.getOrderId(); + orderConfirmed = false; + } + + @EventSourcingHandler + public void on(OrderConfirmedEvent event) { + orderConfirmed = true; + } + + protected OrderAggregate() { + // Required by Axon to build a default Aggregate prior to Event Sourcing + } + +} \ No newline at end of file diff --git a/axon/src/test/java/com/baeldung/axon/commandmodel/MessagesAggregateUnitTest.java b/axon/src/test/java/com/baeldung/axon/commandmodel/MessagesAggregateUnitTest.java deleted file mode 100644 index 46357ddff8..0000000000 --- a/axon/src/test/java/com/baeldung/axon/commandmodel/MessagesAggregateUnitTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.axon.commandmodel; - -import java.util.UUID; - -import org.axonframework.test.aggregate.AggregateTestFixture; -import org.axonframework.test.aggregate.FixtureConfiguration; -import org.junit.*; - -import com.baeldung.axon.coreapi.commands.CreateMessageCommand; -import com.baeldung.axon.coreapi.commands.MarkReadMessageCommand; -import com.baeldung.axon.coreapi.events.MessageCreatedEvent; -import com.baeldung.axon.coreapi.events.MessageReadEvent; - -public class MessagesAggregateUnitTest { - - private FixtureConfiguration fixture; - - @Before - public void setUp() { - fixture = new AggregateTestFixture<>(MessagesAggregate.class); - } - - @Test - public void giveAggregateRoot_whenCreateMessageCommand_thenShouldProduceMessageCreatedEvent() { - String eventText = "Hello, how is your day?"; - String id = UUID.randomUUID().toString(); - fixture.given() - .when(new CreateMessageCommand(id, eventText)) - .expectEvents(new MessageCreatedEvent(id, eventText)); - } - - @Test - public void givenMessageCreatedEvent_whenReadMessageCommand_thenShouldProduceMessageReadEvent() { - String id = UUID.randomUUID().toString(); - - fixture.given(new MessageCreatedEvent(id, "Hello :-)")) - .when(new MarkReadMessageCommand(id)) - .expectEvents(new MessageReadEvent(id)); - } -} \ No newline at end of file diff --git a/axon/src/test/java/com/baeldung/axon/commandmodel/OrderAggregateUnitTest.java b/axon/src/test/java/com/baeldung/axon/commandmodel/OrderAggregateUnitTest.java new file mode 100644 index 0000000000..e7dfdeb794 --- /dev/null +++ b/axon/src/test/java/com/baeldung/axon/commandmodel/OrderAggregateUnitTest.java @@ -0,0 +1,48 @@ +package com.baeldung.axon.commandmodel; + +import java.util.UUID; + +import org.axonframework.test.aggregate.AggregateTestFixture; +import org.axonframework.test.aggregate.FixtureConfiguration; +import org.junit.*; + +import com.baeldung.axon.coreapi.commands.PlaceOrderCommand; +import com.baeldung.axon.coreapi.commands.ShipOrderCommand; +import com.baeldung.axon.coreapi.events.OrderConfirmedEvent; +import com.baeldung.axon.coreapi.events.OrderPlacedEvent; +import com.baeldung.axon.coreapi.events.OrderShippedEvent; + +public class OrderAggregateUnitTest { + + private static final String ORDER_ID = UUID.randomUUID().toString(); + private static final String DEFAULT_PRODUCT = "Deluxe Chair"; + + private FixtureConfiguration fixture; + + @Before + public void setUp() { + fixture = new AggregateTestFixture<>(OrderAggregate.class); + } + + @Test + public void giveNoPriorActivity_whenPlaceOrderCommand_thenShouldPublishOrderPlacedEvent() { + fixture.givenNoPriorActivity() + .when(new PlaceOrderCommand(ORDER_ID, DEFAULT_PRODUCT)) + .expectEvents(new OrderPlacedEvent(ORDER_ID, DEFAULT_PRODUCT)); + } + + @Test + public void givenOrderPlacedEvent_whenShipOrderCommand_thenShouldThrowIllegalStateException() { + fixture.given(new OrderPlacedEvent(ORDER_ID, DEFAULT_PRODUCT)) + .when(new ShipOrderCommand(ORDER_ID)) + .expectException(IllegalStateException.class); + } + + @Test + public void givenOrderPlacedEventAndOrderConfirmedEvent_whenShipOrderCommand_thenShouldPublishOrderShippedEvent() { + fixture.given(new OrderPlacedEvent(ORDER_ID, DEFAULT_PRODUCT), new OrderConfirmedEvent(ORDER_ID)) + .when(new ShipOrderCommand(ORDER_ID)) + .expectEvents(new OrderShippedEvent(ORDER_ID)); + } + +} \ No newline at end of file