Skip to content

Commit f3162fd

Browse files
authored
Merge pull request #510 from commercetools/CTPI-230-commit-fix
#449: ensurePriceChannels in ProductSyncOptions does not create a missing channel
2 parents ac20aca + 53c3061 commit f3162fd

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

src/integration-test/java/com/commercetools/sync/integration/externalsource/products/ProductSyncIT.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
88
import io.sphere.sdk.categories.Category;
99
import io.sphere.sdk.categories.CategoryDraft;
10+
import io.sphere.sdk.channels.Channel;
11+
import io.sphere.sdk.channels.ChannelRole;
12+
import io.sphere.sdk.channels.queries.ChannelByIdGet;
1013
import io.sphere.sdk.client.BadGatewayException;
1114
import io.sphere.sdk.client.ConcurrentModificationException;
1215
import io.sphere.sdk.client.ErrorResponseException;
@@ -17,18 +20,23 @@
1720
import io.sphere.sdk.models.ResourceIdentifier;
1821
import io.sphere.sdk.models.errors.DuplicateFieldError;
1922
import io.sphere.sdk.products.CategoryOrderHints;
23+
import io.sphere.sdk.products.Price;
24+
import io.sphere.sdk.products.PriceDraftBuilder;
25+
import io.sphere.sdk.products.PriceDraftDsl;
2026
import io.sphere.sdk.products.Product;
2127
import io.sphere.sdk.products.ProductDraft;
2228
import io.sphere.sdk.products.ProductDraftBuilder;
2329
import io.sphere.sdk.products.ProductVariantDraft;
2430
import io.sphere.sdk.products.ProductVariantDraftBuilder;
31+
import io.sphere.sdk.products.ProductVariantDraftDsl;
2532
import io.sphere.sdk.products.attributes.AttributeDraft;
2633
import io.sphere.sdk.products.commands.ProductCreateCommand;
2734
import io.sphere.sdk.products.commands.ProductUpdateCommand;
2835
import io.sphere.sdk.products.commands.updateactions.RemoveFromCategory;
2936
import io.sphere.sdk.products.commands.updateactions.SetAttribute;
3037
import io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants;
3138
import io.sphere.sdk.products.commands.updateactions.SetTaxCategory;
39+
import io.sphere.sdk.products.queries.ProductByKeyGet;
3240
import io.sphere.sdk.products.queries.ProductQuery;
3341
import io.sphere.sdk.producttypes.ProductType;
3442
import io.sphere.sdk.queries.PagedQueryResult;
@@ -37,16 +45,19 @@
3745
import io.sphere.sdk.states.StateType;
3846
import io.sphere.sdk.taxcategories.TaxCategory;
3947
import io.sphere.sdk.utils.CompletableFutureUtils;
48+
import io.sphere.sdk.utils.MoneyImpl;
4049
import org.junit.jupiter.api.AfterAll;
4150
import org.junit.jupiter.api.BeforeAll;
4251
import org.junit.jupiter.api.BeforeEach;
4352
import org.junit.jupiter.api.Test;
4453

4554
import javax.annotation.Nonnull;
4655
import java.util.ArrayList;
56+
import java.util.Collections;
4757
import java.util.HashMap;
4858
import java.util.List;
4959
import java.util.Locale;
60+
import java.util.Objects;
5061
import java.util.Optional;
5162
import java.util.Set;
5263
import java.util.concurrent.CompletableFuture;
@@ -179,6 +190,52 @@ void sync_withNewProduct_shouldCreateProduct() {
179190
assertThat(warningCallBackMessages).isEmpty();
180191
}
181192

