Search in sources :

Example 16 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class CategoryUpdateActionUtilsTest method buildChangeOrderHintUpdateAction_WithNullValues_ShouldNotBuildUpdateActionAndCallCallback.

@Test
void buildChangeOrderHintUpdateAction_WithNullValues_ShouldNotBuildUpdateActionAndCallCallback() {
    when(MOCK_OLD_CATEGORY.getId()).thenReturn("oldCatId");
    final CategoryDraft newCategory = mock(CategoryDraft.class);
    when(newCategory.getOrderHint()).thenReturn(null);
    final ArrayList<Object> callBackResponse = new ArrayList<>();
    final TriConsumer<SyncException, Optional<CategoryDraft>, Optional<Category>> updateActionWarningCallBack = (exception, newResource, oldResource) -> callBackResponse.add(exception.getMessage());
    final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(CTP_CLIENT).warningCallback(updateActionWarningCallBack).build();
    final Optional<UpdateAction<Category>> changeOrderHintUpdateAction = buildChangeOrderHintUpdateAction(MOCK_OLD_CATEGORY, newCategory, categorySyncOptions);
    assertThat(changeOrderHintUpdateAction).isNotNull();
    assertThat(changeOrderHintUpdateAction).isNotPresent();
    assertThat(callBackResponse).hasSize(1);
    assertThat(callBackResponse.get(0)).isEqualTo("Cannot unset 'orderHint' field of category with id 'oldCatId'.");
}
Also used : CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction) CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) SetExternalId(io.sphere.sdk.categories.commands.updateactions.SetExternalId) CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CategoryUpdateActionUtils.buildChangeNameUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeNameUpdateAction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SetDescription(io.sphere.sdk.categories.commands.updateactions.SetDescription) UpdateAction(io.sphere.sdk.commands.UpdateAction) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) CategoryUpdateActionUtils.buildChangeParentUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeParentUpdateAction) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ChangeName(io.sphere.sdk.categories.commands.updateactions.ChangeName) SetMetaDescription(io.sphere.sdk.categories.commands.updateactions.SetMetaDescription) SetMetaKeywords(io.sphere.sdk.categories.commands.updateactions.SetMetaKeywords) ArrayList(java.util.ArrayList) SetMetaTitle(io.sphere.sdk.categories.commands.updateactions.SetMetaTitle) Locale(java.util.Locale) SphereClient(io.sphere.sdk.client.SphereClient) CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction) TriConsumer(com.commercetools.sync.commons.utils.TriConsumer) CategoryUpdateActionUtils.buildSetExternalIdUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetExternalIdUpdateAction) Category(io.sphere.sdk.categories.Category) ChangeOrderHint(io.sphere.sdk.categories.commands.updateactions.ChangeOrderHint) Mockito.when(org.mockito.Mockito.when) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) ChangeSlug(io.sphere.sdk.categories.commands.updateactions.ChangeSlug) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) CategoryUpdateActionUtils.buildSetDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetDescriptionUpdateAction) CATEGORY_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.CATEGORY_KEY_1_RESOURCE_PATH) Optional(java.util.Optional) ChangeParent(io.sphere.sdk.categories.commands.updateactions.ChangeParent) CategoryUpdateActionUtils.buildChangeSlugUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeSlugUpdateAction) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) Optional(java.util.Optional) CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction) CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction) CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction) CategoryUpdateActionUtils.buildChangeNameUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeNameUpdateAction) UpdateAction(io.sphere.sdk.commands.UpdateAction) CategoryUpdateActionUtils.buildChangeParentUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeParentUpdateAction) CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction) CategoryUpdateActionUtils.buildSetExternalIdUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetExternalIdUpdateAction) CategoryUpdateActionUtils.buildSetDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetDescriptionUpdateAction) CategoryUpdateActionUtils.buildChangeSlugUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeSlugUpdateAction) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ArrayList(java.util.ArrayList) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Test(org.junit.jupiter.api.Test)

Example 17 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class CategoryUpdateActionUtilsTest method buildChangeParentUpdateAction_WithBothNullValues_ShouldNotBuildUpdateActionAndNotCallCallback.

