private static FieldDefinition localizedEnumFieldDefinition() { final List<LocalizedEnumValue> localizedEnumValues = asList("1", "2").stream() .map(s -> LocalizedEnumValue.of("key" + s, en("label " + s))) .collect(toList()); return fieldDefinition(LocalizedEnumFieldType.of(localizedEnumValues), LOCALIZED_ENUM_FIELD_NAME); }
@Test public void shouldSerializeLocalizedEnumValueCorrectly() { final LocalizedString localizedString = LocalizedString.of(Locale.GERMAN, "german-text"); final LocalizedEnumValue localizedEnumValue = LocalizedEnumValue.of("key", localizedString); final AttributeDraft attributeDraft = AttributeDraft.of(AttributeAccess.ofLocalizedEnumValue().ofName("name"), localizedEnumValue); final JsonNode jsonValue = attributeDraft.getValue(); assertThat(jsonValue.get("key").asText()).isEqualTo(localizedEnumValue.getKey()); final JsonNode jsonLabel = jsonValue.get("label"); assertThat(jsonLabel.get("de").asText()).isEqualTo(localizedEnumValue.getLabel().get(Locale.GERMAN)); } }
@Test public void execution() { final Project project = client().executeBlocking(ProjectGet.of()); final CartClassificationDraft cartClassification = CartClassificationDraftBuilder.of(Collections.singleton(LocalizedEnumValue.of("Small", LocalizedString.of(Locale.ENGLISH, "Small", Locale.GERMAN, "Klein")))).build(); final Project updatedProjectCartClassification = client().executeBlocking(ProjectUpdateCommand.of(project, SetShippingRateInputType.of(cartClassification))); assertThat(updatedProjectCartClassification.getShippingRateInputType()).isNotNull(); assertThat(updatedProjectCartClassification.getShippingRateInputType().getType()).isEqualTo("CartClassification"); assertThat(((CartClassification)updatedProjectCartClassification.getShippingRateInputType()).getValues()).containsOnly( LocalizedEnumValue.of("Small", LocalizedString.of(Locale.ENGLISH, "Small", Locale.GERMAN, "Klein")) ); } }
@Test public void changeLocalizedEnumValueLabel() throws Exception { final String attributeName = randomKey(); final LocalizedString label1 = LocalizedString.ofEnglish("label 1"); final LocalizedString label2 = LocalizedString.ofEnglish("label 2"); final LocalizedString newLabel2 = LocalizedString.ofEnglish("label 2 (updated)"); final AttributeDefinition attributeDefinition = AttributeDefinitionBuilder.of(attributeName, randomSlug(), LocalizedEnumAttributeType.of( LocalizedEnumValue.of("key1", label1), LocalizedEnumValue.of("key2", label2) )).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, ChangeLocalizedEnumValueLabel.of(attributeName, LocalizedEnumValue.of("key2", newLabel2)))); final LocalizedEnumAttributeType updatedAttributeType = (LocalizedEnumAttributeType) updatedProductType.getAttribute(attributeName).getAttributeType(); assertThat(updatedAttributeType.getValues()) .containsExactly(LocalizedEnumValue.of("key1", label1), LocalizedEnumValue.of("key2", newLabel2)); return updatedProductType; }); }
@Test public void addLocalizedEnumValue() throws Exception { withUpdateableProductType(client(), productType -> { final String attributeName = "color"; assertThat(productType.getAttribute(attributeName)).isNotNull(); final LocalizedEnumValue value = LocalizedEnumValue.of("brown", LocalizedString.of(Locale.ENGLISH, "brown").plus(GERMAN, "braun")); final ProductType updatedProductType = client().executeBlocking(ProductTypeUpdateCommand.of(productType, AddLocalizedEnumValue.of(attributeName, value))); assertThat(updatedProductType.getAttribute(attributeName).getAttributeType()) .isInstanceOf(LocalizedEnumAttributeType.class) .matches(type -> ((LocalizedEnumAttributeType) type).getValues().contains(value)); return updatedProductType; }); }
@Test public void changeEnumValueKey(){ withUpdateableProductType(client(), productType -> { final String attributeName = "color"; final LocalizedEnumAttributeType attributeType = (LocalizedEnumAttributeType) productType.getAttribute(attributeName) .getAttributeType(); assertThat(attributeType.getValues()).contains(LocalizedEnumValue.of("red", LocalizedString.of(ENGLISH,"red",GERMAN,"rot"))); final ProductType updatedProductType = client().executeBlocking(ProductTypeUpdateCommand.of(productType, ChangeEnumKey.of(attributeName, "red","rouge"))); final LocalizedEnumAttributeType updatedAttributeType = (LocalizedEnumAttributeType) updatedProductType.getAttribute(attributeName) .getAttributeType(); assertThat(updatedAttributeType.getValues()).contains(LocalizedEnumValue.of("rouge", LocalizedString.of(ENGLISH,"red",GERMAN,"rot"))); return updatedProductType; }); }
@Test public void addLocalizedEnumValue() { withUpdateableType(client(), type -> { final String name = TypeFixtures.LOCALIZED_ENUM_FIELD_NAME; final LocalizedEnumValue newLocalizedEnumValue = LocalizedEnumValue.of("key-new", en("label new")); final Type updatedType = client().executeBlocking(TypeUpdateCommand.of(type, AddLocalizedEnumValue.of(name, newLocalizedEnumValue))); assertThat(updatedType.getFieldDefinitionByName(name).getType()) .isInstanceOf(LocalizedEnumFieldType.class) .matches(fieldType -> ((LocalizedEnumFieldType) fieldType).getValues().contains(newLocalizedEnumValue), "contains the new enum value"); return updatedType; }); }
@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)); }
@Test public void constructWithAllValuesSet() { final LocalizedEnumValue yellow = LocalizedEnumValue.of("yellow", LocalizedString.of(ENGLISH, "yellow").plus(GERMAN, "gelb")); final ProductVariantImportDraft productVariantImportDraft = ProductVariantImportDraftBuilder.of("product-id", 1) .attributes(
set.add(LocalizedEnumValue.of("Small", LocalizedString.of(Locale.ENGLISH, "Small", Locale.GERMANY, "Klein"))); set.add(LocalizedEnumValue.of("Medium", LocalizedString.of(Locale.ENGLISH, "Medium", Locale.GERMANY, "Mittel"))); set.add(LocalizedEnumValue.of("Heavy", LocalizedString.of(Locale.ENGLISH, "Heavy", Locale.GERMANY, "Schwergut")));
LocalizedEnumValue.of("yellow", LocalizedString.of(ENGLISH, "yellow").plus(GERMAN, "gelb")); final ProductVariantImportDraft productVariantImportDraft = ProductVariantImportDraftBuilder.of(product.getId(), 1) .attributes(
@BeforeClass public static void createProductType() throws Exception { final LocalizedEnumValue green = LocalizedEnumValue.of("green", LocalizedString.of(ENGLISH, "green").plus(GERMAN, "grün")); final LocalizedEnumValue red = LocalizedEnumValue.of("red", LocalizedString.of(ENGLISH, "red").plus(GERMAN, "rot")); final AttributeDefinition color = AttributeDefinitionBuilder .build(); final LocalizedEnumValue cold = LocalizedEnumValue.of("cold", LocalizedString.of(ENGLISH, "Wash at or below 30°C ").plus(GERMAN, "30°C")); final LocalizedEnumValue hot = LocalizedEnumValue.of("hot", LocalizedString.of(ENGLISH, "Wash at or below 60°C ").plus(GERMAN, "60°C")); final LocalizedEnumValue tumbleDrying = LocalizedEnumValue.of("tumbleDrying", LocalizedString.of(ENGLISH, "tumble drying").plus(GERMAN, "Trommeltrocknen")); final LocalizedEnumValue noTumbleDrying = LocalizedEnumValue.of("noTumbleDrying", LocalizedString.of(ENGLISH, "no tumble drying").plus(GERMAN, "Nicht im Trommeltrockner trocknen")); final SetAttributeType laundryLabelType = SetAttributeType.of(LocalizedEnumAttributeType.of(cold, hot, tumbleDrying, noTumbleDrying));
assertThat(masterVariant.findAttribute(COLOR_ATTR_NAME, AttributeAccess.ofLocalizedEnumValue())) .overridingErrorMessage("on the get side, the while enum is delivered") .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")); final LocalizedEnumValue tumbleDrying = LocalizedEnumValue.of("tumbleDrying", LocalizedString.of(ENGLISH, "tumble drying").plus(GERMAN, "Trommeltrocknen")); assertThat(masterVariant.findAttribute(LAUNDRY_SYMBOLS_ATTR_NAME, AttributeAccess.ofLocalizedEnumValueSet()))
final NamedAttributeAccess<LocalDate> availableSince = AttributeAccess.ofDate().ofName(AVAILABLE_SINCE_ATTR_NAME); final LocalizedEnumValue cold = LocalizedEnumValue.of("cold", LocalizedString.of(ENGLISH, "Wash at or below 30°C ").plus(GERMAN, "30°C")); final LocalizedEnumValue tumbleDrying = LocalizedEnumValue.of("tumbleDrying", LocalizedString.of(ENGLISH, "tumble drying").plus(GERMAN, "Trommeltrocknen")); final Reference<Product> productReference = ProductFixtures.referenceableProduct(client()).toReference(); .plusAttribute(color, LocalizedEnumValue.of("green", LocalizedString.of(ENGLISH, "green").plus(GERMAN, "grün")))//will extract the key .plusAttribute(size, EnumValue.of("S", "S")) .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));