Skip to content

Commit a04de2d

Browse files
committed
DEVX-640: spotless fixes
1 parent 3618811 commit a04de2d

File tree

3 files changed

+116
-88
lines changed

3 files changed

+116
-88
lines changed

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

Lines changed: 103 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
12
package com.commercetools.sdk;
23

4+
import java.time.LocalDate;
5+
import java.time.ZonedDateTime;
6+
import java.util.*;
7+
import java.util.stream.Collectors;
8+
39
import com.commercetools.api.models.common.CentPrecisionMoney;
410
import com.commercetools.api.models.common.LocalizedString;
511
import com.commercetools.api.models.common.Money;
@@ -19,62 +25,60 @@
1925
import com.commercetools.importapi.models.productvariants.BooleanAttribute;
2026
import com.commercetools.importapi.models.productvariants.NumberAttribute;
2127

22-
import java.time.LocalDate;
23-
import java.time.ZonedDateTime;
24-
import java.util.*;
25-
import java.util.function.Function;
26-
import java.util.stream.Collectors;
27-
2828
public final class ProductUtil {
2929

3030
public static ProductDraftImport toProductDraftImport(ProductProjection product) {
31-
var draft = ProductDraftImport.builder().key(product.getKey())
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))
38-
.categories(extractCategoryKeyReference(product))
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())
44-
.masterVariant(extractProductVariantDraftImport(product.getMasterVariant()))
45-
.variants(extractProductVariantDraftImport(product))
46-
.taxCategory(getTaxCategoryKeyReference(product))
47-
.state(getStateKeyReference(product))
48-
.priceMode(mapPriceModeToImportApi(product))
49-
.attributes(product.getAttributes().stream().map(ProductUtil::mapAttribute).collect(Collectors.toList()));
50-
return draft.build();
31+
var draft = ProductDraftImport.builder()
32+
.key(product.getKey())
33+
.productType(p -> p.key(product.getProductType().getObj().getKey()))
34+
.name(l -> getLocalizedStringBuilder(product.getName()))
35+
.slug(l -> getLocalizedStringBuilder(product.getSlug()))
36+
.description(Optional.ofNullable(product.getDescription())
37+
.map(ProductUtil::getLocalizedStringBuilder)
38+
.map(LocalizedStringBuilder::build)
39+
.orElse(null))
40+
.categories(extractCategoryKeyReference(product))
41+
.metaTitle(Optional.ofNullable(product.getMetaTitle())
42+
.map(ProductUtil::getLocalizedStringBuilder)
43+
.map(LocalizedStringBuilder::build)
44+
.orElse(null))
45+
.metaDescription(
46+
(com.commercetools.importapi.models.common.LocalizedString) product.getMetaDescription())
47+
.metaKeywords((com.commercetools.importapi.models.common.LocalizedString) product.getMetaKeywords())
48+
.masterVariant(extractProductVariantDraftImport(product.getMasterVariant()))
49+
.variants(extractProductVariantDraftImport(product))
50+
.taxCategory(getTaxCategoryKeyReference(product))
51+
.state(getStateKeyReference(product))
52+
.priceMode(mapPriceModeToImportApi(product))
53+
.attributes(
54+
product.getAttributes().stream().map(ProductUtil::mapAttribute).collect(Collectors.toList()));
55+
return draft.build();
5156
}
5257