@Test
void buildChangeParentUpdateAction_WithBothNullValues_ShouldNotBuildUpdateActionAndNotCallCallback() {
    when(MOCK_OLD_CATEGORY.getId()).thenReturn("oldCatId");
    when(MOCK_OLD_CATEGORY.getParent()).thenReturn(null);
    final CategoryDraft newCategory = mock(CategoryDraft.class);
    when(newCategory.getParent()).thenReturn(null);
    final ArrayList<Object> callBackResponse = new ArrayList<>();
    final TriConsumer<SyncException, Optional<CategoryDraft>, Optional<Category>> updateActionWarningCallBack = (exception, newResource, oldResource) -> callBackResponse.add(exception.getMessage());
    final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(CTP_CLIENT).warningCallback(updateActionWarningCallBack).build();
    final Optional<UpdateAction<Category>> changeParentUpdateAction = buildChangeParentUpdateAction(MOCK_OLD_CATEGORY, newCategory, categorySyncOptions);
    assertThat(changeParentUpdateAction).isNotNull();
    assertThat(changeParentUpdateAction).isNotPresent();
    assertThat(callBackResponse).isEmpty();
}
Also used : CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction) CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) SetExternalId(io.sphere.sdk.categories.commands.updateactions.SetExternalId) CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CategoryUpdateActionUtils.buildChangeNameUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeNameUpdateAction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SetDescription(io.sphere.sdk.categories.commands.updateactions.SetDescription) UpdateAction(io.sphere.sdk.commands.UpdateAction) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) CategoryUpdateActionUtils.buildChangeParentUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeParentUpdateAction) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ChangeName(io.sphere.sdk.categories.commands.updateactions.ChangeName) SetMetaDescription(io.sphere.sdk.categories.commands.updateactions.SetMetaDescription) SetMetaKeywords(io.sphere.sdk.categories.commands.updateactions.SetMetaKeywords) ArrayList(java.util.ArrayList) SetMetaTitle(io.sphere.sdk.categories.commands.updateactions.SetMetaTitle) Locale(java.util.Locale) SphereClient(io.sphere.sdk.client.SphereClient) CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction) TriConsumer(com.commercetools.sync.commons.utils.TriConsumer) CategoryUpdateActionUtils.buildSetExternalIdUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetExternalIdUpdateAction) Category(io.sphere.sdk.categories.Category) ChangeOrderHint(io.sphere.sdk.categories.commands.updateactions.ChangeOrderHint) Mockito.when(org.mockito.Mockito.when) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) ChangeSlug(io.sphere.sdk.categories.commands.updateactions.ChangeSlug) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) CategoryUpdateActionUtils.buildSetDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetDescriptionUpdateAction) CATEGORY_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.CATEGORY_KEY_1_RESOURCE_PATH) Optional(java.util.Optional) ChangeParent(io.sphere.sdk.categories.commands.updateactions.ChangeParent) CategoryUpdateActionUtils.buildChangeSlugUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeSlugUpdateAction) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) Optional(java.util.Optional) CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction) CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction) CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction) CategoryUpdateActionUtils.buildChangeNameUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeNameUpdateAction) UpdateAction(io.sphere.sdk.commands.UpdateAction) CategoryUpdateActionUtils.buildChangeParentUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeParentUpdateAction) CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction) CategoryUpdateActionUtils.buildSetExternalIdUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetExternalIdUpdateAction) CategoryUpdateActionUtils.buildSetDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetDescriptionUpdateAction) CategoryUpdateActionUtils.buildChangeSlugUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeSlugUpdateAction) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ArrayList(java.util.ArrayList) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Test(org.junit.jupiter.api.Test)

Example 18 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class CategoryUpdateActionUtilsTest method buildChangeOrderHintUpdateAction_WithBothNullValues_ShouldNotBuildUpdateActionAndNotCallCallback.

