Search in sources :

Example 1 with DuplicateNameException

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

the class FieldDefinitionsUpdateActionUtils method buildRemoveFieldDefinitionOrFieldDefinitionUpdateActions.

/**
 * Checks if there are any field definitions which are not existing in the {@code
 * newFieldDefinitions}. If there are, then "remove" field definition update actions are built.
 * Otherwise, if the field definition still exists in the new field definition, then compare the
 * field definition fields (label, etc..), and add the computed actions to the list of update
 * actions.
 *
 * <p>Note: If the field type field is different, the old field definition is removed and the new
 * field definition is added with the new field type.
 *
 * @param oldFieldDefinitions the list of old {@link FieldDefinition}s.
 * @param newFieldDefinitions the list of new {@link FieldDefinition}s.
 * @return a list of field definition update actions if there are field that are not existing in
 *     the new draft. If the field definition still exists in the new draft, then compare the
 *     field definition fields (name, label, etc..), and add the computed actions to the list of
 *     update actions. Otherwise, if the field definitions are identical, an empty optional is
 *     returned.
 * @throws DuplicateNameException in case there are field definitions drafts with duplicate names.
 * @throws DuplicateKeyException in case there are enum values with duplicate keys.
 */
@Nonnull
private static List<UpdateAction<Type>> buildRemoveFieldDefinitionOrFieldDefinitionUpdateActions(@Nonnull final List<FieldDefinition> oldFieldDefinitions, @Nonnull final List<FieldDefinition> newFieldDefinitions) {
    final Map<String, FieldDefinition> newFieldDefinitionsNameMap = newFieldDefinitions.stream().collect(toMap(FieldDefinition::getName, fieldDefinition -> fieldDefinition, (fieldDefinitionA, fieldDefinitionB) -> {
        throw new DuplicateNameException(format("Field definitions have duplicated names. " + "Duplicated field definition name: '%s'. Field definitions names are " + "expected to be unique inside their type.", fieldDefinitionA.getName()));
    }));
    return oldFieldDefinitions.stream().map(oldFieldDefinition -> {
        final String oldFieldDefinitionName = oldFieldDefinition.getName();
        final FieldDefinition matchingNewFieldDefinition = newFieldDefinitionsNameMap.get(oldFieldDefinitionName);
        return ofNullable(matchingNewFieldDefinition).map(newFieldDefinition -> {
            if (newFieldDefinition.getType() != null) {
                // field type is required so if null we let CTP to throw exception
                if (haveSameFieldType(oldFieldDefinition.getType(), newFieldDefinition.getType())) {
                    return buildActions(oldFieldDefinition, newFieldDefinition);
                } else {
                    // type
                    return Arrays.asList(RemoveFieldDefinition.of(oldFieldDefinitionName), AddFieldDefinition.of(newFieldDefinition));
                }
            } else {
                return new ArrayList<UpdateAction<Type>>();
            }
        }).orElseGet(() -> singletonList(RemoveFieldDefinition.of(oldFieldDefinitionName)));
    }).flatMap(Collection::stream).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) UpdateAction(io.sphere.sdk.commands.UpdateAction) Collections.singletonList(java.util.Collections.singletonList) FieldDefinition(io.sphere.sdk.types.FieldDefinition) ArrayList(java.util.ArrayList) FieldDefinitionUpdateActionUtils.buildActions(com.commercetools.sync.types.utils.FieldDefinitionUpdateActionUtils.buildActions) Collectors.toMap(java.util.stream.Collectors.toMap) DuplicateKeyException(com.commercetools.sync.commons.exceptions.DuplicateKeyException) Map(java.util.Map) RemoveFieldDefinition(io.sphere.sdk.types.commands.updateactions.RemoveFieldDefinition) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) FieldType(io.sphere.sdk.types.FieldType) ChangeFieldDefinitionOrder(io.sphere.sdk.types.commands.updateactions.ChangeFieldDefinitionOrder) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) AddFieldDefinition(io.sphere.sdk.types.commands.updateactions.AddFieldDefinition) DuplicateNameException(com.commercetools.sync.commons.exceptions.DuplicateNameException) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) Optional(java.util.Optional) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) Type(io.sphere.sdk.types.Type) DuplicateNameException(com.commercetools.sync.commons.exceptions.DuplicateNameException) UpdateAction(io.sphere.sdk.commands.UpdateAction) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) FieldDefinition(io.sphere.sdk.types.FieldDefinition) RemoveFieldDefinition(io.sphere.sdk.types.commands.updateactions.RemoveFieldDefinition) AddFieldDefinition(io.sphere.sdk.types.commands.updateactions.AddFieldDefinition) Nonnull(javax.annotation.Nonnull)

Example 2 with DuplicateNameException

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

