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