@Test
void buildChangeOrderHintUpdateAction_WithBothNullValues_ShouldNotBuildUpdateActionAndNotCallCallback() {
    when(MOCK_OLD_CATEGORY.getId()).thenReturn("oldCatId");
    when(MOCK_OLD_CATEGORY.getOrderHint()).thenReturn(null);
    final CategoryDraft newCategory = mock(CategoryDraft.class);
    when(newCategory.getOrderHint()).thenReturn(null);
    final ArrayList<Object> callBackResponse = new ArrayList<>();
    final TriConsumer<SyncException, Optional<CategoryDraft>, Optional<Category>> updateActionWarningCallBack = (exception, newResource, oldResource) -> callBackResponse.add(exception.getMessage());
    final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(CTP_CLIENT).warningCallback(updateActionWarningCallBack).build();
    final Optional<UpdateAction<Category>> changeOrderHintUpdateAction = buildChangeOrderHintUpdateAction(MOCK_OLD_CATEGORY, newCategory, categorySyncOptions);
    assertThat(changeOrderHintUpdateAction).isNotNull();
    assertThat(changeOrderHintUpdateAction).isNotPresent();
    assertThat(callBackResponse).isEmpty();
}
Also used : CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction) CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) SetExternalId(io.sphere.sdk.categories.commands.updateactions.SetExternalId) CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CategoryUpdateActionUtils.buildChangeNameUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeNameUpdateAction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SetDescription(io.sphere.sdk.categories.commands.updateactions.SetDescription) UpdateAction(io.sphere.sdk.commands.UpdateAction) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) CategoryUpdateActionUtils.buildChangeParentUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeParentUpdateAction) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ChangeName(io.sphere.sdk.categories.commands.updateactions.ChangeName) SetMetaDescription(io.sphere.sdk.categories.commands.updateactions.SetMetaDescription) SetMetaKeywords(io.sphere.sdk.categories.commands.updateactions.SetMetaKeywords) ArrayList(java.util.ArrayList) SetMetaTitle(io.sphere.sdk.categories.commands.updateactions.SetMetaTitle) Locale(java.util.Locale) SphereClient(io.sphere.sdk.client.SphereClient) CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction) TriConsumer(com.commercetools.sync.commons.utils.TriConsumer) CategoryUpdateActionUtils.buildSetExternalIdUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetExternalIdUpdateAction) Category(io.sphere.sdk.categories.Category) ChangeOrderHint(io.sphere.sdk.categories.commands.updateactions.ChangeOrderHint) Mockito.when(org.mockito.Mockito.when) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) ChangeSlug(io.sphere.sdk.categories.commands.updateactions.ChangeSlug) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) CategoryUpdateActionUtils.buildSetDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetDescriptionUpdateAction) CATEGORY_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.CATEGORY_KEY_1_RESOURCE_PATH) Optional(java.util.Optional) ChangeParent(io.sphere.sdk.categories.commands.updateactions.ChangeParent) CategoryUpdateActionUtils.buildChangeSlugUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeSlugUpdateAction) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) Optional(java.util.Optional) CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaDescriptionUpdateAction) CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaTitleUpdateAction) CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetMetaKeywordsUpdateAction) CategoryUpdateActionUtils.buildChangeNameUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeNameUpdateAction) UpdateAction(io.sphere.sdk.commands.UpdateAction) CategoryUpdateActionUtils.buildChangeParentUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeParentUpdateAction) CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeOrderHintUpdateAction) CategoryUpdateActionUtils.buildSetExternalIdUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetExternalIdUpdateAction) CategoryUpdateActionUtils.buildSetDescriptionUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildSetDescriptionUpdateAction) CategoryUpdateActionUtils.buildChangeSlugUpdateAction(com.commercetools.sync.categories.utils.CategoryUpdateActionUtils.buildChangeSlugUpdateAction) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ArrayList(java.util.ArrayList) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Test(org.junit.jupiter.api.Test)

Example 19 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class ProductVariantUpdateActionUtils method buildProductVariantPricesUpdateActions.

/**
 * Compares the {@link List} of {@link io.sphere.sdk.products.Price}s of a {@link
 * ProductVariantDraft} and a {@link ProductVariant} and returns a {@link List} of {@link
 * UpdateAction}&lt;{@link Product}&gt;. If both the {@link ProductVariantDraft} and the {@link
 * ProductVariant} have identical list of prices, then no update action is needed and hence an
 * empty {@link List} is returned.
 *
 * @param oldProduct the product which should be updated.
 * @param newProduct the product draft.
 * @param oldProductVariant the {@link ProductVariant} which should be updated.
 * @param newProductVariant the {@link ProductVariantDraft} where we get the new list of prices.
 * @param syncOptions the sync options wrapper which contains options related to the sync process
 *     supplied by the user. For example, custom callbacks to call in case of warnings or errors
 *     occurring on the build update action process. And other options (See {@link
 *     ProductSyncOptions} for more info).
 * @return a list that contains all the update actions needed, otherwise an empty list if no
 *     update actions are needed.
 */
