use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withANullDraftInBatch_ShouldNotSyncItAndTriggerErrorCallBack.
@Test
void sync_withANullDraftInBatch_ShouldNotSyncItAndTriggerErrorCallBack() {
// Prepare batches from external source
final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_CHANGED_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey()), null, null, categoryResourceIdentifiersWithKeys, categoryOrderHintsWithKeys);
final List<ProductDraft> batch = new ArrayList<>();
batch.add(productDraft);
batch.add(null);
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(batch));
assertThat(syncStatistics).hasValues(2, 0, 1, 1, 0);
assertThat(errorCallBackExceptions).hasSize(1);
assertThat(errorCallBackMessages).hasSize(1);
assertThat(errorCallBackMessages.get(0)).isEqualToIgnoringCase("ProductDraft is null.");
assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withMultipleBatchSyncing_ShouldSync.
@Test
void sync_withMultipleBatchSyncing_ShouldSync() {
// Prepare existing products with keys: productKey1, productKey2, productKey3.
final ProductDraft key2Draft = createProductDraft(PRODUCT_KEY_2_RESOURCE_PATH, productType.toReference(), targetTaxCategory.toReference(), targetProductState.toReference(), categoryReferencesWithIds, product.getMasterData().getStaged().getCategoryOrderHints());
executeBlocking(CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(key2Draft)));
final ProductDraft key3Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, productType.toReference()).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey3").slug(LocalizedString.of(Locale.ENGLISH, "slug3")).masterVariant(ProductVariantDraftBuilder.of().key("v3").build()).taxCategory(TaxCategory.referenceOfId(targetTaxCategory.getId())).build();
executeBlocking(CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(key3Draft)));
// Prepare batches from external source
final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_CHANGED_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey()), ResourceIdentifier.ofKey(targetTaxCategory.getKey()), ResourceIdentifier.ofKey(targetProductState.getKey()), categoryResourceIdentifiersWithKeys, categoryOrderHintsWithKeys);
final List<ProductDraft> batch1 = new ArrayList<>();
batch1.add(productDraft);
final ProductDraft key4Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey4").slug(LocalizedString.of(Locale.ENGLISH, "slug4")).masterVariant(ProductVariantDraftBuilder.of().key("v4").sku("sku4").build()).build();
final List<ProductDraft> batch2 = new ArrayList<>();
batch2.add(key4Draft);
final ProductDraft key3DraftNewSlug = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey3").slug(LocalizedString.of(Locale.ENGLISH, "newSlug")).masterVariant(ProductVariantDraftBuilder.of().key("v3").sku("sku3").build()).build();
final List<ProductDraft> batch3 = new ArrayList<>();
batch3.add(key3DraftNewSlug);
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(batch1).thenCompose(result -> productSync.sync(batch2)).thenCompose(result -> productSync.sync(batch3)));
assertThat(syncStatistics).hasValues(3, 1, 2, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withNewProductWithExistingSlug_shouldNotCreateProduct.
@Test
void sync_withNewProductWithExistingSlug_shouldNotCreateProduct() {
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).slug(product.getMasterData().getStaged().getSlug()).build();
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraft)));
assertThat(syncStatistics).hasValues(1, 0, 0, 1, 0);
final String duplicatedSlug = product.getMasterData().getStaged().getSlug().get(Locale.ENGLISH);
assertThat(errorCallBackExceptions).hasSize(1).allSatisfy(exception -> {
assertThat(exception).isExactlyInstanceOf(ErrorResponseException.class);
final ErrorResponseException errorResponse = ((ErrorResponseException) exception);
final List<DuplicateFieldError> fieldErrors = errorResponse.getErrors().stream().map(sphereError -> {
assertThat(sphereError.getCode()).isEqualTo(DuplicateFieldError.CODE);
return sphereError.as(DuplicateFieldError.class);
}).collect(toList());
assertThat(fieldErrors).hasSize(1);
assertThat(fieldErrors).allSatisfy(error -> {
assertThat(error.getField()).isEqualTo("slug.en");
assertThat(error.getDuplicateValue()).isEqualTo(duplicatedSlug);
});
});
assertThat(errorCallBackMessages).hasSize(1).allSatisfy(errorMessage -> {
assertThat(errorMessage).contains("\"code\" : \"DuplicateField\"");
assertThat(errorMessage).contains("\"field\" : \"slug.en\"");
assertThat(errorMessage).contains(format("\"duplicateValue\" : \"%s\"", duplicatedSlug));
});
assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withNewProduct_shouldCreateProduct.
@Test
void sync_withNewProduct_shouldCreateProduct() {
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).build();
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraft)));
assertThat(syncStatistics).hasValues(1, 1, 0, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withEqualProduct_shouldNotUpdateProduct.
@Test
void sync_withEqualProduct_shouldNotUpdateProduct() {
final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_RESOURCE_PATH, ProductType.referenceOfId(productType.getKey()), ResourceIdentifier.ofKey(targetTaxCategory.getKey()), ResourceIdentifier.ofKey(targetProductState.getKey()), categoryResourceIdentifiersWithKeys, categoryOrderHintsWithKeys);
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraft)));
assertThat(syncStatistics).hasValues(1, 0, 0, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
}
Aggregations