protected SphereRequest<Product> deleteCommand(final Product item) { return ProductDeleteCommand.of(item); }
public static DeleteCommand<Product> of(final Versioned<Product> versioned) { return new ProductDeleteCommand(versioned); } }
@Test public void byKey() { ProductTypeFixtures.withEmptyProductType(client(), productType -> { final String key = randomKey(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), ProductVariantDraftBuilder.of().build()).key(key).build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); final Product deletedProduct = client().executeBlocking(ProductDeleteCommand.ofKey(key, product.getVersion())); assertThat(client().executeBlocking(ProductByKeyGet.of(key))).isNull(); }); } }
@Test public void message() { ProductTypeFixtures.withEmptyProductType(client(), productType -> { final String key = randomKey(); final Image image = createExternalImage(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), ProductVariantDraftBuilder.of().images(image).build()).key(key).build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); final Product deletedProduct = client().executeBlocking(ProductDeleteCommand.ofKey(key, product.getVersion())); Query<ProductDeletedMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(deletedProduct)) .withSort(m -> m.createdAt().sort().desc()) .withExpansionPaths(m -> m.resource()) .withLimit(1L) .forMessageType(ProductDeletedMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<ProductDeletedMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final ProductDeletedMessage message = queryResult.head().get(); assertThat(message.getResource().getId()).isEqualTo(product.getId()); assertThat(message.getRemovedImageUrls()).containsExactly(image.getUrl()); }); }); } }
/** 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)); }); }
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)); }); }
@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))); } }
@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)); }); }