Merge pull request #7386 from smcvb/BAEL-3099

[BAEL-3099] Second update to Axon article
This commit is contained in:
Loredana Crusoveanu 2019-07-22 22:19:18 +03:00 committed by GitHub
commit 79a50387d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 12 deletions

View File

@ -18,12 +18,6 @@
<groupId>org.axonframework</groupId> <groupId>org.axonframework</groupId>
<artifactId>axon-spring-boot-starter</artifactId> <artifactId>axon-spring-boot-starter</artifactId>
<version>${axon.version}</version> <version>${axon.version}</version>
<exclusions>
<exclusion>
<groupId>org.axonframework</groupId>
<artifactId>axon-server-connector</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
@ -58,7 +52,7 @@
</dependencies> </dependencies>
<properties> <properties>
<axon.version>4.0.3</axon.version> <axon.version>4.1.2</axon.version>
</properties> </properties>
</project> </project>

View File

@ -13,6 +13,7 @@ import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent; import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
import com.baeldung.axon.coreapi.events.OrderPlacedEvent; import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
import com.baeldung.axon.coreapi.events.OrderShippedEvent; import com.baeldung.axon.coreapi.events.OrderShippedEvent;
import com.baeldung.axon.coreapi.exceptions.UnconfirmedOrderException;
@Aggregate @Aggregate
public class OrderAggregate { public class OrderAggregate {
@ -34,7 +35,7 @@ public class OrderAggregate {
@CommandHandler @CommandHandler
public void handle(ShipOrderCommand command) { public void handle(ShipOrderCommand command) {
if (!orderConfirmed) { if (!orderConfirmed) {
throw new IllegalStateException("Cannot ship an order which has not been confirmed yet."); throw new UnconfirmedOrderException();
} }
apply(new OrderShippedEvent(orderId)); apply(new OrderShippedEvent(orderId));
@ -43,12 +44,12 @@ public class OrderAggregate {
@EventSourcingHandler @EventSourcingHandler
public void on(OrderPlacedEvent event) { public void on(OrderPlacedEvent event) {
this.orderId = event.getOrderId(); this.orderId = event.getOrderId();
orderConfirmed = false; this.orderConfirmed = false;
} }
@EventSourcingHandler @EventSourcingHandler
public void on(OrderConfirmedEvent event) { public void on(OrderConfirmedEvent event) {
orderConfirmed = true; this.orderConfirmed = true;
} }
protected OrderAggregate() { protected OrderAggregate() {

View File

@ -0,0 +1,8 @@
package com.baeldung.axon.coreapi.exceptions;
public class UnconfirmedOrderException extends IllegalStateException {
public UnconfirmedOrderException() {
super("Cannot ship an order which has not been confirmed yet.");
}
}

View File

@ -5,6 +5,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.axonframework.config.ProcessingGroup;
import org.axonframework.eventhandling.EventHandler; import org.axonframework.eventhandling.EventHandler;
import org.axonframework.queryhandling.QueryHandler; import org.axonframework.queryhandling.QueryHandler;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -16,6 +17,7 @@ import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
import com.baeldung.axon.coreapi.queries.OrderedProduct; import com.baeldung.axon.coreapi.queries.OrderedProduct;
@Service @Service
@ProcessingGroup("ordered-products")
public class OrderedProductsEventHandler { public class OrderedProductsEventHandler {
private final Map<String, OrderedProduct> orderedProducts = new HashMap<>(); private final Map<String, OrderedProduct> orderedProducts = new HashMap<>();

View File

@ -0,0 +1 @@
spring.application.name=Order Management Service

View File

@ -2,6 +2,7 @@ package com.baeldung.axon.commandmodel;
import java.util.UUID; import java.util.UUID;
import com.baeldung.axon.coreapi.exceptions.UnconfirmedOrderException;
import org.axonframework.test.aggregate.AggregateTestFixture; import org.axonframework.test.aggregate.AggregateTestFixture;
import org.axonframework.test.aggregate.FixtureConfiguration; import org.axonframework.test.aggregate.FixtureConfiguration;
import org.junit.*; import org.junit.*;
@ -41,12 +42,12 @@ public class OrderAggregateUnitTest {
} }
@Test @Test
public void givenOrderPlacedEvent_whenShipOrderCommand_thenShouldThrowIllegalStateException() { public void givenOrderPlacedEvent_whenShipOrderCommand_thenShouldThrowUnconfirmedOrderException() {
String orderId = UUID.randomUUID().toString(); String orderId = UUID.randomUUID().toString();
String product = "Deluxe Chair"; String product = "Deluxe Chair";
fixture.given(new OrderPlacedEvent(orderId, product)) fixture.given(new OrderPlacedEvent(orderId, product))
.when(new ShipOrderCommand(orderId)) .when(new ShipOrderCommand(orderId))
.expectException(IllegalStateException.class); .expectException(UnconfirmedOrderException.class);
} }
@Test @Test