use of com.commercetools.sync.commons.exceptions.SyncException 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();
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class CustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithBothNullCustomFields_ShouldNotBuildUpdateActions.
@Test
void buildNonNullCustomFieldsUpdateActions_WithBothNullCustomFields_ShouldNotBuildUpdateActions() {
final Asset oldAsset = mock(Asset.class);
when(oldAsset.getCustom()).thenReturn(null);
final AssetDraft newAssetDraft = mock(AssetDraft.class);
when(newAssetDraft.getCustom()).thenReturn(null);
// Mock custom options error callback
final ArrayList<Object> callBackResponses = new ArrayList<>();
final QuadConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>, List<UpdateAction<Product>>> errorCallback = (exception, newResource, oldResource, updateActions) -> {
callBackResponses.add(exception.getMessage());
callBackResponses.add(exception.getCause());
};
// Mock sync options
final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(CTP_CLIENT).errorCallback(errorCallback).build();
final List<UpdateAction<Product>> updateActions = buildCustomUpdateActions(maiNewResource, oldAsset, newAssetDraft, new AssetCustomActionBuilder(), 10, Asset::getId, asset -> Asset.resourceTypeId(), Asset::getKey, productSyncOptions);
assertThat(updateActions).isNotNull();
assertThat(updateActions).isEmpty();
assertThat(callBackResponses).isEmpty();
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class GenericUpdateActionUtilsTest method buildTypedSetCustomTypeUpdateAction_WithNullNewIdCategoryAsset_ShouldNotBuildCategoryUpdateAction.
@Test
void buildTypedSetCustomTypeUpdateAction_WithNullNewIdCategoryAsset_ShouldNotBuildCategoryUpdateAction() {
final ArrayList<String> errorMessages = new ArrayList<>();
final QuadConsumer<SyncException, Optional<CategoryDraft>, Optional<Category>, List<UpdateAction<Category>>> errorCallback = (exception, oldResource, newResource, updateActions) -> errorMessages.add(exception.getMessage());
// Mock sync options
final CategorySyncOptions categorySyncOptions = of(mock(SphereClient.class)).errorCallback(errorCallback).build();
final Optional<UpdateAction<Category>> updateAction = buildTypedSetCustomTypeUpdateAction(null, new HashMap<>(), mock(Asset.class), new AssetCustomActionBuilder(), 1, Asset::getId, assetResource -> Asset.resourceTypeId(), Asset::getKey, categorySyncOptions);
assertThat(errorMessages).hasSize(1);
assertThat(errorMessages.get(0)).isEqualTo("Failed to build 'setCustomType' update action on the asset with" + " id 'null'. Reason: New Custom Type id is blank (null/empty).");
assertThat(updateAction).isEmpty();
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class ProductTypeBatchValidatorTest method validateAndCollectReferencedKeys_WithMixOfValidAndInvalidDrafts_ShouldValidateCorrectly.
@Test
void validateAndCollectReferencedKeys_WithMixOfValidAndInvalidDrafts_ShouldValidateCorrectly() {
final AttributeDefinitionDraft attributeDefinitionDraft = AttributeDefinitionDraftBuilder.of(StringAttributeType.of(), "foo", ofEnglish("koko"), true).build();
final AttributeDefinitionDraft nestedTypeAttrDefDraft = AttributeDefinitionDraftBuilder.of(NestedAttributeType.of(ProductType.referenceOfId("x")), "validNested", ofEnglish("koko"), true).build();
final AttributeDefinitionDraft setOfNestedTypeAttrDefDraft = AttributeDefinitionDraftBuilder.of(SetAttributeType.of(NestedAttributeType.of(ProductType.referenceOfId("y"))), "setOfNested", ofEnglish("koko"), true).build();
final AttributeDefinitionDraft invalidNestedTypeAttrDefDraft = AttributeDefinitionDraftBuilder.of(NestedAttributeType.of(ProductType.referenceOfId("")), "invalidNested", ofEnglish("koko"), true).build();
final AttributeDefinitionDraft setOfInvalidNestedTypeAttrDefDraft = AttributeDefinitionDraftBuilder.of(SetAttributeType.of(NestedAttributeType.of(ProductType.referenceOfId(""))), "setOfInvalidNested", ofEnglish("koko"), true).build();
final List<AttributeDefinitionDraft> attributes = new ArrayList<>();
attributes.add(attributeDefinitionDraft);
attributes.add(nestedTypeAttrDefDraft);
attributes.add(setOfNestedTypeAttrDefDraft);
attributes.add(invalidNestedTypeAttrDefDraft);
attributes.add(setOfInvalidNestedTypeAttrDefDraft);
final ProductTypeDraft productTypeDraft = ProductTypeDraftBuilder.of("foo", "foo", "foo", attributes).build();
final ProductTypeDraft productTypeDraftWithEmptyKey = ProductTypeDraftBuilder.of("", "foo", "foo", attributes).build();
final ProductTypeDraft validProductTypeDraftWithReferences = ProductTypeDraftBuilder.of("bar", "bar", "bar", asList(attributeDefinitionDraft, nestedTypeAttrDefDraft, setOfNestedTypeAttrDefDraft)).build();
final ProductTypeDraft draftWithEmptyAttributes = ProductTypeDraftBuilder.of("bar", "bar", "bar", emptyList()).build();
final ProductTypeDraft draftWithNullAttributes = ProductTypeDraftBuilder.of("bar", "bar", "bar", null).build();
final List<ProductTypeDraft> productTypeDrafts = new ArrayList<>();
productTypeDrafts.add(productTypeDraft);
productTypeDrafts.add(null);
productTypeDrafts.add(productTypeDraftWithEmptyKey);
productTypeDrafts.add(validProductTypeDraftWithReferences);
productTypeDrafts.add(draftWithEmptyAttributes);
productTypeDrafts.add(draftWithNullAttributes);
final ProductTypeBatchValidator batchValidator = new ProductTypeBatchValidator(syncOptions, syncStatistics);
final ImmutablePair<Set<ProductTypeDraft>, Set<String>> pair = batchValidator.validateAndCollectReferencedKeys(productTypeDrafts);
assertThat(pair.getLeft()).containsExactlyInAnyOrder(validProductTypeDraftWithReferences, draftWithEmptyAttributes, draftWithNullAttributes);
assertThat(pair.getRight()).containsExactlyInAnyOrder("x", "y");
final String expectedExceptionMessage = format(PRODUCT_TYPE_HAS_INVALID_REFERENCES, productTypeDraft.getKey(), "[invalidNested, setOfInvalidNested]");
assertThat(errorCallBackMessages).containsExactlyInAnyOrderElementsOf(asList(expectedExceptionMessage, PRODUCT_TYPE_DRAFT_IS_NULL, format(PRODUCT_TYPE_DRAFT_KEY_NOT_SET, productTypeDraftWithEmptyKey.getName())));
final Predicate<Throwable> invalidReferencePredicate = throwable -> expectedExceptionMessage.equals(throwable.getMessage()) && BLANK_ID_VALUE_ON_REFERENCE.equals(throwable.getCause().getMessage());
final Condition<Throwable> invalidReferenceCondition = new Condition<>(invalidReferencePredicate, "ReferenceResolutionException: " + "ProductTypeDraft with Key 'foo' has invalid references on attributeDraft with name 'nested'.");
final Predicate<Throwable> nullDraftPredicate = throwable -> PRODUCT_TYPE_DRAFT_IS_NULL.equals(throwable.getMessage()) && throwable instanceof SyncException;
final Condition<Throwable> nullDraftCondition = new Condition<>(nullDraftPredicate, "SyncException: ProductTypeDraft is null.");
final Predicate<Throwable> blankProductTypeKeyPredicate = throwable -> format(PRODUCT_TYPE_DRAFT_KEY_NOT_SET, productTypeDraftWithEmptyKey.getName()).equals(throwable.getMessage()) && throwable instanceof SyncException;
final Condition<Throwable> blankKeyCondition = new Condition<>(blankProductTypeKeyPredicate, "SyncException: ProductTypeDraft has blank key.");
assertThat(errorCallBackExceptions).hasSize(3).haveExactly(1, invalidReferenceCondition).haveExactly(1, nullDraftCondition).haveExactly(1, blankKeyCondition);
}
Aggregations