Search in sources :

Example 11 with WaitingToBeResolvedProducts

use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.

the class ProductSyncWithReferencedProductsInAnyOrderIT method sync_withMissingParent_shouldSyncCorrectly.

@Test
void sync_withMissingParent_shouldSyncCorrectly() {
    // preparation
    final String productReferenceAttributeName = "product-reference";
    final String parentProductKey = "parent-product-key";
    final String parentProductKey2 = "parent-product-key2";
    final AttributeDraft productReferenceAttribute = AttributeDraft.of(productReferenceAttributeName, asSet(Reference.of(Product.referenceTypeId(), parentProductKey), Reference.of(Product.referenceTypeId(), parentProductKey2)));
    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 childDraft2 = ProductDraftBuilder.of(productType, ofEnglish("foo-2"), ofEnglish("foo-slug-2"), ProductVariantDraftBuilder.of().key("foo-2").sku("foo-2").attributes(productReferenceAttribute).build()).key("foo-2").build();
    final ProductDraft parentDraft = ProductDraftBuilder.of(productType, ofEnglish(parentProductKey2), ofEnglish(parentProductKey2), ProductVariantDraftBuilder.of().sku(parentProductKey2).key(parentProductKey2).build()).key(parentProductKey2).build();
    // test
    final ProductSync productSync = new ProductSync(syncOptions);
    final ProductSyncStatistics syncStatistics = productSync.sync(asList(childDraft1, childDraft2, parentDraft)).toCompletableFuture().join();
    final Product syncedParent = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentProductKey)).toCompletableFuture().join();
    // assertion
    assertThat(syncedParent).isNull();
    assertThat(syncStatistics).hasValues(3, 1, 0, 0, 2);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(actions).isEmpty();
    final UnresolvedReferencesService<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
    final Set<WaitingToBeResolvedProducts> waitingDrafts = unresolvedReferencesService.fetch(asSet(childDraft1.getKey(), childDraft2.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    assertThat(waitingDrafts).containsExactlyInAnyOrder(new WaitingToBeResolvedProducts(childDraft1, singleton(parentProductKey)), new WaitingToBeResolvedProducts(childDraft2, singleton(parentProductKey)));
}
Also used : ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) Product(io.sphere.sdk.products.Product) ProductSync(com.commercetools.sync.products.ProductSync) Test(org.junit.jupiter.api.Test)

Example 12 with WaitingToBeResolvedProducts

use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts 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)

Example 13 with WaitingToBeResolvedProducts

use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.

the class ProductSyncWithReferencedProductsIT method sync_withNonExistingProductReferenceAsAttribute_ShouldFailCreatingTheProduct.

@Test
void sync_withNonExistingProductReferenceAsAttribute_ShouldFailCreatingTheProduct() {
    // preparation
    final AttributeDraft productReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), "nonExistingKey"));
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productReferenceAttribute).build();
    final ProductDraft productDraftWithProductReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
    // test
    final ProductSync productSync = new ProductSync(syncOptions);
    final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(productDraftWithProductReference)).toCompletableFuture().join();
    // assertion
    assertThat(syncStatistics).hasValues(1, 0, 0, 0, 1);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(actions).isEmpty();
    final UnresolvedReferencesServiceImpl<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
    final Set<WaitingToBeResolvedProducts> waitingToBeResolvedDrafts = unresolvedReferencesService.fetch(asSet(productDraftWithProductReference.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    assertThat(waitingToBeResolvedDrafts).singleElement().matches(waitingToBeResolvedDraft -> {
        assertThat(waitingToBeResolvedDraft.getProductDraft().getKey()).isEqualTo(productDraftWithProductReference.getKey());
        assertThat(waitingToBeResolvedDraft.getMissingReferencedProductKeys()).containsExactly("nonExistingKey");
        return true;
    });
}
Also used : ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) ProductSync(com.commercetools.sync.products.ProductSync) Test(org.junit.jupiter.api.Test)

Example 14 with WaitingToBeResolvedProducts

use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.

the class ProductSyncWithNestedReferencedProductsIT method sync_withNonExistingNestedProductReferenceAsAttribute_ShouldFailCreatingTheProduct.