193+
@Test
194+
void sync_withMissingPriceChannel_shouldCreateProductDistributionPriceChannel() {
195+
PriceDraftDsl priceDraftWithMissingChannelRef = PriceDraftBuilder.of(MoneyImpl.of("20", "EUR"))
196+
.channel(ResourceIdentifier.ofId("missingId")).build();
197+
198+
ProductVariantDraftDsl masterVariantDraft = ProductVariantDraftBuilder.of(
199+
ProductVariantDraftDsl.of()
200+
.withKey("v2")
201+
.withSku("1065833")
202+
.withPrices(Collections.singletonList(priceDraftWithMissingChannelRef)))
203+
.build();
204+
205+
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH,
206+
ProductType.referenceOfId(productType.getKey()))
207+
.masterVariant(masterVariantDraft)
208+
.taxCategory(null)
209+
.state(null)
210+
.build();
211+
212+
213+
final Consumer<String> warningCallBack = warningMessage -> warningCallBackMessages.add(warningMessage);
214+
215+
ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT)
216+
.errorCallback(this::collectErrors)
217+
.warningCallback(warningCallBack)
218+
.ensurePriceChannels(true)
219+
.build();
220+
221+
final ProductSync productSync = new ProductSync(syncOptions);
222+
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraft)));
223+
224+
assertThat(syncStatistics).hasValues(1, 1, 0, 0, 0);
225+
assertThat(errorCallBackExceptions).isEmpty();
226+
assertThat(errorCallBackMessages).isEmpty();
227+
assertThat(warningCallBackMessages).isEmpty();
228+
229+
Product productFromTargetProject = executeBlocking(
230+
CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(productDraft.getKey())));
231+
List<Price> prices = productFromTargetProject.getMasterData().getStaged().getMasterVariant().getPrices();
232+
assertThat(prices.size()).isEqualTo(1);
233+
234+
Channel channel = executeBlocking(CTP_TARGET_CLIENT.execute(ChannelByIdGet.of(
235+
Objects.requireNonNull(Objects.requireNonNull(prices.get(0).getChannel()).getId()))));
236+
assertThat(channel.getRoles()).containsOnly(ChannelRole.PRODUCT_DISTRIBUTION);
237+
}
238+
182239
@Test
183240
void sync_withNewProductAndBeforeCreateCallback_shouldCreateProduct() {
184241
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH,

src/main/java/com/commercetools/sync/products/ProductSync.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.commercetools.sync.services.impl.TypeServiceImpl;
2727
import com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl;
2828
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29+
import io.sphere.sdk.channels.ChannelRole;
2930
import io.sphere.sdk.commands.UpdateAction;
3031
import io.sphere.sdk.products.Product;
3132
import io.sphere.sdk.products.ProductDraft;
@@ -34,6 +35,7 @@
3435
import javax.annotation.Nonnull;
3536
import javax.annotation.Nullable;
3637
import java.util.Collection;
38+
import java.util.Collections;
3739
import java.util.HashSet;
3840
import java.util.List;
3941
import java.util.Map;
@@ -85,7 +87,7 @@ public ProductSync(@Nonnull final ProductSyncOptions productSyncOptions) {
8587
new ProductTypeServiceImpl(productSyncOptions),
8688
new CategoryServiceImpl(CategorySyncOptionsBuilder.of(productSyncOptions.getCtpClient()).build()),
8789
new TypeServiceImpl(productSyncOptions),
88-
new ChannelServiceImpl(productSyncOptions),
90+
new ChannelServiceImpl(productSyncOptions, Collections.singleton(ChannelRole.PRODUCT_DISTRIBUTION)),
8991
new CustomerGroupServiceImpl(productSyncOptions),
9092
new TaxCategoryServiceImpl(productSyncOptions),
9193
new StateServiceImpl(productSyncOptions),

src/main/java/com/commercetools/sync/services/impl/ChannelServiceImpl.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import io.sphere.sdk.channels.queries.ChannelQueryModel;
1414

1515
import javax.annotation.Nonnull;
16-
import java.util.Collections;
1716
import java.util.Optional;
1817
import java.util.Set;
1918
import java.util.concurrent.CompletionStage;
@@ -32,11 +31,6 @@ public ChannelServiceImpl(
3231
this.channelRoles = channelRoles;
3332
}
3433

35-
public ChannelServiceImpl(@Nonnull final BaseSyncOptions syncOptions) {
36-
super(syncOptions);
37-
this.channelRoles = Collections.emptySet();
38-
}
39-
4034
@Nonnull
4135
@Override
4236
public CompletionStage<Optional<String>> fetchCachedChannelId(@Nonnull final String key) {

0 commit comments

Comments
 (0)