Search in sources :

Example 36 with ProductTypeSyncStatistics

use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.

the class ProductTypeSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    // preparation
    final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", emptyList());
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception);
    }).build();
    final ProductTypeService mockProductTypeService = mock(ProductTypeService.class);
    when(mockProductTypeService.fetchMatchingProductTypesByKeys(singleton(newProductTypeDraft.getKey()))).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    when(mockProductTypeService.cacheKeysToIds(anySet())).thenReturn(CompletableFuture.completedFuture(emptyMap()));
    final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions, mockProductTypeService);
    // test
    final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
    // assertions
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to fetch existing product types with keys: '[foo]'.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
    assertThat(productTypeSyncStatistics).hasValues(1, 0, 0, 1, 0);
}
Also used : Reference(io.sphere.sdk.models.Reference) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductTypeCreateCommand(io.sphere.sdk.producttypes.commands.ProductTypeCreateCommand) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) ProductTypeService(com.commercetools.sync.services.ProductTypeService) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) ProductTypeUpdateCommand(io.sphere.sdk.producttypes.commands.ProductTypeUpdateCommand) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Collections.emptyList(java.util.Collections.emptyList) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) Sets.newLinkedHashSet(org.assertj.core.util.Sets.newLinkedHashSet) Optional(java.util.Optional) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) Mockito.mock(org.mockito.Mockito.mock) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) SphereException(io.sphere.sdk.models.SphereException) ChangeDescription(io.sphere.sdk.producttypes.commands.updateactions.ChangeDescription) Collections.emptyMap(java.util.Collections.emptyMap) AttributeDefinitionDraftBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder) Collections.emptySet(java.util.Collections.emptySet) ProductType.referenceOfId(io.sphere.sdk.producttypes.ProductType.referenceOfId) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Lists.list(org.assertj.core.util.Lists.list) Mockito.when(org.mockito.Mockito.when) ChangeName(io.sphere.sdk.producttypes.commands.updateactions.ChangeName) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Collections(java.util.Collections) ArrayList(java.util.ArrayList) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) SphereException(io.sphere.sdk.models.SphereException) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) ProductTypeService(com.commercetools.sync.services.ProductTypeService) SphereClient(io.sphere.sdk.client.SphereClient) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.jupiter.api.Test)

Example 37 with ProductTypeSyncStatistics

use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.

the class ProductTypeSyncTest method sync_WithNullAttributeDefinitions_ShouldSyncCorrectly.

@Test
void sync_WithNullAttributeDefinitions_ShouldSyncCorrectly() {
    // preparation
    final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", null);
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final List<UpdateAction> actions = new ArrayList<>();
    final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(mock(SphereClient.class)).beforeUpdateCallback((generatedActions, draft, productType) -> {
        actions.addAll(generatedActions);
        return generatedActions;
    }).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception);
    }).build();
    final ProductTypeService mockProductTypeService = mock(ProductTypeServiceImpl.class);
    final ProductType existingProductType = mock(ProductType.class);
    when(existingProductType.getKey()).thenReturn(newProductTypeDraft.getKey());
    when(mockProductTypeService.fetchMatchingProductTypesByKeys(singleton(newProductTypeDraft.getKey()))).thenReturn(CompletableFuture.completedFuture(singleton(existingProductType)));
    when(mockProductTypeService.fetchMatchingProductTypesByKeys(emptySet())).thenReturn(CompletableFuture.completedFuture(Collections.emptySet()));
    when(mockProductTypeService.updateProductType(any(), any())).thenReturn(CompletableFuture.completedFuture(existingProductType));
    when(mockProductTypeService.cacheKeysToIds(anySet())).thenReturn(CompletableFuture.completedFuture(emptyMap()));
    final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions, mockProductTypeService);
    // test
    final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
    // assertions
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(actions).containsExactly(ChangeName.of(newProductTypeDraft.getName()), ChangeDescription.of(newProductTypeDraft.getDescription()));
    assertThat(productTypeSyncStatistics).hasValues(1, 0, 1, 0, 0);
}
Also used : Reference(io.sphere.sdk.models.Reference) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductTypeCreateCommand(io.sphere.sdk.producttypes.commands.ProductTypeCreateCommand) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) ProductTypeService(com.commercetools.sync.services.ProductTypeService) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) ProductTypeUpdateCommand(io.sphere.sdk.producttypes.commands.ProductTypeUpdateCommand) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Collections.emptyList(java.util.Collections.emptyList) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) Sets.newLinkedHashSet(org.assertj.core.util.Sets.newLinkedHashSet) Optional(java.util.Optional) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) Mockito.mock(org.mockito.Mockito.mock) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) SphereException(io.sphere.sdk.models.SphereException) ChangeDescription(io.sphere.sdk.producttypes.commands.updateactions.ChangeDescription) Collections.emptyMap(java.util.Collections.emptyMap) AttributeDefinitionDraftBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder) Collections.emptySet(java.util.Collections.emptySet) ProductType.referenceOfId(io.sphere.sdk.producttypes.ProductType.referenceOfId) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Lists.list(org.assertj.core.util.Lists.list) Mockito.when(org.mockito.Mockito.when) ChangeName(io.sphere.sdk.producttypes.commands.updateactions.ChangeName) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Collections(java.util.Collections) UpdateAction(io.sphere.sdk.commands.UpdateAction) ArrayList(java.util.ArrayList) ProductType(io.sphere.sdk.producttypes.ProductType) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) ProductTypeService(com.commercetools.sync.services.ProductTypeService) Test(org.junit.jupiter.api.Test)

