public interface AttributeDraft
final NamedAttributeAccess<MonetaryAmount> moneyAttribute = AttributeAccess.ofMoney().ofName(MONEY_ATTRIBUTE_NAME); final AttributeDraft moneyAttributeValue = AttributeDraft.of(moneyAttribute, EURO_10); final NamedAttributeAccess<LocalizedEnumValue> colorAttribute = Colors.ATTRIBUTE; final LocalizedEnumValue color = Colors.RED; final AttributeDraft colorAttributeValue = AttributeDraft.of(colorAttribute, color); final NamedAttributeAccess<EnumValue> sizeAttribute = Sizes.ATTRIBUTE; final AttributeDraft sizeValue = AttributeDraft.of(sizeAttribute, Sizes.M); withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().getStaged().getVariants()).isEmpty(); assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final PriceDraft price = PriceDraft.of(MoneyImpl.of(new BigDecimal("12.34"), EUR)).withCountry(DE); final List<PriceDraft> prices = asList(price); final List<AttributeDraft> attributeValues = asList(moneyAttributeValue, colorAttributeValue, sizeValue); final String sku = randomKey(); final String key = randomKey(); final Image image = Image.of("url", ImageDimensions.of(3, 5)); final AssetSource assetSource = AssetSourceBuilder.ofUri("https://commercetools.com/binaries/content/gallery/commercetoolswebsite/homepage/cases/rewe.jpg") .key("rewe-showcase") .contentType("image/jpg") .dimensionsOfWidthAndHeight(1934, 1115) .build(); final LocalizedString name = LocalizedString.ofEnglish("REWE show case"); final LocalizedString description = LocalizedString.ofEnglish("screenshot of the REWE webshop on a mobile and a notebook"); final AssetDraft assetDraft = AssetDraftBuilder.of(singletonList(assetSource), name) .description(description) .tags("desktop-sized", "jpg-format", "REWE", "awesome") .build(); product = client().executeBlocking(ProductUpdateCommand.of(product, AddAsset.ofSku(product.getMasterData().getStaged().getMasterVariant().getSku(), assetDraft))); final List<Asset> assets = product.getMasterData().getStaged().getMasterVariant().getAssets(); assertThat(assets).hasSize(1); final AddVariant updateAction = AddVariant.of(attributeValues, prices, sku) .withKey(key) .withImages(singletonList(image)) .withAssets(assets); final ProductUpdateCommand addVariantCommand = ProductUpdateCommand.of(product, updateAction); final Product productWithVariant = client().executeBlocking(addVariantCommand); final ProductVariant variant = productWithVariant.getMasterData().getStaged().getVariants().get(0); assertThat(productWithVariant.getMasterData().hasStagedChanges()).isTrue(); assertThat(variant.getId()).isEqualTo(2); assertThat(variant.findAttribute(moneyAttribute).get()).isEqualTo(EURO_10); assertThat(variant.findAttribute(colorAttribute).get()).isEqualTo(color); assertThat(variant.findAttribute(sizeAttribute).get()).isEqualTo(Sizes.M); assertThat(variant.getSku()).isEqualTo(sku); assertThat(variant.getKey()).isEqualTo(key); assertThat(variant.getImages()).containsExactly(image); assertThat(variant.getAssets().get(0).getKey()).isEqualTo(assets.get(0).getKey()); final Product productWithoutVariant = client().executeBlocking(ProductUpdateCommand.of(productWithVariant, RemoveVariant.of(variant))); assertThat(productWithoutVariant.getMasterData().getStaged().getVariants()).isEmpty(); final List<AssetDraft> assetDrafts = product.getMasterData().getStaged().getMasterVariant().getAssets().stream().map(a -> AssetDraftBuilder.of(a).build()).collect(toList()); assertThat(assets).hasSize(1); final AddVariant updateAction2 = AddVariant.of(attributeValues, prices, sku) .withKey(key) .withImages(singletonList(image)) .withAssetDrafts(assetDrafts); final ProductUpdateCommand addVariantCommand2 = ProductUpdateCommand.of(productWithoutVariant, updateAction2); final Product productWithVariant2 = client().executeBlocking(addVariantCommand2); final ProductVariant variant2 = productWithVariant2.getMasterData().getStaged().getVariants().get(0); assertThat(productWithVariant2.getMasterData().hasStagedChanges()).isTrue(); assertThat(variant2.getId()).isEqualTo(3); assertThat(variant2.findAttribute(moneyAttribute).get()).isEqualTo(EURO_10); assertThat(variant2.findAttribute(colorAttribute).get()).isEqualTo(color); assertThat(variant2.findAttribute(sizeAttribute).get()).isEqualTo(Sizes.M); assertThat(variant2.getSku()).isEqualTo(sku); assertThat(variant2.getKey()).isEqualTo(key); assertThat(variant2.getImages()).containsExactly(image); assertThat(variant2.getAssets().get(0).getKey()).isEqualTo(assets.get(0).getKey()); return productWithVariant2; });
See the test code.
AddVariant
,
ProductVariantDraft
Modifier and Type | Method and Description |
---|---|
String |
getName() |
com.fasterxml.jackson.databind.JsonNode |
getValue() |
static <T> AttributeDraft |
of(NamedAttributeAccess<T> namedAccess,
T value) |
static <T> AttributeDraft |
of(String name,
AttributeAccess<T> access,
T value) |
static AttributeDraft |
of(String name,
com.fasterxml.jackson.databind.JsonNode value) |
static <T> AttributeDraft |
of(String name,
T value) |
String getName()
com.fasterxml.jackson.databind.JsonNode getValue()
static <T> AttributeDraft of(String name, T value)
static AttributeDraft of(String name, com.fasterxml.jackson.databind.JsonNode value)
static <T> AttributeDraft of(String name, AttributeAccess<T> access, T value)
static <T> AttributeDraft of(NamedAttributeAccess<T> namedAccess, T value)