@Nonnull
public static List<UpdateAction<Product>> buildProductVariantPricesUpdateActions(@Nullable final ProductProjection oldProduct, @Nonnull final ProductDraft newProduct, @Nonnull final ProductVariant oldProductVariant, @Nonnull final ProductVariantDraft newProductVariant, @Nonnull final ProductSyncOptions syncOptions) {
    final List<Price> oldPrices = oldProductVariant.getPrices();
    final List<PriceDraft> newPrices = newProductVariant.getPrices();
    final List<UpdateAction<Product>> updateActions = buildRemoveUpdateActions(oldPrices, newPrices, PriceCompositeId::of, PriceCompositeId::of, price -> RemovePrice.of(price, true));
    final Integer variantId = oldProductVariant.getId();
    final Map<PriceCompositeId, Price> oldPricesMap = collectionToMap(oldPrices, PriceCompositeId::of);
    emptyIfNull(newPrices).forEach(newPrice -> {
        if (newPrice == null) {
            syncOptions.applyErrorCallback(new SyncException(format("Failed to build prices update actions " + "for one price on the variant with id '%d' and key '%s'. Reason: %s", variantId, oldProductVariant.getKey(), NULL_PRODUCT_VARIANT_PRICE)), oldProduct, newProduct, null);
        } else {
            final PriceCompositeId newPriceCompositeId = PriceCompositeId.of(newPrice);
            final Price matchingOldPrice = oldPricesMap.get(newPriceCompositeId);
            final List<UpdateAction<Product>> updateOrAddPrice = ofNullable(matchingOldPrice).map(oldPrice -> buildActions(newProduct, variantId, oldPrice, newPrice, syncOptions)).orElseGet(() -> singletonList(AddPrice.ofVariantId(variantId, newPrice, true)));
            updateActions.addAll(updateOrAddPrice);
        }
    });
    return sortPriceActions(updateActions);
}
Also used : ProductVariant(io.sphere.sdk.products.ProductVariant) AddExternalImage(io.sphere.sdk.products.commands.updateactions.AddExternalImage) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CollectionUtils.collectionToMap(com.commercetools.sync.commons.utils.CollectionUtils.collectionToMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) HashMap(java.util.HashMap) PriceCompositeId(com.commercetools.sync.internals.helpers.PriceCompositeId) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) ProductAssetActionFactory(com.commercetools.sync.products.helpers.ProductAssetActionFactory) UnorderedCollectionSyncUtils.buildRemoveUpdateActions(com.commercetools.sync.internals.utils.UnorderedCollectionSyncUtils.buildRemoveUpdateActions) Attribute(io.sphere.sdk.products.attributes.Attribute) RemovePrice(io.sphere.sdk.products.commands.updateactions.RemovePrice) CollectionUtils.emptyIfNull(com.commercetools.sync.commons.utils.CollectionUtils.emptyIfNull) ProductVariantPriceUpdateActionUtils.buildActions(com.commercetools.sync.products.utils.ProductVariantPriceUpdateActionUtils.buildActions) Map(java.util.Map) CollectionUtils.filterCollection(com.commercetools.sync.commons.utils.CollectionUtils.filterCollection) Image(io.sphere.sdk.products.Image) SetSku(io.sphere.sdk.products.commands.updateactions.SetSku) AssetsUpdateActionUtils.buildAssetsUpdateActions(com.commercetools.sync.commons.utils.AssetsUpdateActionUtils.buildAssetsUpdateActions) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) SetAttribute(io.sphere.sdk.products.commands.updateactions.SetAttribute) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) AssetDraft(io.sphere.sdk.models.AssetDraft) ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction(com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) MoveImageToPosition(io.sphere.sdk.products.commands.updateactions.MoveImageToPosition) RemoveImage(io.sphere.sdk.products.commands.updateactions.RemoveImage) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) Price(io.sphere.sdk.products.Price) ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA(com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) PriceDraft(io.sphere.sdk.products.PriceDraft) Product(io.sphere.sdk.products.Product) String.format(java.lang.String.format) Objects(java.util.Objects) List(java.util.List) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) AddPrice(io.sphere.sdk.products.commands.updateactions.AddPrice) Optional(java.util.Optional) UpdateActionsSortUtils.sortPriceActions(com.commercetools.sync.internals.utils.UpdateActionsSortUtils.sortPriceActions) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) RemovePrice(io.sphere.sdk.products.commands.updateactions.RemovePrice) Price(io.sphere.sdk.products.Price) AddPrice(io.sphere.sdk.products.commands.updateactions.AddPrice) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction(com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) PriceCompositeId(com.commercetools.sync.internals.helpers.PriceCompositeId) PriceDraft(io.sphere.sdk.products.PriceDraft) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Nonnull(javax.annotation.Nonnull)

Example 20 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class ProductVariantUpdateActionUtils method buildProductVariantAttributesUpdateActions.

