Skip to content

Commit bd0a1ef

Browse files
committed
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
1 parent 2279246 commit bd0a1ef

4 files changed

Lines changed: 106 additions & 78 deletions

File tree

axon/src/main/java/com/baeldung/axon/commandmodel/MessagesAggregate.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.axon.commandmodel;
2+
3+
import static org.axonframework.modelling.command.AggregateLifecycle.apply;
4+
5+
import org.axonframework.commandhandling.CommandHandler;
6+
import org.axonframework.eventsourcing.EventSourcingHandler;
7+
import org.axonframework.modelling.command.AggregateIdentifier;
8+
import org.axonframework.spring.stereotype.Aggregate;
9+
10+
import com.baeldung.axon.coreapi.commands.ConfirmOrderCommand;
11+
import com.baeldung.axon.coreapi.commands.PlaceOrderCommand;
12+
import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
13+
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
14+
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
15+
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
16+
17+
@Aggregate
18+
public class OrderAggregate {
19+
20+
@AggregateIdentifier
21+
private String orderId;
22+
private boolean orderConfirmed;
23+
24+
@CommandHandler
25+
public OrderAggregate(PlaceOrderCommand command) {
26+
apply(new OrderPlacedEvent(command.getOrderId(), command.getProduct()));
27+
}
28+
29+
@CommandHandler
30+
public void handle(ConfirmOrderCommand command) {
31+
apply(new OrderConfirmedEvent(orderId));
32+
}
33+
34+
@CommandHandler
35+
public void handle(ShipOrderCommand command) {
36+
if (!orderConfirmed) {
37+
throw new IllegalStateException("Cannot ship an order which has not ben confirmed yet.");
38+
}
39+
40+
apply(new OrderShippedEvent(orderId));
41+
}
42+
43+
@EventSourcingHandler
44+
public void on(OrderPlacedEvent event) {
45+
this.orderId = event.getOrderId();
46+
orderConfirmed = false;
47+
}
48+
49+
@EventSourcingHandler
50+
public void on(OrderConfirmedEvent event) {
51+
orderConfirmed = true;
52+
}
53+
54+
protected OrderAggregate() {
55+
// Required by Axon to build a default Aggregate prior to Event Sourcing
56+
}
57+
58+
}

axon/src/test/java/com/baeldung/axon/commandmodel/MessagesAggregateUnitTest.java

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.axon.commandmodel;
2+
3+
import java.util.UUID;
4+
5+
import org.axonframework.test.aggregate.AggregateTestFixture;
6+
import org.axonframework.test.aggregate.FixtureConfiguration;
7+
import org.junit.*;
8+
9+
import com.baeldung.axon.coreapi.commands.PlaceOrderCommand;
10+
import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
11+
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
12+
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
13+
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
14+
15+
public class OrderAggregateUnitTest {
16+
17+
private static final String ORDER_ID = UUID.randomUUID().toString();
18+
private static final String DEFAULT_PRODUCT = "Deluxe Chair";
19+
20+
private FixtureConfiguration<OrderAggregate> fixture;
21+
22+
@Before
23+
public void setUp() {
24+
fixture = new AggregateTestFixture<>(OrderAggregate.class);
25+
}
26+
27+
@Test
28+
public void giveNoPriorActivity_whenPlaceOrderCommand_thenShouldPublishOrderPlacedEvent() {
29+
fixture.givenNoPriorActivity()
30+
.when(new PlaceOrderCommand(ORDER_ID, DEFAULT_PRODUCT))
31+
.expectEvents(new OrderPlacedEvent(ORDER_ID, DEFAULT_PRODUCT));
32+
}
33+
34+
@Test
35+
public void givenOrderPlacedEvent_whenShipOrderCommand_thenShouldThrowIllegalStateException() {
36+
fixture.given(new OrderPlacedEvent(ORDER_ID, DEFAULT_PRODUCT))
37+
.when(new ShipOrderCommand(ORDER_ID))
38+
.expectException(IllegalStateException.class);
39+
}
40+
41+
@Test
42+
public void givenOrderPlacedEventAndOrderConfirmedEvent_whenShipOrderCommand_thenShouldPublishOrderShippedEvent() {
43+
fixture.given(new OrderPlacedEvent(ORDER_ID, DEFAULT_PRODUCT), new OrderConfirmedEvent(ORDER_ID))
44+
.when(new ShipOrderCommand(ORDER_ID))
45+
.expectEvents(new OrderShippedEvent(ORDER_ID));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)