public static OrderFromCartCreateCommand of(final OrderFromCartDraft draft) { return new OrderFromCartCreateCommand(draft); }
private CompletionStage<Order> createOrder(final Cart cart) { final String orderNumber = generateOrderNumber(); final OrderFromCartDraft orderDraft = OrderFromCartDraft.of(cart, orderNumber, orderInitialPaymentState(cart)); return sphere().execute(OrderFromCartCreateCommand.of(orderDraft)) .thenApplyAsync(order -> { injector().getInstance(OrderInSession.class).store(order); injector().getInstance(CartInSession.class).remove(); return order; }, defaultContext()); } }
@Test public void orderFromCartDraft() { withStateByBuilder(client(), builder -> builder.type(StateType.ORDER_STATE), state -> { withFilledCart(client(), cart -> { final OrderFromCartDraft orderFromCartDraft = OrderFromCartDraftBuilder.of(cart.getId(), cart.getVersion()) .shipmentState(ShipmentState.SHIPPED) .state(state.toReference()) .orderState(OrderState.CANCELLED) .build(); final Order order = client().executeBlocking(OrderFromCartCreateCommand.of(cart).withDraft(orderFromCartDraft)); assertThat(order.getState().getId()).isEqualTo(state.getId()); assertThat(order.getOrderState()).isEqualTo(OrderState.CANCELLED); assertThat(order).isNotNull(); assertThat(order.getShipmentState()).isEqualTo(ShipmentState.SHIPPED); client().executeBlocking(OrderDeleteCommand.of(order)); }); }); } }
@Test public void order() { withDiscountCodeOfPredicate("totalPrice > \"800.00 EUR\"", (DiscountCode discountCode) -> withFilledCart(client(), cart -> { final Cart cartWithDiscountCode = client().executeBlocking(CartUpdateCommand.of(cart, AddDiscountCode.of(discountCode))); assertThat(cartWithDiscountCode.getTotalPrice()).isEqualTo(MoneyImpl.of("37.02", EUR)); assertThat(cartWithDiscountCode.getDiscountCodes()).hasSize(1); assertThat(cartWithDiscountCode.getDiscountCodes().get(0).getState()) .as("the discount (code) does not match the cart") .isEqualTo(DOES_NOT_MATCH_CART); final Throwable throwable = catchThrowable(() -> client().executeBlocking(OrderFromCartCreateCommand.of(cartWithDiscountCode))); assertThat(throwable).isInstanceOf(ErrorResponseException.class); final ErrorResponseException e = (ErrorResponseException) throwable; assertThat(e.hasErrorCode(DiscountCodeNonApplicableError.CODE)).isTrue(); })); }
public static void withOrderOfCustomLineItems(final BlockingSphereClient client, final Consumer<Order> f) { withTaxCategory(client, taxCategory -> { final Cart cart = createCartWithShippingAddress(client); final MonetaryAmount money = MoneyImpl.of("23.50", EUR); final String slug = "thing-slug"; final LocalizedString name = en("thing"); final long quantity = 5; final CustomLineItemDraft item = CustomLineItemDraft.of(name, slug, money, taxCategory, quantity, null); final Cart cartWith5 = client.executeBlocking(CartUpdateCommand.of(cart, AddCustomLineItem.of(item))); final Order order = client.executeBlocking(OrderFromCartCreateCommand.of(cartWith5)); f.accept(order); }); }
public static OrderFromCartCreateCommand of(final Versioned<Cart> cart) { return new OrderFromCartCreateCommand(OrderFromCartDraft.of(cart)); } }
@Test public void order() { withTaxedProduct(client(), product -> { final int itemsLeft = 3; final String sku = getSku(product); client().executeBlocking(InventoryEntryCreateCommand.of(InventoryEntryDraft.of(sku, itemsLeft))); final int itemCountToOrder = 5; final CartDraftDsl draft = CartDraft.of(DefaultCurrencyUnits.EUR) .withCountry(CountryCode.DE) .withInventoryMode(InventoryMode.RESERVE_ON_ORDER) .withShippingAddress(Address.of(CountryCode.DE)) .withLineItems(Collections.singletonList(LineItemDraft.of(product, 1, itemCountToOrder))); final Cart cart = client().executeBlocking(CartCreateCommand.of(draft)); final Throwable throwable = catchThrowable(() -> client().executeBlocking(OrderFromCartCreateCommand.of(cart))); assertThat(throwable).isInstanceOf(ErrorResponseException.class); final ErrorResponseException e = (ErrorResponseException) throwable; assertThat(e.hasErrorCode(OutOfStockError.CODE)); final OutOfStockError outOfStockError = e.getErrors().stream() .filter(err -> err.getCode().equals(OutOfStockError.CODE)) .findFirst().get().as(OutOfStockError.class); assertThat(outOfStockError.getSkus()).containsExactly(sku); assertThat(outOfStockError.getLineItems()).containsExactly(cart.getLineItems().get(0).getId()); }); }
@Test public void execution() throws Exception { withStateByBuilder(client(), builder -> builder.type(StateType.ORDER_STATE), state -> { withFilledCart(client(), cart -> { final Order order = client().executeBlocking(OrderFromCartCreateCommand.of(cart)); final Order updatedOrder = client().executeBlocking(OrderUpdateCommand.of(order, TransitionState.of(state)) .plusExpansionPaths(OrderExpansionModel::state) .plusExpansionPaths(OrderExpansionModel::cart) ); assertThat(updatedOrder.getLineItems()).isEqualTo(cart.getLineItems()); assertThat(updatedOrder.getCustomLineItems()).isEqualTo(cart.getCustomLineItems()); assertThat(updatedOrder.getCart().getId()).isEqualTo(cart.getId()); assertThat(updatedOrder.getCart()).is(expanded()); final Cart orderedCart = updatedOrder.getCart().getObj(); assertThat(orderedCart).isNotNull(); assertThat(orderedCart.getId()).isEqualTo(cart.getId()); assertThat(orderedCart.getCartState()).isEqualTo(CartState.ORDERED); assertThat(updatedOrder.getState()).is(expanded()); //to be able to delete the state transition we have to delete the associated order. client().executeBlocking(OrderDeleteCommand.of(updatedOrder)); }); }); }
final Throwable throwable = catchThrowable(() -> client().executeBlocking(OrderFromCartCreateCommand.of(cart))); assertThat(throwable).isInstanceOf(ErrorResponseException.class); final ErrorResponseException e = (ErrorResponseException) throwable;
@Test public void shouldChangeShippingMethodStateWithNonMatchingShippingMethod() throws Exception { withTaxedProduct(client(), product -> { withDynamicShippingMethodForGermany(client(), CartPredicate.of("customer.email=\"john@example.com\""), shippingMethod -> { final CartDraft cartDraft = CartDraftBuilder.of(DefaultCurrencyUnits.EUR) .country(CountryCode.DE).shippingAddress(GERMAN_ADDRESS) .customerEmail("john@example.com") .shippingMethod(shippingMethod) .lineItems(Arrays.asList(LineItemDraft.of(product.getId(), 1, 1))) .build(); withCartDraft(client(), cartDraft, cart -> { assertThat(cart.getShippingInfo().getShippingMethodState()).isEqualTo(ShippingMethodState.MATCHES_CART); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, SetCustomerEmail.of("jon@doe.com"))); final OrderFromCartCreateCommand orderCreateCommand = OrderFromCartCreateCommand.of(updatedCart); assertThatThrownBy(() -> client().executeBlocking(orderCreateCommand)) .isInstanceOf(ErrorResponseException.class) .hasMessageContaining("does not match"); final Cart cartWitUpdatedState = client().executeBlocking(CartByIdGet.of(updatedCart)); assertThat(cartWitUpdatedState.getShippingInfo().getShippingMethodState()).isEqualTo(ShippingMethodState.DOES_NOT_MATCH_CART); return cartWitUpdatedState; }); }); }); }
public static void withNonUpdatedOrder(final BlockingSphereClient client, final Customer customer, final UnaryOperator<Order> op) { withFilledCart(client, cart -> { final TaxCategory taxCategory = TaxCategoryFixtures.defaultTaxCategory(client); final SetCustomShippingMethod shippingMethodAction = SetCustomShippingMethod.of("custom shipping method", ShippingRate.of(EURO_10), taxCategory); final SetCustomerEmail emailAction = SetCustomerEmail.of(CUSTOMER_EMAIL); final Cart updatedCart = client.executeBlocking(CartUpdateCommand.of(cart, asList(shippingMethodAction, emailAction))); final CustomerSignInCommand signInCommand = CustomerSignInCommand.of(customer.getEmail(), CustomerFixtures.PASSWORD, cart.getId()); final CustomerSignInResult signInResult = client.executeBlocking(signInCommand); final Order order = client.executeBlocking(OrderFromCartCreateCommand.of(signInResult.getCart())); final Order orderToDelete = op.apply(order); client.executeBlocking(OrderDeleteCommand.of(orderToDelete)); }); }
private static Order createOrderFromCart(BlockingSphereClient client, Customer customer, Cart cart) { final TaxCategory taxCategory = TaxCategoryFixtures.defaultTaxCategory(client); final SetCustomShippingMethod shippingMethodAction = SetCustomShippingMethod.of("custom shipping method", ShippingRate.of(EURO_10), taxCategory); final SetCustomerEmail emailAction = SetCustomerEmail.of(CUSTOMER_EMAIL); client.executeBlocking(CartUpdateCommand.of(cart, asList(shippingMethodAction, emailAction))); final CustomerSignInCommand signInCommand = CustomerSignInCommand.of(customer.getEmail(), CustomerFixtures.PASSWORD, cart.getId()); final CustomerSignInResult signInResult = client.executeBlocking(signInCommand); final Order order = client.executeBlocking(OrderFromCartCreateCommand.of(signInResult.getCart())); return client.executeBlocking(OrderUpdateCommand.of(order, asList( ChangeShipmentState.of(ShipmentState.READY), ChangePaymentState.of(PaymentState.PENDING) ))); }