Skip to content

Commit 3618811

Browse files
committed
DEVX-640: updating test with json example + fixing nullPointerExceptions
1 parent c0a4a48 commit 3618811

File tree

7 files changed

+236
-41
lines changed

7 files changed

+236
-41
lines changed

commercetools/commercetools-importapi-utils/src/main/java/com/commercetools/sdk/ProductUtil.java

Lines changed: 83 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
import com.commercetools.api.models.common.CentPrecisionMoney;
44
import com.commercetools.api.models.common.LocalizedString;
55
import com.commercetools.api.models.common.Money;
6+
import com.commercetools.api.models.product.ProductPriceModeEnum;
67
import com.commercetools.api.models.product.ProductProjection;
78
import com.commercetools.api.models.product.ProductVariant;
89
import com.commercetools.api.models.product_type.*;
10+
import com.commercetools.api.models.state.State;
11+
import com.commercetools.api.models.state.StateReference;
12+
import com.commercetools.api.models.tax_category.TaxCategory;
13+
import com.commercetools.api.models.tax_category.TaxCategoryReference;
914
import com.commercetools.importapi.models.common.*;
1015
import com.commercetools.importapi.models.productdrafts.PriceDraftImport;
1116
import com.commercetools.importapi.models.productdrafts.ProductDraftImport;
@@ -17,58 +22,109 @@
1722
import java.time.LocalDate;
1823
import java.time.ZonedDateTime;
1924
import java.util.*;
25+
import java.util.function.Function;
2026
import java.util.stream.Collectors;
2127