the class FieldDefinitionsUpdateActionUtils method buildUpdateActions.

/**
 * Compares a list of {@link FieldDefinition}s with a list of {@link FieldDefinition}s. The method
 * serves as an implementation for field definitions syncing and building the required update
 * actions (AddFieldDefinition, RemoveFieldDefinition, ChangeFieldDefinitionOrder) and 1-1 update
 * actions on field definitions (e.g. changeFieldDefinitionLabel, etc..) for the required
 * resource.
 *
 * @param oldFieldDefinitions the old list of field definitions.
 * @param newFieldDefinitions the new list of field definitions drafts.
 * @return a list of field definitions update actions if the list of field definitions is not
 *     identical. Otherwise, if the field definitions are identical, an empty list is returned.
 * @throws BuildUpdateActionException in case there are field definitions with duplicate names or
 *     enums duplicate keys.
 */
@Nonnull
private static List<UpdateAction<Type>> buildUpdateActions(@Nonnull final List<FieldDefinition> oldFieldDefinitions, @Nonnull final List<FieldDefinition> newFieldDefinitions) throws BuildUpdateActionException {
    try {
        final List<UpdateAction<Type>> updateActions = buildRemoveFieldDefinitionOrFieldDefinitionUpdateActions(oldFieldDefinitions, newFieldDefinitions);
        updateActions.addAll(buildAddFieldDefinitionUpdateActions(oldFieldDefinitions, newFieldDefinitions));
        buildChangeFieldDefinitionOrderUpdateAction(oldFieldDefinitions, newFieldDefinitions).ifPresent(updateActions::add);
        return updateActions;
    } catch (final DuplicateNameException | DuplicateKeyException 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 3 with DuplicateNameException

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

the class BuildFieldDefinitionUpdateActionsTest method buildFieldDefinitionsUpdateActions_WithDuplicateFieldDefNames_ShouldNotBuildActionsAndTriggerErrorCb.

@Test
void buildFieldDefinitionsUpdateActions_WithDuplicateFieldDefNames_ShouldNotBuildActionsAndTriggerErrorCb() {
    final Type oldType = readObjectFromResource(TYPE_WITH_FIELDS_ABC, Type.class);
    final TypeDraft newTypeDraft = readObjectFromResource(TYPE_WITH_FIELDS_ABB, TypeDraft.class);
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final TypeSyncOptions syncOptions = TypeSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception.getCause());
    }).build();
    final List<UpdateAction<Type>> updateActions = buildFieldDefinitionsUpdateActions(oldType, newTypeDraft, syncOptions);
    assertThat(updateActions).isEmpty();
    assertThat(errorMessages).hasSize(1);
    assertThat(errorMessages.get(0)).matches("Failed to build update actions for the field definitions of the " + "type with the key 'key'. Reason: .*DuplicateNameException: Field definitions " + "have duplicated names. Duplicated field definition name: 'b'. Field definitions names are " + "expected to be unique inside their type.");
    assertThat(exceptions).hasSize(1);
    assertThat(exceptions.get(0)).isExactlyInstanceOf(BuildUpdateActionException.class);
    assertThat(exceptions.get(0).getMessage()).contains("Field definitions have duplicated names. " + "Duplicated field definition name: 'b'. Field definitions names are expected to be unique " + "inside their type.");
    assertThat(exceptions.get(0).getCause()).isExactlyInstanceOf(DuplicateNameException.class);
}
Also used : TypeDraft(io.sphere.sdk.types.TypeDraft) TypeSyncOptionsBuilder(com.commercetools.sync.types.TypeSyncOptionsBuilder) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) ResourceTypeIdsSetBuilder(io.sphere.sdk.types.ResourceTypeIdsSetBuilder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) ChangeFieldDefinitionLabel(io.sphere.sdk.types.commands.updateactions.ChangeFieldDefinitionLabel) Collections.singletonList(java.util.Collections.singletonList) TypeSyncOptions(com.commercetools.sync.types.TypeSyncOptions) FieldDefinition(io.sphere.sdk.types.FieldDefinition) ArrayList(java.util.ArrayList) LocalizedEnumFieldType(io.sphere.sdk.types.LocalizedEnumFieldType) ChangeLocalizedEnumValueOrder(io.sphere.sdk.types.commands.updateactions.ChangeLocalizedEnumValueOrder) Arrays.asList(java.util.Arrays.asList) SphereClient(io.sphere.sdk.client.SphereClient) EnumValue(io.sphere.sdk.models.EnumValue) TypeUpdateActionUtils.buildFieldDefinitionsUpdateActions(com.commercetools.sync.types.utils.TypeUpdateActionUtils.buildFieldDefinitionsUpdateActions) TypeDraftBuilder(io.sphere.sdk.types.TypeDraftBuilder) AddLocalizedEnumValue(io.sphere.sdk.types.commands.updateactions.AddLocalizedEnumValue) ChangeEnumValueOrder(io.sphere.sdk.types.commands.updateactions.ChangeEnumValueOrder) RemoveFieldDefinition(io.sphere.sdk.types.commands.updateactions.RemoveFieldDefinition) StringFieldType(io.sphere.sdk.types.StringFieldType) AddEnumValue(io.sphere.sdk.types.commands.updateactions.AddEnumValue) FieldDefinitionFixtures(com.commercetools.sync.types.utils.FieldDefinitionFixtures) Collections.emptySet(java.util.Collections.emptySet) ChangeFieldDefinitionOrder(io.sphere.sdk.types.commands.updateactions.ChangeFieldDefinitionOrder) Collections.emptyList(java.util.Collections.emptyList) LocalizedEnumValue(io.sphere.sdk.models.LocalizedEnumValue) AddFieldDefinition(io.sphere.sdk.types.commands.updateactions.AddFieldDefinition) Mockito.when(org.mockito.Mockito.when) DuplicateNameException(com.commercetools.sync.commons.exceptions.DuplicateNameException) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) TextInputHint(io.sphere.sdk.models.TextInputHint) EnumFieldType(io.sphere.sdk.types.EnumFieldType) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) SetFieldType(io.sphere.sdk.types.SetFieldType) Mockito.mock(org.mockito.Mockito.mock) Type(io.sphere.sdk.types.Type) LocalizedEnumFieldType(io.sphere.sdk.types.LocalizedEnumFieldType) StringFieldType(io.sphere.sdk.types.StringFieldType) EnumFieldType(io.sphere.sdk.types.EnumFieldType) SetFieldType(io.sphere.sdk.types.SetFieldType) Type(io.sphere.sdk.types.Type) UpdateAction(io.sphere.sdk.commands.UpdateAction) SphereClient(io.sphere.sdk.client.SphereClient) ArrayList(java.util.ArrayList) TypeSyncOptions(com.commercetools.sync.types.TypeSyncOptions) LocalizedString(io.sphere.sdk.models.LocalizedString) TypeDraft(io.sphere.sdk.types.TypeDraft) Test(org.junit.jupiter.api.Test)

