This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#91 Implement full product draft conversion
- Loading branch information
Showing
7 changed files
with
165 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/com/diconiumwvv/storesservice/products/ProductCTConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.diconiumwvv.storesservice.products; | ||
|
||
import com.diconiumwvv.storesservice.products.dtos.ProductDraftDTO; | ||
import io.sphere.sdk.channels.Channel; | ||
import io.sphere.sdk.models.DefaultCurrencyUnits; | ||
import io.sphere.sdk.models.LocalizedString; | ||
import io.sphere.sdk.models.ResourceIdentifier; | ||
import io.sphere.sdk.products.*; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.text.Normalizer; | ||
import java.util.Locale; | ||
|
||
@Service | ||
public class ProductCTConverter { | ||
|
||
static Locale userLocale = Locale.GERMANY; | ||
|
||
ProductVariantDraft convertVariantDraftWithPrice(ProductDraftDTO productDraftDTO, Channel store) { | ||
return ProductVariantDraftBuilder.of() | ||
.price( | ||
PriceDraft.of( | ||
productDraftDTO.getPrice().getValue(), | ||
DefaultCurrencyUnits.EUR // TODO read currency unit from input? | ||
).withChannel(store) | ||
) | ||
.build(); | ||
} | ||
|
||
|
||
LocalizedString buildSlug(LocalizedString productName, Channel store) { | ||
String defaultLocaleName = productName.get(userLocale); | ||
String defaultLocaleSlug = store.getKey() + "_" + defaultLocaleName; | ||
String normalizedSlug = normalizeString(defaultLocaleSlug); | ||
return LocalizedString.of(userLocale, normalizedSlug); | ||
} | ||
|
||
private String normalizeString(String original) { | ||
String normalized = Normalizer.normalize(original, Normalizer.Form.NFD); | ||
normalized = normalized.replaceAll("[^a-zA-Z_\\-]", ""); | ||
return normalized.toLowerCase(); | ||
} | ||
|
||
public ProductDraft convert(ProductDraftDTO productDraftDTO, Channel store, String productTypeKey) { | ||
return ProductDraftBuilder.of( | ||
ResourceIdentifier.ofKey(productTypeKey), | ||
productDraftDTO.getName(), | ||
buildSlug(productDraftDTO.getName(), store), | ||
convertVariantDraftWithPrice(productDraftDTO, store) | ||
).build(); | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
...n/java/com/diconiumwvv/storesservice/products/ProductDraftDTOToProductDraftConverter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/main/java/com/diconiumwvv/storesservice/products/dtos/ProductDraftDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/com/diconiumwvv/storesservice/products/ProductCTConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.diconiumwvv.storesservice.products; | ||
|
||
import com.diconiumwvv.storesservice.products.dtos.PriceDTO; | ||
import com.diconiumwvv.storesservice.products.dtos.ProductDraftDTO; | ||
import io.sphere.sdk.channels.Channel; | ||
import io.sphere.sdk.json.SphereJsonUtils; | ||
import io.sphere.sdk.models.LocalizedString; | ||
import io.sphere.sdk.products.ProductDraft; | ||
import io.sphere.sdk.products.ProductVariantDraft; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Locale; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ProductCTConverterTest { | ||
|
||
private static final String DEFAULT_PRODUCT_KEY = "Default-Product-Key"; | ||
private static final Locale TEST_LOCALE = Locale.GERMANY; | ||
|
||
private static final String PRODUCT_NAME = "Product Name"; | ||
private static final String PRODUCT_DESCRIPTION = "Product Description"; | ||
private static final String STORE_KEY = "test-channel-key"; | ||
private static final String STORE_ID = "test-channel-id"; | ||
|
||
private final ProductCTConverter productCTConverter = new ProductCTConverter(); | ||
|
||
private Channel testStore; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
testStore = setUpTestStore(); | ||
} | ||
|
||
private Channel setUpTestStore() { | ||
|
||
return SphereJsonUtils.readObject("{\n" + | ||
" \"id\": \"" + STORE_ID + "\",\n" + | ||
" \"key\": \"" + STORE_KEY + "\"\n" + | ||
"}", Channel.typeReference()); | ||
} | ||
|
||
private ProductDraftDTO buildProductDraft(String productName, String productDescription) { | ||
return ProductDraftDTO.builder() | ||
.name(LocalizedString.of(TEST_LOCALE, productName)) | ||
.description(LocalizedString.of(TEST_LOCALE, productDescription)) | ||
.price(PriceDTO.builder().value(BigDecimal.TEN).currencyCode("EUR").build()) | ||
.build(); | ||
} | ||
|
||
@Test | ||
void convertProductDraft() { | ||
final ProductDraftDTO testDraft = buildProductDraft(PRODUCT_NAME, PRODUCT_DESCRIPTION); | ||
|
||
ProductDraft convertedProductDraft = productCTConverter.convert(testDraft, testStore, DEFAULT_PRODUCT_KEY); | ||
ProductVariantDraft masterVariant = convertedProductDraft.getMasterVariant(); | ||
|
||
assertThat(convertedProductDraft).isNotNull(); | ||
assertThat(convertedProductDraft.getProductType().getKey()).isEqualTo(DEFAULT_PRODUCT_KEY); | ||
assertThat(convertedProductDraft.getSlug().get(TEST_LOCALE)).contains(STORE_KEY); | ||
|
||
assertThat(masterVariant).isNotNull(); | ||
assertThat(masterVariant.getPrices()).isNotNull(); | ||
assertThat(masterVariant.getPrices().get(0)).isNotNull(); | ||
assertThat(masterVariant.getPrices().get(0).getChannel()).isNotNull(); | ||
assertThat(masterVariant.getPrices().get(0).getChannel().getId()).isEqualTo(STORE_ID); | ||
|
||
|
||
} | ||
|
||
@Test | ||
void normalizeNameForSlug() { | ||
final ProductDraftDTO testDraft = buildProductDraft("name%with+special?CHARACTERS&", PRODUCT_DESCRIPTION); | ||
ProductDraft convertedProductDraft = productCTConverter.convert(testDraft, testStore, DEFAULT_PRODUCT_KEY); | ||
|
||
// test for product name normalization | ||
String createdSlug = convertedProductDraft.getSlug().get(TEST_LOCALE); | ||
assertThat(createdSlug).doesNotContain("%"); | ||
assertThat(createdSlug).doesNotContain("+"); | ||
assertThat(createdSlug).doesNotContain("?"); | ||
assertThat(createdSlug).doesNotContain("&"); | ||
assertThat(createdSlug).contains("special"); | ||
assertThat(createdSlug).contains("characters"); | ||
assertThat(createdSlug).contains(STORE_KEY.toLowerCase()); | ||
} | ||
} |