Search in sources :

Example 1 with UnresolvedReferencesService

use of com.commercetools.sync.services.UnresolvedReferencesService in project commercetools-sync-java by commercetools.

the class UnresolvedReferencesServiceImplIT method setupTest.

@BeforeEach
void setupTest() {
    deleteWaitingToBeResolvedCustomObjects(CTP_TARGET_CLIENT, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class);
    errorCallBackMessages = new ArrayList<>();
    errorCallBackExceptions = new ArrayList<>();
    warningCallBackMessages = new ArrayList<>();
    final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception);
    }).warningCallback((syncException, productDraft, product) -> warningCallBackMessages.add(syncException.getMessage())).build();
    unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(productSyncOptions);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) CustomObjectByKeyGet(io.sphere.sdk.customobjects.queries.CustomObjectByKeyGet) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CustomObject(io.sphere.sdk.customobjects.CustomObject) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) ArrayList(java.util.ArrayList) Collections.singleton(java.util.Collections.singleton) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) ProductDraft(io.sphere.sdk.products.ProductDraft) PRODUCT_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_1_RESOURCE_PATH) DigestUtils.sha1Hex(org.apache.commons.codec.digest.DigestUtils.sha1Hex) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) Set(java.util.Set) PRODUCT_KEY_SPECIAL_CHARS_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_KEY_SPECIAL_CHARS_RESOURCE_PATH) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) Optional(java.util.Optional) CustomObjectITUtils.deleteWaitingToBeResolvedCustomObjects(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteWaitingToBeResolvedCustomObjects) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with UnresolvedReferencesService

use of com.commercetools.sync.services.UnresolvedReferencesService in project commercetools-sync-java by commercetools.

the class ProductSyncWithReferencedProductsInAnyOrderIT method sync_withFailToFetchCustomObject_shouldSyncCorrectly.

@SuppressWarnings("unchecked")
@Test
void sync_withFailToFetchCustomObject_shouldSyncCorrectly() {
    // preparation
    final String productReferenceAttributeName = "product-reference";
    final String parentProductKey = "parent-product-key";
    final AttributeDraft productReferenceAttribute = AttributeDraft.of(productReferenceAttributeName, Reference.of(Product.referenceTypeId(), parentProductKey));
    final ProductDraft childDraft1 = ProductDraftBuilder.of(productType, ofEnglish("foo"), ofEnglish("foo-slug"), ProductVariantDraftBuilder.of().key("foo").sku("foo").attributes(productReferenceAttribute).build()).key(product.getKey()).build();
    final ProductDraft parentDraft = ProductDraftBuilder.of(productType, ofEnglish(parentProductKey), ofEnglish(parentProductKey), ProductVariantDraftBuilder.of().sku(parentProductKey).key(parentProductKey).build()).key(parentProductKey).build();
    final SphereClient ctpClient = spy(CTP_TARGET_CLIENT);
    final BadGatewayException gatewayException = new BadGatewayException("failed to respond.");
    when(ctpClient.execute(any(CustomObjectQuery.class))).thenReturn(CompletableFutureUtils.failed(gatewayException)).thenCallRealMethod();
    syncOptions = ProductSyncOptionsBuilder.of(ctpClient).errorCallback((syncException, draft, product, updateActions) -> collectErrors(syncException.getMessage(), syncException)).beforeUpdateCallback(this::collectActions).build();
    // test
    final ProductSync productSync = new ProductSync(syncOptions);
    final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(childDraft1)).thenCompose(ignoredResult -> productSync.sync(singletonList(parentDraft))).toCompletableFuture().join();
    // assertion
    assertThat(syncStatistics).hasValues(2, 1, 0, 1, 0);
    assertThat(errorCallBackMessages).containsExactly("Failed to fetch ProductDrafts waiting to be resolved with keys '[foo]'.");
    assertThat(errorCallBackExceptions).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(BadGatewayException.class);
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(actions).isEmpty();
    final UnresolvedReferencesService<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
    final Set<WaitingToBeResolvedProducts> waitingDrafts = unresolvedReferencesService.fetch(asSet(childDraft1.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    assertThat(waitingDrafts).containsExactly(new WaitingToBeResolvedProducts(childDraft1, singleton(parentProductKey)));
}
Also used : CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Reference(io.sphere.sdk.models.Reference) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) Collections.singletonList(java.util.Collections.singletonList) ProductITUtils.deleteProductSyncTestData(com.commercetools.sync.integration.commons.utils.ProductITUtils.deleteProductSyncTestData) AfterAll(org.junit.jupiter.api.AfterAll) Collections.singleton(java.util.Collections.singleton) BeforeAll(org.junit.jupiter.api.BeforeAll) Arrays.asList(java.util.Arrays.asList) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) TriConsumer(com.commercetools.sync.commons.utils.TriConsumer) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Collections.emptyList(java.util.Collections.emptyList) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Product(io.sphere.sdk.products.Product) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Test(org.junit.jupiter.api.Test) 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) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) ProductSyncMockUtils.createReferenceObject(com.commercetools.sync.products.ProductSyncMockUtils.createReferenceObject) Optional(java.util.Optional) REFERENCE_TYPE_ID_FIELD(com.commercetools.sync.commons.utils.ResourceIdentifierUtils.REFERENCE_TYPE_ID_FIELD) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) 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) CustomObject(io.sphere.sdk.customobjects.CustomObject) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Attribute(io.sphere.sdk.products.attributes.Attribute) Assertions.as(org.assertj.core.api.Assertions.as) ProductSync(com.commercetools.sync.products.ProductSync) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) REFERENCE_ID_FIELD(com.commercetools.sync.commons.utils.ResourceIdentifierUtils.REFERENCE_ID_FIELD) Mockito.when(org.mockito.Mockito.when) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) CustomObjectITUtils.deleteWaitingToBeResolvedCustomObjects(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteWaitingToBeResolvedCustomObjects) WaitingToBeResolved(com.commercetools.sync.commons.models.WaitingToBeResolved) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ProductSync(com.commercetools.sync.products.ProductSync) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) ProductDraft(io.sphere.sdk.products.ProductDraft) SphereClient(io.sphere.sdk.client.SphereClient) CompletionException(java.util.concurrent.CompletionException) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) BadGatewayException(io.sphere.sdk.client.BadGatewayException) Test(org.junit.jupiter.api.Test)

Aggregations

WaitingToBeResolvedProducts (com.commercetools.sync.commons.models.WaitingToBeResolvedProducts)2 CustomObjectITUtils.deleteWaitingToBeResolvedCustomObjects (com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteWaitingToBeResolvedCustomObjects)2 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)2 ProductSyncOptions (com.commercetools.sync.products.ProductSyncOptions)2 ProductSyncOptionsBuilder (com.commercetools.sync.products.ProductSyncOptionsBuilder)2 UnresolvedReferencesService (com.commercetools.sync.services.UnresolvedReferencesService)2 UnresolvedReferencesServiceImpl (com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl)2 CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY (com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY)2 CustomObject (io.sphere.sdk.customobjects.CustomObject)2 ProductDraft (io.sphere.sdk.products.ProductDraft)2 SphereInternalUtils.asSet (io.sphere.sdk.utils.SphereInternalUtils.asSet)2 ArrayList (java.util.ArrayList)2 Collections.singleton (java.util.Collections.singleton)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)1