@Test
void sync_withNonExistingNestedProductReferenceAsAttribute_ShouldFailCreatingTheProduct() {
    // preparation
    final ObjectNode nestedAttributeValue = createNestedAttributeValueReferences("product-reference", createReferenceObject("nonExistingKey", Product.referenceTypeId()));
    final AttributeDraft productReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(nestedAttributeValue));
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productReferenceAttribute).build();
    final ProductDraft productDraftWithProductReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
    // test
    final ProductSync productSync = new ProductSync(syncOptions);
    final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(productDraftWithProductReference)).toCompletableFuture().join();
    // assertion
    assertThat(syncStatistics).hasValues(1, 0, 0, 0, 1);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(actions).isEmpty();
    final UnresolvedReferencesServiceImpl<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
    final Set<WaitingToBeResolvedProducts> waitingToBeResolvedDrafts = unresolvedReferencesService.fetch(asSet(productDraftWithProductReference.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    assertThat(waitingToBeResolvedDrafts).singleElement().matches(waitingToBeResolvedDraft -> {
        assertThat(waitingToBeResolvedDraft.getProductDraft().getKey()).isEqualTo(productDraftWithProductReference.getKey());
        assertThat(waitingToBeResolvedDraft.getMissingReferencedProductKeys()).containsExactly("nonExistingKey");
        return true;
    });
}
Also used : ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) ProductSync(com.commercetools.sync.products.ProductSync) Test(org.junit.jupiter.api.Test)

Example 15 with WaitingToBeResolvedProducts

use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.

the class UnresolvedReferencesServiceImplTest method fetch_OnSuccess_ShouldReturnMock.

@SuppressWarnings("unchecked")
@Test
void fetch_OnSuccess_ShouldReturnMock() {
    // preparation
    final CustomObject customObjectMock = mock(CustomObject.class);
    final ProductDraft productDraftMock = mock(ProductDraft.class);
    when(productDraftMock.getKey()).thenReturn("product-draft-key");
    final WaitingToBeResolvedProducts waitingToBeResolved = new WaitingToBeResolvedProducts(productDraftMock, singleton("test-ref"));
    when(customObjectMock.getValue()).thenReturn(waitingToBeResolved);
    final PagedQueryResult result = getMockPagedQueryResult(singletonList(customObjectMock));
    when(productSyncOptions.getCtpClient().execute(any(CustomObjectQuery.class))).thenReturn(completedFuture(result));
    // test
    final Set<WaitingToBeResolvedProducts> toBeResolvedOptional = service.fetch(singleton("product-draft-key"), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    // assertions
    assertThat(toBeResolvedOptional).containsOnly(waitingToBeResolved);
}
Also used : PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) CustomObject(io.sphere.sdk.customobjects.CustomObject) ProductDraft(io.sphere.sdk.products.ProductDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) Test(org.junit.jupiter.api.Test)

Aggregations

WaitingToBeResolvedProducts (com.commercetools.sync.commons.models.WaitingToBeResolvedProducts)19 ProductDraft (io.sphere.sdk.products.ProductDraft)19 Test (org.junit.jupiter.api.Test)18 UnresolvedReferencesServiceImpl (com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl)8 ProductSyncStatistics (com.commercetools.sync.products.helpers.ProductSyncStatistics)7 CustomObject (io.sphere.sdk.customobjects.CustomObject)7 ProductSync (com.commercetools.sync.products.ProductSync)6 AttributeDraft (io.sphere.sdk.products.attributes.AttributeDraft)6 SyncException (com.commercetools.sync.commons.exceptions.SyncException)4 ProductVariantDraft (io.sphere.sdk.products.ProductVariantDraft)4 UnresolvedReferencesService (com.commercetools.sync.services.UnresolvedReferencesService)3 CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY (com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 CustomObjectQuery (io.sphere.sdk.customobjects.queries.CustomObjectQuery)3 Reference (io.sphere.sdk.models.Reference)3 Product (io.sphere.sdk.products.Product)3 PagedQueryResult (io.sphere.sdk.queries.PagedQueryResult)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3