private CartDraftDsl cartDraftWithAdditionalInfo(final CartDraft cartDraft) { final CountryCode country = injector.getInstance(UserContext.class).country(); final CustomerInSession customerInSession = injector.getInstance(CustomerInSession.class); return CartDraftBuilder.of(cartDraft) .country(country) .shippingAddress(Address.of(country)) .customerId(customerInSession.findCustomerId().orElse(null)) .customerEmail(customerInSession.findCustomerEmail().orElse(null)) .build(); }
/** * Updates the country of the cart, both {@code country} and {@code shippingAddress} country fields. * This is necessary in order to obtain prices with tax calculation. * @param cart the cart which country needs to be updated * @param country the country to set in the cart * @param expansionPathContainer expansion paths to be honored * @return the completionStage of a cart with the given country */ private CompletionStage<Cart> updateCartCountry(final Cart cart, final CountryCode country, final ExpansionPathContainer<Cart> expansionPathContainer) { // TODO Handle case where some line items do not exist for this country final Address shippingAddress = Optional.ofNullable(cart.getShippingAddress()) .map(address -> address.withCountry(country)) .orElseGet(() -> Address.of(country)); final CartUpdateCommand updateCommand = CartUpdateCommand.of(cart, asList(SetShippingAddress.of(shippingAddress), SetCountry.of(country))) .withExpansionPaths(expansionPathContainer); return injector.getInstance(SphereClient.class).execute(updateCommand); } }
public final static List<Address> createAddressArray() { final Address address1 = Address.of(CountryCode.DE).withKey(SphereTestUtils.randomKey()); final Address address2 = Address.of(CountryCode.FR).withKey(SphereTestUtils.randomKey()); final Address address3 = Address.of(CountryCode.MA).withKey(SphereTestUtils.randomKey()); final Address address4 = Address.of(CountryCode.IT).withKey(SphereTestUtils.randomKey()); return Arrays.asList(address1, address2, address3, address4); }
public final static List<Address> createAddressArray() { final Address address1 = Address.of(CountryCode.DE).withKey(SphereTestUtils.randomKey()); final Address address2 = Address.of(CountryCode.FR).withKey(SphereTestUtils.randomKey()); final Address address3 = Address.of(CountryCode.MA).withKey(SphereTestUtils.randomKey()); final Address address4 = Address.of(CountryCode.IT).withKey(SphereTestUtils.randomKey()); return Arrays.asList(address1, address2, address3, address4); }
@Test public void externalId() { final String externalId = "030000000"; final Address address = Address.of(CountryCode.DE).withExternalId(externalId); assertThat(address.getExternalId()).isEqualTo(externalId); } }
@Test public void fax() { final String fax = "030000000"; final Address address = Address.of(CountryCode.DE).withFax(fax); assertThat(address.getFax()).isEqualTo(fax); }
@Test public void testUpdateAddressesToCartUpdateAction() { withFilledCartAndMultipleAddresses(client(), cart -> { final List<Address> addresses = cart.getItemShippingAddresses(); final Address firstAddress = addresses.get(0); assertThat(firstAddress.getCountry()).isNotEqualByComparingTo(CountryCode.CA); final String firstAddressKey = firstAddress.getKey(); final Address updatedAddress = Address.of(CountryCode.CA).withKey(firstAddressKey); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, UpdateItemShippingAddress.of(updatedAddress))); List<Address> newAddresses = updatedCart.getItemShippingAddresses().stream().filter(address -> address.getKey().equals(firstAddressKey)).collect(Collectors.toList()); assertThat(newAddresses).hasSize(1); assertThat(newAddresses.get(0).getCountry()).isEqualByComparingTo(CountryCode.CA); }); }
@Test public void equalsIgnoreId() { final Address addressWithoutId = Address.of(CountryCode.DE); final Address addressWithId = addressWithoutId.withId("foo"); assertThat(addressWithoutId) .isNotEqualTo(addressWithId) .matches(address -> address.equalsIgnoreId(addressWithId)); }
@Test public void addShippingAddressId() { final List<Address> addresses = asList(Address.of(DE), Address.of(FR)); final CustomerDraft draft = newCustomerDraft().withAddresses(addresses); withCustomer(client(), draft, customer -> { assertThat(customer.getShippingAddressIds()).isEmpty(); final Address addressForShipping = customer.getAddresses().get(1); final String addressId = addressForShipping.getId(); final AddShippingAddressId updateAction = AddShippingAddressId.of(addressId); final Customer updatedCustomer = client().executeBlocking(CustomerUpdateCommand.of(customer, updateAction)); assertThat(updatedCustomer.getShippingAddressIds()).containsExactly(addressId); }); }
@Test public void addBillingAddressId() { final List<Address> addresses = asList(Address.of(DE), Address.of(FR)); final CustomerDraft draft = newCustomerDraft().withAddresses(addresses); withCustomer(client(), draft, customer -> { assertThat(customer.getBillingAddressIds()).isEmpty(); final Address addressForBilling = customer.getAddresses().get(1); final String addressId = addressForBilling.getId(); final AddBillingAddressId updateAction = AddBillingAddressId.of(addressId); final Customer updatedCustomer = client().executeBlocking(CustomerUpdateCommand.of(customer, updateAction)); assertThat(updatedCustomer.getBillingAddressIds()).containsExactly(addressId); }); }
@Test public void removeBillingAddressId() { final List<Address> addresses = asList(Address.of(DE), Address.of(FR)); final CustomerDraft draft = newCustomerDraft() .withAddresses(addresses) .withBillingAddresses(singletonList(1)); withCustomer(client(), draft, customer -> { assertThat(customer.getBillingAddressIds()).isNotEmpty(); final String addressId = customer.getBillingAddressIds().get(0); final RemoveBillingAddressId updateAction = RemoveBillingAddressId.of(addressId); final Customer updatedCustomer = client().executeBlocking(CustomerUpdateCommand.of(customer, updateAction)); assertThat(updatedCustomer.getBillingAddressIds()).isEmpty(); }); }
@Test public void createByJson() { final JsonNodeReferenceResolver referenceResolver = new JsonNodeReferenceResolver(); withB2cCustomerGroup(client(), customerGroup -> { referenceResolver.addResourceByKey("b2c", customerGroup); final CustomerDraft customerDraft = draftFromJsonResource("drafts-tests/customer.json", CustomerDraftDsl.class, referenceResolver); CustomerFixtures.withCustomer(client(), customerDraft, customer -> { assertThat(customer.getLastName()).isEqualTo("Osgood"); assertThat(customer.getCustomerGroup()).isEqualTo(customerGroup.toReference()); assertThat(customer.getAddresses()).hasSize(2); assertThat(customer.getDefaultShippingAddress().withId(null)) .isEqualTo(Address.of(DE).withLastName("Osgood").withExternalId("external-ID-DE")); }); }); }
@Test public void removeShippingAddressId() { final List<Address> addresses = asList(Address.of(DE), Address.of(FR)); final CustomerDraft draft = newCustomerDraft() .withAddresses(addresses) .withShippingAddresses(singletonList(1)); withCustomer(client(), draft, customer -> { assertThat(customer.getShippingAddressIds()).isNotEmpty(); final String addressId = customer.getShippingAddressIds().get(0); final RemoveShippingAddressId updateAction = RemoveShippingAddressId.of(addressId); final Customer updatedCustomer = client().executeBlocking(CustomerUpdateCommand.of(customer, updateAction)); assertThat(updatedCustomer.getShippingAddressIds()).isEmpty(); }); } }
private static void withTaxedCartWithProduct(final BlockingSphereClient client, final long centAmount, final double taxRate, final boolean taxIncluded, final Function<Cart, CartLike<?>> operator) { withTaxedAndPricedProduct(client, centAmount, taxRate, taxIncluded, product -> { final LineItemDraft lineItemDraft = LineItemDraft.of(product, MASTER_VARIANT_ID, 1); final CartDraftDsl cartDraft = CartDraft.of(EUR) .withLineItems(singletonList(lineItemDraft)) .withShippingAddress(Address.of(CountryCode.DE)) .withCountry(CountryCode.DE); withCartDraft(client, cartDraft, operator); }); }
@Test public void testUpdateAddressesToOrderUpdateAction() { withFilledCartAndMultipleAddresses(client(), cart -> { withCustomer(client(),customer -> { withOrder(client(),customer,cart,order -> { final List<Address> addresses = order.getItemShippingAddresses(); final Address firstAddress = addresses.get(0); assertThat(firstAddress.getCountry()).isNotEqualByComparingTo(CountryCode.CA); final String firstAddressKey = firstAddress.getKey(); final Address updatedAddress = Address.of(CountryCode.CA).withKey(firstAddressKey); final Order updatedOrder= client().executeBlocking(OrderUpdateCommand.of(order, UpdateItemShippingAddress.of(updatedAddress))); List<Address> newAddresses = updatedOrder.getItemShippingAddresses().stream().filter(address -> address.getKey().equals(firstAddressKey)).collect(Collectors.toList()); assertThat(newAddresses).hasSize(1); assertThat(newAddresses.get(0).getCountry()).isEqualByComparingTo(CountryCode.CA); return updatedOrder; }); }); }); }
@Test public void setAddress() throws Exception { withUpdatableChannelOfRole(client(), asSet(PRIMARY), channel -> { final Address address = Address.of(DE).withCity("Berlin"); final Channel updatedChannel = client().executeBlocking(ChannelUpdateCommand.of(channel, SetAddress.of(address))); assertThat(updatedChannel.getAddress()).isEqualTo(address); return updatedChannel; }); }
@Test public void execution() throws Exception { final String key = "demo-key"; final Address address = Address.of(DE).withCity("Berlin"); final Point geoLocation = Point.of(52.5200070, 13.4049540); final ChannelDraft channelDraft = ChannelDraft.of(key) .withName(LocalizedString.of(ENGLISH, "name")) .withDescription(LocalizedString.of(ENGLISH, "description")) .withRoles(ChannelRole.INVENTORY_SUPPLY) .withAddress(address) .withGeoLocation(geoLocation); final Channel channel = client().executeBlocking(ChannelCreateCommand.of(channelDraft)); assertThat(channel.getKey()).isEqualTo(key); assertThat(channel.getName()).isEqualTo(LocalizedString.of(ENGLISH, "name")); assertThat(channel.getDescription()).isEqualTo(LocalizedString.of(ENGLISH, "description")); assertThat(channel.getRoles()).isEqualTo(asSet(ChannelRole.INVENTORY_SUPPLY)); assertThat(channel.getAddress()).isEqualTo(address); assertThat(channel.getGeoLocation()).isEqualTo(geoLocation); }
@Test public void addCustomShippingMethod() { final CartDraft draft = CartDraft.of(EUR) .withTaxMode(TaxMode.EXTERNAL) .withShippingAddress(Address.of(DE)); withCartDraft(client(), draft, (Cart cart) -> { final String taxRateName = "special tax"; final double taxRate = 0.20; final ExternalTaxRateDraft externalTaxRate = ExternalTaxRateDraftBuilder.ofAmount(taxRate, taxRateName, DE).build(); final ShippingRate shippingRate = ShippingRate.of(EURO_10,null, Collections.emptyList()); final SetCustomShippingMethod action = SetCustomShippingMethod.ofExternalTaxCalculation("name", shippingRate, externalTaxRate); final Cart cartWithShippingMethod = client().executeBlocking(CartUpdateCommand.of(cart, action)); final CartShippingInfo shippingInfo = cartWithShippingMethod.getShippingInfo(); assertThat(shippingInfo).isNotNull(); assertThat(shippingInfo.getShippingRate()).isEqualTo(shippingRate); assertThat(shippingInfo.getTaxCategory()).isNull(); assertThat(shippingInfo.getTaxedPrice().getTotalNet()).isEqualTo(EURO_10); assertThat(shippingInfo.getTaxedPrice().getTotalGross()).isEqualTo(EURO_12); assertThat(shippingInfo.getTaxRate().getName()).isEqualTo(taxRateName); return cartWithShippingMethod; }); }
public static void withCartWithLineItems(final BlockingSphereClient client, final List<LineItemDraft> lineItemsDraft, final UnaryOperator<Cart> op) { final CartDraftDsl cartDraft = CartDraft.of(EUR) .withTaxMode(TaxMode.EXTERNAL) .withCountry(DE) .withShippingAddress(Address.of(DE)) .withLineItems(lineItemsDraft); final CartCreateCommand cmd = CartCreateCommand.of(cartDraft); final Cart cart = client.executeBlocking(cmd); final Cart updatedCart = op.apply(cart); client.executeBlocking(CartDeleteCommand.of(updatedCart)); } }
@Test public void setBillingAddress() { withOrder(client(), order -> { assertThat(order.getBillingAddress()).isNull(); final Address newAddress = Address.of(DE).withStreetNumber("5"); final Order updatedOrder = client().executeBlocking(OrderUpdateCommand.of(order, SetBillingAddress.of(newAddress))); assertThat(updatedOrder.getBillingAddress().getStreetNumber()).isEqualTo("5"); //there is also a message final Query<OrderBillingAddressSetMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(order)) .forMessageType(OrderBillingAddressSetMessage.MESSAGE_HINT); assertEventually(() -> { final Optional<OrderBillingAddressSetMessage> billingAddressSetMessageOptional = client().executeBlocking(messageQuery).head(); assertThat(billingAddressSetMessageOptional).isPresent(); final OrderBillingAddressSetMessage orderBillingAddressSetMessage = billingAddressSetMessageOptional.get(); assertThat(orderBillingAddressSetMessage.getAddress()).isEqualTo(newAddress); }); return updatedOrder; }); }