53-
private static LocalizedStringBuilder getLocalizedStringBuilder(
54-
LocalizedString s) {
58+
private static LocalizedStringBuilder getLocalizedStringBuilder(LocalizedString s) {
5559
return com.commercetools.importapi.models.common.LocalizedString.builder().values(s.values());
5660
}
5761

58-
private static com.commercetools.importapi.models.common.ProductPriceModeEnum mapPriceModeToImportApi(ProductProjection product) {
62+
private static com.commercetools.importapi.models.common.ProductPriceModeEnum mapPriceModeToImportApi(
63+
ProductProjection product) {
5964
if (product.getPriceMode() == null) {
6065
return null;
61-
}if (product.getPriceMode().equals(ProductPriceModeEnum.ProductPriceModeEnumEnum.EMBEDDED)) {
66+
}
67+
if (product.getPriceMode().equals(ProductPriceModeEnum.ProductPriceModeEnumEnum.EMBEDDED)) {
6268
return com.commercetools.importapi.models.common.ProductPriceModeEnum.ProductPriceModeEnumEnum.EMBEDDED;
6369
}
6470
return com.commercetools.importapi.models.common.ProductPriceModeEnum.ProductPriceModeEnumEnum.STANDALONE;
6571
}
6672

67-
private static StateKeyReference getStateKeyReference(
68-
ProductProjection product) {
73+
private static StateKeyReference getStateKeyReference(ProductProjection product) {
6974
var key = Optional.ofNullable(product.getState()).map(StateReference::getObj).map(State::getKey).orElse(null);
7075
if (key != null) {
7176
return StateKeyReference.builder().key(key).build();
7277
}
7378
return null;
7479
}
7580

76-
private static TaxCategoryKeyReference getTaxCategoryKeyReference(
77-
ProductProjection product) {
81+
private static TaxCategoryKeyReference getTaxCategoryKeyReference(ProductProjection product) {
7882
var key = Optional.ofNullable(product.getTaxCategory())
7983
.map(TaxCategoryReference::getObj)
8084
.map(TaxCategory::getKey)
@@ -86,52 +90,76 @@ private static TaxCategoryKeyReference getTaxCategoryKeyReference(
8690
}
8791

8892
private static List<ProductVariantDraftImport> extractProductVariantDraftImport(ProductProjection product) {
89-
return product.getVariants().stream().map(ProductUtil::extractProductVariantDraftImport).collect(Collectors.toList());
93+
return product.getVariants()
94+
.stream()
95+
.map(ProductUtil::extractProductVariantDraftImport)
96+
.collect(Collectors.toList());
9097
}
9198

9299
private static ProductVariantDraftImport extractProductVariantDraftImport(ProductVariant variant) {
93-
return ProductVariantDraftImport.builder().key(variant.getKey()).sku(variant.getSku())
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+
return ProductVariantDraftImport.builder()
101+
.key(variant.getKey())
102+
.sku(variant.getSku())
103+
.images(variant.getImages()
104+
.stream()
105+
.map(i -> com.commercetools.importapi.models.common.Image.builder()
106+
.dimensions(d -> com.commercetools.importapi.models.common.AssetDimensions.builder()
107+
.w(i.getDimensions().getW())
108+
.h(i.getDimensions().getH()))
109+
.url(i.getUrl())
110+
.label(i.getLabel())
111+
.build())
112+
.collect(Collectors.toList()))
100113
.prices(mapPricesToImportApi(variant))
101-
.attributes(variant.getAttributes().stream().map(
102-
ProductUtil::mapAttribute).collect(Collectors.toList()))
114+
.attributes(
115+
variant.getAttributes().stream().map(ProductUtil::mapAttribute).collect(Collectors.toList()))
103116
.assets(importAssets(variant.getAssets()))
104117
.build();
105118
}
106119

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());
120+
private static List<PriceDraftImport> mapPricesToImportApi(ProductVariant variant) {
121+
return variant.getPrices()
122+
.stream()
123+
.map(p -> PriceDraftImport.builder()
124+
.key(p.getKey())
125+
.value(v -> (p.getValue() instanceof CentPrecisionMoney)
126+
? v.centPrecisionBuilder()
127+
.centAmount(p.getValue().getCentAmount())
128+
.currencyCode(p.getValue().getCurrencyCode())
129+
.fractionDigits(p.getValue().getFractionDigits())
130+
: v.highPrecisionBuilder()
131+
.centAmount(p.getValue().getCentAmount())
132+
.currencyCode(p.getValue().getCurrencyCode())
133+
.fractionDigits(p.getValue().getFractionDigits()))
134+
.build())
135+
.collect(Collectors.toList());
115136
}
116137

117-
private static List<com.commercetools.importapi.models.common.Asset> importAssets(List<com.commercetools.api.models.common.Asset> assets){
138+
private static List<com.commercetools.importapi.models.common.Asset> importAssets(
139+
List<com.commercetools.api.models.common.Asset> assets) {
118140
if (assets == null) {
119141
return null;
120142
}
121-
return assets.stream().map(a -> com.commercetools.importapi.models.common.Asset.builder().key(a.getKey())
122-
.name(getLocalizedStringBuilder(a.getName()).build()).build()).collect(
123-
Collectors.toList());
143+
return assets.stream()
144+
.map(a -> com.commercetools.importapi.models.common.Asset.builder()
145+
.key(a.getKey())
146+
.name(getLocalizedStringBuilder(a.getName()).build())
147+
.build())
148+
.collect(Collectors.toList());
124149
}
150+
125151
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());
152+
return product.getCategories()
153+
.stream()
154+
.map(c -> CategoryKeyReference.builder().key(c.getObj().getKey()).build())
155+
.collect(Collectors.toList());
128156
}
129157

130158
private static Attribute mapAttribute(com.commercetools.api.models.product.Attribute attribute) {
131159
Object value = attribute.getValue();
132160

133161
if (value instanceof String) {
134-
return Attribute.textBuilder().name(attribute.getName()).value((String) value).build();
162+
return Attribute.textBuilder().name(attribute.getName()).value((String) value).build();
135163
}
136164
if (value instanceof Integer) {
137165
return Attribute.numberBuilder().name(attribute.getName()).value(((Integer) value).doubleValue()).build();
@@ -146,14 +174,22 @@ private static Attribute mapAttribute(com.commercetools.api.models.product.Attri
146174
return Attribute.numberBuilder().name(attribute.getName()).value(((Long) value).doubleValue()).build();
147175
}
148176
if (value instanceof LocalizedString) {
149-
return Attribute.ltextBuilder().name(attribute.getName()).value(
150-
getLocalizedStringBuilder(((LocalizedString) value)).build()).build();
177+
return Attribute.ltextBuilder()
178+
.name(attribute.getName())
179+
.value(getLocalizedStringBuilder(((LocalizedString) value)).build())
180+
.build();
151181
}
152182
if (value instanceof AttributePlainEnumValue) {
153-
return Attribute.enumBuilder().name(attribute.getName()).value(((AttributePlainEnumValue) value).getKey()).build();
183+
return Attribute.enumBuilder()
184+
.name(attribute.getName())
185+
.value(((AttributePlainEnumValue) value).getKey())
186+
.build();
154187
}
155188
if (value instanceof AttributeLocalizedEnumValue) {
156-
return Attribute.enumBuilder().name(attribute.getName()).value(((AttributeLocalizedEnumValue) value).getKey()).build();
189+
return Attribute.enumBuilder()
190+
.name(attribute.getName())
191+
.value(((AttributeLocalizedEnumValue) value).getKey())
192+
.build();
157193
}
158194
if (value instanceof Money) {
159195
return Attribute.moneyBuilder().name(attribute.getName()).value((TypedMoney) value).build();
@@ -171,8 +207,10 @@ private static Attribute mapAttribute(com.commercetools.api.models.product.Attri
171207
return Attribute.referenceBuilder().name(attribute.getName()).value((KeyReference) value).build();
172208
}
173209
if (value instanceof AttributeConstraintEnum) {
174-
return Attribute.enumBuilder().name(attribute.getName()).value(
175-
String.valueOf((AttributeConstraintEnum) value)).build();
210+
return Attribute.enumBuilder()
211+
.name(attribute.getName())
212+
.value(String.valueOf((AttributeConstraintEnum) value))
213+
.build();
176214
}
177215
if (value instanceof AttributeSetType) {
178216
var elementType = ((AttributeSetType) value).getElementType();

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,36 @@
1+
12
package com.commercetools.sdk;
23

3-
import com.commercetools.api.models.category.Category;
4-
import com.commercetools.api.models.category.CategoryReference;
4+
import static com.commercetools.sdk.ProductUtil.toProductDraftImport;
5+
import static com.commercetools.sdk.TestUtils.stringFromResource;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
58
import com.commercetools.api.models.product.ProductProjection;
69
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;
1010
import com.fasterxml.jackson.databind.ObjectMapper;
11-
import io.vrap.rmf.base.client.utils.json.JsonUtils;
12-
import org.junit.jupiter.api.Test;
1311

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;
12+
import io.vrap.rmf.base.client.utils.json.JsonUtils;
2213

23-
import static com.commercetools.sdk.ProductUtil.toProductDraftImport;
24-
import static com.commercetools.sdk.TestUtils.stringFromResource;
25-
import static org.junit.jupiter.api.Assertions.*;
14+
import org.junit.jupiter.api.Test;
2615

2716
public class ProductUtilTest {
2817
String productProjectionExample = "src/test/resources/product-projection.example.json";
2918
private final ObjectMapper objectMapper = new ObjectMapper();
3019

3120
@Test
3221
void shouldDeserializeProductProjectionCorrectly() throws Exception {
33-
var testProduct = JsonUtils.fromJsonString(stringFromResource(productProjectionExample), ProductProjection.class);
22+
var testProduct = JsonUtils.fromJsonString(stringFromResource(productProjectionExample),
23+
ProductProjection.class);
3424

3525
assertNotNull(testProduct, "The product object should not be null.");
3626
assertInstanceOf(ProductProjectionImpl.class, testProduct,
37-
"The product should be an instance of ProductProjectionImpl based on the annotation.");
27+
"The product should be an instance of ProductProjectionImpl based on the annotation.");
3828
}
3929

4030
@Test
4131
void productTransformTest() {
42-
var testProduct = JsonUtils.fromJsonString(stringFromResource(productProjectionExample), ProductProjection.class);
32+
var testProduct = JsonUtils.fromJsonString(stringFromResource(productProjectionExample),
33+
ProductProjection.class);
4334
var transformedProduct = toProductDraftImport(testProduct);
4435
assertEquals(testProduct.getKey(), transformedProduct.getKey());
4536
//assertEquals(testProduct.getName(), transformedProduct.getName());

commercetools/commercetools-importapi-utils/src/test/java/com/commercetools/sdk/TestUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
12
package com.commercetools.sdk;
23

34
import java.io.IOException;
45
import java.io.UncheckedIOException;
5-
import java.nio.charset.StandardCharsets;
66
import java.nio.file.Files;
77
import java.nio.file.Path;
8-
import java.util.Objects;
98

109
public class TestUtils {
1110
public static String stringFromResource(final String resourcePath) {

0 commit comments

Comments
 (0)