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());
}
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);
}
}
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);
}
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());
}
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);
}
}
Aggregations