BAEL-2435 Minor clean ups

-Add toString() function to messages/query-model
-Fix typo in exception
-Reorder event handlers in OrderedProductsEventHandler
-Replace usage of constants for local fields
-Add missing Aggregate test case
This commit is contained in:
Steven van Beelen 2018-12-28 15:58:41 +01:00
parent d5ad67a801
commit 03306112bd
11 changed files with 88 additions and 25 deletions

View File

@ -34,7 +34,7 @@ public class OrderAggregate {
@CommandHandler
public void handle(ShipOrderCommand command) {
if (!orderConfirmed) {
throw new IllegalStateException("Cannot ship an order which has not ben confirmed yet.");
throw new IllegalStateException("Cannot ship an order which has not been confirmed yet.");
}
apply(new OrderShippedEvent(orderId));

View File

@ -33,4 +33,11 @@ public class ConfirmOrderCommand {
final ConfirmOrderCommand other = (ConfirmOrderCommand) obj;
return Objects.equals(this.orderId, other.orderId);
}
@Override
public String toString() {
return "ConfirmOrderCommand{" +
"orderId='" + orderId + '\'' +
'}';
}
}

View File

@ -40,4 +40,12 @@ public class PlaceOrderCommand {
return Objects.equals(this.orderId, other.orderId)
&& Objects.equals(this.product, other.product);
}
@Override
public String toString() {
return "PlaceOrderCommand{" +
"orderId='" + orderId + '\'' +
", product='" + product + '\'' +
'}';
}
}

View File

@ -33,4 +33,11 @@ public class ShipOrderCommand {
final ShipOrderCommand other = (ShipOrderCommand) obj;
return Objects.equals(this.orderId, other.orderId);
}
@Override
public String toString() {
return "ShipOrderCommand{" +
"orderId='" + orderId + '\'' +
'}';
}
}

View File

@ -30,4 +30,11 @@ public class OrderConfirmedEvent {
final OrderConfirmedEvent other = (OrderConfirmedEvent) obj;
return Objects.equals(this.orderId, other.orderId);
}
@Override
public String toString() {
return "OrderConfirmedEvent{" +
"orderId='" + orderId + '\'' +
'}';
}
}

View File

@ -37,4 +37,12 @@ public class OrderPlacedEvent {
return Objects.equals(this.orderId, other.orderId)
&& Objects.equals(this.product, other.product);
}
@Override
public String toString() {
return "OrderPlacedEvent{" +
"orderId='" + orderId + '\'' +
", product='" + product + '\'' +
'}';
}
}

View File

@ -30,4 +30,11 @@ public class OrderShippedEvent {
final OrderShippedEvent other = (OrderShippedEvent) obj;
return Objects.equals(this.orderId, other.orderId);
}
@Override
public String toString() {
return "OrderShippedEvent{" +
"orderId='" + orderId + '\'' +
'}';
}
}

View File

@ -52,4 +52,13 @@ public class OrderedProduct {
&& Objects.equals(this.product, other.product)
&& Objects.equals(this.orderStatus, other.orderStatus);
}
@Override
public String toString() {
return "OrderedProduct{" +
"orderId='" + orderId + '\'' +
", product='" + product + '\'' +
", orderStatus=" + orderStatus +
'}';
}
}

View File

