use of com.commercetools.sync.producttypes.ProductTypeSync in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithoutName_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithoutName_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
// Draft without "name" throws a commercetools exception because "name" is a required value
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, null, PRODUCT_TYPE_DESCRIPTION_1, asList(ATTRIBUTE_DEFINITION_DRAFT_1, ATTRIBUTE_DEFINITION_DRAFT_2));
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("Missing required value");
return true;
});
assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 1, 0);
}
use of com.commercetools.sync.producttypes.ProductTypeSync in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method syncDrafts_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate.
@Test
void syncDrafts_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate() {
// Preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdateAndNotFoundFetchOnRetry();
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(errorMessages.get(0)).contains(format("Failed to update product type with key: '%s'. Reason: Not found when attempting to fetch while " + "retrying after concurrency modification.", productTypeDraft.getKey()));
}
use of com.commercetools.sync.producttypes.ProductTypeSync in project commercetools-sync-java by commercetools.
the class ProductTypeWithNestedAttributeSyncIT method sync_WithUpdatedProductType_WithNewNestedAttributeInSameBatch_ShouldUpdateProductTypeAddingAttribute.
@Test
void sync_WithUpdatedProductType_WithNewNestedAttributeInSameBatch_ShouldUpdateProductTypeAddingAttribute() {
// preparation
final AttributeDefinitionDraft nestedTypeAttr = AttributeDefinitionDraftBuilder.of(AttributeDefinitionBuilder.of("nestedattr", ofEnglish("nestedattr"), SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(NestedAttributeType.of(ProductType.referenceOfId(PRODUCT_TYPE_KEY_1))))))).build()).searchable(false).build();
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, asList(ATTRIBUTE_DEFINITION_DRAFT_1, ATTRIBUTE_DEFINITION_DRAFT_2, nestedTypeAttr));
final ProductTypeDraft productTypeDraft5 = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_5, PRODUCT_TYPE_NAME_5, PRODUCT_TYPE_DESCRIPTION_5, singletonList(ATTRIBUTE_DEFINITION_DRAFT_3));
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// tests
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(asList(newProductTypeDraft, productTypeDraft5)).toCompletableFuture().join();
// assertions
final Optional<ProductType> productType1 = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_1);
assert productType1.isPresent();
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(builtUpdateActions).containsExactly(AddAttributeDefinition.of(AttributeDefinitionDraftBuilder.of(AttributeDefinitionBuilder.of("nestedattr", ofEnglish("nestedattr"), SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(NestedAttributeType.of(productType1.get())))))).build()).searchable(false).build()));
assertThat(productTypeSyncStatistics).hasValues(2, 1, 1, 0, 0);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 product types were processed in total" + " (1 created, 1 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).isEmpty();
final Optional<ProductType> productType5 = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_5);
assertThat(productType5).hasValueSatisfying(productType -> {
assertThat(productType.getName()).isEqualTo(PRODUCT_TYPE_NAME_5);
assertThat(productType.getDescription()).isEqualTo(PRODUCT_TYPE_DESCRIPTION_5);
assertThat(productType.getAttributes()).hasSize(1);
});
assertThat(productType1).hasValueSatisfying(productType -> assertThat(productType.getAttributes()).hasSize(3));
}
use of com.commercetools.sync.producttypes.ProductTypeSync in project commercetools-sync-java by commercetools.
the class ProductTypeWithNestedAttributeSyncIT method sync_WithUpdatedProductType_WithRemovedNestedAttributeInLaterBatch_ShouldReturnProperStatistics.
@Test
void sync_WithUpdatedProductType_WithRemovedNestedAttributeInLaterBatch_ShouldReturnProperStatistics() {
// preparation
final AttributeDefinitionDraft nestedTypeAttr = AttributeDefinitionDraftBuilder.of(AttributeDefinitionBuilder.of("newNested", ofEnglish("nestedattr"), NestedAttributeType.of(ProductType.referenceOfId("non-existing-product-type"))).build()).searchable(false).build();
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, asList(ATTRIBUTE_DEFINITION_DRAFT_1, ATTRIBUTE_DEFINITION_DRAFT_2, nestedTypeAttr));
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// tests
productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
final ProductTypeDraft newProductTypeDraftWithoutNested = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, asList(ATTRIBUTE_DEFINITION_DRAFT_1, ATTRIBUTE_DEFINITION_DRAFT_2));
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraftWithoutNested)).toCompletableFuture().join();
// assertions
final Optional<ProductType> productType1 = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_1);
assert productType1.isPresent();
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(builtUpdateActions).isEmpty();
assertThat(productTypeSyncStatistics).hasValues(2, 0, 0, 0, 0);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 product types were processed in total" + " (0 created, 0 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).isEmpty();
assertThat(productType1).hasValueSatisfying(productType -> assertThat(productType.getAttributes()).hasSize(2));
}
use of com.commercetools.sync.producttypes.ProductTypeSync in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithoutKey_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithoutKey_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// Draft without key throws an error
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(null, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, asList(ATTRIBUTE_DEFINITION_DRAFT_1, ATTRIBUTE_DEFINITION_DRAFT_1));
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);
}).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
final String expectedErrorMessage = format("ProductTypeDraft with name: %s doesn't have a key. " + "Please make sure all productType drafts have keys.", newProductTypeDraft.getName());
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo(expectedErrorMessage);
assertThat(exceptions).hasSize(1).singleElement().matches(throwable -> {
assertThat(throwable).isInstanceOf(SyncException.class);
assertThat(throwable.getMessage()).isEqualTo(expectedErrorMessage);
return true;
});
assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 1, 0);
}
Aggregations