Skip to content

Commit 10a0db3

Browse files
Merge pull request #112 from commercetools/99-attribute-prices
99 attribute prices
2 parents 53a1fe4 + 9bb0cf4 commit 10a0db3

28 files changed

Lines changed: 1340 additions & 205 deletions

gradle-scripts/dependencies.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ext{
2-
commercetoolsJvmSdkVersion = '1.20.0'
2+
commercetoolsJvmSdkVersion = '1.23.1'
33
mockitoVersion = '2.8.9'
44
slf4jVersion = '1.6.2'
55
jUnitVersion = '4.12'

src/integration-test/java/com/commercetools/sync/integration/commons/utils/ProductITUtils.java

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package com.commercetools.sync.integration.commons.utils;
22

33
import com.commercetools.sync.commons.utils.CtpQueryUtils;
4+
import io.sphere.sdk.channels.Channel;
45
import io.sphere.sdk.client.SphereClient;
6+
import io.sphere.sdk.models.Reference;
7+
import io.sphere.sdk.products.PriceDraft;
8+
import io.sphere.sdk.products.PriceDraftBuilder;
59
import io.sphere.sdk.products.Product;
10+
import io.sphere.sdk.products.ProductDraft;
11+
import io.sphere.sdk.products.ProductDraftBuilder;
12+
import io.sphere.sdk.products.ProductVariantDraft;
13+
import io.sphere.sdk.products.ProductVariantDraftBuilder;
614
import io.sphere.sdk.products.commands.ProductDeleteCommand;
715
import io.sphere.sdk.products.commands.ProductUpdateCommand;
816
import io.sphere.sdk.products.commands.updateactions.Unpublish;
17+
import io.sphere.sdk.products.expansion.ProductExpansionModel;
918
import io.sphere.sdk.products.queries.ProductQuery;
1019
import io.sphere.sdk.producttypes.ProductType;
1120
import io.sphere.sdk.producttypes.ProductTypeDraft;
@@ -22,14 +31,11 @@
2231

2332
import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories;
2433
import static com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes;
34+
import static com.commercetools.sync.integration.inventories.utils.InventoryITUtils.deleteSupplyChannels;
2535
import static io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource;
36+
import static java.util.stream.Collectors.toList;
2637

2738
public final class ProductITUtils {
28-
public static final String PRODUCT_KEY_1_CHANGED_RESOURCE_PATH = "product-key-1-changed.json";
29-
public static final String PRODUCT_KEY_2_RESOURCE_PATH = "product-key-2.json";
30-
public static final String PRODUCT_TYPE_RESOURCE_PATH = "product-type.json";
31-
public static final String PRODUCT_TYPE_NO_KEY_RESOURCE_PATH = "product-type-no-key.json";
32-
3339
/**
3440
* This method blocks to create a product type, which is defined by the JSON resource found in the supplied
3541
* {@code jsonResourcePath}, in the CTP project defined by the supplied {@code ctpClient}.
@@ -57,6 +63,7 @@ public static void deleteProductSyncTestData(@Nonnull final SphereClient ctpClie
5763
deleteProductTypes(ctpClient);
5864
deleteAllCategories(ctpClient);
5965
deleteTypes(ctpClient);
66+
deleteSupplyChannels(ctpClient);
6067
}
6168

6269
/**
@@ -100,4 +107,48 @@ static void deleteProductTypes(@Nonnull final SphereClient ctpClient) {
100107
.toArray(new CompletableFuture[productTypeDeleteFutures.size()])))
101108
.toCompletableFuture().join();
102109
}
110+
111+
/**
112+
* Builds the query for fetching products from the source CTP project with all the needed expansions.
113+
* @return the query for fetching products from the source CTP project with all the needed expansions.
114+
*/
115+
public static ProductQuery getProductQuery() {
116+
return ProductQuery.of().withLimit(SphereClientUtils.QUERY_MAX_LIMIT)
117+
.withExpansionPaths(ProductExpansionModel::productType)
118+
.plusExpansionPaths(productProductExpansionModel ->
119+
productProductExpansionModel.masterData().staged().categories())
120+
.plusExpansionPaths(channelExpansionModel ->
121+
channelExpansionModel.masterData().staged().allVariants().prices().channel());
122+
}
123+
124+
/**
125+
* Gets the supplied {@link ProductDraft} with the price channel reference attached.
126+
*
127+
* @param productDraft TODO
128+
* @param channelReference TODO
129+
* @return TODO.
130+
*/
131+
public static ProductDraft getDraftWithPriceChannelReferences(@Nonnull final ProductDraft productDraft,
132+
@Nonnull final Reference<Channel> channelReference) {
133+
final List<ProductVariantDraft> allVariants = productDraft
134+
.getVariants().stream().map(productVariant -> {
135+
final List<PriceDraft> priceDraftsWithChannelReferences =
136+
productVariant.getPrices().stream()
137+
.map(price -> PriceDraftBuilder.of(price).channel(channelReference).build())
138+
.collect(toList());
139+
return ProductVariantDraftBuilder.of(productVariant)
140+
.prices(priceDraftsWithChannelReferences)
141+
.build();
142+
})
143+
.collect(toList());
144+
final List<PriceDraft> masterVariantPriceDrafts = productDraft
145+
.getMasterVariant().getPrices().stream().map(price -> PriceDraftBuilder.of(price)
146+
.channel(channelReference)
147+
.build()).collect(toList());
148+
return ProductDraftBuilder.of(productDraft)
149+
.masterVariant(ProductVariantDraftBuilder.of(productDraft.getMasterVariant())
150+
.prices(masterVariantPriceDrafts).build())
151+
.variants(allVariants)
152+
.build();
153+
}
103154
}

src/integration-test/java/com/commercetools/sync/integration/ctpprojectsource/products/ProductReferenceResolverIT.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.commercetools.sync.products.ProductSyncOptionsBuilder;
88
import com.commercetools.sync.products.helpers.ProductSyncStatistics;
99
import io.sphere.sdk.categories.Category;
10-
import io.sphere.sdk.models.Reference;
10+
import io.sphere.sdk.models.ResourceIdentifier;
1111
import io.sphere.sdk.products.Product;
1212
import io.sphere.sdk.products.ProductDraft;
1313
import io.sphere.sdk.products.commands.ProductCreateCommand;
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.List;
2424
import java.util.Locale;
25+
import java.util.Set;
2526
import java.util.concurrent.CompletionException;
2627
import java.util.stream.Collectors;
2728

@@ -31,24 +32,26 @@
3132
import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories;
3233
import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType;
3334
import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts;
34-
import static com.commercetools.sync.integration.commons.utils.ProductITUtils.PRODUCT_TYPE_NO_KEY_RESOURCE_PATH;
35-
import static com.commercetools.sync.integration.commons.utils.ProductITUtils.PRODUCT_TYPE_RESOURCE_PATH;
3635
import static com.commercetools.sync.integration.commons.utils.ProductITUtils.createProductType;
3736
import static com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts;
3837
import static com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData;
3938
import static com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT;
4039
import static com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT;
4140
import static com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH;
41+
import static com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_NO_KEY_RESOURCE_PATH;
42+
import static com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH;
4243
import static com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft;
4344
import static com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints;
4445
import static java.lang.String.format;
46+
import static java.util.stream.Collectors.toSet;
4547
import static org.assertj.core.api.Java6Assertions.assertThat;
4648

4749
public class ProductReferenceResolverIT {
4850
private static ProductType productTypeSource;
4951
private static ProductType noKeyProductTypeSource;
5052

51-
private static List<Reference<Category>> categoryReferences;
53+
private static Set<ResourceIdentifier<Category>> sourceCategoryResourcesWithIds;
54+
private static Set<ResourceIdentifier<Category>> sourceCategories;
5255
private ProductSync productSync;
5356
private List<String> errorCallBackMessages;
5457
private List<String> warningCallBackMessages;
@@ -69,9 +72,16 @@ public static void setup() {
6972
OLD_CATEGORY_CUSTOM_TYPE_NAME, CTP_SOURCE_CLIENT);
7073

7174
createCategories(CTP_TARGET_CLIENT, getCategoryDrafts(null, 2));
72-
categoryReferences =
73-
createCategories(CTP_SOURCE_CLIENT, getCategoryDrafts(null, 2))
74-
.stream().map(Category::toReference).collect(Collectors.toList());
75+
sourceCategories = createCategories(CTP_SOURCE_CLIENT, getCategoryDrafts(null, 2))
76+
.stream()
77+
.map(category -> ResourceIdentifier.<Category>ofIdOrKey(category.getId(), category.getKey(),
78+
Category.referenceTypeId())).collect(Collectors.toSet());
79+
sourceCategoryResourcesWithIds =
80+
sourceCategories.stream()
81+
.map(categoryResourceIdentifier ->
82+
ResourceIdentifier.<Category>ofId(categoryResourceIdentifier.getId(),
83+
Category.referenceTypeId()))
84+
.collect(toSet());
7585

7686
createProductType(PRODUCT_TYPE_RESOURCE_PATH, CTP_TARGET_CLIENT);
7787
createProductType(PRODUCT_TYPE_NO_KEY_RESOURCE_PATH, CTP_TARGET_CLIENT);
@@ -117,7 +127,8 @@ public static void tearDown() {
117127
@Test
118128
public void sync_withNewProductWithExistingCategoryAndProductTypeReferences_ShouldCreateProduct() {
119129
final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_RESOURCE_PATH,
120-
productTypeSource.toReference(), categoryReferences, createRandomCategoryOrderHints(categoryReferences));
130+
productTypeSource.toReference(), sourceCategoryResourcesWithIds,
131+
createRandomCategoryOrderHints(sourceCategories));
121132
CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(productDraft)).toCompletableFuture().join();
122133

123134
final ProductQuery productQuery = ProductQuery.of().withLimit(SphereClientUtils.QUERY_MAX_LIMIT)
@@ -147,8 +158,8 @@ public void sync_withNewProductWithExistingCategoryAndProductTypeReferences_Shou
147158
@Test
148159
public void sync_withNewProductWithNoProductTypeKey_ShouldFailCreatingTheProduct() {
149160
final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_RESOURCE_PATH,
150-
noKeyProductTypeSource.toReference(), categoryReferences,
151-
createRandomCategoryOrderHints(categoryReferences));
161+
noKeyProductTypeSource.toReference(), sourceCategoryResourcesWithIds,
162+
createRandomCategoryOrderHints(sourceCategories));
152163
CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(productDraft)).toCompletableFuture().join();
153164

154165
final ProductQuery productQuery = ProductQuery.of().withLimit(SphereClientUtils.QUERY_MAX_LIMIT)

0 commit comments

Comments
 (0)