public ProductVariantDraftBuilder attributes(final AttributeDraft ... attributes) { return attributes(asList(attributes)); }
public ProductVariantDraftBuilder prices(final PriceDraft ... prices) { return prices(asList(prices)); }
/** * Creates a new builder with the values of this object. * * @return new builder */ public ProductVariantDraftBuilder newBuilder() { return new ProductVariantDraftBuilder(assets, attributes, images, key, prices, sku); }
private ProductVariantDraft variantDraft() { final AttributeDraft attributeDraft = AttributeDraft.of(ATTR_NAME, AttributeAccess.ofLocalizedEnumValueSet(), new HashSet<>(ALL_ATTRIBUTE_VALUES)); return ProductVariantDraftBuilder.of() .attributes(attributeDraft) .sku(randomKey()) .build(); }
@Test public void ofTemplateShouldCopyAllProperties() throws Exception { final ProductVariantDraftDsl template = ProductVariantDraftBuilder.of() .attributes(Collections.emptyList()) .assets(Collections.emptyList()) .key(randomString()) .sku(randomString()) .prices(Collections.emptyList()) .build(); final ProductVariantDraftDsl copy = ProductVariantDraftBuilder.of(template).build(); assertThat(copy).isEqualTo(template); }
@Test public void productAttribute() { checkFilter(builder -> { final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of(builder.getMasterVariant()) .attributes(AttributeDraft.of("stringfield", "hello")) .build(); return builder.masterVariant(masterVariant); }, m -> m.allVariants().attribute().ofString("stringfield")); }
@Test public void createsWithAllVariants() { final List<ProductVariantDraft> allVariants = IntStream.range(0, 9) .mapToObj(i -> ProductVariantDraftBuilder.of().sku("sku-" + i).build()) .collect(toList()); final Reference<ProductType> productType = ProductType.referenceOfId("product-type-id"); final ProductDraft productDraft = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), allVariants).build(); assertThat(productDraft.getMasterVariant().getSku()).isEqualTo("sku-0"); assertThat(productDraft.getVariants().get(3).getSku()).isEqualTo("sku-4"); }
@Test public void prices() { checkFilter(builder -> { final ProductVariantDraft oldMaster = builder.getMasterVariant(); final ProductVariantDraft masterWithPrices = ProductVariantDraftBuilder.of(oldMaster) .prices(singletonList(PriceDraft.of(EURO_1))) .build(); return builder.masterVariant(masterWithPrices); }, m -> m.allVariants().prices()); }
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)); }); }
public static void withProductHavingImages(final BlockingSphereClient client, final UnaryOperator<Product> op) { withUpdateableProduct(client, productDraftBuilder -> { final ProductVariantDraft masterVariant = productDraftBuilder.getMasterVariant(); final ProductVariantDraft variantDraft = ProductVariantDraftBuilder.of(masterVariant) .images(createExternalImage()) .sku(randomKey()) .build(); return productDraftBuilder.masterVariant(variantDraft); }, op); }
@Test public void invalidTypeCausesException() throws Exception { final ProductType productType = fetchProductTypeByName(); final ProductVariantDraft masterVariantDraft = ProductVariantDraftBuilder.of() .plusAttribute(COLOR_ATTR_NAME, 1)//1 is of illegal type and of illegal key .build(); final ProductDraft draft = ProductDraftBuilder .of(productType, en("basic shirt"), randomSlug(), masterVariantDraft) .build(); assertThatThrownBy(() -> client().executeBlocking(ProductCreateCommand.of(draft))) .isInstanceOf(ErrorResponseException.class) .matches(e -> ((ErrorResponseException)e).hasErrorCode(InvalidField.CODE)); }
public static void withProductHavingAssets(final BlockingSphereClient client, final UnaryOperator<Product> op) { withUpdateableProduct(client, productDraftBuilder -> { final ProductVariantDraft masterVariant = productDraftBuilder.getMasterVariant(); final ProductVariantDraft variantDraft = ProductVariantDraftBuilder.of(masterVariant) .assets(asList(getAssetDraft1(), getAssetDraft2())) .sku(randomKey()) .build(); return productDraftBuilder.masterVariant(variantDraft); }, op); } public static void withProductHavingImages(final BlockingSphereClient client, final UnaryOperator<Product> op) {
public static Product referenceableProduct(final BlockingSphereClient client) { final ProductType productType = ProductTypeFixtures.defaultProductType(client); final ProductVariantDraft variantDraft = ProductVariantDraftBuilder.of().price(PRICE).build(); final String slugEn = "referenceable-product-2"; final ProductDraft productDraft = ProductDraftBuilder.of(productType, en("referenceable product"), en(slugEn), variantDraft).build(); return client.executeBlocking(ProductQuery.of().bySlug(ProductProjectionType.STAGED, ENGLISH, slugEn)).head() .orElseGet(() -> client.executeBlocking(ProductCreateCommand.of(productDraft))); }
@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)); }); }
public ProductVariantDraftDsl withAttributes(@Nullable final List<AttributeDraft> attributes) { return newBuilder().attributes(attributes).build(); }
public <T> ProductVariantDraftBuilder plusAttribute(final String name, final T value) { return plusAttribute(AttributeDraft.of(name, value)); }