use of com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT 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.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT in project commercetools-sync-java by commercetools.
the class CategorySyncIT method syncDrafts_fromCategoriesWithoutKeys_ShouldNotUpdateCategories.
@Test
void syncDrafts_fromCategoriesWithoutKeys_ShouldNotUpdateCategories() {
final CategoryDraft oldCategoryDraft1 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "cat1"), LocalizedString.of(Locale.ENGLISH, "furniture1")).custom(getCustomFieldsDraft()).key("newKey1").build();
final CategoryDraft oldCategoryDraft2 = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "cat2"), LocalizedString.of(Locale.ENGLISH, "furniture2")).custom(getCustomFieldsDraft()).key("newKey2").build();
// Create two categories in the source with Keys.
List<CompletableFuture<Category>> futureCreations = new ArrayList<>();
futureCreations.add(CTP_SOURCE_CLIENT.execute(CategoryCreateCommand.of(oldCategoryDraft1)).toCompletableFuture());
futureCreations.add(CTP_SOURCE_CLIENT.execute(CategoryCreateCommand.of(oldCategoryDraft2)).toCompletableFuture());
CompletableFuture.allOf(futureCreations.toArray(new CompletableFuture[futureCreations.size()])).join();
// Create two categories in the target without Keys.
futureCreations = new ArrayList<>();
final CategoryDraft newCategoryDraft1 = CategoryDraftBuilder.of(oldCategoryDraft1).key(null).build();
final CategoryDraft newCategoryDraft2 = CategoryDraftBuilder.of(oldCategoryDraft2).key(null).build();
futureCreations.add(CTP_TARGET_CLIENT.execute(CategoryCreateCommand.of(newCategoryDraft1)).toCompletableFuture());
futureCreations.add(CTP_TARGET_CLIENT.execute(CategoryCreateCommand.of(newCategoryDraft2)).toCompletableFuture());
CompletableFuture.allOf(futureCreations.toArray(new CompletableFuture[futureCreations.size()])).join();
// ---------
final List<Category> categories = CTP_SOURCE_CLIENT.execute(CategoryQuery.of()).toCompletableFuture().join().getResults();
final List<CategoryDraft> categoryDrafts = CategoryTransformUtils.toCategoryDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, categories).join();
final CategorySyncStatistics syncStatistics = categorySync.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(2, 0, 0, 2, 0);
assertThat(callBackErrorResponses).hasSize(2).allSatisfy(errorMessage -> {
assertThat(errorMessage).contains("\"code\" : \"DuplicateField\"");
assertThat(errorMessage).contains("\"field\" : \"slug.en\"");
});
assertThat(callBackExceptions).hasSize(2).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(callBackWarningResponses).isEmpty();
}
use of com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT in project commercetools-sync-java by commercetools.
the class CustomerSyncIT method prepareUpdatedCustomerDrafts.
private List<CustomerDraft> prepareUpdatedCustomerDrafts(@Nonnull final List<Customer> customers) {
final Store storeCologne = createStore(CTP_TARGET_CLIENT, "store-cologne");
final List<CustomerDraft> customerDrafts = CustomerTransformUtils.toCustomerDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, customers).join();
return customerDrafts.stream().map(customerDraft -> CustomerDraftBuilder.of(customerDraft).plusStores(ResourceIdentifier.ofKey(storeCologne.getKey())).custom(CustomFieldsDraft.ofTypeKeyAndJson("customer-type-gold", createCustomFieldsJsonMap())).addresses(singletonList(Address.of(CountryCode.DE).withCity("cologne").withKey("address1"))).defaultBillingAddress(0).billingAddresses(singletonList(0)).defaultShippingAddress(0).shippingAddresses(singletonList(0)).build()).collect(Collectors.toList());
}
use of com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT in project commercetools-sync-java by commercetools.
the class ProductReferenceResolverIT method setupTest.
/**
* Deletes Products and Types from target CTP projects, then it populates target CTP project with
* product test data.
*/
@BeforeEach
void setupTest() {
deleteAllProducts(CTP_TARGET_CLIENT);
deleteAllProducts(CTP_SOURCE_CLIENT);
errorCallBackMessages = new ArrayList<>();
errorCallBackExceptions = new ArrayList<>();
warningCallBackMessages = new ArrayList<>();
final ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).build();
productSync = new ProductSync(syncOptions);
referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
}
use of com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT in project commercetools-sync-java by commercetools.
the class CartDiscountSyncIT method sync_WithUpdates_ShouldReturnProperStatistics.
@Test
void sync_WithUpdates_ShouldReturnProperStatistics() {
// preparation
final List<CartDiscount> cartDiscounts = CTP_SOURCE_CLIENT.execute(CartDiscountQuery.of()).toCompletableFuture().join().getResults();
final String newTypeKey = "new-type";
createCartDiscountCustomType(newTypeKey, Locale.ENGLISH, newTypeKey, CTP_SOURCE_CLIENT);
final Type newTargetCustomType = createCartDiscountCustomType(newTypeKey, Locale.ENGLISH, newTypeKey, CTP_TARGET_CLIENT);
final List<CartDiscountDraft> cartDiscountDrafts = CartDiscountTransformUtils.toCartDiscountDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, cartDiscounts).join();
// Apply some changes
final List<CartDiscountDraft> updatedCartDiscountDrafts = cartDiscountDrafts.stream().map(draft -> CartDiscountDraftBuilder.of(draft).cartPredicate(CartPredicate.of("totalPrice >= \"100 EUR\"")).value(AbsoluteCartDiscountValue.of(MoneyImpl.of(40, EUR))).target(ShippingCostTarget.of()).custom(CustomFieldsDraft.ofTypeKeyAndJson(newTypeKey, emptyMap())).build()).collect(Collectors.toList());
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final List<UpdateAction<CartDiscount>> updateActionsList = new ArrayList<>();
final CartDiscountSyncOptions cartDiscountSyncOptions = CartDiscountSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).beforeUpdateCallback((updateActions, newCartDiscount, oldCartDiscount) -> {
updateActionsList.addAll(updateActions);
return updateActions;
}).build();
final CartDiscountSync cartDiscountSync = new CartDiscountSync(cartDiscountSyncOptions);
// test
final CartDiscountSyncStatistics cartDiscountSyncStatistics = cartDiscountSync.sync(updatedCartDiscountDrafts).toCompletableFuture().join();
// assertion
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(updateActionsList).containsExactly(ChangeValue.of(CartDiscountValue.ofAbsolute(Collections.singletonList(MoneyImpl.of(40, EUR)))), ChangeCartPredicate.of("totalPrice >= \"100 EUR\""), ChangeTarget.of(ShippingCostTarget.of()), SetCustomType.ofTypeIdAndJson(newTargetCustomType.getId(), emptyMap()));
assertThat(cartDiscountSyncStatistics).hasValues(2, 1, 1, 0);
assertThat(cartDiscountSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 cart discounts were processed in total" + " (1 created, 1 updated and 0 failed to sync).");
}
Aggregations