Search in sources :

Example 1 with CTP_SOURCE_CLIENT

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());
}
Also used : ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) SUPPLY_CHANNEL_KEY_1(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.SUPPLY_CHANNEL_KEY_1) Reference(io.sphere.sdk.models.Reference) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) PRODUCT_KEY_2_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_2_RESOURCE_PATH) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) StateITUtils.createState(com.commercetools.sync.integration.commons.utils.StateITUtils.createState) ProductITUtils.createPricesCustomType(com.commercetools.sync.integration.commons.utils.ProductITUtils.createPricesCustomType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) PRODUCT_KEY_1_CHANGED_WITH_PRICES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_CHANGED_WITH_PRICES_RESOURCE_PATH) TaxCategoryITUtils.createTaxCategory(com.commercetools.sync.integration.commons.utils.TaxCategoryITUtils.createTaxCategory) ProductSyncMockUtils.createProductDraftBuilder(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) Channel(io.sphere.sdk.channels.Channel) Collections.singletonList(java.util.Collections.singletonList) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) StateType(io.sphere.sdk.states.StateType) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductITUtils.getDraftWithPriceReferences(com.commercetools.sync.integration.commons.utils.ProductITUtils.getDraftWithPriceReferences) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) ENGLISH(java.util.Locale.ENGLISH) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) Product(io.sphere.sdk.products.Product) PRODUCT_KEY_1_CHANGED_ATTRIBUTES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_CHANGED_ATTRIBUTES_RESOURCE_PATH) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) State(io.sphere.sdk.states.State) Test(org.junit.jupiter.api.Test) ProductSyncMockUtils.getReferenceSetAttributeDraft(com.commercetools.sync.products.ProductSyncMockUtils.getReferenceSetAttributeDraft) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Publish(io.sphere.sdk.products.commands.updateactions.Publish) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ChannelCreateCommand(io.sphere.sdk.channels.commands.ChannelCreateCommand) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) ChannelDraft(io.sphere.sdk.channels.ChannelDraft) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductByKeyGet(io.sphere.sdk.products.queries.ProductByKeyGet) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) SyncFilter(com.commercetools.sync.products.SyncFilter) ProductTransformUtils(com.commercetools.sync.products.utils.ProductTransformUtils) Attribute(io.sphere.sdk.products.attributes.Attribute) ATTRIBUTES(com.commercetools.sync.products.ActionGroup.ATTRIBUTES) ProductSync(com.commercetools.sync.products.ProductSync) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) SetAttribute(io.sphere.sdk.products.commands.updateactions.SetAttribute) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Collections.emptyMap(java.util.Collections.emptyMap) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH) PRODUCT_KEY_1_CHANGED_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_CHANGED_RESOURCE_PATH) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ProductSyncMockUtils.getProductReferenceWithId(com.commercetools.sync.products.ProductSyncMockUtils.getProductReferenceWithId) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) Collections(java.util.Collections) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) Type(io.sphere.sdk.types.Type) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProductSyncMockUtils.getReferenceSetAttributeDraft(com.commercetools.sync.products.ProductSyncMockUtils.getReferenceSetAttributeDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) Product(io.sphere.sdk.products.Product) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) ProductSync(com.commercetools.sync.products.ProductSync) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Test(org.junit.jupiter.api.Test)

Example 2 with CTP_SOURCE_CLIENT

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();
}
Also used : CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) BeforeEach(org.junit.jupiter.api.BeforeEach) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) CategoryTransformUtils(com.commercetools.sync.categories.utils.CategoryTransformUtils) ITUtils.deleteTypesFromTargetAndSource(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypesFromTargetAndSource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) CompletableFuture(java.util.concurrent.CompletableFuture) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) ArrayList(java.util.ArrayList) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) CategorySync(com.commercetools.sync.categories.CategorySync) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) CategoryITUtils.deleteAllCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories) Category(io.sphere.sdk.categories.Category) CategoryCreateCommand(io.sphere.sdk.categories.commands.CategoryCreateCommand) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) ITUtils.createCustomFieldsJsonMap(com.commercetools.sync.integration.commons.utils.ITUtils.createCustomFieldsJsonMap) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) Collectors.toList(java.util.stream.Collectors.toList) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) List(java.util.List) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) CategoryITUtils.getCustomFieldsDraft(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCustomFieldsDraft) CategoryITUtils.createChildren(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createChildren) QueryExecutionUtils(io.sphere.sdk.queries.QueryExecutionUtils) CategoryITUtils.getCategoryDraftsWithPrefix(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDraftsWithPrefix) Collections(java.util.Collections) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) Category(io.sphere.sdk.categories.Category) ArrayList(java.util.ArrayList) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) CompletableFuture(java.util.concurrent.CompletableFuture) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) Test(org.junit.jupiter.api.Test)