Example 38 with ProductTypeSyncStatistics

use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.

the class ProductTypeSyncTest method sync_WithEmptyAttributeDefinitions_ShouldSyncCorrectly.

@Test
void sync_WithEmptyAttributeDefinitions_ShouldSyncCorrectly() {
    // preparation
    final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts("foo", "name", "desc", emptyList());
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception);
    }).build();
    final ProductTypeService mockProductTypeService = mock(ProductTypeServiceImpl.class);
    final ProductType existingProductType = mock(ProductType.class);
    when(existingProductType.getKey()).thenReturn(newProductTypeDraft.getKey());
    when(mockProductTypeService.fetchMatchingProductTypesByKeys(singleton(newProductTypeDraft.getKey()))).thenReturn(CompletableFuture.completedFuture(singleton(existingProductType)));
    when(mockProductTypeService.fetchMatchingProductTypesByKeys(emptySet())).thenReturn(CompletableFuture.completedFuture(Collections.emptySet()));
    when(mockProductTypeService.updateProductType(any(), any())).thenReturn(CompletableFuture.completedFuture(existingProductType));
    when(mockProductTypeService.cacheKeysToIds(anySet())).thenReturn(CompletableFuture.completedFuture(emptyMap()));
    final ProductTypeSync productTypeSync = new ProductTypeSync(syncOptions, mockProductTypeService);
    // test
    final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(singletonList(newProductTypeDraft)).toCompletableFuture().join();
    // assertions
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(productTypeSyncStatistics).hasValues(1, 0, 1, 0, 0);
}
Also used : Reference(io.sphere.sdk.models.Reference) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductTypeCreateCommand(io.sphere.sdk.producttypes.commands.ProductTypeCreateCommand) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) ProductTypeService(com.commercetools.sync.services.ProductTypeService) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) ProductTypeUpdateCommand(io.sphere.sdk.producttypes.commands.ProductTypeUpdateCommand) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Collections.emptyList(java.util.Collections.emptyList) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) Sets.newLinkedHashSet(org.assertj.core.util.Sets.newLinkedHashSet) Optional(java.util.Optional) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) Mockito.mock(org.mockito.Mockito.mock) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) SphereException(io.sphere.sdk.models.SphereException) ChangeDescription(io.sphere.sdk.producttypes.commands.updateactions.ChangeDescription) Collections.emptyMap(java.util.Collections.emptyMap) AttributeDefinitionDraftBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder) Collections.emptySet(java.util.Collections.emptySet) ProductType.referenceOfId(io.sphere.sdk.producttypes.ProductType.referenceOfId) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Lists.list(org.assertj.core.util.Lists.list) Mockito.when(org.mockito.Mockito.when) ChangeName(io.sphere.sdk.producttypes.commands.updateactions.ChangeName) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Collections(java.util.Collections) ArrayList(java.util.ArrayList) ProductType(io.sphere.sdk.producttypes.ProductType) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) ProductTypeService(com.commercetools.sync.services.ProductTypeService) SphereClient(io.sphere.sdk.client.SphereClient) Test(org.junit.jupiter.api.Test)

