Search in sources :

Example 6 with WaitingToBeResolvedProducts

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

the class ProductSyncWithNestedReferencedProductsIT method sync_withNestedProductReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct.

@Test
void sync_withNestedProductReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct() {
    // preparation
    final ObjectNode nestedAttributeValue = createNestedAttributeValueSetOfReferences("product-reference-set", createReferenceObject(testProduct1.getKey(), Product.referenceTypeId()), 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 7 with WaitingToBeResolvedProducts

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

the class CleanupUnresolvedReferenceCustomObjectsIT method createSampleUnresolvedReferences.

void createSampleUnresolvedReferences() {
    final ProductDraft sampleProductDraft = SphereJsonUtils.readObjectFromResource(PRODUCT_KEY_1_RESOURCE_PATH, ProductDraft.class);
    final CategoryDraft sampleCategoryDraft = SphereJsonUtils.readObjectFromResource(CATEGORY_KEY_1_RESOURCE_PATH, CategoryDraft.class);
    final Set<Reference<State>> sampleTransitions = new HashSet<>(Arrays.asList(State.referenceOfId("id1"), State.referenceOfId("id2")));
    final List<WaitingToBeResolvedProducts> productUnresolvedReferences = new ArrayList<>();
    final List<WaitingToBeResolvedCategories> categoryUnresolvedReferences = new ArrayList<>();
    final List<WaitingToBeResolvedTransitions> transitionUnresolvedReferences = new ArrayList<>();
    for (int i = 1; i <= 5; i++) {
        productUnresolvedReferences.add(new WaitingToBeResolvedProducts(ProductDraftBuilder.of(sampleProductDraft).key(format("productKey%s", i)).build(), asSet("foo", "bar")));
        categoryUnresolvedReferences.add(new WaitingToBeResolvedCategories(CategoryDraftBuilder.of(sampleCategoryDraft).key(format("categoryKey%s", i)).build(), asSet("foo", "bar")));
        transitionUnresolvedReferences.add(new WaitingToBeResolvedTransitions(StateDraftBuilder.of(format("stateKeys%s", i), StateType.LINE_ITEM_STATE).transitions(sampleTransitions).build(), asSet("foo", "bar")));
    }
    CompletableFuture.allOf(CompletableFuture.allOf(productUnresolvedReferences.stream().map(draft -> unresolvedReferencesServiceForProducts.save(draft, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class)).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new)), CompletableFuture.allOf(categoryUnresolvedReferences.stream().map(draft -> unresolvedReferencesServiceForCategories.save(draft, CUSTOM_OBJECT_CATEGORY_CONTAINER_KEY, WaitingToBeResolvedCategories.class)).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new)), CompletableFuture.allOf(transitionUnresolvedReferences.stream().map(draft -> unresolvedReferencesServiceForTransitions.save(draft, CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY, WaitingToBeResolvedTransitions.class)).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new))).join();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY) Arrays(java.util.Arrays) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) Reference(io.sphere.sdk.models.Reference) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) CompletableFuture(java.util.concurrent.CompletableFuture) WaitingToBeResolvedCategories(com.commercetools.sync.commons.models.WaitingToBeResolvedCategories) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StateType(io.sphere.sdk.states.StateType) 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) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) StateDraftBuilder(io.sphere.sdk.states.StateDraftBuilder) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) StateSyncOptionsBuilder(com.commercetools.sync.states.StateSyncOptionsBuilder) Set(java.util.Set) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) State(io.sphere.sdk.states.State) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) WaitingToBeResolvedTransitions(com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions) List(java.util.List) CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY) CompletionStage(java.util.concurrent.CompletionStage) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) CleanupUnresolvedReferenceCustomObjects(com.commercetools.sync.commons.CleanupUnresolvedReferenceCustomObjects) CATEGORY_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.CATEGORY_KEY_1_RESOURCE_PATH) CUSTOM_OBJECT_CATEGORY_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_CATEGORY_CONTAINER_KEY) CustomObjectITUtils.deleteWaitingToBeResolvedCustomObjects(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteWaitingToBeResolvedCustomObjects) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) Reference(io.sphere.sdk.models.Reference) ArrayList(java.util.ArrayList) WaitingToBeResolvedCategories(com.commercetools.sync.commons.models.WaitingToBeResolvedCategories) CompletableFuture(java.util.concurrent.CompletableFuture) WaitingToBeResolvedTransitions(com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions) ProductDraft(io.sphere.sdk.products.ProductDraft) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) CompletionStage(java.util.concurrent.CompletionStage) HashSet(java.util.HashSet)