Example 4 with DuplicateNameException

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

the class AttributeDefinitionsUpdateActionUtils method buildRemoveAttributeDefinitionOrAttributeDefinitionUpdateActions.

/**
 * Checks if there are any attribute definitions which are not existing in the {@code
 * newAttributeDefinitionsDrafts}. If there are, then "remove" attribute definition update actions
 * are built. Otherwise, if the attribute definition still exists in the new draft, then compare
 * the attribute definition fields (name, label, etc..), and add the computed actions to the list
 * of update actions.
 *
 * @param oldAttributeDefinitions the list of old {@link AttributeDefinition}s.
 * @param newAttributeDefinitionsDrafts the list of new {@link AttributeDefinitionDraft}s.
 * @return a list of attribute definition update actions if there are attribute definitions that
 *     are not existing in the new draft. If the attribute definition still exists in the new
 *     draft, then compare the attribute definition fields (name, label, etc..), and add the
 *     computed actions to the list of update actions. Otherwise, if the attribute definitions are
 *     identical, an empty optional is returned.
 * @throws DuplicateNameException in case there are attribute definitions drafts with duplicate
 *     names.
 * @throws DuplicateKeyException in case there are enum values with duplicate keys.
 * @throws UnsupportedOperationException in case the attribute type field changes.
 */
