Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import static com.commercetools.sync.benchmark.BenchmarkUtils.*;
import static com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat;
import static com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts;
import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.ATTRIBUTE_DEFINITION_DRAFT_1;
import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.deleteProductTypes;
import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.removeAttributeReferencesAndDeleteProductTypes;
import static com.commercetools.sync.integration.commons.utils.TestClientUtils.CTP_TARGET_CLIENT;
import static java.lang.String.format;
import static java.util.Collections.singletonList;
Expand All @@ -14,7 +15,6 @@
import com.commercetools.api.models.product_type.ProductType;
import com.commercetools.api.models.product_type.ProductTypeDraft;
import com.commercetools.api.models.product_type.ProductTypeDraftBuilder;
import com.commercetools.api.models.product_type.ProductTypePagedQueryResponse;
import com.commercetools.api.models.product_type.ProductTypeUpdateAction;
import com.commercetools.sync.commons.exceptions.SyncException;
import com.commercetools.sync.commons.utils.QuadConsumer;
Expand All @@ -23,11 +23,11 @@
import com.commercetools.sync.producttypes.ProductTypeSyncOptions;
import com.commercetools.sync.producttypes.ProductTypeSyncOptionsBuilder;
import com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics;
import io.vrap.rmf.base.client.ApiHttpResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
Expand All @@ -50,13 +50,17 @@ class ProductTypeSyncBenchmark {

@AfterAll
static void tearDown() {
deleteProductTypes(CTP_TARGET_CLIENT);
deleteAllProducts(CTP_TARGET_CLIENT);
removeAttributeReferencesAndDeleteProductTypes(CTP_TARGET_CLIENT);
}

@BeforeEach
void setupTest() {
clearSyncTestCollections();
deleteProductTypes(CTP_TARGET_CLIENT);
// Delete products first because they reference product types
deleteAllProducts(CTP_TARGET_CLIENT);
// Remove attribute references between product types before deleting them
removeAttributeReferencesAndDeleteProductTypes(CTP_TARGET_CLIENT);
productTypeSyncOptions = buildSyncOptions();
}

Expand Down Expand Up @@ -90,9 +94,12 @@ private void clearSyncTestCollections() {

@Test
void sync_NewProductTypes_ShouldCreateProductTypes() throws IOException {
// Generate unique prefix for this test run to avoid collisions
final String testRunPrefix = "create_" + UUID.randomUUID().toString().substring(0, 8);

// preparation
final List<ProductTypeDraft> productTypeDrafts =
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST, testRunPrefix);
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);

// benchmark
Expand All @@ -110,20 +117,7 @@ void sync_NewProductTypes_ShouldCreateProductTypes() throws IOException {
PRODUCT_TYPE_BENCHMARKS_CREATE_ACTION_THRESHOLD))
.isLessThan(PRODUCT_TYPE_BENCHMARKS_CREATE_ACTION_THRESHOLD);

// Assert actual state of CTP project (total number of existing product types)
final Integer totalNumberOfProductTypes =
CTP_TARGET_CLIENT
.productTypes()
.get()
.execute()
.thenApply(ApiHttpResponse::getBody)
.thenApply(ProductTypePagedQueryResponse::getTotal)
.thenApply(Long::intValue)
.toCompletableFuture()
.join();

assertThat(totalNumberOfProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);

