@Test public void setCustomShippingMethod() throws Exception { withTaxCategory(client(), taxCategory -> { final Cart cart = createCartWithShippingAddress(client()); assertThat(cart.getShippingInfo()).isNull(); final MonetaryAmount price = MoneyImpl.of(new BigDecimal("23.50"), EUR); final ShippingRate shippingRate = ShippingRate.of(price, null, Collections.emptyList()); final String shippingMethodName = "custom-shipping"; final SetCustomShippingMethod action = SetCustomShippingMethod.of(shippingMethodName, shippingRate, taxCategory); final Cart cartWithShippingMethod = client().executeBlocking(CartUpdateCommand.of(cart, action)); final CartShippingInfo shippingInfo = cartWithShippingMethod.getShippingInfo(); assertThat(shippingInfo.getPrice()).isEqualTo(price); assertThat(shippingInfo.getShippingMethod()).isNull(); assertThat(shippingInfo.getShippingMethodName()).isEqualTo(shippingMethodName); assertThat(shippingInfo.getShippingRate()).isEqualTo(shippingRate); assertThat(shippingInfo.getTaxCategory()).isEqualTo(taxCategory.toReference()); assertThat(shippingInfo.getTaxRate()).isNotNull(); }); }
public static void withUpdateableShippingMethod(final BlockingSphereClient client, final UnaryOperator<ShippingMethodDraftBuilder> builderMapper, final Function<ShippingMethod, ShippingMethod> f) { withTaxCategory(client, taxCategory -> { final ShippingMethodDraftBuilder builder = ShippingMethodDraftBuilder.of(randomString(), "test shipping method", taxCategory.toReference(), asList(), false); final ShippingMethodDraft draft = builderMapper.apply(builder).build(); final ShippingMethod shippingMethod = client.executeBlocking(ShippingMethodCreateCommand.of(draft)); final ShippingMethod possiblyUpdatedShippingMethod = f.apply(shippingMethod); client.executeBlocking(ShippingMethodDeleteCommand.of(possiblyUpdatedShippingMethod)); }); }
@Test public void createByJson() { withTaxCategory(client(), taxCategory -> { withZone(client(), zone -> { final JsonNodeReferenceResolver referenceResolver = new JsonNodeReferenceResolver(); referenceResolver.addResourceByKey("standard-tax", taxCategory); referenceResolver.addResourceByKey("zone-id", zone); final ShippingMethodDraft draft = draftFromJsonResource("drafts-tests/shippingMethod.json", ShippingMethodDraft.class, referenceResolver); withShippingMethod(client(), draft, shippingMethod -> { assertThat(shippingMethod.getName()).isEqualTo("demo shipping method"); assertThat(shippingMethod.getTaxCategory()).isEqualTo(taxCategory.toReference()); final ZoneRate zoneRate = shippingMethod.getZoneRates().get(0); assertThat(zoneRate.getZone()).isEqualTo(zone.toReference()); final ShippingRate shippingRate = zoneRate.getShippingRates().get(0); assertThat(shippingRate.getPrice()).isEqualTo(EURO_20); }); }, COUNTRY_CODE); }); } }
@Test public void setTaxCategory() throws Exception { TaxCategoryFixtures.withTransientTaxCategory(client(), taxCategory -> withUpdateableProduct(client(), product -> { assertThat(product.getTaxCategory()).isNotEqualTo(taxCategory); final ProductUpdateCommand command = ProductUpdateCommand.of(product, SetTaxCategory.of(taxCategory)); final Product updatedProduct = client().executeBlocking(command); assertThat(updatedProduct.getTaxCategory()).isEqualTo(taxCategory.toReference()); return updatedProduct; }) ); }
@Ignore @Test public void addMergesSameCustomLineItems() throws Exception { withTaxCategory(client(), taxCategory -> { final Cart cart = createCartWithCountry(client()); assertThat(cart.getCustomLineItems()).isEmpty(); final MonetaryAmount money = MoneyImpl.of("23.50", EUR); final String slug = "thing-slug";//you handle to identify the custom line item 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, Arrays.asList(AddCustomLineItem.of(item), AddCustomLineItem.of(item)))); assertThat(cartWith5.getCustomLineItems()).hasSize(1); final CustomLineItem customLineItem = cartWith5.getCustomLineItems().get(0); assertThat(customLineItem.getMoney()).isEqualTo(money); assertThat(customLineItem.getName()).isEqualTo(name); assertThat(customLineItem.getQuantity()).isEqualTo(quantity * 2); assertThat(customLineItem.getSlug()).isEqualTo(slug); final Set<ItemState> state = customLineItem.getState(); assertThat(state).hasSize(1); assertThat(state).extracting("quantity").containsOnly(quantity * 2); assertThat(customLineItem.getTaxCategory()).isEqualTo(taxCategory.toReference()); final CartQuery cartQuery = CartQuery.of() .withPredicates(m -> m.customLineItems().slug().is(customLineItem.getSlug()) .and(m.id().is(cart.getId()))); assertThat(client().executeBlocking(cartQuery).head().get().getId()).isEqualTo(cart.getId()); }); }
@Test public void addCustomLineItem() throws Exception { withTaxCategory(client(), taxCategory -> { final Cart cart = createCartWithCountry(client()); assertThat(cart.getCustomLineItems()).isEmpty(); final MonetaryAmount money = MoneyImpl.of("23.50", EUR); final String slug = "thing-slug";//you handle to identify the custom line item 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))); assertThat(cartWith5.getCustomLineItems()).hasSize(1); final CustomLineItem customLineItem = cartWith5.getCustomLineItems().get(0); assertThat(customLineItem.getMoney()).isEqualTo(money); assertThat(customLineItem.getName()).isEqualTo(name); assertThat(customLineItem.getQuantity()).isEqualTo(quantity); assertThat(customLineItem.getSlug()).isEqualTo(slug); final Set<ItemState> state = customLineItem.getState(); assertThat(state).hasSize(1); assertThat(state).extracting("quantity").containsOnly(quantity); assertThat(customLineItem.getTaxCategory()).isEqualTo(taxCategory.toReference()); final CartQuery cartQuery = CartQuery.of() .withPredicates(m -> m.customLineItems().slug().is(customLineItem.getSlug()) .and(m.id().is(cart.getId()))); assertThat(client().executeBlocking(cartQuery).head().get().getId()).isEqualTo(cart.getId()); }); }
final long quantity = 16; final MonetaryAmount money = EURO_20; final Reference<TaxCategory> taxCategoryReference = defaultTaxCategory(client()).toReference(); final String id = "an id"; final String slug = "a-slug";
public static void withUpdateableDynamicShippingMethod(final BlockingSphereClient client, final CartPredicate cartPredicate, final Function<ShippingMethod, ShippingMethod> f) { withTaxCategory(client, taxCategory -> { final ShippingMethodDraft draft = ShippingMethodDraftBuilder .of(randomString(), "test shipping method", taxCategory.toReference(), asList(), false) .predicate(cartPredicate) .build(); final ShippingMethod shippingMethod = client.executeBlocking(ShippingMethodCreateCommand.of(draft)); final ShippingMethod possiblyUpdatedShippingMethod = f.apply(shippingMethod); client.executeBlocking(ShippingMethodDeleteCommand.of(possiblyUpdatedShippingMethod)); }); }
@Test public void createProductByJsonDraft() throws Exception { final JsonNodeReferenceResolver referenceResolver = new JsonNodeReferenceResolver(); withCategory(client(), category -> { withProductType(client(), randomKey(), productType -> { withTaxCategory(client(), taxCategory -> { withStateByBuilder(client(), stateBuilder -> stateBuilder.initial(true).type(StateType.PRODUCT_STATE), state -> { referenceResolver.addResourceByKey("t-shirt-category", category); referenceResolver.addResourceByKey("t-shirt-product-type", productType); referenceResolver.addResourceByKey("standard-tax", taxCategory); referenceResolver.addResourceByKey("initial-product-state", state); final ProductDraft productDraft = draftFromJsonResource("drafts-tests/product.json", ProductDraft.class, referenceResolver); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); assertThat(product.getProductType()).isEqualTo(productType.toReference()); assertThat(product.getTaxCategory()).isEqualTo(taxCategory.toReference()); assertThat(product.getState()).isEqualTo(state.toReference()); final ProductData productData = product.getMasterData().getStaged(); assertThat(productData.getName()).isEqualTo(LocalizedString.ofEnglish("red shirt")); assertThat(productData.getCategories()).extracting("id").contains(category.getId()); client().executeBlocking(ProductDeleteCommand.of(product)); }); }); }); }); }
@Test public void createProductWithExternalImage() throws Exception { withStateByBuilder(client(), stateBuilder -> stateBuilder.initial(true).type(PRODUCT_STATE), initialProductState -> { final ProductType productType = ProductTypeFixtures.defaultProductType(client()); final Image image = Image.ofWidthAndHeight("http://www.commercetools.com/assets/img/ct_logo_farbe.gif", 460, 102, "commercetools logo"); final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of() .images(image) .build(); final TaxCategory taxCategory = defaultTaxCategory(client()); final SearchKeywords searchKeywords = SearchKeywords.of(Locale.ENGLISH, asList(SearchKeyword.of("foo bar baz", CustomSuggestTokenizer.of(asList("foo, baz"))))); final ProductDraft productDraft = ProductDraftBuilder .of(productType, en("product with external image"), randomSlug(), masterVariant) .taxCategory(taxCategory) .searchKeywords(searchKeywords) .state(initialProductState) .build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); final Image loadedImage = product.getMasterData().getStaged().getMasterVariant().getImages().get(0); assertThat(loadedImage).isEqualTo(image); assertThat(product.getTaxCategory()).isEqualTo(taxCategory.toReference()); assertThat(product.getMasterData().getStaged().getSearchKeywords()).isEqualTo(searchKeywords); assertThat(product.getState()).isEqualTo(initialProductState.toReference()); //clean up test client().executeBlocking(ProductDeleteCommand.of(product)); }); }
@Test public void createShippingMethodWithPredicate() throws Exception { final CurrencyUnit currencyUnit = USD; final TaxRateDraft taxRate = TaxRateDraft.of("x20", 0.20, true, COUNTRY_CODE); withZone(client(), zone -> { withTaxCategory(client(), TaxCategoryDraft.of("taxcat", asList(taxRate)), taxCategory -> { final ZoneRateDraft zoneRateDraft = ZoneRateDraftBuilder.of(zone.toResourceIdentifier(), asList(ShippingRate.of(MoneyImpl.of(30, currencyUnit)))).build(); final ShippingMethodDraft draft = ShippingMethodDraftBuilder.of("standard shipping", "description", taxCategory.toReference(), asList(zoneRateDraft), false) .predicate(CartPredicate.of("customer.email = \"john@example.com\"")) .build(); final ShippingMethod shippingMethod = client().executeBlocking(ShippingMethodCreateCommand.of(draft)); //deletion client().executeBlocking(ShippingMethodDeleteCommand.of(shippingMethod)); }); }, COUNTRY_CODE); }
final ShippingRate shippingRate = ShippingRate.of(price, freeAbove, Collections.EMPTY_LIST); final TaxRate taxRate = taxCategory.getTaxRates().get(0); final Reference<TaxCategory> taxCategoryRef = taxCategory.toReference(); final Reference<ShippingMethod> shippingMethodRef = shippingMethod.toReference(); final ZonedDateTime createdAt = SphereTestUtils.now().minusSeconds(4);