Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.cq.commerce.core.components.internal.models.v1.common;

import java.util.Locale;
import java.util.Map;

import javax.annotation.Nullable;
Expand All @@ -29,6 +30,9 @@
import com.adobe.cq.commerce.core.components.models.common.ProductListItem;
import com.adobe.cq.commerce.core.components.services.urls.UrlProvider;
import com.adobe.cq.commerce.core.components.services.urls.UrlProvider.ParamsBuilder;
import com.adobe.cq.commerce.magento.graphql.GroupedProduct;
import com.adobe.cq.commerce.magento.graphql.ProductImage;
import com.adobe.cq.commerce.magento.graphql.ProductInterface;
import com.adobe.cq.wcm.core.components.models.datalayer.ComponentData;
import com.adobe.cq.wcm.core.components.util.ComponentUtils;
import com.day.cq.wcm.api.Page;
Expand All @@ -50,6 +54,7 @@ public class ProductListItemImpl extends DataLayerListItem implements ProductLis
private UrlProvider urlProvider;
private CommerceIdentifier identifier;
private Boolean isStaged;
private ProductInterface product;

public ProductListItemImpl(String sku, String slug, String name, Price price, String imageURL, String imageAlt, Page productPage,
String activeVariantSku, SlingHttpServletRequest request, UrlProvider urlProvider, String parentId,
Expand All @@ -70,6 +75,29 @@ public ProductListItemImpl(String sku, String slug, String name, Price price, St
: CommerceIdentifierImpl.fromProductSku(sku);
}

public ProductListItemImpl(ProductInterface product, Page productPage, String activeVariantSku, SlingHttpServletRequest request,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the third constructor for this class. I think we should consider using another pattern for constructing these objects, maybe a "Builder"?

UrlProvider urlProvider, String parentId) {
super(parentId, productPage.getContentResource());
this.product = product;
this.sku = product.getSku();
this.slug = product.getUrlKey();
this.name = product.getName();
this.isStaged = product.getStaged();

ProductImage productImage = product.getSmallImage();
this.imageURL = productImage == null ? null : productImage.getUrl();
this.imageAlt = productImage == null ? null : StringUtils.defaultIfBlank(productImage.getLabel(), name);

this.productPage = productPage;
boolean isStartPrice = product instanceof GroupedProduct;
Locale locale = productPage == null ? Locale.getDefault() : productPage.getLanguage(false);
this.price = new PriceImpl(product.getPriceRange(), locale, isStartPrice);

this.activeVariantSku = activeVariantSku;
this.request = request;
this.urlProvider = urlProvider;
}

public ProductListItemImpl(CommerceIdentifier identifier, String parentId, Page productPage) {
super(parentId, productPage.getContentResource());
this.identifier = identifier;
Expand Down Expand Up @@ -187,4 +215,9 @@ public String getDataLayerCurrency() {
public CommerceIdentifier getCommerceIdentifier() {
return identifier;
}

@Override
public ProductInterface getProduct() {
return product;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.osgi.annotation.versioning.ConsumerType;

import com.adobe.cq.commerce.magento.graphql.ProductInterface;
import com.adobe.cq.wcm.core.components.models.ListItem;

@ConsumerType
Expand Down Expand Up @@ -77,4 +78,11 @@ public interface ProductListItem extends ListItem {
default Boolean isStaged() {
return false;
};

/**
* Returns the backend product using the GraphQL {@code ProductInterface}.
*
* @return The product.
*/
ProductInterface getProduct();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@Version("2.0.0")
@Version("2.1.0")
package com.adobe.cq.commerce.core.components.models.common;

import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.cq.commerce.core.search.internal.converters;

import java.util.Locale;
import java.util.function.Function;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -24,13 +23,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.commerce.core.components.internal.models.v1.common.PriceImpl;
import com.adobe.cq.commerce.core.components.internal.models.v1.common.ProductListItemImpl;
import com.adobe.cq.commerce.core.components.models.common.Price;
import com.adobe.cq.commerce.core.components.models.common.ProductListItem;
import com.adobe.cq.commerce.core.components.services.urls.UrlProvider;
import com.adobe.cq.commerce.magento.graphql.GroupedProduct;
import com.adobe.cq.commerce.magento.graphql.ProductImage;
import com.adobe.cq.commerce.magento.graphql.ProductInterface;
import com.adobe.cq.wcm.core.components.util.ComponentUtils;
import com.day.cq.wcm.api.Page;
Expand All @@ -44,7 +39,6 @@ public class ProductToProductListItemConverter implements Function<ProductInterf

private final Resource parentResource;
private final Page productPage;
private final Locale locale;
private final UrlProvider urlProvider;

private final SlingHttpServletRequest request;
Expand All @@ -53,35 +47,18 @@ public ProductToProductListItemConverter(final Page productPage, final SlingHttp
Resource parentResource) {
this.parentResource = parentResource;
this.productPage = productPage;
this.locale = productPage.getLanguage(false);
this.request = request;
this.urlProvider = urlProvider;
}

@Override
public ProductListItem apply(final ProductInterface product) {
try {
boolean isStartPrice = product instanceof GroupedProduct;
Price price = new PriceImpl(product.getPriceRange(), locale, isStartPrice);
final ProductImage smallImage = product.getSmallImage();

String resourceType = parentResource.getResourceType();
String prefix = StringUtils.substringAfterLast(resourceType, "/");
String path = parentResource.getPath();
String parentId = ComponentUtils.generateId(prefix, path);
String parentId = ComponentUtils.generateId(prefix, parentResource.getPath());

ProductListItem productListItem = new ProductListItemImpl(product.getSku(),
product.getUrlKey(),
product.getName(),
price,
smallImage == null ? null : smallImage.getUrl(),
smallImage == null ? null : smallImage.getLabel(),
productPage,
null, // search results aren't targeting specific variant
request,
urlProvider,
parentId,
product.getStaged());
ProductListItem productListItem = new ProductListItemImpl(product, productPage, null, request, urlProvider, parentId);

return productListItem;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,68 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import com.adobe.cq.commerce.core.components.models.common.CommerceIdentifier;
import com.adobe.cq.commerce.core.components.models.common.ProductListItem;
import com.adobe.cq.commerce.magento.graphql.CurrencyEnum;
import com.adobe.cq.commerce.magento.graphql.Money;
import com.adobe.cq.commerce.magento.graphql.PriceRange;
import com.adobe.cq.commerce.magento.graphql.ProductImage;
import com.adobe.cq.commerce.magento.graphql.ProductInterface;
import com.adobe.cq.wcm.core.components.models.datalayer.ComponentData;
import com.day.cq.wcm.api.Page;

import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ProductListItemTest {

final static String sku = "product-sku";
final static String name = "product-name";
final static String urlKey = "product-url_key";
final static String imageUrl = "http://www.image.com/image.jpg";
final static String imageAlt = "Some image";

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PriceRange priceRange;

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Money money;

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ProductImage image;

private Page productPage;
private ProductInterface product;

@Before
public void setUp() {
Resource contentResource = Mockito.mock(Resource.class);
productPage = Mockito.mock(Page.class);
Mockito.when(productPage.getContentResource()).thenReturn(contentResource);
when(productPage.getContentResource()).thenReturn(contentResource);

// setup test product
product = Mockito.mock(ProductInterface.class);
when(product.getSku()).thenReturn(sku);
when(product.getName()).thenReturn(name);
when(product.getUrlKey()).thenReturn(urlKey);
when(image.getLabel()).thenReturn(imageAlt);
when(image.getUrl()).thenReturn(imageUrl);
when(product.getSmallImage()).thenReturn(image);
when(money.getCurrency()).thenReturn(CurrencyEnum.USD);
when(money.getValue()).thenReturn(12.34);
when(priceRange.getMinimumPrice().getFinalPrice()).thenReturn(money);
when(priceRange.getMinimumPrice().getRegularPrice()).thenReturn(money);
when(product.getPriceRange()).thenReturn(priceRange);
}

@Test
public void testCreateProductListItem() {
String sku = "expected";
String urlKey = "expectedUrlKey";

CommerceIdentifier identifier = new CommerceIdentifierImpl(urlKey, CommerceIdentifier.IdentifierType.URL_KEY,
CommerceIdentifier.EntityType.PRODUCT);
ProductListItem productListItem = new ProductListItemImpl(identifier, "", productPage);
Expand All @@ -50,22 +89,31 @@ public void testCreateProductListItem() {

@Test
public void testCreateProductListItem2() {
String sku = "product-sku";
String name = "product-name";
String urlKey = "product-url_key";
String imageUrl = "http://www.image.com/image.jpg";
String imageAlt = "Some image";

ProductListItem productListItem = new ProductListItemImpl(sku, urlKey, name, null, imageUrl, imageAlt, productPage, null, null,
null, "1", false);

Assert.assertEquals(name, productListItem.getTitle());
Assert.assertEquals(sku, productListItem.getSKU());
Assert.assertEquals(urlKey, productListItem.getSlug());
Assert.assertEquals(imageUrl, productListItem.getImageURL());
Assert.assertEquals(imageAlt, productListItem.getImageAlt());
Assert.assertEquals(StringUtils.EMPTY, productListItem.getURL());
}

@Test
public void testCreateProductListItem3() {
ProductListItem productListItem = new ProductListItemImpl(product, productPage, null, null,
null, "1");

Assert.assertEquals(product.getSku(), productListItem.getSKU());
Assert.assertEquals(product.getName(), productListItem.getTitle());
Assert.assertEquals(product.getUrlKey(), productListItem.getSlug());
Assert.assertEquals(imageUrl, productListItem.getImageURL());
Assert.assertEquals(imageAlt, productListItem.getImageAlt());
Assert.assertTrue(productListItem.getId().indexOf("1") == 0);
Assert.assertEquals(product, productListItem.getProduct());
}

@Test
public void testGetComponentData() {
CommerceIdentifier identifier = new CommerceIdentifierImpl("none", CommerceIdentifier.IdentifierType.URL_KEY,
Expand Down