Skip to content

Commit

Permalink
Merge pull request #364 from commercetools/order-update-test
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Nov 9, 2022
2 parents 7bcd2f2 + 7ef0536 commit e8e64f3
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 12 deletions.
Expand Up @@ -13,21 +13,27 @@
import com.commercetools.api.models.customer.Customer;
import commercetools.utils.CommercetoolsTestUtils;

import io.vrap.rmf.base.client.error.NotFoundException;

import org.junit.jupiter.api.Assertions;

public class CartsFixtures {
public static Cart deleteCart(final String id, final Long version) {
Cart cart = CommercetoolsTestUtils.getProjectApiRoot()
.carts()
.withId(id)
.delete()
.withVersion(version)
.executeBlocking()
.getBody();

Assertions.assertNotNull(cart);
Assertions.assertEquals(cart.getId(), id);

Cart cart = null;
try {
cart = CommercetoolsTestUtils.getProjectApiRoot()
.carts()
.withId(id)
.delete()
.withVersion(version)
.executeBlocking()
.getBody();

Assertions.assertNotNull(cart);
Assertions.assertEquals(cart.getId(), id);
}
catch (NotFoundException ignored) {
}
return cart;
}

Expand Down
@@ -0,0 +1,36 @@

package commercetools.order;

import com.commercetools.api.models.order.Order;
import com.commercetools.api.models.order.OrderState;
import com.commercetools.api.models.order.OrderUpdate;
import commercetools.utils.CommercetoolsTestUtils;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class OrderIntegrationTests {

@Test
public void testOrderUpdate() {
OrdersFixtures.withUpdateableOrder(order -> {
Assertions.assertThat(order).isNotNull();

Assertions.assertThat(order.getBillingAddress()).isNull();
Order updatedOrder = CommercetoolsTestUtils.getProjectApiRoot()
.orders()
.withId(order.getId())
.post(OrderUpdate.builder()
.version(order.getVersion())
.plusActions(builder -> builder.setBillingAddressBuilder()
.address(address -> address.country("DE").state("Berlin")))
.plusActions(builder -> builder.changeOrderStateBuilder().orderState(OrderState.CONFIRMED))
.build())
.executeBlocking()
.getBody();

Assertions.assertThat(updatedOrder.getBillingAddress().getCountry()).isEqualTo("DE");
return updatedOrder;
});
}
}
@@ -1,8 +1,15 @@

package commercetools.order;

import java.util.function.Consumer;
import java.util.function.UnaryOperator;

import com.commercetools.api.models.cart.*;
import com.commercetools.api.models.order.Order;
import com.commercetools.api.models.order.OrderFromCartDraft;
import com.commercetools.api.models.order_edit.OrderEdit;
import commercetools.cart.CartsFixtures;
import commercetools.product.ProductFixtures;
import commercetools.utils.CommercetoolsTestUtils;

import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -38,4 +45,77 @@ public static OrderEdit deleteOrderEdit(final String id, final Long version) {

return orderEdit;
}

private static void withCreateOrder(final Consumer<Order> execFn) {
ProductFixtures.withPublishedProduct(product -> {
CartsFixtures.withUpdateableCart(cart -> {
Cart updatedCart = CommercetoolsTestUtils.getProjectApiRoot()
.carts()
.withId(cart.getId())
.post(CartUpdate.builder()
.version(cart.getVersion())
.plusActions(builder -> builder.addLineItemBuilder()
.sku(product.getMasterData().getCurrent().getMasterVariant().getSku()))
.plusActions(builder -> builder.setShippingAddressBuilder()
.address(
baseAddressBuilder -> baseAddressBuilder.country("DE").state("Berlin")))
.build())
.executeBlocking()
.getBody();

OrderFromCartDraft orderDraft = OrderFromCartDraft.builder()
.cart((CartResourceIdentifier) updatedCart.toResourceIdentifier())
.version(updatedCart.getVersion())
.build();

Order order = createOrder(orderDraft);
execFn.accept(order);

try {
return CommercetoolsTestUtils.getProjectApiRoot()
.carts()
.withId(updatedCart.getId())
.get()
.executeBlocking()
.getBody();
}
catch (Exception ignored) {
}

return updatedCart;
});

});
}

public static void withOrder(final Consumer<Order> consumer) {
withCreateOrder(order -> {
try {
consumer.accept(order);
}
finally {
deleteOrder(order.getId(), order.getVersion());
}
});
}

public static void withUpdateableOrder(final UnaryOperator<Order> operator) {
withCreateOrder(order -> {
try {
order = operator.apply(order);
}
finally {
deleteOrder(order.getId(), order.getVersion());
}
});
}

public static Order createOrder(final OrderFromCartDraft orderDraft) {
Order order = CommercetoolsTestUtils.getProjectApiRoot().orders().post(orderDraft).executeBlocking().getBody();

Assertions.assertNotNull(order);
Assertions.assertEquals(order.getCart().getId(), orderDraft.getCart().getId());

return order;
}
}
Expand Up @@ -42,6 +42,19 @@ public static void withProduct(final Consumer<Product> consumer) {
})));
}

public static void withPublishedProduct(final Consumer<Product> consumer) {
withTaxCategory(
taxCategory -> withCategory(category -> withProductType(createProductTypeDraft(), productType -> {
Product product = createProduct(productType, category, taxCategory, true);
try {
consumer.accept(product);
}
finally {
deleteProductById(product.getId(), product.getVersion());
}
})));
}

public static void withUpdateableProduct(final UnaryOperator<Product> operator) {
withTaxCategory(
taxCategory -> withCategory(category -> withProductType(createProductTypeDraft(), productType -> {
Expand Down
Expand Up @@ -71,7 +71,7 @@ public CompletableFuture<ApiHttpResponse<byte[]>> invoke(ApiHttpRequest request,
if (throwable instanceof ConcurrentModificationException) {
final Long newVersion = ((ConcurrentModificationException) throwable).getCurrentVersion();
final JsonNode jsonNode;
if (newVersion != null) {
if (newVersion != null && request.getMethod() == ApiHttpMethod.POST) {
try {
jsonNode = mapper.readTree(request.getBody());
if (jsonNode instanceof ObjectNode && jsonNode.has(VERSION)) {
Expand Down

0 comments on commit e8e64f3

Please sign in to comment.