Search in sources :

Example 6 with BuildUpdateActionException

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

the class AttributeDefinitionsUpdateActionUtils method buildUpdateActions.

/**
 * Compares a list of {@link AttributeDefinition}s with a list of {@link
 * AttributeDefinitionDraft}s. The method serves as an implementation for attribute definitions
 * syncing. The method takes in functions for building the required update actions (AddAttribute,
 * RemoveAttribute, ChangeAttributeOrder and 1-1 update actions on attribute definitions (e.g.
 * changeAttributeName, changeAttributeLabel, etc..) for the required resource.
 *
 * @param oldAttributeDefinitions the old list of attribute definitions.
 * @param newAttributeDefinitionsDrafts the new list of attribute definitions drafts.
 * @return a list of attribute definitions update actions if the list of attribute definitions is
 *     not identical. Otherwise, if the attribute definitions are identical, an empty list is
 *     returned.
 * @throws BuildUpdateActionException in case there are attribute definitions drafts with
 *     duplicate names, enums duplicate keys or unsupported attribute definition type change.
 */
@Nonnull
private static List<UpdateAction<ProductType>> buildUpdateActions(@Nonnull final List<AttributeDefinition> oldAttributeDefinitions, @Nonnull final List<AttributeDefinitionDraft> newAttributeDefinitionsDrafts) throws BuildUpdateActionException {
    try {
        final List<UpdateAction<ProductType>> updateActions = buildRemoveAttributeDefinitionOrAttributeDefinitionUpdateActions(oldAttributeDefinitions, newAttributeDefinitionsDrafts);
        updateActions.addAll(buildAddAttributeDefinitionUpdateActions(oldAttributeDefinitions, newAttributeDefinitionsDrafts));
        buildChangeAttributeDefinitionOrderUpdateAction(oldAttributeDefinitions, newAttributeDefinitionsDrafts).ifPresent(updateActions::add);
        return updateActions;
    } catch (final DuplicateNameException | DuplicateKeyException | UnsupportedOperationException exception) {
        throw new BuildUpdateActionException(exception);
    }
}
Also used : UpdateAction(io.sphere.sdk.commands.UpdateAction) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) DuplicateNameException(com.commercetools.sync.commons.exceptions.DuplicateNameException) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) DuplicateKeyException(com.commercetools.sync.commons.exceptions.DuplicateKeyException) Nonnull(javax.annotation.Nonnull)

Example 7 with BuildUpdateActionException

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

the class CustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithNullNewCategoryTypeId_ShouldBuildUpdateActions.

