Skip to content

Commit 0330611

Browse files
committed
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
1 parent d5ad67a commit 0330611

11 files changed

Lines changed: 84 additions & 21 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void handle(ConfirmOrderCommand command) {
3434
@CommandHandler
3535
public void handle(ShipOrderCommand command) {
3636
if (!orderConfirmed) {
37-
throw new IllegalStateException("Cannot ship an order which has not ben confirmed yet.");
37+
throw new IllegalStateException("Cannot ship an order which has not been confirmed yet.");
3838
}
3939

4040
apply(new OrderShippedEvent(orderId));

axon/src/main/java/com/baeldung/axon/coreapi/commands/ConfirmOrderCommand.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@ public boolean equals(Object obj) {
3333
final ConfirmOrderCommand other = (ConfirmOrderCommand) obj;
3434
return Objects.equals(this.orderId, other.orderId);
3535
}
36+
37+
@Override
38+
public String toString() {
39+
return "ConfirmOrderCommand{" +
40+
"orderId='" + orderId + '\'' +
41+
'}';
42+
}
3643
}

axon/src/main/java/com/baeldung/axon/coreapi/commands/PlaceOrderCommand.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@ public boolean equals(Object obj) {
4040
return Objects.equals(this.orderId, other.orderId)
4141
&& Objects.equals(this.product, other.product);
4242
}
43+
44+
@Override
45+
public String toString() {
46+
return "PlaceOrderCommand{" +
47+
"orderId='" + orderId + '\'' +
48+
", product='" + product + '\'' +
49+
'}';
50+
}
4351
}

axon/src/main/java/com/baeldung/axon/coreapi/commands/ShipOrderCommand.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@ public boolean equals(Object obj) {
3333
final ShipOrderCommand other = (ShipOrderCommand) obj;
3434
return Objects.equals(this.orderId, other.orderId);
3535
}
36+
37+
@Override
38+
public String toString() {
39+
return "ShipOrderCommand{" +
40+
"orderId='" + orderId + '\'' +
41+
'}';
42+
}
3643
}

axon/src/main/java/com/baeldung/axon/coreapi/events/OrderConfirmedEvent.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ public boolean equals(Object obj) {
3030
final OrderConfirmedEvent other = (OrderConfirmedEvent) obj;
3131
return Objects.equals(this.orderId, other.orderId);
3232
}
33+
34+
@Override
35+
public String toString() {
36+
return "OrderConfirmedEvent{" +
37+
"orderId='" + orderId + '\'' +
38+
'}';
39+
}
3340
}

axon/src/main/java/com/baeldung/axon/coreapi/events/OrderPlacedEvent.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ public boolean equals(Object obj) {
3737
return Objects.equals(this.orderId, other.orderId)
3838
&& Objects.equals(this.product, other.product);
3939
}
40+
41+
@Override
42+
public String toString() {
43+
return "OrderPlacedEvent{" +
44+
"orderId='" + orderId + '\'' +
45+
", product='" + product + '\'' +
46+
'}';
47+
}
4048
}

axon/src/main/java/com/baeldung/axon/coreapi/events/OrderShippedEvent.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ public boolean equals(Object obj) {
3030
final OrderShippedEvent other = (OrderShippedEvent) obj;
3131
return Objects.equals(this.orderId, other.orderId);
3232
}
33+
34+
@Override
35+
public String toString() {
36+
return "OrderShippedEvent{" +
37+
"orderId='" + orderId + '\'' +
38+
'}';
39+
}
3340
}

axon/src/main/java/com/baeldung/axon/coreapi/queries/OrderedProduct.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,13 @@ public boolean equals(Object obj) {
5252
&& Objects.equals(this.product, other.product)
5353
&& Objects.equals(this.orderStatus, other.orderStatus);
5454
}
55+
56+
@Override
57+
public String toString() {
58+
return "OrderedProduct{" +
59+
"orderId='" + orderId + '\'' +
60+
", product='" + product + '\'' +
61+
", orderStatus=" + orderStatus +
62+
'}';
63+
}
5564
}

axon/src/main/java/com/baeldung/axon/gui/OrderRestEndpoint.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
@RestController
2020
public class OrderRestEndpoint {
2121

22-
private static final String DEFAULT_PRODUCT = "Deluxe Chair";
23-
2422
private final CommandGateway commandGateway;
2523
private final QueryGateway queryGateway;
2624

@@ -32,23 +30,23 @@ public OrderRestEndpoint(CommandGateway commandGateway, QueryGateway queryGatewa
3230
@PostMapping("/ship-order")
3331
public void shipOrder() {
3432
String orderId = UUID.randomUUID().toString();
35-
commandGateway.send(new PlaceOrderCommand(orderId, DEFAULT_PRODUCT));
33+
commandGateway.send(new PlaceOrderCommand(orderId, "Deluxe Chair"));
3634
commandGateway.send(new ConfirmOrderCommand(orderId));
3735
commandGateway.send(new ShipOrderCommand(orderId));
3836
}
3937

4038
@PostMapping("/ship-unconfirmed-order")
4139
public void shipUnconfirmedOrder() {
4240
String orderId = UUID.randomUUID().toString();
43-
commandGateway.send(new PlaceOrderCommand(orderId, DEFAULT_PRODUCT));
41+
commandGateway.send(new PlaceOrderCommand(orderId, "Deluxe Chair"));
4442
// This throws an exception, as an Order cannot be shipped if it has not been confirmed yet.
4543
commandGateway.send(new ShipOrderCommand(orderId));
4644
}
4745

4846
@GetMapping("/all-orders")
4947
public List<OrderedProduct> findAllOrderedProducts() {
5048
return queryGateway.query(new FindAllOrderedProductsQuery(), ResponseTypes.multipleInstancesOf(OrderedProduct.class))
51-
.join();
49+
.join();
5250
}
5351

5452
}

axon/src/main/java/com/baeldung/axon/querymodel/OrderedProductsEventHandler.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ public class OrderedProductsEventHandler {
2424
public void on(OrderPlacedEvent event) {
2525
String orderId = event.getOrderId();
2626
orderedProducts.put(orderId, new OrderedProduct(orderId, event.getProduct()));
27-
2827
}
2928

3029
@EventHandler
31-
public void on(OrderShippedEvent event) {
30+
public void on(OrderConfirmedEvent event) {
3231
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
33-
orderedProduct.setOrderShipped();
32+
orderedProduct.setOrderConfirmed();
3433
return orderedProduct;
3534
});
3635
}
3736

3837
@EventHandler
39-
public void on(OrderConfirmedEvent event) {
38+
public void on(OrderShippedEvent event) {
4039
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
41-
orderedProduct.setOrderConfirmed();
40+
orderedProduct.setOrderShipped();
4241
return orderedProduct;
4342
});
4443
}

0 commit comments

Comments
 (0)