Skip to content

Commit

Permalink
#99: Add channel support in IT utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
heshamMassoud committed Sep 27, 2017
1 parent 4aa6594 commit 2174972
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package com.commercetools.sync.integration.commons.utils;

import com.commercetools.sync.commons.utils.CtpQueryUtils;
import io.sphere.sdk.channels.Channel;
import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.models.Reference;
import io.sphere.sdk.products.PriceDraft;
import io.sphere.sdk.products.PriceDraftBuilder;
import io.sphere.sdk.products.Product;
import io.sphere.sdk.products.ProductDraft;
import io.sphere.sdk.products.ProductDraftBuilder;
import io.sphere.sdk.products.ProductVariantDraft;
import io.sphere.sdk.products.ProductVariantDraftBuilder;
import io.sphere.sdk.products.commands.ProductDeleteCommand;
import io.sphere.sdk.products.commands.ProductUpdateCommand;
import io.sphere.sdk.products.commands.updateactions.Unpublish;
import io.sphere.sdk.products.expansion.ProductExpansionModel;
import io.sphere.sdk.products.queries.ProductQuery;
import io.sphere.sdk.producttypes.ProductType;
import io.sphere.sdk.producttypes.ProductTypeDraft;
Expand All @@ -22,7 +31,9 @@

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

public final class ProductITUtils {
/**
Expand Down Expand Up @@ -52,6 +63,7 @@ public static void deleteProductSyncTestData(@Nonnull final SphereClient ctpClie
deleteProductTypes(ctpClient);
deleteAllCategories(ctpClient);
deleteTypes(ctpClient);
deleteSupplyChannels(ctpClient);
}

/**
Expand Down Expand Up @@ -108,4 +120,35 @@ public static ProductQuery getProductQuery() {
.plusExpansionPaths(channelExpansionModel ->
channelExpansionModel.masterData().staged().allVariants().prices().channel());
}

/**
* Gets the supplied {@link ProductDraft} with the price channel reference attached.
*
* @param productDraft TODO
* @param channelReference TODO
* @return TODO.
*/
public static ProductDraft getDraftWithPriceChannelReferences(@Nonnull final ProductDraft productDraft,
@Nonnull final Reference<Channel> channelReference) {
final List<ProductVariantDraft> allVariants = productDraft
.getVariants().stream().map(productVariant -> {
final List<PriceDraft> priceDraftsWithChannelReferences =
productVariant.getPrices().stream()
.map(price -> PriceDraftBuilder.of(price).channel(channelReference).build())
.collect(toList());
return ProductVariantDraftBuilder.of(productVariant)
.prices(priceDraftsWithChannelReferences)
.build();
})
.collect(toList());
final List<PriceDraft> masterVariantPriceDrafts = productDraft
.getMasterVariant().getPrices().stream().map(price -> PriceDraftBuilder.of(price)
.channel(channelReference)
.build()).collect(toList());
return ProductDraftBuilder.of(productDraft)
.masterVariant(ProductVariantDraftBuilder.of(productDraft.getMasterVariant())
.prices(masterVariantPriceDrafts).build())
.variants(allVariants)
.build();
}
}

0 comments on commit 2174972

Please sign in to comment.