Search in sources :

Example 16 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 StateSyncIT method sync_WithDeletedTransition_ShouldRemoveTransitions.

@Test
void sync_WithDeletedTransition_ShouldRemoveTransitions() {
    final StateDraft stateCDraft = createStateDraft(keyC);
    final State stateC = createStateInSource(stateCDraft);
    final StateDraft tagetStateCDraft = createStateDraft(keyC);
    final State targetStateC = createStateInTarget(tagetStateCDraft);
    final StateDraft stateBDraft = createStateDraft(keyB, stateC);
    final State stateB = createStateInSource(stateBDraft);
    final StateDraft tagetStateBDraft = createStateDraft(keyB, targetStateC);
    final State targetStateB = createStateInTarget(tagetStateBDraft);
    final StateDraft stateADraft = createStateDraft(keyA, stateB);
    final State stateA = createStateInSource(stateADraft);
    final StateDraft tagetStateADraft = createStateDraft(keyA, targetStateB, targetStateC);
    final State targetStateA = createStateInTarget(tagetStateADraft);
    Assertions.assertThat(targetStateB.getTransitions().size()).isEqualTo(1);
    Assertions.assertThat(targetStateA.getTransitions().size()).isEqualTo(2);
    final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(3).build();
    final StateSync stateSync = new StateSync(stateSyncOptions);
    final List<StateDraft> stateDrafts = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, Arrays.asList(stateA, stateB, stateC)).join();
    // test
    final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
    assertThat(stateSyncStatistics).hasValues(3, 0, 1, 0, 0);
    CtpQueryUtils.queryAll(CTP_TARGET_CLIENT, StateQueryBuilder.of().plusPredicates(q -> q.key().is(keyA)).build(), Function.identity()).thenApply(fetchedCategories -> fetchedCategories.stream().flatMap(List::stream).collect(Collectors.toList())).thenAccept(resultStates -> {
        Assertions.assertThat(resultStates.size()).isEqualTo(1);
        Assertions.assertThat(resultStates.get(0).getTransitions().size()).isEqualTo(1);
    }).toCompletableFuture().join();
}
Also used : CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) Arrays(java.util.Arrays) Reference(io.sphere.sdk.models.Reference) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) StateTransformUtils(com.commercetools.sync.states.utils.StateTransformUtils) StateCreateCommand(io.sphere.sdk.states.commands.StateCreateCommand) Arrays.asList(java.util.Arrays.asList) StateITUtils.getStateByKey(com.commercetools.sync.integration.commons.utils.StateITUtils.getStateByKey) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) StateType(io.sphere.sdk.states.StateType) Assertions(org.assertj.core.api.Assertions) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ThreadLocalRandom.current(java.util.concurrent.ThreadLocalRandom.current) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) StateUpdateCommand(io.sphere.sdk.states.commands.StateUpdateCommand) Set(java.util.Set) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) State(io.sphere.sdk.states.State) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) StateITUtils.deleteStatesFromTargetAndSource(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStatesFromTargetAndSource) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) List(java.util.List) CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) CompletionStage(java.util.concurrent.CompletionStage) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) Optional(java.util.Optional) StateDraft(io.sphere.sdk.states.StateDraft) StateITUtils.deleteStates(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStates) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) StateQuery(io.sphere.sdk.states.queries.StateQuery) BadRequestException(io.sphere.sdk.client.BadRequestException) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) Mockito.spy(org.mockito.Mockito.spy) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) State.referenceOfId(io.sphere.sdk.states.State.referenceOfId) StateReferenceResolver(com.commercetools.sync.states.helpers.StateReferenceResolver) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) StateRole(io.sphere.sdk.states.StateRole) Nonnull(javax.annotation.Nonnull) StateDraftBuilder(io.sphere.sdk.states.StateDraftBuilder) StateQueryBuilder(io.sphere.sdk.states.queries.StateQueryBuilder) StateSyncOptionsBuilder(com.commercetools.sync.states.StateSyncOptionsBuilder) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) StateSync(com.commercetools.sync.states.StateSync) Mockito.when(org.mockito.Mockito.when) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) CtpQueryUtils(com.commercetools.sync.commons.utils.CtpQueryUtils) WaitingToBeResolvedTransitions(com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Collections(java.util.Collections) StateSync(com.commercetools.sync.states.StateSync) StateDraft(io.sphere.sdk.states.StateDraft) State(io.sphere.sdk.states.State) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Test(org.junit.jupiter.api.Test)

Example 17 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_withChangedAttributes_ShouldUpdateProducts.

@Test
void sync_withChangedAttributes_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 existing products in target project with keys (productKey1)
    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();
    // Create existing product with productKey1 in source project with changed attributes
    final ProductDraft newProductDraftWithProductReference = createProductDraftBuilder(PRODUCT_KEY_1_CHANGED_ATTRIBUTES_RESOURCE_PATH, sourceProductType.toReference()).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(1, 0, 1, 0);
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    final AttributeDraft priceInfoAttrDraft = AttributeDraft.of("priceInfo", JsonNodeFactory.instance.textNode("100/kg"));
    final AttributeDraft angebotAttrDraft = AttributeDraft.of("angebot", JsonNodeFactory.instance.textNode("big discount"));
    assertThat(updateActions).containsExactlyInAnyOrder(SetAttributeInAllVariants.of(priceInfoAttrDraft, true), SetAttribute.of(1, angebotAttrDraft, true), SetAttributeInAllVariants.ofUnsetAttribute("size", true), SetAttributeInAllVariants.ofUnsetAttribute("rinderrasse", true), SetAttributeInAllVariants.ofUnsetAttribute("herkunft", true), SetAttributeInAllVariants.ofUnsetAttribute("teilstueck", true), SetAttributeInAllVariants.ofUnsetAttribute("fuetterung", true), SetAttributeInAllVariants.ofUnsetAttribute("reifung", true), SetAttributeInAllVariants.ofUnsetAttribute("haltbarkeit", true), SetAttributeInAllVariants.ofUnsetAttribute("verpackung", true), SetAttributeInAllVariants.ofUnsetAttribute("anlieferung", true), SetAttributeInAllVariants.ofUnsetAttribute("zubereitung", true), SetAttribute.ofUnsetAttribute(1, "localisedText", 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) 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) ProductSyncMockUtils.getReferenceSetAttributeDraft(com.commercetools.sync.products.ProductSyncMockUtils.getReferenceSetAttributeDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ProductSync(com.commercetools.sync.products.ProductSync) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Test(org.junit.jupiter.api.Test)

Example 18 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 ProductTypeWithNestedAttributeSyncIT method sync_WithUpdates_ShouldReturnProperStatistics.

@Test
void sync_WithUpdates_ShouldReturnProperStatistics() {
    // preparation
    populateProjectWithNestedAttributes(CTP_TARGET_CLIENT);
    final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
    // only update the nested types
    final List<ProductTypeDraft> productTypeDrafts = ProductTypeTransformUtils.toProductTypeDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, productTypes).join().stream().map(productType -> {
        final List<AttributeDefinitionDraft> attributeDefinitionDrafts = productType.getAttributes().stream().map(attribute -> {
            if (attribute.getAttributeType() instanceof NestedAttributeType) {
                return AttributeDefinitionDraftBuilder.of(attribute).label(ofEnglish("new-label")).build();
            }
            return AttributeDefinitionDraftBuilder.of(attribute).build();
        }).collect(Collectors.toList());
        return ProductTypeDraftBuilder.of(productType).attributes(attributeDefinitionDrafts).build();
    }).collect(Collectors.toList());
    final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
    // test
    final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(productTypeDrafts).toCompletableFuture().join();
    // assertion
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(builtUpdateActions).containsExactly(ChangeAttributeDefinitionLabel.of("nestedattr2", ofEnglish("new-label")));
    assertThat(productTypeSyncStatistics).hasValues(4, 0, 1, 0, 0);
    assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 4 product types were processed in total" + " (0 created, 1 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) ArrayList(java.util.ArrayList) ProductTypeSyncOptionsBuilder(com.commercetools.sync.producttypes.ProductTypeSyncOptionsBuilder) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) ChangeAttributeDefinitionLabel(io.sphere.sdk.producttypes.commands.updateactions.ChangeAttributeDefinitionLabel) ProductTypeTransformUtils(com.commercetools.sync.producttypes.utils.ProductTypeTransformUtils) AttributeDefinitionDraftBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) ProductTypeITUtils.populateProjectWithNestedAttributes(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.populateProjectWithNestedAttributes) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Collectors(java.util.stream.Collectors) ProductTypeSyncOptions(com.commercetools.sync.producttypes.ProductTypeSyncOptions) Test(org.junit.jupiter.api.Test) NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductTypeSync(com.commercetools.sync.producttypes.ProductTypeSync) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductTypeSync(com.commercetools.sync.producttypes.ProductTypeSync) ProductType(io.sphere.sdk.producttypes.ProductType) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ArrayList(java.util.ArrayList) List(java.util.List) NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Test(org.junit.jupiter.api.Test)

