|
7 | 7 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
8 | 8 | import io.sphere.sdk.categories.Category; |
9 | 9 | 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; |
10 | 13 | import io.sphere.sdk.client.BadGatewayException; |
11 | 14 | import io.sphere.sdk.client.ConcurrentModificationException; |
12 | 15 | import io.sphere.sdk.client.ErrorResponseException; |
|
17 | 20 | import io.sphere.sdk.models.ResourceIdentifier; |
18 | 21 | import io.sphere.sdk.models.errors.DuplicateFieldError; |
19 | 22 | 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; |
20 | 26 | import io.sphere.sdk.products.Product; |
21 | 27 | import io.sphere.sdk.products.ProductDraft; |
22 | 28 | import io.sphere.sdk.products.ProductDraftBuilder; |
23 | 29 | import io.sphere.sdk.products.ProductVariantDraft; |
24 | 30 | import io.sphere.sdk.products.ProductVariantDraftBuilder; |
| 31 | +import io.sphere.sdk.products.ProductVariantDraftDsl; |
25 | 32 | import io.sphere.sdk.products.attributes.AttributeDraft; |
26 | 33 | import io.sphere.sdk.products.commands.ProductCreateCommand; |
27 | 34 | import io.sphere.sdk.products.commands.ProductUpdateCommand; |
28 | 35 | import io.sphere.sdk.products.commands.updateactions.RemoveFromCategory; |
29 | 36 | import io.sphere.sdk.products.commands.updateactions.SetAttribute; |
30 | 37 | import io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants; |
31 | 38 | import io.sphere.sdk.products.commands.updateactions.SetTaxCategory; |
| 39 | +import io.sphere.sdk.products.queries.ProductByKeyGet; |
32 | 40 | import io.sphere.sdk.products.queries.ProductQuery; |
33 | 41 | import io.sphere.sdk.producttypes.ProductType; |
34 | 42 | import io.sphere.sdk.queries.PagedQueryResult; |
|
37 | 45 | import io.sphere.sdk.states.StateType; |
38 | 46 | import io.sphere.sdk.taxcategories.TaxCategory; |
39 | 47 | import io.sphere.sdk.utils.CompletableFutureUtils; |
| 48 | +import io.sphere.sdk.utils.MoneyImpl; |
40 | 49 | import org.junit.jupiter.api.AfterAll; |
41 | 50 | import org.junit.jupiter.api.BeforeAll; |
42 | 51 | import org.junit.jupiter.api.BeforeEach; |
43 | 52 | import org.junit.jupiter.api.Test; |
44 | 53 |
|
45 | 54 | import javax.annotation.Nonnull; |
46 | 55 | import java.util.ArrayList; |
| 56 | +import java.util.Collections; |
47 | 57 | import java.util.HashMap; |
48 | 58 | import java.util.List; |
49 | 59 | import java.util.Locale; |
| 60 | +import java.util.Objects; |
50 | 61 | import java.util.Optional; |
51 | 62 | import java.util.Set; |
52 | 63 | import java.util.concurrent.CompletableFuture; |
@@ -179,6 +190,52 @@ void sync_withNewProduct_shouldCreateProduct() { |
179 | 190 | assertThat(warningCallBackMessages).isEmpty(); |
180 | 191 | } |
181 | 192 |
|
| 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 | + |
182 | 239 | @Test |
183 | 240 | void sync_withNewProductAndBeforeCreateCallback_shouldCreateProduct() { |
184 | 241 | final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, |
|
0 commit comments