|
| 1 | +package com.commercetools.sync.integration.externalsource.products; |
| 2 | + |
| 3 | +import com.commercetools.sync.commons.exceptions.ReferenceResolutionException; |
| 4 | +import com.commercetools.sync.products.ProductSync; |
| 5 | +import com.commercetools.sync.products.ProductSyncOptions; |
| 6 | +import com.commercetools.sync.products.ProductSyncOptionsBuilder; |
| 7 | +import com.commercetools.sync.products.helpers.ProductSyncStatistics; |
| 8 | +import io.sphere.sdk.categories.Category; |
| 9 | +import io.sphere.sdk.models.Reference; |
| 10 | +import io.sphere.sdk.products.ProductDraft; |
| 11 | +import io.sphere.sdk.producttypes.ProductType; |
| 12 | +import org.junit.AfterClass; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.BeforeClass; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Locale; |
| 20 | +import java.util.function.BiConsumer; |
| 21 | +import java.util.function.Consumer; |
| 22 | + |
| 23 | +import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY; |
| 24 | +import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_NAME; |
| 25 | +import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories; |
| 26 | +import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType; |
| 27 | +import static com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDrafts; |
| 28 | +import static com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteAllProducts; |
| 29 | +import static com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData; |
| 30 | +import static com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType; |
| 31 | +import static com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT; |
| 32 | +import static com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH; |
| 33 | +import static com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH; |
| 34 | +import static com.commercetools.sync.products.ProductSyncMockUtils.createProductDraft; |
| 35 | +import static com.commercetools.sync.products.ProductSyncMockUtils.createRandomCategoryOrderHints; |
| 36 | +import static com.commercetools.tests.utils.CompletionStageUtil.executeBlocking; |
| 37 | +import static io.sphere.sdk.producttypes.ProductType.referenceOfId; |
| 38 | +import static java.util.Collections.singletonList; |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | +import static com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat; |
| 41 | + |
| 42 | +public class ProductReferenceResolverIT { |
| 43 | + |
| 44 | + private static ProductType productType; |
| 45 | + private static List<Category> categories; |
| 46 | + private List<String> errorCallBackMessages; |
| 47 | + private List<String> warningCallBackMessages; |
| 48 | + private List<Throwable> errorCallBackExceptions; |
| 49 | + |
| 50 | + @BeforeClass |
| 51 | + public static void setup() { |
| 52 | + deleteProductSyncTestData(CTP_TARGET_CLIENT); |
| 53 | + createCategoriesCustomType(OLD_CATEGORY_CUSTOM_TYPE_KEY, Locale.ENGLISH, |
| 54 | + OLD_CATEGORY_CUSTOM_TYPE_NAME, CTP_TARGET_CLIENT); |
| 55 | + categories = createCategories(CTP_TARGET_CLIENT, getCategoryDrafts(null, 2)); |
| 56 | + productType = createProductType(PRODUCT_TYPE_RESOURCE_PATH, CTP_TARGET_CLIENT); |
| 57 | + } |
| 58 | + |
| 59 | + @Before |
| 60 | + public void setupPerTest() { |
| 61 | + clearSyncTestCollections(); |
| 62 | + deleteAllProducts(CTP_TARGET_CLIENT); |
| 63 | + } |
| 64 | + |
| 65 | + private void clearSyncTestCollections() { |
| 66 | + errorCallBackMessages = new ArrayList<>(); |
| 67 | + errorCallBackExceptions = new ArrayList<>(); |
| 68 | + warningCallBackMessages = new ArrayList<>(); |
| 69 | + } |
| 70 | + |
| 71 | + private ProductSyncOptions getProductSyncOptions() { |
| 72 | + final BiConsumer<String, Throwable> errorCallBack = (errorMessage, exception) -> { |
| 73 | + errorCallBackMessages.add(errorMessage); |
| 74 | + errorCallBackExceptions.add(exception); |
| 75 | + }; |
| 76 | + |
| 77 | + final Consumer<String> warningCallBack = warningMessage -> warningCallBackMessages.add(warningMessage); |
| 78 | + |
| 79 | + return ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT) |
| 80 | + .errorCallback(errorCallBack) |
| 81 | + .warningCallback(warningCallBack) |
| 82 | + .build(); |
| 83 | + } |
| 84 | + |
| 85 | + @AfterClass |
| 86 | + public static void tearDown() { |
| 87 | + deleteProductSyncTestData(CTP_TARGET_CLIENT); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void sync_withNewProductWithInvalidCategoryReferences_ShouldFailCreatingTheProduct() { |
| 92 | + // Replace the id with key for one category reference but not the other. |
| 93 | + final List<Reference<Category>> invalidCategoryReferences = new ArrayList<>(); |
| 94 | + invalidCategoryReferences.add(Category.referenceOfId(categories.get(0).getId())); |
| 95 | + invalidCategoryReferences.add(Category.referenceOfId(categories.get(1).getKey())); |
| 96 | + |
| 97 | + // Create a product with the invalid category references. (i.e. not ready for reference resolution). |
| 98 | + final ProductDraft productDraft = |
| 99 | + createProductDraft(PRODUCT_KEY_1_RESOURCE_PATH, referenceOfId(productType.getKey()), null, |
| 100 | + null, invalidCategoryReferences, createRandomCategoryOrderHints(invalidCategoryReferences)); |
| 101 | + |
| 102 | + final ProductSync productSync = new ProductSync(getProductSyncOptions()); |
| 103 | + final ProductSyncStatistics syncStatistics = |
| 104 | + executeBlocking(productSync.sync(singletonList(productDraft))); |
| 105 | + |
| 106 | + assertThat(syncStatistics).hasValues(1, 0, 0, 1); |
| 107 | + assertThat(errorCallBackExceptions).hasSize(1); |
| 108 | + final Throwable exception = errorCallBackExceptions.get(0); |
| 109 | + assertThat(exception).isExactlyInstanceOf(ReferenceResolutionException.class) |
| 110 | + .hasMessageContaining("Failed to resolve reference 'category' on ProductDraft with " |
| 111 | + + "key:'productKey1'") |
| 112 | + .hasMessageContaining("Reason: Found a UUID in the id field. Expecting a key without a " |
| 113 | + + "UUID value."); |
| 114 | + |
| 115 | + assertThat(errorCallBackMessages).hasSize(1); |
| 116 | + assertThat(errorCallBackMessages.get(0)) |
| 117 | + .contains("Failed to resolve references on ProductDraft with key:'productKey1'"); |
| 118 | + assertThat(warningCallBackMessages).isEmpty(); |
| 119 | + } |
| 120 | +} |
0 commit comments