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:
parent
d5ad67a801
commit
03306112bd
@ -34,7 +34,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 ben confirmed yet.");
|
throw new IllegalStateException("Cannot ship an order which has not been confirmed yet.");
|
||||||
}
|
}
|
||||||
|
|
||||||
apply(new OrderShippedEvent(orderId));
|
apply(new OrderShippedEvent(orderId));
|
||||||
|
@ -33,4 +33,11 @@ public class ConfirmOrderCommand {
|
|||||||
final ConfirmOrderCommand other = (ConfirmOrderCommand) obj;
|
final ConfirmOrderCommand other = (ConfirmOrderCommand) obj;
|
||||||
return Objects.equals(this.orderId, other.orderId);
|
return Objects.equals(this.orderId, other.orderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ConfirmOrderCommand{" +
|
||||||
|
"orderId='" + orderId + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,4 +40,12 @@ public class PlaceOrderCommand {
|
|||||||
return Objects.equals(this.orderId, other.orderId)
|
return Objects.equals(this.orderId, other.orderId)
|
||||||
&& Objects.equals(this.product, other.product);
|
&& Objects.equals(this.product, other.product);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "PlaceOrderCommand{" +
|
||||||
|
"orderId='" + orderId + '\'' +
|
||||||
|
", product='" + product + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
@ -33,4 +33,11 @@ public class ShipOrderCommand {
|
|||||||
final ShipOrderCommand other = (ShipOrderCommand) obj;
|
final ShipOrderCommand other = (ShipOrderCommand) obj;
|
||||||
return Objects.equals(this.orderId, other.orderId);
|
return Objects.equals(this.orderId, other.orderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ShipOrderCommand{" +
|
||||||
|
"orderId='" + orderId + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
@ -30,4 +30,11 @@ public class OrderConfirmedEvent {
|
|||||||
final OrderConfirmedEvent other = (OrderConfirmedEvent) obj;
|
final OrderConfirmedEvent other = (OrderConfirmedEvent) obj;
|
||||||
return Objects.equals(this.orderId, other.orderId);
|
return Objects.equals(this.orderId, other.orderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "OrderConfirmedEvent{" +
|
||||||
|
"orderId='" + orderId + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,4 +37,12 @@ public class OrderPlacedEvent {
|
|||||||
return Objects.equals(this.orderId, other.orderId)
|
return Objects.equals(this.orderId, other.orderId)
|
||||||
&& Objects.equals(this.product, other.product);
|
&& Objects.equals(this.product, other.product);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "OrderPlacedEvent{" +
|
||||||
|
"orderId='" + orderId + '\'' +
|
||||||
|
", product='" + product + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
@ -30,4 +30,11 @@ public class OrderShippedEvent {
|
|||||||
final OrderShippedEvent other = (OrderShippedEvent) obj;
|
final OrderShippedEvent other = (OrderShippedEvent) obj;
|
||||||
return Objects.equals(this.orderId, other.orderId);
|
return Objects.equals(this.orderId, other.orderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "OrderShippedEvent{" +
|
||||||
|
"orderId='" + orderId + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
@ -52,4 +52,13 @@ public class OrderedProduct {
|
|||||||
&& Objects.equals(this.product, other.product)
|
&& Objects.equals(this.product, other.product)
|
||||||
&& Objects.equals(this.orderStatus, other.orderStatus);
|
&& Objects.equals(this.orderStatus, other.orderStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "OrderedProduct{" +
|
||||||
|
"orderId='" + orderId + '\'' +
|
||||||
|
", product='" + product + '\'' +
|
||||||
|
", orderStatus=" + orderStatus +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,6 @@ import com.baeldung.axon.coreapi.queries.OrderedProduct;
|
|||||||
@RestController
|
@RestController
|
||||||
public class OrderRestEndpoint {
|
public class OrderRestEndpoint {
|
||||||
|
|
||||||
private static final String DEFAULT_PRODUCT = "Deluxe Chair";
|
|
||||||
|
|
||||||
private final CommandGateway commandGateway;
|
private final CommandGateway commandGateway;
|
||||||
private final QueryGateway queryGateway;
|
private final QueryGateway queryGateway;
|
||||||
|
|
||||||
@ -32,7 +30,7 @@ public class OrderRestEndpoint {
|
|||||||
@PostMapping("/ship-order")
|
@PostMapping("/ship-order")
|
||||||
public void shipOrder() {
|
public void shipOrder() {
|
||||||
String orderId = UUID.randomUUID().toString();
|
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 ConfirmOrderCommand(orderId));
|
||||||
commandGateway.send(new ShipOrderCommand(orderId));
|
commandGateway.send(new ShipOrderCommand(orderId));
|
||||||
}
|
}
|
||||||
@ -40,7 +38,7 @@ public class OrderRestEndpoint {
|
|||||||
@PostMapping("/ship-unconfirmed-order")
|
@PostMapping("/ship-unconfirmed-order")
|
||||||
public void shipUnconfirmedOrder() {
|
public void shipUnconfirmedOrder() {
|
||||||
String orderId = UUID.randomUUID().toString();
|
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.
|
// This throws an exception, as an Order cannot be shipped if it has not been confirmed yet.
|
||||||
commandGateway.send(new ShipOrderCommand(orderId));
|
commandGateway.send(new ShipOrderCommand(orderId));
|
||||||
}
|
}
|
||||||
|
@ -24,15 +24,6 @@ public class OrderedProductsEventHandler {
|
|||||||
public void on(OrderPlacedEvent event) {
|
public void on(OrderPlacedEvent event) {
|
||||||
String orderId = event.getOrderId();
|
String orderId = event.getOrderId();
|
||||||
orderedProducts.put(orderId, new OrderedProduct(orderId, event.getProduct()));
|
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
|
@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
|
@QueryHandler
|
||||||
public List<OrderedProduct> handle(FindAllOrderedProductsQuery query) {
|
public List<OrderedProduct> handle(FindAllOrderedProductsQuery query) {
|
||||||
return new ArrayList<>(orderedProducts.values());
|
return new ArrayList<>(orderedProducts.values());
|
||||||
|
@ -6,6 +6,7 @@ import org.axonframework.test.aggregate.AggregateTestFixture;
|
|||||||
import org.axonframework.test.aggregate.FixtureConfiguration;
|
import org.axonframework.test.aggregate.FixtureConfiguration;
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
|
|
||||||
|
import com.baeldung.axon.coreapi.commands.ConfirmOrderCommand;
|
||||||
import com.baeldung.axon.coreapi.commands.PlaceOrderCommand;
|
import com.baeldung.axon.coreapi.commands.PlaceOrderCommand;
|
||||||
import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
|
import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
|
||||||
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
|
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
|
||||||
@ -14,9 +15,6 @@ import com.baeldung.axon.coreapi.events.OrderShippedEvent;
|
|||||||
|
|
||||||
public class OrderAggregateUnitTest {
|
public class OrderAggregateUnitTest {
|
||||||
|
|
||||||
private static final String ORDER_ID = UUID.randomUUID().toString();
|
|
||||||
private static final String DEFAULT_PRODUCT = "Deluxe Chair";
|
|
||||||
|
|
||||||
private FixtureConfiguration<OrderAggregate> fixture;
|
private FixtureConfiguration<OrderAggregate> fixture;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@ -26,23 +24,38 @@ public class OrderAggregateUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void giveNoPriorActivity_whenPlaceOrderCommand_thenShouldPublishOrderPlacedEvent() {
|
public void giveNoPriorActivity_whenPlaceOrderCommand_thenShouldPublishOrderPlacedEvent() {
|
||||||
|
String orderId = UUID.randomUUID().toString();
|
||||||
|
String product = "Deluxe Chair";
|
||||||
fixture.givenNoPriorActivity()
|
fixture.givenNoPriorActivity()
|
||||||
.when(new PlaceOrderCommand(ORDER_ID, DEFAULT_PRODUCT))
|
.when(new PlaceOrderCommand(orderId, product))
|
||||||
.expectEvents(new OrderPlacedEvent(ORDER_ID, DEFAULT_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
|
@Test
|
||||||
public void givenOrderPlacedEvent_whenShipOrderCommand_thenShouldThrowIllegalStateException() {
|
public void givenOrderPlacedEvent_whenShipOrderCommand_thenShouldThrowIllegalStateException() {
|
||||||
fixture.given(new OrderPlacedEvent(ORDER_ID, DEFAULT_PRODUCT))
|
String orderId = UUID.randomUUID().toString();
|
||||||
.when(new ShipOrderCommand(ORDER_ID))
|
String product = "Deluxe Chair";
|
||||||
|
fixture.given(new OrderPlacedEvent(orderId, product))
|
||||||
|
.when(new ShipOrderCommand(orderId))
|
||||||
.expectException(IllegalStateException.class);
|
.expectException(IllegalStateException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOrderPlacedEventAndOrderConfirmedEvent_whenShipOrderCommand_thenShouldPublishOrderShippedEvent() {
|
public void givenOrderPlacedEventAndOrderConfirmedEvent_whenShipOrderCommand_thenShouldPublishOrderShippedEvent() {
|
||||||
fixture.given(new OrderPlacedEvent(ORDER_ID, DEFAULT_PRODUCT), new OrderConfirmedEvent(ORDER_ID))
|
String orderId = UUID.randomUUID().toString();
|
||||||
.when(new ShipOrderCommand(ORDER_ID))
|
String product = "Deluxe Chair";
|
||||||
.expectEvents(new OrderShippedEvent(ORDER_ID));
|
fixture.given(new OrderPlacedEvent(orderId, product), new OrderConfirmedEvent(orderId))
|
||||||
|
.when(new ShipOrderCommand(orderId))
|
||||||
|
.expectEvents(new OrderShippedEvent(orderId));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user