Example 8 with WaitingToBeResolvedProducts

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

the class ProductSync method resolveNowReadyReferences.

@Nonnull
private CompletionStage<Void> resolveNowReadyReferences(final Map<String, String> keyToIdCache) {
    // We delete anyways the keys from the statistics before we attempt resolution, because even if
    // resolution fails
    // the products that failed to be synced would be counted as failed.
    final Set<String> referencingDraftKeys = readyToResolve.stream().map(statistics::removeAndGetReferencingKeys).filter(Objects::nonNull).flatMap(Set::stream).collect(Collectors.toSet());
    if (referencingDraftKeys.isEmpty()) {
        return CompletableFuture.completedFuture(null);
    }
    final Set<ProductDraft> readyToSync = new HashSet<>();
    final Set<WaitingToBeResolvedProducts> waitingDraftsToBeUpdated = new HashSet<>();
    return unresolvedReferencesService.fetch(referencingDraftKeys, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).handle(ImmutablePair::new).thenCompose(fetchResponse -> {
        final Set<WaitingToBeResolvedProducts> waitingDrafts = fetchResponse.getKey();
        final Throwable fetchException = fetchResponse.getValue();
        if (fetchException != null) {
            final String errorMessage = format(UNRESOLVED_REFERENCES_STORE_FETCH_FAILED, referencingDraftKeys);
            handleError(new SyncException(errorMessage, fetchException), referencingDraftKeys.size());
            return CompletableFuture.completedFuture(null);
        }
        waitingDrafts.forEach(waitingDraft -> {
            final Set<String> missingReferencedProductKeys = waitingDraft.getMissingReferencedProductKeys();
            missingReferencedProductKeys.removeAll(readyToResolve);
            if (missingReferencedProductKeys.isEmpty()) {
                readyToSync.add(waitingDraft.getProductDraft());
            } else {
                waitingDraftsToBeUpdated.add(waitingDraft);
            }
        });
        return updateWaitingDrafts(waitingDraftsToBeUpdated).thenCompose(aVoid -> syncBatch(readyToSync, keyToIdCache)).thenCompose(aVoid -> removeFromWaiting(readyToSync));
    });
}
Also used : CustomerServiceImpl(com.commercetools.sync.services.impl.CustomerServiceImpl) SyncException(com.commercetools.sync.commons.exceptions.SyncException) BaseSync(com.commercetools.sync.commons.BaseSync) ProductUpdateActionUtils.getAllVariants(com.commercetools.sync.products.utils.ProductUpdateActionUtils.getAllVariants) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductBatchValidator(com.commercetools.sync.products.helpers.ProductBatchValidator) Collectors.toMap(java.util.stream.Collectors.toMap) ProductTypeService(com.commercetools.sync.services.ProductTypeService) Map(java.util.Map) TaxCategoryService(com.commercetools.sync.services.TaxCategoryService) SyncUtils.batchElements(com.commercetools.sync.commons.utils.SyncUtils.batchElements) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) TaxCategorySyncOptionsBuilder(com.commercetools.sync.taxcategories.TaxCategorySyncOptionsBuilder) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CategoryService(com.commercetools.sync.services.CategoryService) StateService(com.commercetools.sync.services.StateService) Product(io.sphere.sdk.products.Product) ChannelServiceImpl(com.commercetools.sync.services.impl.ChannelServiceImpl) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) CustomObjectSyncOptionsBuilder(com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder) Objects(java.util.Objects) CustomerSyncOptionsBuilder(com.commercetools.sync.customers.CustomerSyncOptionsBuilder) ChannelService(com.commercetools.sync.services.ChannelService) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) List(java.util.List) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) CompletionStage(java.util.concurrent.CompletionStage) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) Function.identity(java.util.function.Function.identity) TaxCategoryServiceImpl(com.commercetools.sync.services.impl.TaxCategoryServiceImpl) Optional(java.util.Optional) CustomObjectServiceImpl(com.commercetools.sync.services.impl.CustomObjectServiceImpl) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) CompletableFuture(java.util.concurrent.CompletableFuture) ProductReferenceResolver(com.commercetools.sync.products.helpers.ProductReferenceResolver) HashSet(java.util.HashSet) CustomerGroupServiceImpl(com.commercetools.sync.services.impl.CustomerGroupServiceImpl) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) ProductSyncUtils.buildActions(com.commercetools.sync.products.utils.ProductSyncUtils.buildActions) TypeService(com.commercetools.sync.services.TypeService) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) CustomerService(com.commercetools.sync.services.CustomerService) ChannelRole(io.sphere.sdk.channels.ChannelRole) StateSyncOptionsBuilder(com.commercetools.sync.states.StateSyncOptionsBuilder) Optional.ofNullable(java.util.Optional.ofNullable) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) ProductService(com.commercetools.sync.services.ProductService) Collections(java.util.Collections) ProductDraft(io.sphere.sdk.products.ProductDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) Objects(java.util.Objects) SyncException(com.commercetools.sync.commons.exceptions.SyncException) HashSet(java.util.HashSet) Nonnull(javax.annotation.Nonnull)