Example 19 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 ShoppingListSyncIT method sync_WithUpdatedCustomerOnShoppingList_ShouldReturnProperStatistics.

@Test
void sync_WithUpdatedCustomerOnShoppingList_ShouldReturnProperStatistics() {
    final List<ShoppingList> shoppingLists = CTP_SOURCE_CLIENT.execute(buildShoppingListQuery()).toCompletableFuture().join().getResults();
    createSampleCustomerJaneDoe(CTP_SOURCE_CLIENT);
    final Customer sampleCustomerJaneDoe = createSampleCustomerJaneDoe(CTP_TARGET_CLIENT);
    final List<ShoppingListDraft> updatedShoppingListDrafts = ShoppingListTransformUtils.toShoppingListDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, shoppingLists).join().stream().map(shoppingListDraft -> ShoppingListDraftBuilder.of(shoppingListDraft).name(LocalizedString.ofEnglish("second-shopping-list")).anonymousId(null).customer(ResourceIdentifier.ofKey(sampleCustomerJaneDoe.getKey())).build()).collect(Collectors.toList());
    final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(updatedShoppingListDrafts).toCompletableFuture().join();
    assertThat(errorMessages).isEmpty();
    assertThat(warningMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    // order is important, otherwise the error below could occur:
    // "message" : "The resource was already claimed by a customer..
    // "action" : {
    // "action" : "setAnonymousId"
    // }
    assertThat(updateActionList).containsExactly(ChangeName.of(LocalizedString.ofEnglish("second-shopping-list")), SetAnonymousId.of(null), SetCustomer.of(Reference.of(Customer.referenceTypeId(), sampleCustomerJaneDoe.getId())));
    AssertionsForStatistics.assertThat(shoppingListSyncStatistics).hasValues(2, 1, 1, 0);
    assertThat(shoppingListSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 shopping lists were processed in total " + "(1 created, 1 updated and 0 failed to sync).");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Reference(io.sphere.sdk.models.Reference) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) SetAnonymousId(io.sphere.sdk.shoppinglists.commands.updateactions.SetAnonymousId) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ShoppingListSyncOptions(com.commercetools.sync.shoppinglists.ShoppingListSyncOptions) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ShoppingListSyncOptionsBuilder(com.commercetools.sync.shoppinglists.ShoppingListSyncOptionsBuilder) ShoppingListTransformUtils(com.commercetools.sync.shoppinglists.utils.ShoppingListTransformUtils) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) ShoppingListITUtils.createShoppingList(com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.createShoppingList) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) ShoppingListDraftBuilder(io.sphere.sdk.shoppinglists.ShoppingListDraftBuilder) CustomerITUtils.createSampleCustomerJaneDoe(com.commercetools.sync.integration.commons.utils.CustomerITUtils.createSampleCustomerJaneDoe) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) ShoppingListSync(com.commercetools.sync.shoppinglists.ShoppingListSync) ShoppingListSyncStatistics(com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics) ShoppingListITUtils.deleteShoppingListTestData(com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.deleteShoppingListTestData) ChangeName(io.sphere.sdk.shoppinglists.commands.updateactions.ChangeName) ShoppingListITUtils.createSampleShoppingListCarrotCake(com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.createSampleShoppingListCarrotCake) Customer(io.sphere.sdk.customers.Customer) Collectors(java.util.stream.Collectors) ShoppingListReferenceResolutionUtils.buildShoppingListQuery(com.commercetools.sync.shoppinglists.utils.ShoppingListReferenceResolutionUtils.buildShoppingListQuery) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) List(java.util.List) SetCustomer(io.sphere.sdk.shoppinglists.commands.updateactions.SetCustomer) AssertionsForStatistics(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) ShoppingListSyncStatistics(com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics) Customer(io.sphere.sdk.customers.Customer) SetCustomer(io.sphere.sdk.shoppinglists.commands.updateactions.SetCustomer) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ShoppingListITUtils.createShoppingList(com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.createShoppingList) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) Test(org.junit.jupiter.api.Test)

Example 20 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 ProductTypeWithNestedAttributeSyncIT method sync_WithOneDraftPerBatchOnEmptyProject_ShouldReturnProperStatistics.

@Test
void sync_WithOneDraftPerBatchOnEmptyProject_ShouldReturnProperStatistics() {
    // preparation
    final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
    final List<ProductTypeDraft> productTypeDrafts = ProductTypeTransformUtils.toProductTypeDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, productTypes).join();
    productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(1).beforeUpdateCallback((actions, draft, oldProductType) -> {
        builtUpdateActions.addAll(actions);
        return actions;
    }).errorCallback((exception, oldResource, newResource, actions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception);
    }).build();
    final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
    // test
    final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(productTypeDrafts).toCompletableFuture().join();
    // assertion
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(builtUpdateActions).isEmpty();
    assertThat(productTypeSyncStatistics).hasValues(4, 4, 0, 0, 0);
    assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 4 product types were processed in total" + " (4 created, 0 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) ArrayList(java.util.ArrayList) ProductTypeSyncOptionsBuilder(com.commercetools.sync.producttypes.ProductTypeSyncOptionsBuilder) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) ChangeAttributeDefinitionLabel(io.sphere.sdk.producttypes.commands.updateactions.ChangeAttributeDefinitionLabel) ProductTypeTransformUtils(com.commercetools.sync.producttypes.utils.ProductTypeTransformUtils) AttributeDefinitionDraftBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) ProductTypeITUtils.populateProjectWithNestedAttributes(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.populateProjectWithNestedAttributes) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Collectors(java.util.stream.Collectors) ProductTypeSyncOptions(com.commercetools.sync.producttypes.ProductTypeSyncOptions) Test(org.junit.jupiter.api.Test) NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductTypeSync(com.commercetools.sync.producttypes.ProductTypeSync) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductTypeSync(com.commercetools.sync.producttypes.ProductTypeSync) ProductType(io.sphere.sdk.producttypes.ProductType) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) 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