Example 3 with CTP_SOURCE_CLIENT

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());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CustomerTransformUtils(com.commercetools.sync.customers.utils.CustomerTransformUtils) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CustomerSyncStatistics(com.commercetools.sync.customers.helpers.CustomerSyncStatistics) Address(io.sphere.sdk.models.Address) CustomerITUtils.deleteCustomerSyncTestData(com.commercetools.sync.integration.commons.utils.CustomerITUtils.deleteCustomerSyncTestData) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) CustomerITUtils.createSampleCustomerJohnDoe(com.commercetools.sync.integration.commons.utils.CustomerITUtils.createSampleCustomerJohnDoe) CustomerQuery(io.sphere.sdk.customers.queries.CustomerQuery) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) Nonnull(javax.annotation.Nonnull) CountryCode(com.neovisionaries.i18n.CountryCode) CustomerITUtils.createSampleCustomerJaneDoe(com.commercetools.sync.integration.commons.utils.CustomerITUtils.createSampleCustomerJaneDoe) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) StoreITUtils.createStore(com.commercetools.sync.integration.commons.utils.StoreITUtils.createStore) CustomerDraftBuilder(io.sphere.sdk.customers.CustomerDraftBuilder) Store(io.sphere.sdk.stores.Store) CustomerSync(com.commercetools.sync.customers.CustomerSync) ITUtils.createCustomFieldsJsonMap(com.commercetools.sync.integration.commons.utils.ITUtils.createCustomFieldsJsonMap) Customer(io.sphere.sdk.customers.Customer) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CustomerSyncOptionsBuilder(com.commercetools.sync.customers.CustomerSyncOptionsBuilder) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) List(java.util.List) CustomerSyncOptions(com.commercetools.sync.customers.CustomerSyncOptions) AssertionsForStatistics(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) StoreITUtils.createStore(com.commercetools.sync.integration.commons.utils.StoreITUtils.createStore) Store(io.sphere.sdk.stores.Store) CustomerDraft(io.sphere.sdk.customers.CustomerDraft)