@Test
void buildNonNullCustomFieldsUpdateActions_WithNullNewCategoryTypeId_ShouldBuildUpdateActions() throws BuildUpdateActionException {
    // Mock old CustomFields
    final CustomFields oldCustomFieldsMock = mock(CustomFields.class);
    when(oldCustomFieldsMock.getType()).thenReturn(Type.referenceOfId("1"));
    // Mock new CustomFieldsDraft
    final CustomFieldsDraft newCustomFieldsMock = mock(CustomFieldsDraft.class);
    when(newCustomFieldsMock.getType()).thenReturn(Type.referenceOfId(null));
    // Mock custom options error callback
    final ArrayList<String> errorMessages = new ArrayList<>();
    final QuadConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>, List<UpdateAction<Product>>> errorCallback = (exception, newResource, oldResource, updateActions) -> errorMessages.add(exception.getMessage());
    // Mock sync options
    final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(CTP_CLIENT).errorCallback(errorCallback).build();
    final Price price = mock(Price.class);
    when(price.getId()).thenReturn(UUID.randomUUID().toString());
    final List<UpdateAction<Product>> updateActions = buildNonNullCustomFieldsUpdateActions(oldCustomFieldsMock, newCustomFieldsMock, price, new PriceCustomActionBuilder(), 1, Price::getId, priceResource -> Price.resourceTypeId(), Price::getId, productSyncOptions);
    assertThat(errorMessages).hasSize(1);
    assertThat(errorMessages.get(0)).isEqualTo(format("Failed to build 'setCustomType' update action on the " + "%s with id '%s'. Reason: New Custom Type id is blank (null/empty).", Price.resourceTypeId(), price.getId()));
    assertThat(updateActions).isEmpty();
}
Also used : CustomUpdateActionUtils.buildCustomUpdateActions(com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildCustomUpdateActions) SetProductPriceCustomType(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomType) AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Reference(io.sphere.sdk.models.Reference) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) Map(java.util.Map) CategoryCustomActionBuilder(com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder) SphereClient(io.sphere.sdk.client.SphereClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) AssetDraft(io.sphere.sdk.models.AssetDraft) AssetDraftBuilder(io.sphere.sdk.models.AssetDraftBuilder) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) Price(io.sphere.sdk.products.Price) CustomFieldsDraft.ofTypeKeyAndJson(io.sphere.sdk.types.CustomFieldsDraft.ofTypeKeyAndJson) Product(io.sphere.sdk.products.Product) UUID(java.util.UUID) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Optional(java.util.Optional) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) CustomFields(io.sphere.sdk.types.CustomFields) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CustomUpdateActionUtils.buildNonNullCustomFieldsUpdateActions(com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildNonNullCustomFieldsUpdateActions) SetAssetCustomField(io.sphere.sdk.products.commands.updateactions.SetAssetCustomField) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SetAssetCustomType(io.sphere.sdk.products.commands.updateactions.SetAssetCustomType) CustomUpdateActionUtils.buildSetCustomFieldsUpdateActions(com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildSetCustomFieldsUpdateActions) Mockito.when(org.mockito.Mockito.when) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) Resource(io.sphere.sdk.models.Resource) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) SetProductPriceCustomField(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomField) Asset(io.sphere.sdk.models.Asset) Type(io.sphere.sdk.types.Type) Optional(java.util.Optional) UpdateAction(io.sphere.sdk.commands.UpdateAction) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) ArrayList(java.util.ArrayList) Product(io.sphere.sdk.products.Product) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CustomFields(io.sphere.sdk.types.CustomFields) Price(io.sphere.sdk.products.Price) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) Test(org.junit.jupiter.api.Test)

Aggregations

BuildUpdateActionException (com.commercetools.sync.commons.exceptions.BuildUpdateActionException)7 UpdateAction (io.sphere.sdk.commands.UpdateAction)6 Nonnull (javax.annotation.Nonnull)6 SyncException (com.commercetools.sync.commons.exceptions.SyncException)4 CommonTypeUpdateActionUtils.buildUpdateAction (com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction)4 DuplicateKeyException (com.commercetools.sync.commons.exceptions.DuplicateKeyException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 DuplicateNameException (com.commercetools.sync.commons.exceptions.DuplicateNameException)2 Asset (io.sphere.sdk.models.Asset)2 AssetDraft (io.sphere.sdk.models.AssetDraft)2 CustomFields (io.sphere.sdk.types.CustomFields)2 CustomFieldsDraft (io.sphere.sdk.types.CustomFieldsDraft)2 CategorySyncOptions (com.commercetools.sync.categories.CategorySyncOptions)1 CategorySyncOptionsBuilder (com.commercetools.sync.categories.CategorySyncOptionsBuilder)1 CategoryCustomActionBuilder (com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder)1 CustomUpdateActionUtils.buildCustomUpdateActions (com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildCustomUpdateActions)1 CustomUpdateActionUtils.buildNonNullCustomFieldsUpdateActions (com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildNonNullCustomFieldsUpdateActions)1 CustomUpdateActionUtils.buildSetCustomFieldsUpdateActions (com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildSetCustomFieldsUpdateActions)1 GenericUpdateActionUtils.buildTypedSetCustomTypeUpdateAction (com.commercetools.sync.commons.utils.GenericUpdateActionUtils.buildTypedSetCustomTypeUpdateAction)1 AttributeMetaData (com.commercetools.sync.products.AttributeMetaData)1