use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withADraftsWithBlankKeysInBatch_ShouldNotSyncItAndTriggerErrorCallBack.
@Test
void sync_withADraftsWithBlankKeysInBatch_ShouldNotSyncItAndTriggerErrorCallBack() {
// Prepare batches from external source
final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_CHANGED_RESOURCE_PATH, ProductType.reference(productType.getKey()), ResourceIdentifier.ofKey(targetTaxCategory.getKey()), ResourceIdentifier.ofKey(targetProductState.getKey()), categoryResourceIdentifiersWithKeys, categoryOrderHintsWithKeys);
// Draft with null key
final ProductDraft key3Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ProductType.reference(productType.getKey())).taxCategory(null).state(null).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key(null).masterVariant(ProductVariantDraftBuilder.of().build()).productType(ResourceIdentifier.ofKey(productType.getKey())).build();
// Draft with empty key
final ProductDraft key4Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ProductType.reference(productType.getKey())).taxCategory(null).state(null).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("").masterVariant(ProductVariantDraftBuilder.of().build()).productType(ResourceIdentifier.ofKey(productType.getKey())).build();
final List<ProductDraft> batch = new ArrayList<>();
batch.add(productDraft);
batch.add(key3Draft);
batch.add(key4Draft);
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(batch));
assertThat(syncStatistics).hasValues(3, 0, 1, 2, 0);
assertThat(errorCallBackExceptions).hasSize(2);
assertThat(errorCallBackMessages).hasSize(2);
assertThat(errorCallBackMessages.get(0)).containsIgnoringCase(format("ProductDraft with name: %s doesn't have a key.", key3Draft.getName()));
assertThat(errorCallBackMessages.get(1)).containsIgnoringCase(format("ProductDraft with name: %s doesn't have a key.", key4Draft.getName()));
assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withNewProductAndBeforeCreateCallback_shouldCreateProduct.
@Test
void sync_withNewProductAndBeforeCreateCallback_shouldCreateProduct() {
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).build();
final String keyPrefix = "callback_";
final ProductSyncOptions options = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> collectErrors(exception.getMessage(), exception.getCause())).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).beforeCreateCallback(draft -> prefixDraftKey(draft, keyPrefix)).build();
final ProductSync productSync = new ProductSync(options);
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();
// Query for a product with key prefixed with "callback_" added by the callback
final String keyWithCallbackPrefix = format("%s%s", keyPrefix, productDraft.getKey());
final Optional<Product> productOptional = CTP_TARGET_CLIENT.execute(ProductQuery.of().withPredicates(QueryPredicate.of(format("key = \"%s\"", keyWithCallbackPrefix)))).toCompletableFuture().join().head();
assertThat(productOptional).isNotEmpty();
final Product fetchedProduct = productOptional.get();
assertThat(fetchedProduct.getKey()).isEqualTo(keyWithCallbackPrefix);
assertThat(fetchedProduct.getMasterData().getCurrent().getName()).isEqualTo(productDraft.getName());
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method syncDrafts_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate.
@Test
void syncDrafts_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate() {
// preparation
final SphereClient spyClient = buildClientWithConcurrentModificationUpdateAndFailedFetchOnRetry();
final ProductSyncOptions spyOptions = ProductSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> collectErrors(exception.getMessage(), exception.getCause())).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).build();
final ProductSync spyProductSync = new ProductSync(spyOptions);
final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_CHANGED_RESOURCE_PATH, ProductType.referenceOfId(productType.getKey()), ResourceIdentifier.ofKey(targetTaxCategory.getKey()), ResourceIdentifier.ofKey(targetProductState.getKey()), categoryResourceIdentifiersWithKeys, categoryOrderHintsWithKeys);
final ProductSyncStatistics syncStatistics = executeBlocking(spyProductSync.sync(singletonList(productDraft)));
// Test and assertion
assertThat(syncStatistics).hasValues(1, 0, 0, 1, 0);
assertThat(errorCallBackMessages).hasSize(1);
assertThat(errorCallBackExceptions).hasSize(1);
assertThat(errorCallBackExceptions.get(0).getCause()).isExactlyInstanceOf(BadGatewayException.class);
assertThat(errorCallBackMessages.get(0)).contains(format("Failed to update Product with key: '%s'. Reason: Failed to fetch from CTP while retrying " + "after concurrency modification.", productDraft.getKey()));
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedCategoriesIT method sync_withChangedNestedCategoryReferenceAsAttribute_shouldUpdateProductReferencingExistingCategory.
@Test
void sync_withChangedNestedCategoryReferenceAsAttribute_shouldUpdateProductReferencingExistingCategory() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueReferences("category-reference", createReferenceObject(testCategory1.getId(), Category.referenceTypeId()));
final AttributeDraft categoryReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(nestedAttributeValue));
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(categoryReferenceAttribute).build();
final ProductDraft productDraftWithCategoryReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraftWithCategoryReference)).toCompletableFuture().join();
final ObjectNode newNestedAttributeValue = createNestedAttributeValueReferences("category-reference", createReferenceObject(testCategory2.getKey(), Category.referenceTypeId()));
final AttributeDraft newProductReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(newNestedAttributeValue));
final ProductVariantDraft newMasterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(newProductReferenceAttribute).build();
final ProductDraft newProductDraftWithCategoryReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), newMasterVariant).key("new-product").build();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(newProductDraftWithCategoryReference)).toCompletableFuture().join();
// assertion
assertThat(syncStatistics).hasValues(1, 0, 1, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
final ObjectNode expectedNestedAttributeValue = createNestedAttributeValueReferences("category-reference", createReferenceObject(testCategory2.getId(), Category.referenceTypeId()));
final AttributeDraft expectedCategoryReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(expectedNestedAttributeValue));
assertThat(actions).containsExactly(SetAttribute.of(1, expectedCategoryReferenceAttribute, true));
final Product createdProduct = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(productDraftWithCategoryReference.getKey())).toCompletableFuture().join();
final Optional<Attribute> createdCategoryReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(categoryReferenceAttribute.getName());
assertThat(createdCategoryReferenceAttribute).hasValueSatisfying(attribute -> {
final JsonNode nestedAttributeNameField = attribute.getValueAsJsonNode().get(0).get(ATTRIBUTE_NAME_FIELD);
final JsonNode nestedAttributeValueField = attribute.getValueAsJsonNode().get(0).get(ATTRIBUTE_VALUE_FIELD);
assertThat(nestedAttributeNameField.asText()).isEqualTo("category-reference");
assertThat(nestedAttributeValueField.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Category.referenceTypeId());
assertThat(nestedAttributeValueField.get(REFERENCE_ID_FIELD).asText()).isEqualTo(testCategory2.getId());
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedCategoriesIT method sync_withNonExistingNestedCategoryReferenceAsAttribute_ShouldFailCreatingTheProduct.
@Test
void sync_withNonExistingNestedCategoryReferenceAsAttribute_ShouldFailCreatingTheProduct() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueReferences("category-reference", createReferenceObject("nonExistingKey", Category.referenceTypeId()));
final AttributeDraft categoryReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(nestedAttributeValue));
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(categoryReferenceAttribute).build();
final ProductDraft productDraftWithCategoryReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(productDraftWithCategoryReference)).toCompletableFuture().join();
// assertion
assertThat(syncStatistics).hasValues(1, 0, 0, 1, 0);
assertThat(errorCallBackExceptions).hasSize(1).singleElement().matches(error -> {
assertThat(error).hasCauseExactlyInstanceOf(ErrorResponseException.class);
final ErrorResponseException errorResponseException = (ErrorResponseException) error.getCause();
assertThat(errorResponseException.getStatusCode()).isEqualTo(400);
assertThat(error.getMessage()).contains("The value '{\"typeId\":\"category\",\"id\":\"nonExistingKey\"}' " + "is not valid for field 'nestedAttribute.category-reference'");
return true;
});
assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("The value '{\"typeId\":\"category\",\"id\":\"nonExistingKey\"}' " + "is not valid for field 'nestedAttribute.category-reference'");
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
}
Aggregations