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
This commit is contained in:
parent
2279246a42
commit
bd0a1efe70
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
|
@ -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<MessagesAggregate> 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));
|
||||
}
|
||||
}
|
|
@ -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<OrderAggregate> 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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue