static <T> AttributeDraft of(final String name, final T value) { final AttributeDraft result; if (value instanceof LocalizedEnumValue) { result = of(name, ((LocalizedEnumValue) value).getKey()); } else if (value instanceof EnumValue) { result = of(name, ((EnumValue) value).getKey()); } else if (value instanceof Set) { final Set<?> set = (Set<?>) value; if (!set.isEmpty()) { final Object setValue = set.stream().findAny().get(); if (setValue instanceof LocalizedEnumValue || setValue instanceof EnumValue) { //WithKey is a interface the enum like implement final Set<String> newValues = set.stream().map(x -> ((WithKey) x).getKey()).collect(toSet()); result = of(name, newValues); } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } return result; }
static <T> AttributeDraft of(final String name, final T value) { final AttributeDraft result; if (value instanceof LocalizedEnumValue) { result = of(name, ((LocalizedEnumValue) value).getKey()); } else if (value instanceof EnumValue) { result = of(name, ((EnumValue) value).getKey()); } else if (value instanceof Set) { final Set<?> set = (Set<?>) value; if (!set.isEmpty()) { final Object setValue = set.stream().findAny().get(); if (setValue instanceof LocalizedEnumValue || setValue instanceof EnumValue) { //WithKey is a interface the enum like implement final Set<String> newValues = set.stream().map(x -> ((WithKey) x).getKey()).collect(toSet()); result = of(name, newValues); } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } return result; }
public static <T> AttributeDraft of(final String name, final T value) { final AttributeDraft result; if (value instanceof LocalizedEnumValue) { result = of(name, ((LocalizedEnumValue) value).getKey()); } else if (value instanceof EnumValue) { result = of(name, ((EnumValue) value).getKey()); } else if (value instanceof Set) { final Set<?> set = (Set<?>) value; if (!set.isEmpty()) { final Object setValue = set.stream().findAny().get(); if (setValue instanceof LocalizedEnumValue || setValue instanceof EnumValue) { //WithKey is a interface the enum like implement final Set<String> newValues = set.stream().map(x -> ((WithKey) x).getKey()).collect(toSet()); result = of(name, newValues); } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } } else { result = of(name, SphereJsonUtils.toJsonNode(value)); } return result; }
@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 facetedSearchOnLocEnumKeySetAttributes() throws Exception { testResultWithTerms(PRODUCT_MODEL.allVariants().attribute().ofLocalizedEnumSet(ATTR_NAME_LOC_ENUM_SET).key().is(LOC_ENUM_THREE.getKey()), ids -> assertThat(ids).containsOnly(product1.getId()), termStats -> assertThat(termStats).containsExactly( TermStats.of(LOC_ENUM_TWO.getKey(), 2L), TermStats.of(LOC_ENUM_THREE.getKey(), 1L))); }
@Test public void facetedSearchOnLocEnumKeyAttributes() throws Exception { testResultWithTerms(PRODUCT_MODEL.allVariants().attribute().ofLocalizedEnum(ATTR_NAME_LOC_ENUM).key().is(LOC_ENUM_TWO.getKey()), ids -> assertThat(ids).containsOnly(product1.getId()), termStats -> assertThat(termStats).containsOnly( TermStats.of(LOC_ENUM_TWO.getKey(), 1L), TermStats.of(LOC_ENUM_THREE.getKey(), 1L))); }
@Test public void onLocEnumKeySetAttributes() throws Exception { testTermStats(FACET.allVariants().attribute().ofLocalizedEnumSet(ATTR_NAME_LOC_ENUM_SET).key().allTerms(), termStats -> assertThat(termStats).containsExactly( TermStats.of(LOC_ENUM_TWO.getKey(), 2L), TermStats.of(LOC_ENUM_THREE.getKey(), 1L))); }
@Test public void changeLocalizedEnumValueOrder() { withUpdateableType(client(), type -> { final String fieldName = TypeFixtures.LOCALIZED_ENUM_FIELD_NAME; final FieldType fieldType = type.getFieldDefinitionByName(fieldName).getType(); final List<LocalizedEnumValue> oldEnumValues = ((LocalizedEnumFieldType) fieldType).getValues(); final List<String> enumKeys = reverse(oldEnumValues .stream() .map(enumValue -> enumValue.getKey()) .collect(Collectors.toList()) ); final Type updatedType = client().executeBlocking(TypeUpdateCommand.of(type, ChangeLocalizedEnumValueOrder.of(fieldName, enumKeys))); final FieldType updatedFieldType = updatedType.getFieldDefinitionByName(fieldName).getType(); final List<LocalizedEnumValue> newEnumValues = ((LocalizedEnumFieldType) updatedFieldType).getValues(); assertThat(newEnumValues).containsAll(oldEnumValues); assertThat(newEnumValues.size()).isEqualTo(oldEnumValues.size()); return updatedType; }); }
@Test public void onLocEnumKeyAttributes() throws Exception { testProductIds(PRODUCT_MODEL.allVariants().attribute().ofLocalizedEnum(ATTR_NAME_LOC_ENUM).key().is(LOC_ENUM_TWO.getKey()), ids -> assertThat(ids).containsOnly(product1.getId())); }
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 onLocEnumKeySetAttributes() throws Exception { testProductIds(PRODUCT_MODEL.allVariants().attribute().ofLocalizedEnumSet(ATTR_NAME_LOC_ENUM_SET).key().is(LOC_ENUM_THREE.getKey()), ids -> assertThat(ids).containsOnly(product1.getId())); }
@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(); }); }