use of com.commercetools.sync.commons.utils.TriConsumer in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withProductContainingAttributeChanges_shouldSyncProductCorrectly.
@Test
void sync_withProductContainingAttributeChanges_shouldSyncProductCorrectly() {
// preparation
final List<UpdateAction<Product>> updateActions = new ArrayList<>();
final TriConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>> warningCallBack = (exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage());
final ProductSyncOptions customOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> collectErrors(exception.getMessage(), exception.getCause())).warningCallback(warningCallBack).beforeUpdateCallback((actions, draft, old) -> {
updateActions.addAll(actions);
return actions;
}).build();
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_1_RESOURCE_PATH, ProductType.referenceOfId(productType.getKey())).categories(emptyList()).taxCategory(null).state(null).build();
// Creating the attribute draft with the changes
final AttributeDraft priceInfoAttrDraft = AttributeDraft.of("priceInfo", JsonNodeFactory.instance.textNode("100/kg"));
final AttributeDraft angebotAttrDraft = AttributeDraft.of("angebot", JsonNodeFactory.instance.textNode("big discount"));
final AttributeDraft unknownAttrDraft = AttributeDraft.of("unknown", JsonNodeFactory.instance.textNode("unknown"));
// Creating the product variant draft with the product reference attribute
final List<AttributeDraft> attributes = asList(priceInfoAttrDraft, angebotAttrDraft, unknownAttrDraft);
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of(productDraft.getMasterVariant()).attributes(attributes).build();
final ProductDraft productDraftWithChangedAttributes = ProductDraftBuilder.of(productDraft).masterVariant(masterVariant).build();
// test
final ProductSync productSync = new ProductSync(customOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraftWithChangedAttributes)));
// assertion
assertThat(syncStatistics).hasValues(1, 0, 1, 0, 0);
final String causeErrorMessage = format(ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA, unknownAttrDraft.getName());
final String expectedErrorMessage = format(FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION, unknownAttrDraft.getName(), productDraft.getMasterVariant().getKey(), productDraft.getKey(), causeErrorMessage);
assertThat(errorCallBackExceptions).hasSize(1);
assertThat(errorCallBackExceptions.get(0).getMessage()).isEqualTo(expectedErrorMessage);
assertThat(errorCallBackExceptions.get(0).getCause().getMessage()).isEqualTo(causeErrorMessage);
assertThat(errorCallBackMessages).containsExactly(expectedErrorMessage);
assertThat(warningCallBackMessages).isEmpty();
assertThat(updateActions).filteredOn(updateAction -> !(updateAction instanceof SetTaxCategory)).filteredOn(updateAction -> !(updateAction instanceof RemoveFromCategory)).containsExactlyInAnyOrder(SetAttributeInAllVariants.of(priceInfoAttrDraft, true), SetAttribute.of(1, angebotAttrDraft, true), SetAttributeInAllVariants.ofUnsetAttribute("size", true), SetAttributeInAllVariants.ofUnsetAttribute("rinderrasse", true), SetAttributeInAllVariants.ofUnsetAttribute("herkunft", true), SetAttributeInAllVariants.ofUnsetAttribute("teilstueck", true), SetAttributeInAllVariants.ofUnsetAttribute("fuetterung", true), SetAttributeInAllVariants.ofUnsetAttribute("reifung", true), SetAttributeInAllVariants.ofUnsetAttribute("haltbarkeit", true), SetAttributeInAllVariants.ofUnsetAttribute("verpackung", true), SetAttributeInAllVariants.ofUnsetAttribute("anlieferung", true), SetAttributeInAllVariants.ofUnsetAttribute("zubereitung", true), SetAttribute.ofUnsetAttribute(1, "localisedText", true), Publish.of());
}
use of com.commercetools.sync.commons.utils.TriConsumer in project commercetools-sync-java by commercetools.
the class CategoryUpdateActionUtilsTest method buildChangeParentUpdateAction_WithNullValues_ShouldNotBuildUpdateActionAndCallCallback.
@Test
void buildChangeParentUpdateAction_WithNullValues_ShouldNotBuildUpdateActionAndCallCallback() {
when(MOCK_OLD_CATEGORY.getId()).thenReturn("oldCatId");
final CategoryDraft newCategory = mock(CategoryDraft.class);
when(newCategory.getParent()).thenReturn(null);
final ArrayList<Object> callBackResponse = new ArrayList<>();
final TriConsumer<SyncException, Optional<CategoryDraft>, Optional<Category>> updateActionWarningCallBack = (exception, newResource, oldResource) -> callBackResponse.add(exception.getMessage());
final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(CTP_CLIENT).warningCallback(updateActionWarningCallBack).build();
final Optional<UpdateAction<Category>> changeParentUpdateAction = buildChangeParentUpdateAction(MOCK_OLD_CATEGORY, newCategory, categorySyncOptions);
assertThat(changeParentUpdateAction).isNotNull();
assertThat(changeParentUpdateAction).isNotPresent();
assertThat(callBackResponse).hasSize(1);
assertThat(callBackResponse.get(0)).isEqualTo("Cannot unset 'parent' field of category with id 'oldCatId'.");
}
Aggregations