@Nonnull
private static List<UpdateAction<ProductType>> buildRemoveAttributeDefinitionOrAttributeDefinitionUpdateActions(@Nonnull final List<AttributeDefinition> oldAttributeDefinitions, @Nonnull final List<AttributeDefinitionDraft> newAttributeDefinitionsDrafts) {
    final Map<String, AttributeDefinitionDraft> newAttributesDefinitionsDraftsNameMap = newAttributeDefinitionsDrafts.stream().collect(toMap(AttributeDefinitionDraft::getName, attributeDefinitionDraft -> attributeDefinitionDraft, (attributeDefinitionDraftA, attributeDefinitionDraftB) -> {
        throw new DuplicateNameException(format("Attribute definitions drafts have duplicated names. " + "Duplicated attribute definition name: '%s'. " + "Attribute definitions names are expected to be unique inside their product type.", attributeDefinitionDraftA.getName()));
    }));
    return oldAttributeDefinitions.stream().map(oldAttributeDefinition -> {
        final String oldAttributeDefinitionName = oldAttributeDefinition.getName();
        final AttributeDefinitionDraft matchingNewAttributeDefinitionDraft = newAttributesDefinitionsDraftsNameMap.get(oldAttributeDefinitionName);
        return ofNullable(matchingNewAttributeDefinitionDraft).map(attributeDefinitionDraft -> {
            // exception
            if (attributeDefinitionDraft.getAttributeType() != null) {
                if (haveSameAttributeType(oldAttributeDefinition.getAttributeType(), matchingNewAttributeDefinitionDraft.getAttributeType())) {
                    return buildActions(oldAttributeDefinition, attributeDefinitionDraft);
                } else {
                    throw new UnsupportedOperationException(format("Due to eventual consistency of 'removeAttributeDefinition' action, " + "changing the attribute definition type (attribute name='%s') is not " + "supported programmatically. " + "Please apply the attribute definition type changes " + "manually through commercetools API or merchant center. " + "For more information please check: https://github.com/commercetools/commercetools-sync-java/blob/master/docs/adr/0003-syncing-attribute-type-changes.md", oldAttributeDefinitionName));
                }
            } else {
                return new ArrayList<UpdateAction<ProductType>>();
            }
        }).orElseGet(() -> singletonList(RemoveAttributeDefinition.of(oldAttributeDefinitionName)));
    }).flatMap(Collection::stream).collect(Collectors.toList());
}
Also used : ProductType(io.sphere.sdk.producttypes.ProductType) UpdateAction(io.sphere.sdk.commands.UpdateAction) LocalizedEnumAttributeType(io.sphere.sdk.products.attributes.LocalizedEnumAttributeType) AddAttributeDefinition(io.sphere.sdk.producttypes.commands.updateactions.AddAttributeDefinition) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Collectors.toMap(java.util.stream.Collectors.toMap) AttributeDefinitionUpdateActionUtils.buildActions(com.commercetools.sync.producttypes.utils.AttributeDefinitionUpdateActionUtils.buildActions) DuplicateKeyException(com.commercetools.sync.commons.exceptions.DuplicateKeyException) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) AttributeDefinition(io.sphere.sdk.products.attributes.AttributeDefinition) Nullable(javax.annotation.Nullable) SetAttributeType(io.sphere.sdk.products.attributes.SetAttributeType) ChangeAttributeOrderByName(io.sphere.sdk.producttypes.commands.updateactions.ChangeAttributeOrderByName) EnumAttributeType(io.sphere.sdk.products.attributes.EnumAttributeType) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) DuplicateNameException(com.commercetools.sync.commons.exceptions.DuplicateNameException) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) Collectors.toList(java.util.stream.Collectors.toList) RemoveAttributeDefinition(io.sphere.sdk.producttypes.commands.updateactions.RemoveAttributeDefinition) List(java.util.List) Stream(java.util.stream.Stream) AttributeType(io.sphere.sdk.products.attributes.AttributeType) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) Optional(java.util.Optional) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) DuplicateNameException(com.commercetools.sync.commons.exceptions.DuplicateNameException) UpdateAction(io.sphere.sdk.commands.UpdateAction) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) Nonnull(javax.annotation.Nonnull)

Example 5 with DuplicateNameException

use of com.commercetools.sync.commons.exceptions.DuplicateNameException 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)

Aggregations

BuildUpdateActionException (com.commercetools.sync.commons.exceptions.BuildUpdateActionException)6 DuplicateNameException (com.commercetools.sync.commons.exceptions.DuplicateNameException)6 UpdateAction (io.sphere.sdk.commands.UpdateAction)6 DuplicateKeyException (com.commercetools.sync.commons.exceptions.DuplicateKeyException)4 CommonTypeUpdateActionUtils.buildUpdateAction (com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction)4 ArrayList (java.util.ArrayList)3 Collections.singletonList (java.util.Collections.singletonList)3 List (java.util.List)3 Nonnull (javax.annotation.Nonnull)3 SphereClient (io.sphere.sdk.client.SphereClient)2 SphereJsonUtils.readObjectFromResource (io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource)2 EnumValue (io.sphere.sdk.models.EnumValue)2 LocalizedEnumValue (io.sphere.sdk.models.LocalizedEnumValue)2 LocalizedString (io.sphere.sdk.models.LocalizedString)2 LocalizedString.ofEnglish (io.sphere.sdk.models.LocalizedString.ofEnglish)2 TextInputHint (io.sphere.sdk.models.TextInputHint)2 AttributeDefinition (io.sphere.sdk.products.attributes.AttributeDefinition)2 AttributeDefinitionDraft (io.sphere.sdk.products.attributes.AttributeDefinitionDraft)2 FieldDefinition (io.sphere.sdk.types.FieldDefinition)2 Type (io.sphere.sdk.types.Type)2