Example 4 with CTP_SOURCE_CLIENT

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();
}
Also used : ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Reference(io.sphere.sdk.models.Reference) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) StateITUtils.createState(com.commercetools.sync.integration.commons.utils.StateITUtils.createState) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryITUtils.getCategoryDrafts(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts) TaxCategoryITUtils.createTaxCategory(com.commercetools.sync.integration.commons.utils.TaxCategoryITUtils.createTaxCategory) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) OLD_CATEGORY_CUSTOM_TYPE_NAME(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME) StateType(io.sphere.sdk.states.StateType) PRODUCT_TYPE_WITH_REFERENCES_FOR_VARIANT_ATTRIBUTES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_WITH_REFERENCES_FOR_VARIANT_ATTRIBUTES_RESOURCE_PATH) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Customer(io.sphere.sdk.customers.Customer) CategoryITUtils.getReferencesWithIds(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getReferencesWithIds) State(io.sphere.sdk.states.State) PRODUCT_TYPE_NO_KEY_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_NO_KEY_RESOURCE_PATH) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) List(java.util.List) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) ProductSyncMockUtils.createProductDraft(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) ProductITUtils.deleteAllProducts(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts) PRODUCT_KEY_1_NO_ATTRIBUTES_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_NO_ATTRIBUTES_RESOURCE_PATH) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ProductTransformUtils(com.commercetools.sync.products.utils.ProductTransformUtils) ProductSync(com.commercetools.sync.products.ProductSync) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) Nonnull(javax.annotation.Nonnull) CustomerITUtils.createSampleCustomerJaneDoe(com.commercetools.sync.integration.commons.utils.CustomerITUtils.createSampleCustomerJaneDoe) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ProductSyncMockUtils.createRandomCategoryOrderHints(com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints) BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER(com.commercetools.sync.commons.helpers.BaseReferenceResolver.BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) ProductSync(com.commercetools.sync.products.ProductSync) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with CTP_SOURCE_CLIENT

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).");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) MoneyImpl(io.sphere.sdk.utils.MoneyImpl) ITUtils.deleteTypesFromTargetAndSource(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypesFromTargetAndSource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CartPredicate(io.sphere.sdk.cartdiscounts.CartPredicate) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) CartDiscountSyncOptionsBuilder(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptionsBuilder) EUR(io.sphere.sdk.models.DefaultCurrencyUnits.EUR) ChangeCartPredicate(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeCartPredicate) CartDiscountQuery(io.sphere.sdk.cartdiscounts.queries.CartDiscountQuery) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountDraftBuilder(io.sphere.sdk.cartdiscounts.CartDiscountDraftBuilder) CartDiscountSyncStatistics(com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) Locale(java.util.Locale) CartDiscountTransformUtils(com.commercetools.sync.cartdiscounts.utils.CartDiscountTransformUtils) CartDiscountSync(com.commercetools.sync.cartdiscounts.CartDiscountSync) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) ChangeTarget(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeTarget) ChangeValue(io.sphere.sdk.cartdiscounts.commands.updateactions.ChangeValue) CartDiscountITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateTargetProject) CartDiscountITUtils.populateSourceProject(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.populateSourceProject) Collections.emptyMap(java.util.Collections.emptyMap) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) CartDiscountValue(io.sphere.sdk.cartdiscounts.CartDiscountValue) AbsoluteCartDiscountValue(io.sphere.sdk.cartdiscounts.AbsoluteCartDiscountValue) CartDiscountITUtils.createCartDiscountCustomType(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.createCartDiscountCustomType) SetCustomType(io.sphere.sdk.cartdiscounts.commands.updateactions.SetCustomType) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) List(java.util.List) CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.deleteCartDiscountsFromTargetAndSource) ShippingCostTarget(io.sphere.sdk.cartdiscounts.ShippingCostTarget) Collections(java.util.Collections) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) Type(io.sphere.sdk.types.Type) CartDiscountSync(com.commercetools.sync.cartdiscounts.CartDiscountSync) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) UpdateAction(io.sphere.sdk.commands.UpdateAction) CartDiscountSyncStatistics(com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics) ArrayList(java.util.ArrayList) CartDiscountSyncOptions(com.commercetools.sync.cartdiscounts.CartDiscountSyncOptions) CartDiscountITUtils.createCartDiscountCustomType(com.commercetools.sync.integration.commons.utils.CartDiscountITUtils.createCartDiscountCustomType) SetCustomType(io.sphere.sdk.cartdiscounts.commands.updateactions.SetCustomType) Type(io.sphere.sdk.types.Type) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Test(org.junit.jupiter.api.Test)

Aggregations

AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)20 CTP_SOURCE_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT)20 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)20 List (java.util.List)20 AfterAll (org.junit.jupiter.api.AfterAll)20 BeforeEach (org.junit.jupiter.api.BeforeEach)20 Test (org.junit.jupiter.api.Test)20 CaffeineReferenceIdToKeyCacheImpl (com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl)19 ReferenceIdToKeyCache (com.commercetools.sync.commons.utils.ReferenceIdToKeyCache)19 ArrayList (java.util.ArrayList)19 Collectors (java.util.stream.Collectors)15 LocalizedString.ofEnglish (io.sphere.sdk.models.LocalizedString.ofEnglish)13 Reference (io.sphere.sdk.models.Reference)13 Collections (java.util.Collections)13 Nonnull (javax.annotation.Nonnull)13 Collections.singletonList (java.util.Collections.singletonList)12 State (io.sphere.sdk.states.State)11 StateType (io.sphere.sdk.states.StateType)11 LocalizedString (io.sphere.sdk.models.LocalizedString)10 Arrays (java.util.Arrays)10