From a39f1dfb65b5af98f5fa09e400ef91fd70d98cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=B6ster?= Date: Wed, 25 Oct 2017 10:07:10 +0200 Subject: [PATCH] Fix encoding of selectable attributes. (#657) * Fix encoding of selectable attributes. * Improve unit test. * Improve unit test. --- ...tableProductAttributeViewModelFactory.java | 2 +- ...eProductAttributeViewModelFactoryTest.java | 94 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 common/test/com/commercetools/sunrise/framework/viewmodels/content/products/SelectableProductAttributeViewModelFactoryTest.java diff --git a/common/app/com/commercetools/sunrise/framework/viewmodels/content/products/SelectableProductAttributeViewModelFactory.java b/common/app/com/commercetools/sunrise/framework/viewmodels/content/products/SelectableProductAttributeViewModelFactory.java index bafd3e651..74a0332e6 100644 --- a/common/app/com/commercetools/sunrise/framework/viewmodels/content/products/SelectableProductAttributeViewModelFactory.java +++ b/common/app/com/commercetools/sunrise/framework/viewmodels/content/products/SelectableProductAttributeViewModelFactory.java @@ -128,7 +128,7 @@ private List attributeCombination(final String attributeKey, final Attri }) .map(variant -> variant.getAttribute(attributeKey)) .filter(Objects::nonNull) - .map(attribute -> attributeFormatter.value(AttributeWithProductType.of(attribute, fixedAttribute.getProductTypeRef()))) + .map(attribute -> attributeFormatter.encodedValue(AttributeWithProductType.of(attribute, fixedAttribute.getProductTypeRef()))) .distinct() .collect(toList()); } diff --git a/common/test/com/commercetools/sunrise/framework/viewmodels/content/products/SelectableProductAttributeViewModelFactoryTest.java b/common/test/com/commercetools/sunrise/framework/viewmodels/content/products/SelectableProductAttributeViewModelFactoryTest.java new file mode 100644 index 000000000..5a7e983cb --- /dev/null +++ b/common/test/com/commercetools/sunrise/framework/viewmodels/content/products/SelectableProductAttributeViewModelFactoryTest.java @@ -0,0 +1,94 @@ +package com.commercetools.sunrise.framework.viewmodels.content.products; + +import com.commercetools.sunrise.ctp.products.ProductAttributesSettings; +import com.commercetools.sunrise.framework.viewmodels.formatters.AttributeFormatter; +import io.sphere.sdk.models.Reference; +import io.sphere.sdk.products.ProductVariant; +import io.sphere.sdk.products.attributes.Attribute; +import io.sphere.sdk.products.attributes.AttributeAccess; +import io.sphere.sdk.producttypes.ProductType; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static java.util.Collections.singletonList; +import static java.util.Collections.singletonMap; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Unit tests for {@link SelectableProductAttributeViewModelFactory}. + */ +@RunWith(MockitoJUnitRunner.class) +public class SelectableProductAttributeViewModelFactoryTest { + private static final String STRING_ATTRIBUTE_NAME = "stringAttribute"; + private static final String NUMBER_ATTRIBUTE_NAME = "numberAttribute"; + private static final String PRODUCT_TYPE_ID = "productTypeId"; + + @Mock + private ProductAttributesSettings fakeProductAttributeSettings; + @Mock + private AttributeFormatter fakeAttributeFormatter; + @Mock + private ProductAttributeFormSelectableOptionViewModelFactory productAttributeFormSelectableOptionViewModelFactory; + @Mock + private Reference fakeProductTypeRef; + + @InjectMocks + private SelectableProductAttributeViewModelFactory viewModelFactory; + + @Test + public void createFromSelectableStringAttributeAndSelectableNumberAttribute() { + final String stringValue = "v1"; + final AttributeWithProductType stringAttributeWithProductType = attributeWithProductType(STRING_ATTRIBUTE_NAME, AttributeAccess.ofString(), stringValue); + when(fakeAttributeFormatter.encodedValue(eq(stringAttributeWithProductType))).thenReturn(stringValue); + + final AttributeWithProductType numberAttributeWithProductType = attributeWithProductType(NUMBER_ATTRIBUTE_NAME, AttributeAccess.ofDouble(), 12.0); + when(fakeAttributeFormatter.encodedValue(eq(numberAttributeWithProductType))).thenReturn("120"); + + mockProductAttributeSettings(STRING_ATTRIBUTE_NAME, NUMBER_ATTRIBUTE_NAME); + final ProductVariant productVariant = mockProductVariant(stringAttributeWithProductType, numberAttributeWithProductType); + final List variants = Collections.singletonList(productVariant); + + final SelectableProductAttributeViewModel attributeViewModel = viewModelFactory.create(variants, stringAttributeWithProductType); + + assertThat(attributeViewModel.getSelectData()) + .hasSize(1) + .containsEntry(stringValue, singletonMap(NUMBER_ATTRIBUTE_NAME, singletonList("120"))); + } + + private AttributeWithProductType attributeWithProductType(final String name, final AttributeAccess attributeAccess, final T value) { + final Attribute attribute = Attribute.of(name, attributeAccess, value); + final AttributeWithProductType attributeWithProductType = AttributeWithProductType.of(attribute, fakeProductTypeRef); + return attributeWithProductType; + } + + private void mockProductAttributeSettings(final String selectablePrimaryAttribute, final String selectableSecondaryAttribute) { + when(fakeProductAttributeSettings.primarySelectable()).thenReturn(Arrays.asList(selectablePrimaryAttribute)); + when(fakeProductAttributeSettings.selectable()).thenReturn(Arrays.asList(selectablePrimaryAttribute, selectableSecondaryAttribute)); + } + + private ProductVariant mockProductVariant(final AttributeWithProductType... attributeWithProductTypes) { + final ProductVariant productVariant = mock(ProductVariant.class); + + final List attributes = Stream.of(attributeWithProductTypes) + .map(AttributeWithProductType::getAttribute) + .collect(Collectors.toList()); + + for (final Attribute attribute : attributes) { + when(productVariant.getAttribute(attribute.getName())).thenReturn(attribute); + } + + return productVariant; + } +}