/**
 * Compares the attributes of a {@link ProductVariantDraft} and a {@link ProductVariant} to build
 * either {@link io.sphere.sdk.products.commands.updateactions.SetAttribute} or {@link
 * io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants} update actions. If
 * both the {@link ProductVariantDraft} and the {@link ProductVariant} have identical list of
 * attributes, then no update action is needed and hence an empty {@link List} is returned.
 *
 * @param oldProduct the product that the variants belong to. It is used only in the error
 *     messages if any.
 * @param newProduct the new product draft.
 * @param oldProductVariant the {@link ProductVariant} which should be updated.
 * @param newProductVariant the {@link ProductVariantDraft} where we get the new list of
 *     attributes.
 * @param attributesMetaData a map of attribute name -&gt; {@link AttributeMetaData}; which
 *     defines attribute information: its name, whether a value is required or not and whether it
 *     has the constraint "SameForAll" or not.
 * @param syncOptions the sync options wrapper which contains options related to the sync process
 *     supplied by the user. For example, custom callbacks to call in case of warnings or errors
 *     occurring on the build update action process. And other options (See {@link
 *     ProductSyncOptions} for more info).
 * @return a list that contains all the update actions needed, otherwise an empty list if no
 *     update actions are needed.
 */
@Nonnull
public static List<UpdateAction<Product>> buildProductVariantAttributesUpdateActions(@Nonnull final ProductProjection oldProduct, @Nonnull final ProductDraft newProduct, @Nonnull final ProductVariant oldProductVariant, @Nonnull final ProductVariantDraft newProductVariant, @Nonnull final Map<String, AttributeMetaData> attributesMetaData, @Nonnull final ProductSyncOptions syncOptions) {
    final String productKey = oldProduct.getKey();
    final Integer oldProductVariantId = oldProductVariant.getId();
    final List<AttributeDraft> newProductVariantAttributes = newProductVariant.getAttributes();
    final List<Attribute> oldProductVariantAttributes = oldProductVariant.getAttributes();
    final List<UpdateAction<Product>> updateActions = buildRemoveUpdateActions(oldProductVariantAttributes, newProductVariantAttributes, Attribute::getName, AttributeDraft::getName, attribute -> {
        try {
            return buildUnSetAttribute(oldProductVariantId, attribute.getName(), attributesMetaData);
        } catch (final BuildUpdateActionException buildUpdateActionException) {
            final String errorMessage = format(FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION, attribute.getName(), newProductVariant.getKey(), productKey, buildUpdateActionException.getMessage());
            syncOptions.applyErrorCallback(new SyncException(errorMessage, new BuildUpdateActionException(errorMessage, buildUpdateActionException)), oldProduct, newProduct, null);
            return null;
        }
    });
    final Map<String, Attribute> oldAttributesMap = collectionToMap(oldProductVariantAttributes, Attribute::getName);
    emptyIfNull(newProductVariantAttributes).forEach(newAttribute -> {
        if (newAttribute == null) {
            final String errorMessage = format(FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION, null, newProductVariant.getKey(), productKey, NULL_PRODUCT_VARIANT_ATTRIBUTE);
            syncOptions.applyErrorCallback(new SyncException(errorMessage, new BuildUpdateActionException(errorMessage)), oldProduct, newProduct, updateActions);
        } else {
            final String newAttributeName = newAttribute.getName();
            final Attribute matchingOldAttribute = oldAttributesMap.get(newAttributeName);
            try {
                buildProductVariantAttributeUpdateAction(oldProductVariantId, matchingOldAttribute, newAttribute, attributesMetaData).ifPresent(updateActions::add);
            } catch (final BuildUpdateActionException buildUpdateActionException) {
                final String errorMessage = format(FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION, newAttributeName, newProductVariant.getKey(), productKey, buildUpdateActionException.getMessage());
                syncOptions.applyErrorCallback(new SyncException(errorMessage, new BuildUpdateActionException(errorMessage, buildUpdateActionException)));
            }
        }
    });
    return updateActions;
}
Also used : AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) Attribute(io.sphere.sdk.products.attributes.Attribute) SetAttribute(io.sphere.sdk.products.commands.updateactions.SetAttribute) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction(com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Nonnull(javax.annotation.Nonnull)

Aggregations

SyncException (com.commercetools.sync.commons.exceptions.SyncException)74 Nonnull (javax.annotation.Nonnull)47 UpdateAction (io.sphere.sdk.commands.UpdateAction)45 Optional (java.util.Optional)39 List (java.util.List)37 CompletionStage (java.util.concurrent.CompletionStage)25 SphereClient (io.sphere.sdk.client.SphereClient)24 String.format (java.lang.String.format)23 ArrayList (java.util.ArrayList)21 Map (java.util.Map)20 Test (org.junit.jupiter.api.Test)20 TriConsumer (com.commercetools.sync.commons.utils.TriConsumer)18 Set (java.util.Set)17 CompletableFuture (java.util.concurrent.CompletableFuture)17 Nullable (javax.annotation.Nullable)16 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Category (io.sphere.sdk.categories.Category)13 BaseSync (com.commercetools.sync.commons.BaseSync)12 QuadConsumer (com.commercetools.sync.commons.utils.QuadConsumer)12