private Attribute attribute(final String attributeName) { return masterVariant.getAttribute(attributeName); } }
protected List<String> attributeCombination(final String attributeKey, final Attribute fixedAttribute, final ProductProjection product) { return product.getAllVariants().stream() .filter(variant -> variant.getAttribute(attributeKey) != null) .filter(variant -> { final Attribute variantAttribute = variant.getAttribute(fixedAttribute.getName()); return variantAttribute != null && variantAttribute.equals(fixedAttribute); }) .map(variant -> attributeValue(variant.getAttribute(attributeKey), userContext.locales(), productDataConfig.getMetaProductType())) .distinct() .collect(toList()); } }
protected void fillAttributeCombinations(final SelectableProductAttributeBean bean, final Attribute attribute, final ProductProjection product) { final List<FormSelectableOptionBean> formOption = new ArrayList<>(); final Map<String, Map<String, List<String>>> selectableData = new HashMap<>(); product.getAllVariants().stream() .map(variant -> variant.getAttribute(attribute.getName())) .filter(attrOption -> attrOption != null) .distinct() .forEach(attrOption -> { final String attrOptionValue = attributeValue(attrOption, userContext.locales(), productDataConfig.getMetaProductType()); formOption.add(createFormOption(attribute, attrOption, attrOptionValue)); selectableData.put(attrOptionValue, createAllowedAttributeCombinations(attrOption, product)); }); bean.setList(formOption); bean.setSelectData(selectableData); }
@Test public void mapMatchingAttributeAbsent() throws Exception { final Attribute attr = variant.getAttribute(BOOLEAN_ATTRIBUTE); assertThat(map(attr)).isEqualTo("<no mapping found>"); }
@Test public void mapMatchingAttributeWithIfGuarded() throws Exception { final Attribute attr = variant.getAttribute(STRING_ATTRIBUTE); assertThat(map(attr)).isEqualTo("val-string-en"); }
@Test public void mapMatchingAttributeWithIfIs() throws Exception { final Attribute attr = variant.getAttribute(LOC_STRING_ATTRIBUTE); assertThat(map(attr)).isEqualTo("val-loc-string-de"); }
private Attribute attribute(final String attributeName) { return product.getMasterData().getStaged().getMasterVariant().getAttribute(attributeName); } }
final List<EnumValue> allVariantsSizeValues = productProjection.getAllVariants() .stream() .map(variant -> variant.getAttribute(sizeAttributeName).getValueAsEnumValue()) .collect(Collectors.toList()); assertThat(allVariantsSizeValues) .as("third variant is now the master variant") .isEqualTo(productProjection.getAllVariants().get(2)); assertThat(newMasterVariant.getAttribute(sizeAttributeName).getValueAsEnumValue()) .isEqualTo(Sizes.X); final List<EnumValue> reorderedVariantsSizeValues = productProjectionWithNewMasterVariant.getAllVariants() .stream() .map(variant -> variant.getAttribute(sizeAttributeName).getValueAsEnumValue()) .collect(Collectors.toList()); assertThat(reorderedVariantsSizeValues).containsExactly(Sizes.X, Sizes.S, Sizes.M);
public void setAttributeWithObjectsWithStaged(final Boolean staged) { withProduct(client(), (Product product) -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, asList( SetAttribute.ofSku(sku, "size", "M", staged), SetAttribute.ofVariantId(MASTER_VARIANT_ID, "color", "red", staged) ))); final ProductVariant masterVariant = updatedProduct.getMasterData().getStaged().getMasterVariant(); assertThat(masterVariant.getAttribute("size").getValueAsEnumValue()).isEqualTo(EnumValue.of("M", "M")); assertThat(masterVariant.getAttribute("color").getValueAsLocalizedEnumValue().getKey()).isEqualTo("red"); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); }); }
@Test public void setAttributeWithObjects() { withProduct(client(), (Product product) -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, asList( SetAttribute.ofSku(sku, "size", "M"), SetAttribute.ofVariantId(MASTER_VARIANT_ID, "color", "red") ))); final ProductVariant masterVariant = updatedProduct.getMasterData().getStaged().getMasterVariant(); assertThat(masterVariant.getAttribute("size").getValueAsEnumValue()).isEqualTo(EnumValue.of("M", "M")); assertThat(masterVariant.getAttribute("color").getValueAsLocalizedEnumValue().getKey()).isEqualTo("red"); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isTrue(); }); }
assertThat(lineItem.getVariant().getId()).isEqualTo(1); assertThat(lineItem.getVariant().getPrices().get(0).getValue()).isEqualTo(EURO_20); assertThat(lineItem.getVariant().getAttribute("foo").getValue(AttributeAccess.ofString())).isEqualTo("bar"); assertThat(lineItem.getTaxRate().getName()).isEqualTo("default-tax"); assertThat(lineItem.getTaxRate().isIncludedInPrice()).isTrue();
assertThat(variantWithAttribute2.isMatchingVariant()).isFalse(); final ProductVariant positiveVariant = productProjection.getVariants().get(1); assertThat(positiveVariant.getAttribute(attributeName).getValueAsString()).isEqualTo("3"); assertThat(positiveVariant.isMatchingVariant()).isTrue(); }); assertThat(productProjectionOptional.get().getVariants().get(0).isMatchingVariant()).isNull(); final ProductVariant positiveVariant = productProjectionOptional.get().getVariants().get(1); assertThat(positiveVariant.getAttribute(attributeName).getValueAsString()).isEqualTo("3"); assertThat(positiveVariant.isMatchingVariant()).isNull(); });
final Boolean found = AttributeExtraction.<Boolean>of(fetchedAttributeDefinition, product.getMasterData().getStaged().getMasterVariant().getAttribute(attributeName)) .ifIs(access, x -> true) .findValue().orElse(false);
@Test public void readAttributeWithoutProductTypeWithJson() throws Exception { final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); final Attribute attr = masterVariant.getAttribute(SIZE_ATTR_NAME); final JsonNode expectedJsonNode = SphereJsonUtils.toJsonNode(EnumValue.of("S", "S")); assertThat(attr.getValue(AttributeAccess.ofJsonNode())).isEqualTo(expectedJsonNode); }
@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); }