@Override public Set<Reference<Category>> getCategories() { return productData.getCategories(); }
@Override public Optional<LocalizedStrings> getDescription() { return productData.getDescription(); }
@Override public Optional<LocalizedStrings> getMetaDescription() { return productData.getMetaDescription(); }
@Test public void setMetaAttributes() throws Exception { withUpdateableProduct(client(), product -> { final MetaAttributes metaAttributes = MetaAttributes.metaAttributesOf(ENGLISH, "commercetools SPHERE.IO™ - Next generation eCommerce", "SPHERE.IO™ is the first and leading Platform-as-a-Service solution for eCommerce.", "Platform-as-a-Service, e-commerce, http, api, tool"); final List<UpdateAction<Product>> updateActions = MetaAttributesUpdateActions.of(metaAttributes); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, updateActions)); final ProductData productData = updatedProduct.getMasterData().getStaged(); assertThat(productData.getMetaTitle()).isEqualTo(metaAttributes.getMetaTitle()); assertThat(productData.getMetaDescription()).isEqualTo(metaAttributes.getMetaDescription()); assertThat(productData.getMetaKeywords()).isEqualTo(metaAttributes.getMetaKeywords()); return updatedProduct; }); }
@Test public void addLineItemOfDraftOfVariantIdentifier() throws Exception { withEmptyCartAndProduct(client(), (cart, product) -> { assertThat(cart.getLineItems()).isEmpty(); final long quantity = 3; final Channel inventorySupplyChannel = ChannelFixtures.persistentChannelOfRole(client(), ChannelRole.INVENTORY_SUPPLY); final Channel distributionChannel = ChannelFixtures.persistentChannelOfRole(client(), ChannelRole.PRODUCT_DISTRIBUTION); ByIdVariantIdentifier variantIdentifier = product.getMasterData().getStaged().getMasterVariant().getIdentifier(); final LineItemDraft lineItemDraft = io.sphere.sdk.carts.LineItemDraftBuilder.ofVariantIdentifier(variantIdentifier, quantity) .distributionChannel(distributionChannel) .supplyChannel(inventorySupplyChannel) .build(); final AddLineItem action = AddLineItem.of(lineItemDraft); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, action)); assertThat(updatedCart.getLineItems()).hasSize(1); final LineItem lineItem = updatedCart.getLineItems().get(0); assertThat(lineItem.getName()).isEqualTo(product.getMasterData().getStaged().getName()); assertThat(lineItem.getQuantity()).isEqualTo(quantity); assertThat(lineItem.getProductSlug()).isEqualTo(product.getMasterData().getStaged().getSlug()); assertThat(lineItem.getVariant().getIdentifier()).isEqualTo(ByIdVariantIdentifier.of(lineItem.getProductId(), lineItem.getVariant().getId())); assertThat(lineItem.getSupplyChannel().toReference()).isEqualTo(inventorySupplyChannel.toReference()); assertThat(lineItem.getDistributionChannel().toReference()).isEqualTo(distributionChannel.toReference()); assertThat(lineItem.getDiscountedPricePerQuantity()).isNotNull().isEmpty(); }); }
@Test public void readAttributeGetValueAs() throws Exception { final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); final String attributeValue = masterVariant.findAttribute(SIZE_ATTR_NAME) .map((Attribute a) -> { final EnumValue enumValue = a.getValueAsEnumValue(); return enumValue.getLabel(); }) .orElse("not found"); assertThat(attributeValue).isEqualTo("S"); }
@Override public LocalizedStrings getName() { return productData.getName(); }
@Override public List<ProductVariant> getVariants() { return productData.getVariants(); }
@Override public LocalizedString getSlug() { return productData.getSlug(); }
@Test public void addLineItem() throws Exception { withEmptyCartAndProduct(client(), (cart, product) -> { assertThat(cart.getLineItems()).isEmpty(); final long quantity = 3; final String productId = product.getId(); final AddLineItem action = AddLineItem.of(productId, MASTER_VARIANT_ID, quantity); final Cart updatedCart = client().executeBlocking(CartUpdateCommand.of(cart, action)); assertThat(updatedCart.getLineItems()).hasSize(1); final LineItem lineItem = updatedCart.getLineItems().get(0); assertThat(lineItem.getName()).isEqualTo(product.getMasterData().getStaged().getName()); assertThat(lineItem.getQuantity()).isEqualTo(quantity); assertThat(lineItem.getProductSlug()).isEqualTo(product.getMasterData().getStaged().getSlug()); assertThat(lineItem.getVariant().getIdentifier()).isEqualTo(ByIdVariantIdentifier.of(lineItem.getProductId(), lineItem.getVariant().getId())); assertThat(lineItem.getDiscountedPricePerQuantity()).isNotNull().isEmpty(); }); }
@Override public SearchKeywords getSearchKeywords() { return productData.getSearchKeywords(); } }
@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)); }); }); }); }); }
@Override public Optional<LocalizedStrings> getMetaKeywords() { return productData.getMetaKeywords(); }
@Override public Optional<LocalizedStrings> getMetaTitle() { return productData.getMetaTitle(); }
@Test public void addToCategory() throws Exception { withProductAndUnconnectedCategory(client(), (final Product product, final Category category) -> { assertThat(product.getMasterData().getStaged().getCategories()).isEmpty(); final String orderHint = "0.123"; final Product productWithCategory = client() .executeBlocking(ProductUpdateCommand.of(product, AddToCategory.of(category, orderHint))); final Reference<Category> categoryReference = productWithCategory.getMasterData().getStaged().getCategories().stream().findAny().get(); assertThat(categoryReference.referencesSameResource(category)).isTrue(); assertThat(productWithCategory.getMasterData().getStaged().getCategoryOrderHints().get(category.getId())).isEqualTo(orderHint); final Product productWithoutCategory = client() .executeBlocking(ProductUpdateCommand.of(productWithCategory, RemoveFromCategory.of(category))); assertThat(productWithoutCategory.getMasterData().getStaged().getCategories()).isEmpty(); }); }
@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 productProjectionCanBeUsedToUpdateAProduct() throws Exception { withUpdateableProduct(client(), product -> { final MetaAttributes metaAttributes = MetaAttributes.metaAttributesOf(ENGLISH, "commercetools SPHERE.IO™ - Next generation eCommerce", "SPHERE.IO™ is the first and leading Platform-as-a-Service solution for eCommerce.", "Platform-as-a-Service, e-commerce, http, api, tool"); final List<UpdateAction<Product>> updateActions = MetaAttributesUpdateActions.of(metaAttributes); final ProductProjection productProjection = client().executeBlocking(ProductProjectionByIdGet.of(product, STAGED)); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(productProjection, updateActions)); final ProductData productData = updatedProduct.getMasterData().getStaged(); assertThat(productData.getMetaTitle()).isEqualTo(metaAttributes.getMetaTitle()); assertThat(productData.getMetaDescription()).isEqualTo(metaAttributes.getMetaDescription()); assertThat(productData.getMetaKeywords()).isEqualTo(metaAttributes.getMetaKeywords()); return updatedProduct; }); }
public static void withProductOfRestockableInDaysAndChannel(final BlockingSphereClient client, final int restockableInDays, @Nullable final Referenceable<Channel> channelReferenceable, final Consumer<Product> productConsumer) { final Reference<Channel> channelReference = Optional.ofNullable(channelReferenceable).map(Referenceable::toReference).orElse(null); ProductFixtures.withProduct(client, product -> { final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); final InventoryEntry inventoryEntry = client.executeBlocking(InventoryEntryCreateCommand.of(InventoryEntryDraft.of(sku, 5, null, restockableInDays, channelReference))); productConsumer.accept(product); client.executeBlocking(InventoryEntryDeleteCommand.of(inventoryEntry)); }); }
@Override public LocalizedString getName() { return productData.getName(); }
@Override public List<ProductVariant> getVariants() { return productData.getVariants(); }