Example 39 with ProductTypeSyncStatistics

use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics 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 40 with ProductTypeSyncStatistics

use of com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics in project commercetools-sync-java by commercetools.

the class ProductTypeSyncIT method sync_WithoutUpdates_ShouldReturnProperStatistics.

@Test
void sync_WithoutUpdates_ShouldReturnProperStatistics() {
    // preparation
    final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
    final List<ProductTypeDraft> productTypeDrafts = productTypes.stream().map(ProductTypeDraftBuilder::of).map(ProductTypeDraftBuilder::build).collect(Collectors.toList());
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final ProductTypeSyncOptions productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception.getCause());
    }).build();
    final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
    // test
    final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(productTypeDrafts).toCompletableFuture().join();
    // assertion
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(productTypeSyncStatistics).hasValues(2, 1, 0, 0, 0);
    assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 product types were processed in total" + " (1 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 : AttributeDefinitionDraftBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder) BeforeEach(org.junit.jupiter.api.BeforeEach) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProductType(io.sphere.sdk.producttypes.ProductType) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Collectors(java.util.stream.Collectors) ProductTypeSyncOptions(com.commercetools.sync.producttypes.ProductTypeSyncOptions) ArrayList(java.util.ArrayList) ProductTypeSyncOptionsBuilder(com.commercetools.sync.producttypes.ProductTypeSyncOptionsBuilder) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) List(java.util.List) ProductTypeITUtils.populateSourceProject(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.populateSourceProject) ProductTypeITUtils.deleteProductTypesFromTargetAndSource(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.deleteProductTypesFromTargetAndSource) 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) ProductTypeITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.populateTargetProject) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductTypeSync(com.commercetools.sync.producttypes.ProductTypeSync) ProductType(io.sphere.sdk.producttypes.ProductType) ArrayList(java.util.ArrayList) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) ProductTypeSyncOptions(com.commercetools.sync.producttypes.ProductTypeSyncOptions) Test(org.junit.jupiter.api.Test)

Aggregations

ProductTypeSyncStatistics (com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics)41 ProductTypeDraft (io.sphere.sdk.producttypes.ProductTypeDraft)41 Test (org.junit.jupiter.api.Test)41 ProductType (io.sphere.sdk.producttypes.ProductType)38 ProductTypeSync (com.commercetools.sync.producttypes.ProductTypeSync)33 AttributeDefinitionDraft (io.sphere.sdk.products.attributes.AttributeDefinitionDraft)31 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)25 AttributeDefinitionDraftBuilder (io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder)25 ProductTypeQuery (io.sphere.sdk.producttypes.queries.ProductTypeQuery)25 ArrayList (java.util.ArrayList)25 List (java.util.List)25 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)25 ProductTypeSyncOptions (com.commercetools.sync.producttypes.ProductTypeSyncOptions)23 UpdateAction (io.sphere.sdk.commands.UpdateAction)23 LocalizedString.ofEnglish (io.sphere.sdk.models.LocalizedString.ofEnglish)23 ProductTypeDraftBuilder (io.sphere.sdk.producttypes.ProductTypeDraftBuilder)23 PagedQueryResult (io.sphere.sdk.queries.PagedQueryResult)22 SyncException (com.commercetools.sync.commons.exceptions.SyncException)21 SphereClient (io.sphere.sdk.client.SphereClient)21 Collections.emptyList (java.util.Collections.emptyList)21