@ -19,8 +19,6 @@ import com.baeldung.axon.coreapi.queries.OrderedProduct;
@RestController
public class OrderRestEndpoint {
private static final String DEFAULT_PRODUCT = "Deluxe Chair";
private final CommandGateway commandGateway;
private final QueryGateway queryGateway;
@ -32,7 +30,7 @@ public class OrderRestEndpoint {
@PostMapping("/ship-order")
public void shipOrder() {
String orderId = UUID.randomUUID().toString();
commandGateway.send(new PlaceOrderCommand(orderId, DEFAULT_PRODUCT));
commandGateway.send(new PlaceOrderCommand(orderId, "Deluxe Chair"));
commandGateway.send(new ConfirmOrderCommand(orderId));
commandGateway.send(new ShipOrderCommand(orderId));
}
@ -40,7 +38,7 @@ public class OrderRestEndpoint {
@PostMapping("/ship-unconfirmed-order")
public void shipUnconfirmedOrder() {
String orderId = UUID.randomUUID().toString();
commandGateway.send(new PlaceOrderCommand(orderId, DEFAULT_PRODUCT));
commandGateway.send(new PlaceOrderCommand(orderId, "Deluxe Chair"));
// This throws an exception, as an Order cannot be shipped if it has not been confirmed yet.
commandGateway.send(new ShipOrderCommand(orderId));
}
@ -48,7 +46,7 @@ public class OrderRestEndpoint {
@GetMapping("/all-orders")
public List<OrderedProduct> findAllOrderedProducts() {
return queryGateway.query(new FindAllOrderedProductsQuery(), ResponseTypes.multipleInstancesOf(OrderedProduct.class))
.join();
.join();
}
}

View File

@ -24,15 +24,6 @@ public class OrderedProductsEventHandler {
public void on(OrderPlacedEvent event) {
String orderId = event.getOrderId();
orderedProducts.put(orderId, new OrderedProduct(orderId, event.getProduct()));
}
@EventHandler
public void on(OrderShippedEvent event) {
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
orderedProduct.setOrderShipped();
return orderedProduct;
});
}
@EventHandler
@ -43,6 +34,14 @@ public class OrderedProductsEventHandler {
});
}
@EventHandler
public void on(OrderShippedEvent event) {
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
orderedProduct.setOrderShipped();
return orderedProduct;
});
}
@QueryHandler
public List<OrderedProduct> handle(FindAllOrderedProductsQuery query) {
return new ArrayList<>(orderedProducts.values());

View File

@ -6,6 +6,7 @@ import org.axonframework.test.aggregate.AggregateTestFixture;
import org.axonframework.test.aggregate.FixtureConfiguration;
import org.junit.*;
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;
@ -14,9 +15,6 @@ 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
@ -26,23 +24,38 @@ public class OrderAggregateUnitTest {
@Test
public void giveNoPriorActivity_whenPlaceOrderCommand_thenShouldPublishOrderPlacedEvent() {
String orderId = UUID.randomUUID().toString();
String product = "Deluxe Chair";
fixture.givenNoPriorActivity()
.when(new PlaceOrderCommand(ORDER_ID, DEFAULT_PRODUCT))
.expectEvents(new OrderPlacedEvent(ORDER_ID, DEFAULT_PRODUCT));
.when(new PlaceOrderCommand(orderId, product))
.expectEvents(new OrderPlacedEvent(orderId, product));
}
@Test
public void givenOrderPlacedEvent_whenConfirmOrderCommand_thenShouldPublishOrderConfirmedEvent() {
String orderId = UUID.randomUUID().toString();
String product = "Deluxe Chair";
fixture.given(new OrderPlacedEvent(orderId, product))
.when(new ConfirmOrderCommand(orderId))
.expectEvents(new OrderConfirmedEvent(orderId));
}
@Test
public void givenOrderPlacedEvent_whenShipOrderCommand_thenShouldThrowIllegalStateException() {
fixture.given(new OrderPlacedEvent(ORDER_ID, DEFAULT_PRODUCT))
.when(new ShipOrderCommand(ORDER_ID))
String orderId = UUID.randomUUID().toString();
String product = "Deluxe Chair";
fixture.given(new OrderPlacedEvent(orderId, product))
.when(new ShipOrderCommand(orderId))
.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));
String orderId = UUID.randomUUID().toString();
String product = "Deluxe Chair";
fixture.given(new OrderPlacedEvent(orderId, product), new OrderConfirmedEvent(orderId))
.when(new ShipOrderCommand(orderId))
.expectEvents(new OrderShippedEvent(orderId));
}
}