use of com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withProductTypeReference_ShouldUpdateProducts.
@Test
void sync_withProductTypeReference_ShouldUpdateProducts() {
// Preparation
// Create custom options with whitelisting and action filter callback..
final ProductSyncOptions customSyncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> errorCallback(exception.getMessage(), exception.getCause())).warningCallback((exception, oldResource, newResources) -> warningCallBackMessages.add(exception.getMessage())).beforeUpdateCallback(this::beforeUpdateCallback).syncFilter(SyncFilter.ofWhiteList(ATTRIBUTES)).build();
final ProductSync customSync = new ProductSync(customSyncOptions);
// Create 3 existing products in target project with keys (productKey1, productKey2 and
// productKey3)
final ProductDraft existingProductDraft = createProductDraft(PRODUCT_KEY_1_RESOURCE_PATH, targetProductType.toReference(), targetTaxCategory.toReference(), targetProductState.toReference(), targetCategoryReferencesWithIds, createRandomCategoryOrderHints(targetCategoryReferencesWithIds));
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(existingProductDraft)).toCompletableFuture().join();
final ProductDraft existingProductDraft2 = createProductDraft(PRODUCT_KEY_2_RESOURCE_PATH, targetProductType.toReference(), targetTaxCategory.toReference(), targetProductState.toReference(), targetCategoryReferencesWithIds, createRandomCategoryOrderHints(targetCategoryReferencesWithIds));
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(existingProductDraft2)).toCompletableFuture().join();
final ProductDraft existingProductDraft3 = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, targetProductType.toReference()).slug(ofEnglish("newSlug3")).key("productKey3").masterVariant(ProductVariantDraftBuilder.of().key("v3").sku("s3").build()).taxCategory(null).state(null).categories(Collections.emptySet()).categoryOrderHints(null).build();
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(existingProductDraft3)).toCompletableFuture().join();
// Create 2 existing products in source project with keys (productKey2 and productKey3)
final ProductDraft newProductDraft2 = createProductDraft(PRODUCT_KEY_2_RESOURCE_PATH, sourceProductType.toReference(), sourceTaxCategory.toReference(), sourceProductState.toReference(), sourceCategoryReferencesWithIds, createRandomCategoryOrderHints(sourceCategoryReferencesWithIds));
final Product product2 = CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(newProductDraft2)).toCompletableFuture().join();
final ProductDraft newProductDraft3 = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, sourceProductType.toReference()).slug(ofEnglish("newSlug3")).key("productKey3").masterVariant(ProductVariantDraftBuilder.of().key("v3").sku("s3").build()).taxCategory(null).state(null).categories(Collections.emptySet()).categoryOrderHints(null).build();
final Product product3 = CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(newProductDraft3)).toCompletableFuture().join();
// Create existing product with productKey1 in source project that has references to products
// with keys
// (productKey2 and productKey3).
final ObjectNode productReferenceValue1 = getProductReferenceWithId(product2.getId());
final ObjectNode productReferenceValue2 = getProductReferenceWithId(product3.getId());
final AttributeDraft productRefAttr = AttributeDraft.of("product-reference", productReferenceValue1);
final AttributeDraft productSetRefAttr = getReferenceSetAttributeDraft("product-reference-set", productReferenceValue1, productReferenceValue2);
final List<AttributeDraft> attributeDrafts = existingProductDraft.getMasterVariant().getAttributes();
attributeDrafts.addAll(Arrays.asList(productRefAttr, productSetRefAttr));
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().key("v1").sku("s1").attributes(attributeDrafts).build();
final ProductDraft newProductDraftWithProductReference = createProductDraftBuilder(PRODUCT_KEY_1_CHANGED_RESOURCE_PATH, sourceProductType.toReference()).masterVariant(masterVariant).taxCategory(sourceTaxCategory.toReference()).state(sourceProductState.toReference()).categories(Collections.emptySet()).categoryOrderHints(null).build();
CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(newProductDraftWithProductReference)).toCompletableFuture().join();
// Test
final List<ProductProjection> products = CTP_SOURCE_CLIENT.execute(ProductProjectionQuery.ofStaged()).toCompletableFuture().join().getResults();
final List<ProductDraft> productDrafts = ProductTransformUtils.toProductDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, products).join();
final ProductSyncStatistics syncStatistics = customSync.sync(productDrafts).toCompletableFuture().join();
// Assertion
assertThat(syncStatistics).hasValues(3, 0, 1, 0);
assertThat(errorCallBackMessages).isEmpty();
assertThat(errorCallBackExceptions).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
final Product targetProduct2 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of("productKey2")).toCompletableFuture().join();
final Product targetProduct3 = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of("productKey3")).toCompletableFuture().join();
final ObjectNode targetProductReferenceValue2 = getProductReferenceWithId(targetProduct2.getId());
final ObjectNode targetProductReferenceValue3 = getProductReferenceWithId(targetProduct3.getId());
final AttributeDraft targetProductRefAttr = AttributeDraft.of("product-reference", targetProductReferenceValue2);
final AttributeDraft targetProductSetRefAttr = getReferenceSetAttributeDraft("product-reference-set", targetProductReferenceValue2, targetProductReferenceValue3);
assertThat(updateActions).containsExactlyInAnyOrder(SetAttributeInAllVariants.of(targetProductRefAttr.getName(), targetProductRefAttr.getValue(), true), SetAttributeInAllVariants.of(targetProductSetRefAttr.getName(), targetProductSetRefAttr.getValue(), true), Publish.of());
}
use of com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withSameSlugInSingleBatch_ShouldNotSyncIt.
@Test
void sync_withSameSlugInSingleBatch_ShouldNotSyncIt() {
// 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 ProductDraft key3Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey3").masterVariant(ProductVariantDraftBuilder.of().key("k3").sku("s3").build()).build();
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").masterVariant(ProductVariantDraftBuilder.of().key("k4").sku("s4").build()).build();
final ProductDraft key5Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey5").masterVariant(ProductVariantDraftBuilder.of().key("k5").sku("s5").build()).build();
final ProductDraft key6Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(null).state(null).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey6").masterVariant(ProductVariantDraftBuilder.of().key("k6").sku("s6").build()).build();
final List<ProductDraft> batch = new ArrayList<>();
batch.add(productDraft);
batch.add(key3Draft);
batch.add(key4Draft);
batch.add(key5Draft);
batch.add(key6Draft);
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(batch));
assertThat(syncStatistics).hasValues(5, 1, 1, 3, 0);
final String duplicatedSlug = key3Draft.getSlug().get(Locale.ENGLISH);
assertThat(errorCallBackExceptions).hasSize(3);
assertThat(errorCallBackExceptions).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(3).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.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH 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.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH 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.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH 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());
}
Aggregations