public static List<EnumValue> getValuesAsListOfEnumValue() { return EnumSet.allOf(CreditCardNetwork.class) .stream() .map(p -> EnumValue.of(p.name(), p.getLabel())) .collect(Collectors.toList()); } }
private static FieldDefinition stateFieldDefinition() { final List<EnumValue> values = asList( EnumValue.of("published", "the category is publicly visible"), EnumValue.of("draft", "the category should not be displayed in the frontend") ); final boolean required = false; final LocalizedString label = en("state of the category concerning to show it publicly"); final String fieldName = "state"; return FieldDefinition .of(EnumFieldType.of(values), fieldName, label, required); }
@Test public void queryByName() throws Exception { withTShirtProductType(type -> { final ProductType productType = client().executeBlocking(ProductTypeQuery.of().byName("t-shirt")).head().get(); final Optional<AttributeDefinition> sizeAttribute = productType.findAttribute("size"); final List<EnumValue> possibleSizeValues = sizeAttribute. map(attrib -> ((EnumAttributeType) attrib.getAttributeType()).getValues()). orElse(Collections.<EnumValue>emptyList()); final List<EnumValue> expected = asList(EnumValue.of("S", "S"), EnumValue.of("M", "M"), EnumValue.of("X", "X")); assertThat(possibleSizeValues).isEqualTo(expected); }); }
@Test public void changePlainEnumValueLabel() throws Exception { final String attributeName = randomKey(); final AttributeDefinition attributeDefinition = AttributeDefinitionBuilder.of(attributeName, randomSlug(), EnumAttributeType.of( EnumValue.of("key1", "label 1"), EnumValue.of("key2", "label 2") )).build(); final String key = randomKey(); final ProductTypeDraft productTypeDraft = ProductTypeDraft.of(key, key, key, singletonList(attributeDefinition)); withUpdateableProductType(client(), () -> productTypeDraft, productType -> { final ProductType updatedProductType = client().executeBlocking(ProductTypeUpdateCommand.of(productType, ChangePlainEnumValueLabel.of(attributeName, EnumValue.of("key2", "label 2 (updated)")))); final EnumAttributeType updatedAttributeType = (EnumAttributeType) updatedProductType.getAttribute(attributeName).getAttributeType(); assertThat(updatedAttributeType.getValues()) .containsExactly(EnumValue.of("key1", "label 1"), EnumValue.of("key2", "label 2 (updated)")); return updatedProductType; }); }
@Test public void execution() throws Exception { //size attribute stuff final EnumValue S = EnumValue.of("S", "S"); final EnumValue M = EnumValue.of("M", "M"); final EnumValue X = EnumValue.of("X", "X"); final List<EnumValue> values = asList(S, M, X); final LocalizedString sizeAttributeLabel = LocalizedString.of(ENGLISH, "size").plus(GERMAN, "Größe"); final AttributeDefinition sizeAttributeDefinition = AttributeDefinitionBuilder.of("size", sizeAttributeLabel, EnumAttributeType.of(values)) .required(true) .attributeConstraint(AttributeConstraint.COMBINATION_UNIQUE) .inputTip(LocalizedString.ofEnglish("size as enum")) .build(); final String name = getName(); final ProductTypeDraft productTypeDraft = ProductTypeDraft.of(randomKey(), name, "a 'T' shaped cloth", asList(sizeAttributeDefinition)); final ProductType productType = client().executeBlocking(ProductTypeCreateCommand.of(productTypeDraft)); assertThat(productType.getName()).isEqualTo(name); assertThat(productType.getDescription()).isEqualTo("a 'T' shaped cloth"); assertThat(productType.getAttributes()).contains(sizeAttributeDefinition); assertThat(productType.getAttributes()).hasSize(1); assertThat(productType.getAttributes().get(0).getInputTip()) .isEqualTo(LocalizedString.ofEnglish("size as enum")); }
@Test public void readAttributeWithoutProductTypeWithName() throws Exception { final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); final Optional<EnumValue> attributeOption = masterVariant.findAttribute(SIZE_ATTR_NAME, AttributeAccess.ofEnumValue()); assertThat(attributeOption).contains(EnumValue.of("S", "S")); }
@Test public void addEnumValue() { withUpdateableType(client(), type -> { final String name = TypeFixtures.ENUM_FIELD_NAME; final EnumValue newEnumValue = EnumValue.of("key-new", "label new"); final Type updatedType = client().executeBlocking(TypeUpdateCommand.of(type, AddEnumValue.of(name, newEnumValue))); assertThat(updatedType.getFieldDefinitionByName(name).getType()) .isInstanceOf(EnumFieldType.class) .matches(fieldType -> ((EnumFieldType) fieldType).getValues().contains(newEnumValue), "contains the new enum value"); return updatedType; }); }
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 readAttributeWithoutProductTypeWithNamedAccess() throws Exception { final NamedAttributeAccess<EnumValue> size = AttributeAccess.ofEnumValue().ofName(SIZE_ATTR_NAME); final ProductVariant masterVariant = createProduct().getMasterData().getStaged().getMasterVariant(); final Optional<EnumValue> attributeOption = masterVariant.findAttribute(size); assertThat(attributeOption).contains(EnumValue.of("S", "S")); }
@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(); }); }
@Test public void addEnumValue() throws Exception { withUpdateableProductType(client(), productType -> { final String attributeName = "size"; assertThat(productType.findAttribute(attributeName)).isPresent(); final EnumValue value = EnumValue.of("XXXL", "XXXL"); final ProductType updatedProductType = client().executeBlocking(ProductTypeUpdateCommand.of(productType, AddEnumValue.of(attributeName, value))); assertThat(updatedProductType.getAttribute(attributeName).getAttributeType()) .isInstanceOf(EnumAttributeType.class) .matches(type -> ((EnumAttributeType) type).getValues().contains(value)); return updatedProductType; }); }
@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 public void updateAttributes() throws Exception { final Product product = createProduct(); final int masterVariantId = 1; final Function<AttributeDraft, SetAttribute> draft = attrDraft -> SetAttribute.of(masterVariantId, attrDraft); final List<SetAttribute> updateActions = asList( draft.apply(AttributeDraft.of(COLOR_ATTR_NAME, "red")),//don't forget: enum like => use only keys draft.apply(AttributeDraft.of(SIZE_ATTR_NAME, "M")), draft.apply(AttributeDraft.of(LAUNDRY_SYMBOLS_ATTR_NAME, asSet("cold"))), draft.apply(AttributeDraft.of(RRP_ATTR_NAME, MoneyImpl.of(20, EUR))) ); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, updateActions)); final ProductVariant masterVariant = updatedProduct.getMasterData().getStaged().getMasterVariant(); assertThat(masterVariant.findAttribute(COLOR_ATTR_NAME, AttributeAccess.ofLocalizedEnumValue())) .contains(LocalizedEnumValue.of("red", LocalizedString.of(ENGLISH, "red").plus(GERMAN, "rot"))); assertThat(masterVariant.findAttribute(SIZE_ATTR_NAME, AttributeAccess.ofEnumValue())) .contains(EnumValue.of("M", "M")); final LocalizedEnumValue cold = LocalizedEnumValue.of("cold", LocalizedString.of(ENGLISH, "Wash at or below 30°C ").plus(GERMAN, "30°C")); assertThat(masterVariant.findAttribute(LAUNDRY_SYMBOLS_ATTR_NAME, AttributeAccess.ofLocalizedEnumValueSet())) .contains(asSet(cold)); assertThat(masterVariant.findAttribute(RRP_ATTR_NAME, AttributeAccess.ofMoney())) .contains(MoneyImpl.of(20, EUR)); }
.contains(LocalizedEnumValue.of("green", LocalizedString.of(ENGLISH, "green").plus(GERMAN, "grün"))); assertThat(masterVariant.findAttribute(SIZE_ATTR_NAME, AttributeAccess.ofEnumValue())) .contains(EnumValue.of("S", "S")); final LocalizedEnumValue cold = LocalizedEnumValue.of("cold", LocalizedString.of(ENGLISH, "Wash at or below 30°C ").plus(GERMAN, "30°C"));
.plusAttribute(size, EnumValue.of("S", "S")) assertThat(masterVariant.findAttribute(color)) .contains(LocalizedEnumValue.of("green", LocalizedString.of(ENGLISH, "green").plus(GERMAN, "grün"))); assertThat(masterVariant.findAttribute(size)).contains(EnumValue.of("S", "S")); assertThat(masterVariant.findAttribute(laundrySymbols)).contains(asSet(cold, tumbleDrying)); assertThat(masterVariant.findAttribute(matchingProducts)).contains(asSet(productReference));