protected SphereRequest<Product> deleteCommand(final Product item) { return ProductDeleteCommand.of(item); }
/** Creates a command object to delete a {@link Product} by its key. @param key the key of the Product to delete, see {@link Product#getKey()} @param version `the current version of the Product, see {@link Product#getVersion()} @return delete command */ static ProductDeleteCommand ofKey(final String key, final Long version) { final Versioned<Product> versioned = Versioned.of("key=" + urlEncode(key), version);//hack for simple reuse return of(versioned); }
public void delete() { final DeleteCommand<Product> command = ProductDeleteCommand.of(product); final CompletionStage<Product> deletedProduct = client.execute(command); } }
public static void destroy(final BlockingSphereClient client, final Data data) { final List<CompletionStage<Product>> productDeletions = data.getAllProducts().stream() .map(product -> client.execute(ProductDeleteCommand.of(product))) .collect(toList()); productDeletions.forEach(stage -> SphereClientUtils.blockingWait(stage, 2, TimeUnit.SECONDS)); client.executeBlocking(ProductTypeDeleteCommand.of(data.getProductType())); }
private static void deleteWithRetry(final BlockingSphereClient client, final Versioned<Product> unPublishedProduct, final int ttl) { if (ttl > 0) { try { client.executeBlocking(ProductDeleteCommand.of(unPublishedProduct)); } catch(final ConcurrentModificationException e) { final Versioned<Product> versioned = Versioned.of(unPublishedProduct.getId(), e.getCurrentVersion()); deleteWithRetry(client, versioned, ttl - 1); } } else { throw new RuntimeException("cannot delete product due to too much concurrent updates, product: " + unPublishedProduct); } }
public static void withProduct(final BlockingSphereClient client, final UnaryOperator<ProductDraftBuilder> builderMapper, final Consumer<Product> productConsumer) { withAttributesProductType(client, productType -> { final ProductDraftBuilder builder = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), ProductVariantDraftBuilder.of().build()); final ProductDraftBuilder updatedBuilder = builderMapper.apply(builder); final Product product = client.executeBlocking(ProductCreateCommand.of(updatedBuilder.build())); productConsumer.accept(product); client.executeBlocking(ProductDeleteCommand.of(product)); }); }
@After public void tearDown() throws Exception { if (product != null) { client().executeBlocking(ProductDeleteCommand.of(product)); } if (productType != null) { client().executeBlocking(ProductTypeDeleteCommand.of(productType)); } }
public static void creatingProduct(final BlockingSphereClient client, final UnaryOperator<ProductDraftBuilder> builderMapper, final UnaryOperator<Product> op) { withProductType(client, randomKey(), productType -> { final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().build(); final ProductDraftBuilder builder = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), masterVariant); final ProductDraft productDraft = builderMapper.apply(builder).build(); final Product product = client.executeBlocking(ProductCreateCommand.of(productDraft)); final Product updatedProduct = op.apply(product); client.executeBlocking(ProductDeleteCommand.of(updatedProduct)); }); }
private void unpublishAndDelete(final Product product) { client().executeBlocking(ProductDeleteCommand.of(client().executeBlocking(ProductUpdateCommand.of(product, Unpublish.of())))); } }
assertThat(found).overridingErrorMessage("the attribute type should be recognized").isTrue(); client().executeBlocking(ProductDeleteCommand.of(product)); cleanUpByName(productTypeName);
private static void withProductInCategory(final BlockingSphereClient client, final Referenceable<Category> category, final Consumer<Product> user) { final ProductType productType = client.executeBlocking(ProductTypeCreateCommand.of(ProductTypeDraft.of(randomKey(), CategoryDocumentationIntegrationTest.class.getSimpleName(), "", asList()))); final LocalizedString name = LocalizedString.of(ENGLISH, "foo"); final Product product = client.executeBlocking(ProductCreateCommand.of(ProductDraftBuilder.of(productType, name, name.slugifiedUnique(), ProductVariantDraftBuilder.of().build()).categories(asSet(category.toReference())).build())); user.accept(product); client.executeBlocking(ProductDeleteCommand.of(product)); client.executeBlocking(ProductTypeDeleteCommand.of(productType)); }
public static void deleteProductsProductTypeAndProductDiscounts(final BlockingSphereClient client, final ProductType productType) { client.executeBlocking(ProductDiscountQuery.of().withLimit(500L)).getResults() .forEach(discount -> client.executeBlocking(ProductDiscountDeleteCommand.of(discount))); if (productType != null) { QueryPredicate<Product> ofProductType = ProductQueryModel.of().productType().is(productType); ProductQuery productsOfProductTypeQuery = ProductQuery.of().withPredicates(ofProductType).withLimit(500L); do { final List<Product> products = client.executeBlocking(productsOfProductTypeQuery).getResults(); final List<Product> unpublishedProducts = products.stream().map( product -> { if (product.getMasterData().isPublished()) { return client.executeBlocking(ProductUpdateCommand.of(product, Unpublish.of())); } else { return product; } } ).collect(toList()); final List<CompletionStage<Product>> stages = new LinkedList<>(); unpublishedProducts.forEach( product -> { final CompletionStage<Product> completionStage = client.execute(ProductDeleteCommand.of(product)); stages.add(completionStage); } ); stages.forEach(stage -> SphereClientUtils.blockingWait(stage, 30, TimeUnit.SECONDS)); deleteProductType(client, productType); } while (client.executeBlocking(productsOfProductTypeQuery).getCount() > 0); } }
@Test public void createProductByProductTypeKeyIdentifiable() { withEmptyProductType(client(), productType -> { final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().build(); final ResourceIdentifiable<ProductType> productTypeResourceIdentifier = productType; final ProductDraft productDraft = ProductDraftBuilder.of(productTypeResourceIdentifier, randomSlug(), randomSlug(), masterVariant).build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); assertThat(product.getProductType()).isEqualTo(productType.toReference()); client().executeBlocking(ProductDeleteCommand.of(product)); }); }
@AfterClass @BeforeClass public static void deleteProductsAndProductType() { final List<ProductType> productTypes = client().executeBlocking(ProductTypeQuery.of().byName(PRODUCT_TYPE_NAME)).getResults(); if (!productTypes.isEmpty()) { final ProductQuery productQuery = ProductQuery.of() .withPredicates(m -> m.productType().isIn(productTypes)) .withLimit(500L); client().executeBlocking(productQuery).getResults().forEach(p -> client().executeBlocking(ProductDeleteCommand.of(p))); productTypes.forEach(p -> client().executeBlocking(ProductTypeDeleteCommand.of(p))); } }
public static void withUpdateableProductOfMultipleVariants(final BlockingSphereClient client, final Function<Product, Versioned<Product>> f) { withProductType(client, randomKey(), productType -> { final AttributeDraft greenColor = AttributeDraft.of(TShirtProductTypeDraftSupplier.Colors.ATTRIBUTE, TShirtProductTypeDraftSupplier.Colors.GREEN); final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of() .attributes(AttributeDraft.of(TShirtProductTypeDraftSupplier.Sizes.ATTRIBUTE, TShirtProductTypeDraftSupplier.Sizes.M), greenColor) .sku(randomKey()) .build(); final List<ProductVariantDraft> variants = Stream.of(TShirtProductTypeDraftSupplier.Sizes.S, TShirtProductTypeDraftSupplier.Sizes.X) .map(size -> AttributeDraft.of(TShirtProductTypeDraftSupplier.Sizes.ATTRIBUTE, size)) .map(attrDraft -> ProductVariantDraftBuilder.of() .attributes(attrDraft, greenColor) .sku(randomKey()) .build()) .collect(toList()); final ProductDraftBuilder builder = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), masterVariant) .variants(variants); final ProductDraft productDraft = builder.build(); final Product product = client.executeBlocking(ProductCreateCommand.of(productDraft)); Versioned<Product> productToDelete = f.apply(product); client.executeBlocking(ProductDeleteCommand.of(productToDelete)); }); }
@Test public void createProductWithCustomPrice() { withEmptyProductType(client(), randomKey(), productType -> withUpdateableType(client(), type -> { final String value = "foo"; final PriceDraft price = PriceDraft.of(EURO_1) .withCustom(CustomFieldsDraft.ofTypeIdAndObjects(type.getId(), singletonMap(STRING_FIELD_NAME, value))); final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().price(price).build(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), masterVariant).build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); final Price loadedPrice = product.getMasterData().getStaged().getMasterVariant().getPrices().get(0); assertThat(loadedPrice.getValue()).isEqualTo(EURO_1); assertThat(loadedPrice.getCustom().getFieldAsString(STRING_FIELD_NAME)).isEqualTo(value); client().executeBlocking(ProductDeleteCommand.of(product)); return type; }) ); }
public static void withProductWithProductReference(final BlockingSphereClient client, final BiConsumer<Product, Product> consumer) { withProduct(client, referencedProduct -> { final ProductType productType = productReferenceProductType(client); final ProductVariantDraft productVariantDraft = ProductVariantDraftBuilder.of().attributes(AttributeDraft.of("productreference", referencedProduct.toReference())).build(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, en("product reference name 1"), randomSlug(), productVariantDraft).build(); final Product product = client.executeBlocking(ProductCreateCommand.of(productDraft)); consumer.accept(product, referencedProduct); client.executeBlocking(ProductDeleteCommand.of(product)); }); }
@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 createProductWithTiers() { withEmptyProductType(client(), randomKey(), productType -> withUpdateableType(client(), type -> { final List<PriceTier> tiers = Arrays.asList(PriceTierBuilder.of(10, EURO_5).build()); final PriceDraft priceWithTiers = PriceDraftBuilder.of(EURO_1) .tiers(tiers) .build(); final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of() .price(priceWithTiers) .build(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), masterVariant).build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); final Price loadedPrice = product.getMasterData().getStaged().getMasterVariant().getPrices().get(0); assertThat(loadedPrice.getValue()).isEqualTo(EURO_1); assertThat(loadedPrice.getTiers()).containsExactlyElementsOf(tiers); client().executeBlocking(ProductDeleteCommand.of(product)); return type; }) ); }