/** * Action to remove a custom attribute. * * @param NamedAttributeAccess object containing the name of the attribute * @param <T> type of the attribute * @return update action */ public static <T> SetAttributeInAllVariants ofUnsetAttribute(final NamedAttributeAccess<T> NamedAttributeAccess) { return ofUnsetAttribute(NamedAttributeAccess, null); }
/** * Action to remove a custom attribute. * * @param name the name of the attribute, consult the product type to find the name * @return update action */ public static SetAttributeInAllVariants ofUnsetAttribute(final String name) { return ofUnsetAttribute(name, null); }
/** * Action to remove a custom attribute. * * @param name the name of the attribute, consult the product type to find the name * @return update action */ public static SetAttributeInAllVariants ofUnsetAttribute(final String name) { return ofUnsetAttribute(name, null); }
/** * Action to remove a custom attribute. * * @param NamedAttributeAccess object containing the name of the attribute * @param <T> type of the attribute * @return update action */ public static <T> SetAttributeInAllVariants ofUnsetAttribute(final NamedAttributeAccess<T> NamedAttributeAccess) { return ofUnsetAttribute(NamedAttributeAccess, null); }
@Test @SuppressWarnings({"varargs", "unchecked"}) public void ofUnsetAttribute() { assertThat(product.getMasterData().getStaged().getAllVariants()) .extracting(variant -> variant.getAttribute(ATTR_NAME).getValueAsLocalizedEnumValueSet()) .as("product attributes present") .contains(new HashSet<>(ALL_ATTRIBUTE_VALUES)); final UpdateAction<Product> action = SetAttributeInAllVariants.ofUnsetAttribute("ProductFlags"); product = client().executeBlocking(ProductUpdateCommand.of(product, action)); assertThat(product.getMasterData().getStaged().getAllVariants()) .extracting(variant -> variant.hasAttribute(ATTR_NAME)) .as("attribute is deleted") .containsExactly(false, false); }
@Test public void setAttributeInAllVariants() throws Exception { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); //the setter contains the name and a JSON mapper, declare it only one time in your project per attribute //example for MonetaryAmount attribute final String moneyAttributeName = MONEY_ATTRIBUTE_NAME; final NamedAttributeAccess<MonetaryAmount> moneyAttribute = AttributeAccess.ofMoney().ofName(moneyAttributeName); final MonetaryAmount newValueForMoney = EURO_10; //example for LocalizedEnumValue attribute final NamedAttributeAccess<LocalizedEnumValue> colorAttribute = Colors.ATTRIBUTE; final LocalizedEnumValue oldValueForColor = Colors.GREEN; final LocalizedEnumValue newValueForColor = Colors.RED; assertThat(product.getMasterData().getStaged().getMasterVariant().findAttribute(moneyAttribute)).isEmpty(); assertThat(product.getMasterData().getStaged().getMasterVariant().findAttribute(colorAttribute)).contains(oldValueForColor); final SetAttributeInAllVariants moneyUpdate = SetAttributeInAllVariants.of(moneyAttribute, newValueForMoney); final SetAttributeInAllVariants localizedEnumUpdate = SetAttributeInAllVariants.of(colorAttribute, newValueForColor); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, asList(moneyUpdate, localizedEnumUpdate))); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().findAttribute(moneyAttribute)).contains(newValueForMoney); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().findAttribute(colorAttribute)).contains(newValueForColor); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isTrue(); final SetAttributeInAllVariants unsetAction = SetAttributeInAllVariants.ofUnsetAttribute(moneyAttribute); final Product productWithoutMoney = client().executeBlocking(ProductUpdateCommand.of(updatedProduct, unsetAction)); assertThat(productWithoutMoney.getMasterData().getStaged().getMasterVariant().findAttribute(moneyAttribute)).isEmpty(); return productWithoutMoney; }); }