2228
public final class ProductUtil {
2329

2430
public static ProductDraftImport toProductDraftImport(ProductProjection product) {
2531
var draft = ProductDraftImport.builder().key(product.getKey())
26-
.productType(p -> p.key(Optional.ofNullable(product.getProductType())
27-
.map( ProductTypeReference::getObj).map(ProductType::getKey).orElse(null)))
28-
.name(l -> l.values(product.getName().values()))
29-
.slug(l -> l.values(product.getSlug().values()))
30-
.description(d -> d.values(
31-
Optional.ofNullable(product.getDescription()).map(LocalizedString::values).orElse(null))) // if not null
32+
.productType(p -> p.key(product.getProductType().getObj().getKey()))
33+
.name(l -> getLocalizedStringBuilder(product.getName()))
34+
.slug(l -> getLocalizedStringBuilder(product.getSlug()))
35+
.description(
36+
Optional.ofNullable(product.getDescription()).map(ProductUtil::getLocalizedStringBuilder).map(
37+
LocalizedStringBuilder::build).orElse(null))
3238
.categories(extractCategoryKeyReference(product))
33-
.metaTitle(t -> t.values(
34-
Optional.ofNullable(product.getMetaTitle()).map(LocalizedString::values).orElse(null)))
35-
.metaDescription((com.commercetools.importapi.models.common.LocalizedString)product.getMetaDescription()) // if not null
36-
.metaKeywords((com.commercetools.importapi.models.common.LocalizedString) product.getMetaKeywords()) // if not null
39+
.metaTitle(
40+
Optional.ofNullable(product.getMetaTitle()).map(ProductUtil::getLocalizedStringBuilder).map(
41+
LocalizedStringBuilder::build).orElse(null))
42+
.metaDescription((com.commercetools.importapi.models.common.LocalizedString)product.getMetaDescription())
43+
.metaKeywords((com.commercetools.importapi.models.common.LocalizedString) product.getMetaKeywords())
3744
.masterVariant(extractProductVariantDraftImport(product.getMasterVariant()))
3845
.variants(extractProductVariantDraftImport(product))
39-
.taxCategory(t -> t.key(product.getTaxCategory().getObj().getKey()))
40-
.state(s -> s.key(product.getState().getObj().getKey()))
41-
//.priceMode(product.getPriceMode())
46+
.taxCategory(getTaxCategoryKeyReference(product))
47+
.state(getStateKeyReference(product))
48+
.priceMode(mapPriceModeToImportApi(product))
4249
.attributes(product.getAttributes().stream().map(ProductUtil::mapAttribute).collect(Collectors.toList()));
4350
return draft.build();
4451
}
4552

53+
private static LocalizedStringBuilder getLocalizedStringBuilder(
54+
LocalizedString s) {
55+
return com.commercetools.importapi.models.common.LocalizedString.builder().values(s.values());
56+
}
57+
58+
private static com.commercetools.importapi.models.common.ProductPriceModeEnum mapPriceModeToImportApi(ProductProjection product) {
59+
if (product.getPriceMode() == null) {
60+
return null;
61+
}if (product.getPriceMode().equals(ProductPriceModeEnum.ProductPriceModeEnumEnum.EMBEDDED)) {
62+
return com.commercetools.importapi.models.common.ProductPriceModeEnum.ProductPriceModeEnumEnum.EMBEDDED;
63+
}
64+
return com.commercetools.importapi.models.common.ProductPriceModeEnum.ProductPriceModeEnumEnum.STANDALONE;
65+
}
66+
67+
private static StateKeyReference getStateKeyReference(
68+
ProductProjection product) {
69+
var key = Optional.ofNullable(product.getState()).map(StateReference::getObj).map(State::getKey).orElse(null);
70+
if (key != null) {
71+
return StateKeyReference.builder().key(key).build();
72+
}
73+
return null;
74+
}
75+
76+
private static TaxCategoryKeyReference getTaxCategoryKeyReference(
77+
ProductProjection product) {
78+
var key = Optional.ofNullable(product.getTaxCategory())
79+
.map(TaxCategoryReference::getObj)
80+
.map(TaxCategory::getKey)
81+
.orElse(null);
82+
if (key != null) {
83+
return TaxCategoryKeyReference.builder().key(key).build();
84+
}
85+
return null;
86+
}
87+
4688
private static List<ProductVariantDraftImport> extractProductVariantDraftImport(ProductProjection product) {
4789
return product.getVariants().stream().map(ProductUtil::extractProductVariantDraftImport).collect(Collectors.toList());
4890
}
4991

5092
private static ProductVariantDraftImport extractProductVariantDraftImport(ProductVariant variant) {
5193
return ProductVariantDraftImport.builder().key(variant.getKey()).sku(variant.getSku())
52-
.setImages(i -> (Image) variant.getImages())
53-
.prices(variant.getPrices().stream().map(p -> PriceDraftImport.builder().key(p.getKey()).value(v ->
54-
(p.getValue() instanceof CentPrecisionMoney) ?
55-
v.centPrecisionBuilder().centAmount(p.getValue().getCentAmount()).currencyCode(p.getValue()
56-
.getCurrencyCode()).fractionDigits(p.getValue().getFractionDigits()) :
57-
v.highPrecisionBuilder().centAmount(p.getValue().getCentAmount()).currencyCode(p.getValue()
58-
.getCurrencyCode()).fractionDigits(p.getValue().getFractionDigits())
59-
).build()).collect(Collectors.toList()))
94+
.images(variant.getImages().stream().map(i -> com.commercetools.importapi.models.common.Image.builder()
95+
.dimensions(d ->
96+
com.commercetools.importapi.models.common.AssetDimensions.builder().w(i.getDimensions().getW()).h(i.getDimensions().getH()))
97+
.url(i.getUrl())
98+
.label(i.getLabel())
99+
.build()).collect(Collectors.toList()))
100+
.prices(mapPricesToImportApi(variant))
60101
.attributes(variant.getAttributes().stream().map(
61102
ProductUtil::mapAttribute).collect(Collectors.toList()))
62-
.assets(importAssets(variant.getAssets())).build();
103+
.assets(importAssets(variant.getAssets()))
104+
.build();
105+
}
106+
107+
private static List<PriceDraftImport> mapPricesToImportApi (ProductVariant variant){
108+
return variant.getPrices().stream().map(p -> PriceDraftImport.builder().key(p.getKey()).value(v ->
109+
(p.getValue() instanceof CentPrecisionMoney) ?
110+
v.centPrecisionBuilder().centAmount(p.getValue().getCentAmount()).currencyCode(p.getValue()
111+
.getCurrencyCode()).fractionDigits(p.getValue().getFractionDigits()) :
112+
v.highPrecisionBuilder().centAmount(p.getValue().getCentAmount()).currencyCode(p.getValue()
113+
.getCurrencyCode()).fractionDigits(p.getValue().getFractionDigits())
114+
).build()).collect(Collectors.toList());
63115
}
64116

65117
private static List<com.commercetools.importapi.models.common.Asset> importAssets(List<com.commercetools.api.models.common.Asset> assets){
118+
if (assets == null) {
119+
return null;
120+
}
66121
return assets.stream().map(a -> com.commercetools.importapi.models.common.Asset.builder().key(a.getKey())
67-
.name((com.commercetools.importapi.models.common.LocalizedString)a.getName()).build()).collect(
122+
.name(getLocalizedStringBuilder(a.getName()).build()).build()).collect(
68123
Collectors.toList());
69124
}
70-
private static CategoryKeyReference extractCategoryKeyReference(ProductProjection product) {
71-
return CategoryKeyReference.builder().key(product.getCategories().get(0).getObj().getKey()).build();
125+
private static List<CategoryKeyReference> extractCategoryKeyReference(ProductProjection product) {
126+
return product.getCategories().stream()
127+
.map(c -> CategoryKeyReference.builder().key(c.getObj().getKey()).build()).collect(Collectors.toList());
72128
}
73129

74130
private static Attribute mapAttribute(com.commercetools.api.models.product.Attribute attribute) {
@@ -90,7 +146,8 @@ private static Attribute mapAttribute(com.commercetools.api.models.product.Attri
90146
return Attribute.numberBuilder().name(attribute.getName()).value(((Long) value).doubleValue()).build();
91147
}
92148
if (value instanceof LocalizedString) {
93-
return Attribute.ltextBuilder().name(attribute.getName()).value(l -> l.values(((LocalizedString) value).values())).build();
149+
return Attribute.ltextBuilder().name(attribute.getName()).value(
150+
getLocalizedStringBuilder(((LocalizedString) value)).build()).build();
94151
}
95152
if (value instanceof AttributePlainEnumValue) {
96153
return Attribute.enumBuilder().name(attribute.getName()).value(((AttributePlainEnumValue) value).getKey()).build();

commercetools/commercetools-importapi-utils/src/test/java/ProductUtilTest.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.commercetools.sdk;
2+
3+
import com.commercetools.api.models.category.Category;
4+
import com.commercetools.api.models.category.CategoryReference;
5+
import com.commercetools.api.models.product.ProductProjection;
6+
import com.commercetools.api.models.product.ProductProjectionImpl;
7+
import com.commercetools.api.models.product.ProductVariantImpl;
8+
import com.commercetools.api.models.product_type.ProductType;
9+
import com.commercetools.api.models.product_type.ProductTypeReference;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import io.vrap.rmf.base.client.utils.json.JsonUtils;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.io.UncheckedIOException;
17+
import java.nio.charset.StandardCharsets;
18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
import java.time.ZonedDateTime;
21+
import java.util.Objects;
22+
23+
import static com.commercetools.sdk.ProductUtil.toProductDraftImport;
24+
import static com.commercetools.sdk.TestUtils.stringFromResource;
25+
import static org.junit.jupiter.api.Assertions.*;
26+
27+
public class ProductUtilTest {
28+
String productProjectionExample = "src/test/resources/product-projection.example.json";
29+
private final ObjectMapper objectMapper = new ObjectMapper();
30+
31+
@Test
32+
void shouldDeserializeProductProjectionCorrectly() throws Exception {
33+
var testProduct = JsonUtils.fromJsonString(stringFromResource(productProjectionExample), ProductProjection.class);
34+
35+
assertNotNull(testProduct, "The product object should not be null.");
36+
assertInstanceOf(ProductProjectionImpl.class, testProduct,
37+
"The product should be an instance of ProductProjectionImpl based on the annotation.");
38+
}
39+
40+
@Test
41+
void productTransformTest() {
42+
var testProduct = JsonUtils.fromJsonString(stringFromResource(productProjectionExample), ProductProjection.class);
43+
var transformedProduct = toProductDraftImport(testProduct);
44+
assertEquals(testProduct.getKey(), transformedProduct.getKey());
45+
//assertEquals(testProduct.getName(), transformedProduct.getName());
46+
}
47+
48+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.commercetools.sdk;
2+
3+
import java.io.IOException;
4+
import java.io.UncheckedIOException;
5+
import java.nio.charset.StandardCharsets;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.util.Objects;
9+
10+
public class TestUtils {
11+
public static String stringFromResource(final String resourcePath) {
12+
try {
13+
return Files.readString(Path.of(resourcePath));
14+
}
15+
catch (final IOException e) {
16+
throw new UncheckedIOException(e);
17+
}
18+
}
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"key": "product-draft-key",
3+
"name": {
4+
"en": "blue shirt"
5+
},
6+
"slug": {
7+
"en": "blue-t-shirt"
8+
},
9+
"productType": {
10+
"typeId": "product-type",
11+
"key": "sample"
12+
},
13+
"masterVariant": {
14+
"key": "master-variant-key",
15+
"attributes": [
16+
{
17+
"type": "reference",
18+
"name": "product-ref-attribute",
19+
"value": {
20+
"key": "category-key",
21+
"typeId": "category"
22+
}
23+
},
24+
{
25+
"type": "reference",
26+
"name": "product-custom-object-ref-attribute",
27+
"value": {
28+
"key": "custom-object-key",
29+
"container": "custom-object-container-key",
30+
"typeId": "key-value-document"
31+
}
32+
}
33+
]
34+
}
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"id": "080feded-4f74-4d31-9309-f7ef6b7f1279",
3+
"version": 3,
4+
"key": "productKey",
5+
"productType": {
6+
"typeId": "product-type",
7+
"id": "1c095f1b-e638-4c7e-86c4-c58df873fca6",
8+
"obj": {
9+
"id": "1c095f1b-e638-4c7e-86c4-c58df873fca6",
10+
"version": 1,
11+
"createdAt": "1970-01-01T00:00:00.001Z",
12+
"lastModifiedAt": "1970-01-01T00:00:00.001Z",
13+
"key": "productTypeKey",
14+
"name": "productTypeName",
15+
"description": "productTypeDescription",
16+
"attributes": []
17+
}
18+
},
19+
"name": {
20+
"en": "Some Products"
21+
},
22+
"categories": [],
23+
"slug": {
24+
"en": "product_slug_jxeutnxwkswk"
25+
},
26+
"masterVariant": {
27+
"key": "masterVariantKey",
28+
"id": 1,
29+
"prices": [],
30+
"images": [],
31+
"attributes": [
32+
{
33+
"name": "text1",
34+
"value": {
35+
"it": "italian1",
36+
"de": "german1",
37+
"en": "englisch1"
38+
}
39+
}
40+
]
41+
},
42+
"variants": [],
43+
"searchKeywords": {},
44+
"hasStagedChanges": false,
45+
"published": true,
46+
"createdAt": "1970-01-01T00:00:00.001Z",
47+
"lastModifiedAt": "2014-01-07T15:25:50.034Z",
48+
"attributes": []
49+
}

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ include 'commercetools:commercetools-reactornetty-client'
1919
include 'commercetools:commercetools-async-http-client'
2020
include 'commercetools:internal-docs'
2121
include 'commercetools:commercetools-money'
22+
23+
include 'commercetools:commercetools-importapi-utils'

0 commit comments

Comments
 (0)