Example 9 with WaitingToBeResolvedProducts

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

the class UnresolvedReferencesServiceImplIT method save_ExistingProductDraftWithoutException_overwritesOldCustomObjectValue.

@Test
void save_ExistingProductDraftWithoutException_overwritesOldCustomObjectValue() {
    // preparation
    final ProductDraft productDraft = SphereJsonUtils.readObjectFromResource(PRODUCT_KEY_1_RESOURCE_PATH, ProductDraft.class);
    final WaitingToBeResolvedProducts productDraftWithUnresolvedRefs = new WaitingToBeResolvedProducts(productDraft, asSet("foo", "bar"));
    unresolvedReferencesService.save(productDraftWithUnresolvedRefs, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    final WaitingToBeResolvedProducts productDraftWithUnresolvedNewRefs = new WaitingToBeResolvedProducts(productDraft, asSet("foo123", "bar123"));
    // test
    final Optional<WaitingToBeResolvedProducts> latestResult = unresolvedReferencesService.save(productDraftWithUnresolvedNewRefs, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    // assertions
    assertThat(latestResult).hasValueSatisfying(waitingToBeResolved -> {
        assertThat(waitingToBeResolved.getProductDraft()).isEqualTo(productDraft);
        assertThat(waitingToBeResolved.getMissingReferencedProductKeys()).isEqualTo(productDraftWithUnresolvedNewRefs.getMissingReferencedProductKeys());
    });
    final CustomObjectByKeyGet<WaitingToBeResolvedProducts> customObjectByKeyGet = CustomObjectByKeyGet.of(CUSTOM_OBJECT_CONTAINER_KEY, sha1Hex(productDraft.getKey()), WaitingToBeResolvedProducts.class);
    final CustomObject<WaitingToBeResolvedProducts> createdCustomObject = CTP_TARGET_CLIENT.execute(customObjectByKeyGet).toCompletableFuture().join();
    assertThat(createdCustomObject.getValue()).isEqualTo(productDraftWithUnresolvedNewRefs);
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
}
Also used : ProductDraft(io.sphere.sdk.products.ProductDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) Test(org.junit.jupiter.api.Test)

Example 10 with WaitingToBeResolvedProducts

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

the class UnresolvedReferencesServiceImplIT method saveFetchAndDelete_WithoutExceptions_shouldWorkCorrectly.

@Test
void saveFetchAndDelete_WithoutExceptions_shouldWorkCorrectly() {
    // preparation
    final ProductDraft productDraft = SphereJsonUtils.readObjectFromResource(PRODUCT_KEY_1_RESOURCE_PATH, ProductDraft.class);
    final WaitingToBeResolvedProducts productDraftWithUnresolvedRefs = new WaitingToBeResolvedProducts(productDraft, asSet("foo", "bar"));
    // test
    final Optional<WaitingToBeResolvedProducts> result = unresolvedReferencesService.save(productDraftWithUnresolvedRefs, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    // assertions
    assertThat(result).hasValueSatisfying(waitingToBeResolved -> assertThat(waitingToBeResolved.getProductDraft()).isEqualTo(productDraft));
    // test
    final Set<WaitingToBeResolvedProducts> waitingDrafts = unresolvedReferencesService.fetch(singleton(productDraft.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    // assertions
    assertThat(waitingDrafts).containsExactly(productDraftWithUnresolvedRefs);
    // test
    final Optional<WaitingToBeResolvedProducts> deletionResult = unresolvedReferencesService.delete(productDraft.getKey(), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
    // assertions
    assertThat(deletionResult).hasValueSatisfying(waitingToBeResolved -> assertThat(waitingToBeResolved.getProductDraft()).isEqualTo(productDraft));
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    assertThat(errorCallBackExceptions).isEmpty();
}
Also used : ProductDraft(io.sphere.sdk.products.ProductDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) 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