Search in sources :

Example 1 with ProductTypeSync

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

the class ProductTypeSyncIT method sync_WithUpdates_ShouldReturnProperStatistics.

@Test
void sync_WithUpdates_ShouldReturnProperStatistics() {
    // preparation
    final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
    final List<ProductTypeDraft> productTypeDrafts = productTypes.stream().map(productType -> {
        final List<AttributeDefinitionDraft> attributeDefinitionDrafts = productType.getAttributes().stream().map(attribute -> AttributeDefinitionDraftBuilder.of(attribute).build()).collect(Collectors.toList());
        return ProductTypeDraftBuilder.of(productType.getKey(), "newName", productType.getDescription(), attributeDefinitionDrafts).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, 1, 0, 0);
    assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 product types were processed in total" + " (1 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 : 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) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) ProductTypeSyncOptions(com.commercetools.sync.producttypes.ProductTypeSyncOptions) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 2 with ProductTypeSync

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

the class ProductTypeWithNestedAttributeSyncIT method sync_WithoutUpdates_ShouldReturnProperStatistics.

@Test
void sync_WithoutUpdates_ShouldReturnProperStatistics() {
    // preparation
    populateProjectWithNestedAttributes(CTP_TARGET_CLIENT);
    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();
    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, 0, 0, 0, 0);
    assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 4 product types were processed in total" + " (0 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 : 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)

Example 3 with ProductTypeSync

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

the class ProductTypeWithNestedAttributeSyncIT method sync_WithEmptyTargetProject_ShouldReturnProperStatistics.

@Test
void sync_WithEmptyTargetProject_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();
    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 : 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)

Example 4 with ProductTypeSync

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

the class ProductTypeSyncBenchmark method sync_NewProductTypes_ShouldCreateProductTypes.

@Test
void sync_NewProductTypes_ShouldCreateProductTypes() throws IOException {
    // preparation
    final List<ProductTypeDraft> productTypeDrafts = buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
    final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
    // benchmark
    final long beforeSyncTime = System.currentTimeMillis();
    final ProductTypeSyncStatistics syncStatistics = executeBlocking(productTypeSync.sync(productTypeDrafts));
    final long totalTime = System.currentTimeMillis() - beforeSyncTime;
    // assert on threshold (based on history of benchmarks; highest was ~12 seconds)
    // double of the highest benchmark
    final int threshold = 24000;
    assertThat(totalTime).withFailMessage(format(THRESHOLD_EXCEEDED_ERROR, totalTime, threshold)).isLessThan(threshold);
    // Assert actual state of CTP project (total number of existing product types)
    final CompletableFuture<Integer> totalNumberOfProductTypes = CTP_TARGET_CLIENT.execute(ProductTypeQuery.of()).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
    executeBlocking(totalNumberOfProductTypes);
    assertThat(totalNumberOfProductTypes).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
    assertThat(syncStatistics).hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, NUMBER_OF_RESOURCE_UNDER_TEST, 0, 0);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    saveNewResult(PRODUCT_TYPE_SYNC, CREATES_ONLY, totalTime);
}
Also used : PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ProductTypeSync(com.commercetools.sync.producttypes.ProductTypeSync) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Test(org.junit.jupiter.api.Test)

Example 5 with ProductTypeSync

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

the class ProductTypeSyncBenchmark method sync_ExistingProductTypes_ShouldUpdateProductTypes.

@Test
void sync_ExistingProductTypes_ShouldUpdateProductTypes() throws IOException {
    // preparation
    final List<ProductTypeDraft> productTypeDrafts = buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
    // Create drafts to target project with different attribute definition name
    CompletableFuture.allOf(productTypeDrafts.stream().map(ProductTypeDraftBuilder::of).map(ProductTypeSyncBenchmark::applyAttributeDefinitionNameChange).map(ProductTypeDraftBuilder::build).map(draft -> CTP_TARGET_CLIENT.execute(ProductTypeCreateCommand.of(draft))).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new)).join();
    final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
    // benchmark
    final long beforeSyncTime = System.currentTimeMillis();
    final ProductTypeSyncStatistics syncStatistics = executeBlocking(productTypeSync.sync(productTypeDrafts));
    final long totalTime = System.currentTimeMillis() - beforeSyncTime;
    // assert on threshold (based on history of benchmarks; highest was ~13 seconds)
    // double of the highest benchmark
    final int threshold = 26000;
    assertThat(totalTime).withFailMessage(format(THRESHOLD_EXCEEDED_ERROR, totalTime, threshold)).isLessThan(threshold);
    // Assert actual state of CTP project (number of updated product types)
    final CompletableFuture<Integer> totalNumberOfUpdatedProductTypes = CTP_TARGET_CLIENT.execute(ProductTypeQuery.of().withPredicates(p -> p.attributes().name().is("attr_name_1"))).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
    executeBlocking(totalNumberOfUpdatedProductTypes);
    assertThat(totalNumberOfUpdatedProductTypes).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
    // Assert actual state of CTP project (total number of existing product types)
    final CompletableFuture<Integer> totalNumberOfProductTypes = CTP_TARGET_CLIENT.execute(ProductTypeQuery.of()).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
    executeBlocking(totalNumberOfProductTypes);
    assertThat(totalNumberOfProductTypes).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
    // Assert statistics
    assertThat(syncStatistics).hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, 0, NUMBER_OF_RESOURCE_UNDER_TEST, 0);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    saveNewResult(PRODUCT_TYPE_SYNC, UPDATES_ONLY, totalTime);
}
Also used : PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ProductTypeSync(com.commercetools.sync.producttypes.ProductTypeSync) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Test(org.junit.jupiter.api.Test)

Aggregations

ProductTypeSync (com.commercetools.sync.producttypes.ProductTypeSync)34 ProductTypeSyncStatistics (com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics)34 ProductTypeDraft (io.sphere.sdk.producttypes.ProductTypeDraft)34 Test (org.junit.jupiter.api.Test)33 ProductType (io.sphere.sdk.producttypes.ProductType)31 ProductTypeSyncOptions (com.commercetools.sync.producttypes.ProductTypeSyncOptions)24 AttributeDefinitionDraft (io.sphere.sdk.products.attributes.AttributeDefinitionDraft)23 ProductTypeSyncOptionsBuilder (com.commercetools.sync.producttypes.ProductTypeSyncOptionsBuilder)18 ProductTypeQuery (io.sphere.sdk.producttypes.queries.ProductTypeQuery)18 List (java.util.List)18 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)17 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)17 AttributeDefinitionDraftBuilder (io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder)17 ArrayList (java.util.ArrayList)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 AfterAll (org.junit.jupiter.api.AfterAll)17 BeforeEach (org.junit.jupiter.api.BeforeEach)17 UpdateAction (io.sphere.sdk.commands.UpdateAction)16 ProductTypeDraftBuilder (io.sphere.sdk.producttypes.ProductTypeDraftBuilder)15 Collectors (java.util.stream.Collectors)15