Skip to content

Commit

Permalink
#409 - Retain original order state in case of failing completion.
Browse files Browse the repository at this point in the history
We now properly reset the order's status in case of an exception being thrown from event listeners.
  • Loading branch information
odrotbohm committed Nov 13, 2022
1 parent 82d0f6b commit 6f5f9c7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/main/java/org/salespointframework/order/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,19 @@ Order complete() {
return this;
}

/**
* Resets the {@link Order}'s state to paid.
*
* @return the current instance.
* @since 8.0.1
*/
Order uncomplete() {

this.orderStatus = OrderStatus.PAID;

return this;
}

/**
* Cancels the current {@link Order} with the given reason. Will publish an {@link OrderCanceled} even
*
Expand Down Expand Up @@ -490,8 +503,8 @@ void verifyConstraints() {
}

/**
* Asserts that the {@link Order} is {@link OrderStatus#OPEN}. Usually a precondition to manipulate the
* {@link Order} state internally.
* Asserts that the {@link Order} is {@link OrderStatus#OPEN}. Usually a precondition to manipulate the {@link Order}
* state internally.
*/
private void assertOrderIsOpen() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,16 @@ public void completeOrder(final T order) {
throw new OrderCompletionFailure(order, "Order is not paid yet!");
}

save((T) order.complete());
try {

save((T) order.complete());

} catch (RuntimeException o_O) {

order.uncomplete();

throw o_O;
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void completesOrderIfAllLineItemsAreAvailableInSufficientQuantity() {
orders.completeOrder(order);
}

@Test // #38
@Test // #38, #409
void failsOrderCompletionIfLineItemsAreNotAvailableInSufficientQuantity() {

var cookie = catalog.save(new Cookie("Double choc", Money.of(1.2, Currencies.EURO)));
Expand All @@ -119,6 +119,7 @@ void failsOrderCompletionIfLineItemsAreNotAvailableInSufficientQuantity() {

assertThatExceptionOfType(OrderCompletionFailure.class) //
.isThrownBy(() -> orders.completeOrder(order));
assertThat(order.isCompleted()).isFalse();
}

@Test // #61
Expand Down

0 comments on commit 6f5f9c7

Please sign in to comment.