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'.");
}
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();
}
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();
}
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}<{@link Product}>. 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);
}
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 -> {@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;
}
Aggregations