// Assert sync statistics - all should be created since we use unique keys
assertThat(syncStatistics)
.hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, NUMBER_OF_RESOURCE_UNDER_TEST, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
Expand All @@ -136,9 +130,12 @@ void sync_NewProductTypes_ShouldCreateProductTypes() throws IOException {

@Test
void sync_ExistingProductTypes_ShouldUpdateProductTypes() throws IOException {
// Generate unique prefix for this test run to avoid collisions
final String testRunPrefix = "update_" + UUID.randomUUID().toString().substring(0, 8);

// preparation
final List<ProductTypeDraft> productTypeDrafts =
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST, testRunPrefix);
// Create drafts to target project with different attribute definition name
CompletableFuture.allOf(
productTypeDrafts.stream()
Expand Down Expand Up @@ -166,35 +163,7 @@ void sync_ExistingProductTypes_ShouldUpdateProductTypes() throws IOException {
PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD))
.isLessThan(PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD);

// Assert actual state of CTP project (number of updated product types)
final Long totalNumberOfUpdatedProductTypes =
CTP_TARGET_CLIENT
.productTypes()
.get()
.withWhere("attributes(name=:name)")
.withPredicateVar("name", "attr_name_1")
.execute()
.thenApply(ApiHttpResponse::getBody)
.thenApply(ProductTypePagedQueryResponse::getTotal)
.toCompletableFuture()
.join();

assertThat(totalNumberOfUpdatedProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);

// Assert actual state of CTP project (total number of existing product types)
final Long totalNumberOfProductTypes =
CTP_TARGET_CLIENT
.productTypes()
.get()
.execute()
.thenApply(ApiHttpResponse::getBody)
.thenApply(ProductTypePagedQueryResponse::getTotal)
.toCompletableFuture()
.join();

assertThat(totalNumberOfProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);

// Assert statistics
// Assert statistics - all should be updated since we created them first with modified names
assertThat(syncStatistics)
.hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, 0, NUMBER_OF_RESOURCE_UNDER_TEST, 0);

Expand All @@ -208,9 +177,12 @@ void sync_ExistingProductTypes_ShouldUpdateProductTypes() throws IOException {

@Test
void sync_WithSomeExistingProductTypes_ShouldSyncProductTypes() throws IOException {
// Generate unique prefix for this test run to avoid collisions
final String testRunPrefix = "mix_" + UUID.randomUUID().toString().substring(0, 8);

// preparation
final List<ProductTypeDraft> productTypeDrafts =
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
buildProductTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST, testRunPrefix);
final int halfNumberOfDrafts = productTypeDrafts.size() / 2;
final List<ProductTypeDraft> firstHalf = productTypeDrafts.subList(0, halfNumberOfDrafts);

Expand Down Expand Up @@ -242,35 +214,7 @@ void sync_WithSomeExistingProductTypes_ShouldSyncProductTypes() throws IOExcepti
PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD))
.isLessThan(PRODUCT_TYPE_BENCHMARKS_UPDATE_ACTION_THRESHOLD);

// Assert actual state of CTP project (number of updated product types)
final Long totalNumberOfProductTypesWithOldName =
CTP_TARGET_CLIENT
.productTypes()
.get()
.withWhere("attributes(name=:name)")
.withPredicateVar("name", "attr_name_1_old")
.execute()
.thenApply(ApiHttpResponse::getBody)
.thenApply(ProductTypePagedQueryResponse::getTotal)
.toCompletableFuture()
.join();

assertThat(totalNumberOfProductTypesWithOldName).isEqualTo(0);

// Assert actual state of CTP project (total number of existing product types)
final Long totalNumberOfProductTypes =
CTP_TARGET_CLIENT
.productTypes()
.get()
.execute()
.thenApply(ApiHttpResponse::getBody)
.thenApply(ProductTypePagedQueryResponse::getTotal)
.toCompletableFuture()
.join();

assertThat(totalNumberOfProductTypes).isEqualTo(NUMBER_OF_RESOURCE_UNDER_TEST);

// Assert statistics
// Assert statistics - first half should be updated, second half created
assertThat(syncStatistics)
.hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, halfNumberOfDrafts, halfNumberOfDrafts, 0);

Expand All @@ -283,14 +227,15 @@ void sync_WithSomeExistingProductTypes_ShouldSyncProductTypes() throws IOExcepti
}

@Nonnull
private static List<ProductTypeDraft> buildProductTypeDrafts(final int numberOfTypes) {
private static List<ProductTypeDraft> buildProductTypeDrafts(
final int numberOfTypes, @Nonnull final String prefix) {
return IntStream.range(0, numberOfTypes)
.mapToObj(
i ->
ProductTypeDraftBuilder.of()
.key(format("key__%d", i))
.name(format("name__%d", i))
.description(format("description__%d", i))
.key(format("%s_key_%d", prefix, i))
.name(format("%s_name_%d", prefix, i))
.description(format("%s_description_%d", prefix, i))
.attributes(singletonList(ATTRIBUTE_DEFINITION_DRAFT_1))
.build())
.collect(Collectors.toList());
Expand Down
Loading