use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithErrorUpdatingTheProductType_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorUpdatingTheProductType_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
// Invalid attribute definition due to having an invalid name.
final AttributeDefinitionDraftDsl invalidAttrDefinition = AttributeDefinitionDraftBuilder.of(MoneyAttributeType.of(), "*invalidName*", ofEnglish("description"), true).searchable(false).build();
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, singletonList(invalidAttrDefinition));
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to update product type with key: 'key_1'.");
assertThat(exceptions).hasSize(1).singleElement().matches(throwable -> {
assertThat(throwable).isExactlyInstanceOf(CompletionException.class);
assertThat(throwable).hasCauseExactlyInstanceOf(ErrorResponseException.class);
assertThat(throwable).hasMessageContaining("InvalidInput");
return true;
});
assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 1, 0);
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithSetOfEnumsAndSetOfLenumsChanges_ShouldUpdateProductType.
@Test
void sync_WithSetOfEnumsAndSetOfLenumsChanges_ShouldUpdateProductType() {
// preparation
final AttributeDefinitionDraft withSetOfEnumsOld = AttributeDefinitionDraftBuilder.of(SetAttributeType.of(EnumAttributeType.of(asList(EnumValue.of("d", "d"), EnumValue.of("b", "newB"), EnumValue.of("a", "a"), EnumValue.of("c", "c")))), "foo", ofEnglish("foo"), false).build();
final AttributeDefinitionDraft withSetOfSetOfLEnumsOld = AttributeDefinitionDraftBuilder.of(SetAttributeType.of(LocalizedEnumAttributeType.of(asList(LocalizedEnumValue.of("d", ofEnglish("d")), LocalizedEnumValue.of("b", ofEnglish("newB")), LocalizedEnumValue.of("a", ofEnglish("a")), LocalizedEnumValue.of("c", ofEnglish("c"))))), "bar", ofEnglish("bar"), false).build();
final ProductTypeDraft oldDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("withSetOfEnums", "withSetOfEnums", "withSetOfEnums", asList(withSetOfEnumsOld, withSetOfSetOfLEnumsOld));
CTP_TARGET_CLIENT.execute(ProductTypeCreateCommand.of(oldDraft)).toCompletableFuture().join();
final AttributeDefinitionDraft withSetOfEnumsNew = AttributeDefinitionDraftBuilder.of(SetAttributeType.of(EnumAttributeType.of(asList(EnumValue.of("a", "a"), EnumValue.of("b", "b"), EnumValue.of("c", "c")))), "foo", ofEnglish("foo"), false).build();
final AttributeDefinitionDraft withSetOfSetOfLEnumsNew = AttributeDefinitionDraftBuilder.of(SetAttributeType.of(LocalizedEnumAttributeType.of(asList(LocalizedEnumValue.of("a", ofEnglish("a")), LocalizedEnumValue.of("b", ofEnglish("newB")), LocalizedEnumValue.of("c", ofEnglish("c"))))), "bar", ofEnglish("bar"), false).build();
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("withSetOfEnums", "withSetOfEnums", "withSetOfEnums", asList(withSetOfEnumsNew, withSetOfSetOfLEnumsNew));
final ProductTypeSyncOptions productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// tests
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
// assertions
assertThat(productTypeSyncStatistics).hasValues(1, 0, 1, 0, 0);
final Optional<ProductType> oldProductTypeAfter = getProductTypeByKey(CTP_TARGET_CLIENT, "withSetOfEnums");
assertThat(oldProductTypeAfter).hasValueSatisfying(productType -> assertAttributesAreEqual(productType.getAttributes(), asList(withSetOfEnumsNew, withSetOfSetOfLEnumsNew)));
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithUpdatedAttributeDefinition_ShouldUpdateProductTypeUpdatingAttribute.
@Test
void sync_WithUpdatedAttributeDefinition_ShouldUpdateProductTypeUpdatingAttribute() {
// Updating ATTRIBUTE_DEFINITION_1 (name = "attr_name_1") changing the label, attribute
// constraint, input tip,
// input hint, isSearchable fields.
final AttributeDefinitionDraft attributeDefinitionDraftUpdated = AttributeDefinitionDraftBuilder.of(StringAttributeType.of(), "attr_name_1", ofEnglish("attr_label_updated"), true).attributeConstraint(AttributeConstraint.NONE).inputTip(ofEnglish("inputTip_updated")).inputHint(TextInputHint.MULTI_LINE).isSearchable(false).build();
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, singletonList(attributeDefinitionDraftUpdated));
final ProductTypeSyncOptions productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
assertThat(productTypeSyncStatistics).hasValues(1, 0, 1, 0, 0);
final Optional<ProductType> oldProductTypeAfter = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_1);
assertThat(oldProductTypeAfter).hasValueSatisfying(productType -> assertAttributesAreEqual(productType.getAttributes(), singletonList(attributeDefinitionDraftUpdated)));
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method syncDrafts_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate.
@Test
void syncDrafts_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate() {
// Preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdateAndFailedFetchOnRetry();
final ProductTypeDraft productTypeDraft = ProductTypeDraftBuilder.of("key", "foo", "description", emptyList()).build();
CTP_TARGET_CLIENT.execute(ProductTypeCreateCommand.of(productTypeDraft)).toCompletableFuture().join();
final String newProductTypeName = "bar";
final ProductTypeDraft updatedDraft = ProductTypeDraftBuilder.of(productTypeDraft).name(newProductTypeName).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> errors = new ArrayList<>();
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
errors.add(exception.getCause());
}).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions);
// Test
final ProductTypeSyncStatistics statistics = productTypeSync.sync(singletonList(updatedDraft)).toCompletableFuture().join();
// Assertion
assertThat(statistics).hasValues(1, 0, 0, 1, 0);
assertThat(errorMessages).hasSize(1);
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getCause()).isExactlyInstanceOf(BadGatewayException.class);
assertThat(errorMessages.get(0)).contains(format("Failed to update product type with key: '%s'. Reason: Failed to fetch from CTP while retrying " + "after concurrency modification.", productTypeDraft.getKey()));
}
use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithUpdatedProductType_ShouldUpdateProductType.
@Test
void sync_WithUpdatedProductType_ShouldUpdateProductType() {
// preparation
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, PRODUCT_TYPE_NAME_2, PRODUCT_TYPE_DESCRIPTION_2, singletonList(ATTRIBUTE_DEFINITION_DRAFT_1));
final ProductTypeSyncOptions productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
// assertion
assertThat(productTypeSyncStatistics).hasValues(1, 0, 1, 0, 0);
final Optional<ProductType> oldProductTypeAfter = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_1);
assertThat(oldProductTypeAfter).hasValueSatisfying(productType -> {
assertThat(productType.getName()).isEqualTo(PRODUCT_TYPE_NAME_2);
assertThat(productType.getDescription()).isEqualTo(PRODUCT_TYPE_DESCRIPTION_2);
assertAttributesAreEqual(productType.getAttributes(), singletonList(ATTRIBUTE_DEFINITION_DRAFT_1));
});
}
Aggregations