Skip to content

Commit 9b9a78f

Browse files
author
Laurentiu Magureanu
authored
CIF-1696: Migrate add to cart code to new generic addProductsToCart mutation (#884)
1 parent b9003d9 commit 9b9a78f

File tree

32 files changed

+2737
-45
lines changed

32 files changed

+2737
-45
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ See the [AEM Content & Commerce documentation](https://experienceleague.adobe.co
2020

2121
- [Product v1](ui.apps/src/main/content/jcr_root/apps/core/cif/components/commerce/product/v1/product)
2222
- [Product v2](ui.apps/src/main/content/jcr_root/apps/core/cif/components/commerce/product/v2/product) - Adobe Commerce EE only with version >= 2.4.2
23+
- [Product v3](ui.apps/src/main/content/jcr_root/apps/core/cif/components/commerce/product/v3/product) - Adobe Commerce EE only with version >= 2.4.4
2324
- [Product List v1](ui.apps/src/main/content/jcr_root/apps/core/cif/components/commerce/productlist/v1/productlist)
2425
- [Product List v2](ui.apps/src/main/content/jcr_root/apps/core/cif/components/commerce/productlist/v2/productlist) - Adobe Commerce EE only with version >= 2.4.2
2526
- [Product Collection v1](ui.apps/src/main/content/jcr_root/apps/core/cif/components/commerce/productcollection/v1/productcollection)

bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public AbstractProductRetriever getProductRetriever() {
352352
}
353353

354354
/* --- Mapping methods --- */
355-
private Variant mapVariant(ConfigurableVariant variant) {
355+
protected Variant mapVariant(ConfigurableVariant variant) {
356356
SimpleProduct product = variant.getProduct();
357357

358358
VariantImpl productVariant = new VariantImpl();
@@ -408,15 +408,15 @@ private Asset mapAsset(MediaGalleryInterface entry) {
408408
return asset;
409409
}
410410

411-
private VariantValue mapVariantValue(ConfigurableProductOptionsValues value) {
411+
protected VariantValue mapVariantValue(ConfigurableProductOptionsValues value) {
412412
VariantValueImpl variantValue = new VariantValueImpl();
413413
variantValue.setId(value.getValueIndex());
414414
variantValue.setLabel(value.getLabel());
415415

416416
return variantValue;
417417
}
418418

419-
private VariantAttribute mapVariantAttribute(ConfigurableProductOptions option) {
419+
protected VariantAttribute mapVariantAttribute(ConfigurableProductOptions option) {
420420
// Get list of values
421421
List<VariantValue> values = option.getValues().parallelStream().map(this::mapVariantValue).collect(Collectors.toList());
422422

@@ -429,7 +429,7 @@ private VariantAttribute mapVariantAttribute(ConfigurableProductOptions option)
429429
return attribute;
430430
}
431431

432-
private String safeDescription(ProductInterface product) {
432+
protected String safeDescription(ProductInterface product) {
433433
ComplexTextValue description = product.getDescription();
434434
if (description == null) {
435435
return null;

bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/VariantImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class VariantImpl implements Variant {
4040

4141
private Map<String, Integer> variantAttributes = new HashMap<>();
4242

43+
private Map<String, String> variantAttributesUid = new HashMap<>();
44+
4345
private Price priceRange;
4446

4547
@Override
@@ -106,6 +108,11 @@ public Map<String, Integer> getVariantAttributes() {
106108
return variantAttributes;
107109
}
108110

111+
@Override
112+
public Map<String, String> getVariantAttributesUid() {
113+
return variantAttributesUid;
114+
}
115+
109116
public void setColor(Integer color) {
110117
this.color = color;
111118
}

bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/VariantValueImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class VariantValueImpl implements VariantValue {
2323

2424
private Integer id;
2525

26+
private String uid;
27+
2628
@Override
2729
public String getLabel() {
2830
return label;
@@ -40,4 +42,13 @@ public Integer getId() {
4042
public void setId(Integer id) {
4143
this.id = id;
4244
}
45+
46+
@Override
47+
public String getUid() {
48+
return uid;
49+
}
50+
51+
public void setUid(String uid) {
52+
this.uid = uid;
53+
}
4354
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2+
~ Copyright 2021 Adobe
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
16+
package com.adobe.cq.commerce.core.components.internal.models.v3.product;
17+
18+
import java.util.List;
19+
import java.util.stream.Collectors;
20+
21+
import javax.annotation.PostConstruct;
22+
23+
import org.apache.sling.api.SlingHttpServletRequest;
24+
import org.apache.sling.models.annotations.Model;
25+
26+
import com.adobe.cq.commerce.core.components.internal.models.v1.product.VariantAttributeImpl;
27+
import com.adobe.cq.commerce.core.components.internal.models.v1.product.VariantValueImpl;
28+
import com.adobe.cq.commerce.core.components.models.product.Product;
29+
import com.adobe.cq.commerce.core.components.models.product.Variant;
30+
import com.adobe.cq.commerce.core.components.models.product.VariantAttribute;
31+
import com.adobe.cq.commerce.core.components.models.product.VariantValue;
32+
import com.adobe.cq.commerce.magento.graphql.ConfigurableAttributeOption;
33+
import com.adobe.cq.commerce.magento.graphql.ConfigurableProductOptions;
34+
import com.adobe.cq.commerce.magento.graphql.ConfigurableProductOptionsValues;
35+
import com.adobe.cq.commerce.magento.graphql.ConfigurableVariant;
36+
37+
@Model(adaptables = SlingHttpServletRequest.class, adapters = Product.class, resourceType = ProductImpl.RESOURCE_TYPE)
38+
public class ProductImpl extends com.adobe.cq.commerce.core.components.internal.models.v2.product.ProductImpl
39+
implements Product {
40+
41+
public static final String RESOURCE_TYPE = "core/cif/components/commerce/product/v3/product";
42+
43+
@PostConstruct
44+
protected void initModel() {
45+
super.initModel();
46+
if (productRetriever != null) {
47+
productRetriever.extendProductQueryWith(p -> p.onConfigurableProduct(cp -> cp
48+
.configurableOptions(o -> o
49+
.values(v -> v.uid()))
50+
.variants(v -> v
51+
.attributes(a -> a.uid()))));
52+
}
53+
}
54+
55+
@Override
56+
protected VariantValue mapVariantValue(ConfigurableProductOptionsValues value) {
57+
VariantValueImpl variantValue = new VariantValueImpl();
58+
variantValue.setId(value.getValueIndex());
59+
variantValue.setUid(value.getUid().toString());
60+
variantValue.setLabel(value.getLabel());
61+
62+
return variantValue;
63+
}
64+
65+
@Override
66+
protected VariantAttribute mapVariantAttribute(ConfigurableProductOptions option) {
67+
// Get list of values
68+
List<VariantValue> values = option.getValues().parallelStream().map(this::mapVariantValue)
69+
.collect(Collectors.toList());
70+
71+
// Create attribute map
72+
VariantAttributeImpl attribute = new VariantAttributeImpl();
73+
attribute.setLabel(option.getLabel());
74+
attribute.setId(option.getAttributeCode());
75+
attribute.setValues(values);
76+
77+
return attribute;
78+
}
79+
80+
@Override
81+
protected Variant mapVariant(ConfigurableVariant variant) {
82+
Variant mappedVariant = super.mapVariant(variant);
83+
84+
// Map variant attributes
85+
for (ConfigurableAttributeOption option : variant.getAttributes()) {
86+
mappedVariant.getVariantAttributesUid().put(option.getCode(), option.getUid().toString());
87+
}
88+
89+
return mappedVariant;
90+
}
91+
}

bundles/core/src/main/java/com/adobe/cq/commerce/core/components/models/product/Variant.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1616
package com.adobe.cq.commerce.core.components.models.product;
1717

18+
import java.util.HashMap;
1819
import java.util.List;
1920
import java.util.Map;
2021

@@ -44,5 +45,9 @@ public interface Variant {
4445

4546
Map<String, Integer> getVariantAttributes();
4647

48+
default Map<String, String> getVariantAttributesUid() {
49+
return new HashMap<>();
50+
}
51+
4752
List<Asset> getAssets();
4853
}

bundles/core/src/main/java/com/adobe/cq/commerce/core/components/models/product/VariantValue.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ public interface VariantValue {
2626
String getLabel();
2727

2828
Integer getId();
29+
30+
default String getUid() {
31+
return null;
32+
}
2933
}

bundles/core/src/main/java/com/adobe/cq/commerce/core/components/models/product/package-info.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
~ See the License for the specific language governing permissions and
1414
~ limitations under the License.
1515
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
16-
@Version("5.3.0")
16+
@Version("5.4.0")
1717
package com.adobe.cq.commerce.core.components.models.product;
1818

19-
import org.osgi.annotation.versioning.Version;
19+
import org.osgi.annotation.versioning.Version;

bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ public class ProductImplTest {
113113

114114
private Resource productResource;
115115
private Resource pageResource;
116-
private ProductInterface product;
117116
private GraphqlClient graphqlClient;
118117

118+
protected ProductInterface product;
119119
protected Product productModel;
120120
protected CloseableHttpClient httpClient;
121121
protected Style style;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2+
~ Copyright 2022 Adobe
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
16+
package com.adobe.cq.commerce.core.components.internal.models.v3.product;
17+
18+
import java.io.IOException;
19+
import java.util.List;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import com.adobe.cq.commerce.core.components.models.product.Variant;
25+
import com.adobe.cq.commerce.core.components.models.product.VariantAttribute;
26+
import com.adobe.cq.commerce.core.components.models.product.VariantValue;
27+
import com.adobe.cq.commerce.core.testing.Utils;
28+
import com.fasterxml.jackson.databind.ObjectMapper;
29+
30+
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertNotNull;
32+
33+
public class ProductImplTest extends com.adobe.cq.commerce.core.components.internal.models.v2.product.ProductImplTest {
34+
35+
@Override
36+
protected void adaptToProduct() {
37+
// This ensures we re-run all the unit tests with version 3 of ProductImpl
38+
productModel = context.request().adaptTo(ProductImpl.class);
39+
}
40+
41+
@Before
42+
public void updateGraphQlResponse() throws IOException {
43+
Utils.setupHttpResponse("graphql/magento-graphql-product-result-uid-variants.json", httpClient, 200,
44+
"{products(filter:{url_key");
45+
Utils.setupHttpResponse("graphql/magento-graphql-product-result-uid-variants.json", httpClient, 200,
46+
"{products(filter:{sku");
47+
}
48+
49+
@Test
50+
public void testUidVariants() throws IOException {
51+
ObjectMapper mapper = new ObjectMapper();
52+
adaptToProduct();
53+
List<Variant> variants = productModel.getVariants();
54+
assertNotNull(variants);
55+
String jsonVariants = productModel.getVariantsJson();
56+
String expectedJsonVariants = Utils.getResource("results/result-product-variants-uid.json");
57+
assertEquals(mapper.readTree(expectedJsonVariants), mapper.readTree(jsonVariants));
58+
}
59+
60+
@Test
61+
public void testGetVariantAttributesUid() throws IOException {
62+
adaptToProduct();
63+
List<VariantAttribute> attributes = productModel.getVariantAttributes();
64+
65+
assertNotNull(attributes);
66+
67+
for (int i = 0; i < attributes.size(); i++) {
68+
VariantAttribute attribute = attributes.get(i);
69+
70+
for (int j = 0; j < attribute.getValues().size(); j++) {
71+
VariantValue value = attribute.getValues().get(j);
72+
assertNotNull(value.getUid());
73+
}
74+
}
75+
}
76+
77+
}

0 commit comments

Comments
 (0)