Search in sources :

Example 1 with UnresolvedReferencesServiceImpl

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

the class StateSyncIT method sync_WithNotExistentStates_ShouldResolveStateLater.

@Test
void sync_WithNotExistentStates_ShouldResolveStateLater() {
    final StateDraft stateCDraft = createStateDraft(keyC);
    final State stateC = createStateInSource(stateCDraft);
    final StateDraft stateBDraft = createStateDraft(keyB, stateC);
    final State stateB = createStateInSource(stateBDraft);
    StateDraft[] draftsWithReplacesKeys = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, asList(stateB, stateC)).join().toArray(new StateDraft[2]);
    final StateDraft stateADraft = createStateDraftReferencingStateDrafts(keyA, draftsWithReplacesKeys);
    final List<StateDraft> stateDrafts = asList(stateADraft);
    final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(1).build();
    final StateSync stateSync = new StateSync(stateSyncOptions);
    // test
    final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
    assertThat(stateSyncStatistics).hasValues(1, 0, 0, 0, 1);
    final UnresolvedReferencesService<WaitingToBeResolvedTransitions> unresolvedTransitionsService = new UnresolvedReferencesServiceImpl<>(stateSyncOptions);
    Set<WaitingToBeResolvedTransitions> result = unresolvedTransitionsService.fetch(Collections.singleton(keyA), CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY, WaitingToBeResolvedTransitions.class).toCompletableFuture().join();
    Assertions.assertThat(result.size()).isEqualTo(1);
    WaitingToBeResolvedTransitions waitingToBeResolvedTransitions = result.iterator().next();
    Assertions.assertThat(waitingToBeResolvedTransitions.getMissingTransitionStateKeys().containsAll(asList(keyB, keyC))).isTrue();
    Assertions.assertThat(waitingToBeResolvedTransitions.getStateDraft().getKey()).isEqualTo(keyA);
}
Also used : StateSync(com.commercetools.sync.states.StateSync) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) StateDraft(io.sphere.sdk.states.StateDraft) WaitingToBeResolvedTransitions(com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions) State(io.sphere.sdk.states.State) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Test(org.junit.jupiter.api.Test)

Example 2 with UnresolvedReferencesServiceImpl

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

the class StateSyncIT method sync_WithAllExistentStates_ShouldResolveAllStates.

@Test
void sync_WithAllExistentStates_ShouldResolveAllStates() {
    final StateDraft stateCDraft = createStateDraft(keyC);
    final State stateC = createStateInSource(stateCDraft);
    final StateDraft stateBDraft = createStateDraft(keyB, stateC);
    final State stateB = createStateInSource(stateBDraft);
    final StateDraft stateADraft = createStateDraft(keyA, stateB, stateC);
    final State stateA = createStateInSource(stateADraft);
    final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(3).build();
    final StateSync stateSync = new StateSync(stateSyncOptions);
    final List<StateDraft> stateDrafts = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, Arrays.asList(stateA, stateB, stateC)).join();
    // test
    final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
    assertThat(stateSyncStatistics).hasValues(3, 3, 0, 0, 0);
    Assertions.assertThat(stateSyncStatistics.getReportMessage()).isEqualTo("Summary: 3 state(s) were processed in total " + "(3 created, 0 updated, 0 failed to sync and 0 state(s) with missing transition(s)).");
    final UnresolvedReferencesService<WaitingToBeResolvedTransitions> unresolvedTransitionsService = new UnresolvedReferencesServiceImpl<>(stateSyncOptions);
    Set<WaitingToBeResolvedTransitions> result = unresolvedTransitionsService.fetch(Collections.singleton(keyA), CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY, WaitingToBeResolvedTransitions.class).toCompletableFuture().join();
    Assertions.assertThat(result.size()).isEqualTo(0);
}
Also used : StateSync(com.commercetools.sync.states.StateSync) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) StateDraft(io.sphere.sdk.states.StateDraft) WaitingToBeResolvedTransitions(com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions) State(io.sphere.sdk.states.State) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Test(org.junit.jupiter.api.Test)

Example 3 with UnresolvedReferencesServiceImpl

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

the class ProductSyncWithReferencedProductsIT method sync_withProductReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct.

@Test
void sync_withProductReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct() {
    // preparation
    final AttributeDraft productReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), product.getKey()));
    final HashSet<Reference<Product>> references = new HashSet<>();
    references.add(Reference.of(Product.referenceTypeId(), "nonExistingKey"));
    references.add(Reference.of(Product.referenceTypeId(), product2.getKey()));
    final AttributeDraft productReferenceSetAttribute = AttributeDraft.of("product-reference-set", references);
    final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productReferenceAttribute, productReferenceSetAttribute).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) Reference(io.sphere.sdk.models.Reference) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) ProductSync(com.commercetools.sync.products.ProductSync) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with UnresolvedReferencesServiceImpl

use of com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl 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 5 with UnresolvedReferencesServiceImpl

use of com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl 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)

Aggregations

UnresolvedReferencesServiceImpl (com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl)9 Test (org.junit.jupiter.api.Test)9 WaitingToBeResolvedProducts (com.commercetools.sync.commons.models.WaitingToBeResolvedProducts)7 ProductDraft (io.sphere.sdk.products.ProductDraft)7 ProductSync (com.commercetools.sync.products.ProductSync)6 ProductSyncStatistics (com.commercetools.sync.products.helpers.ProductSyncStatistics)6 AttributeDraft (io.sphere.sdk.products.attributes.AttributeDraft)6 ProductVariantDraft (io.sphere.sdk.products.ProductVariantDraft)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 WaitingToBeResolvedTransitions (com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions)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 CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY (com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY)2 StateSync (com.commercetools.sync.states.StateSync)2 StateSyncOptions (com.commercetools.sync.states.StateSyncOptions)2 StateSyncStatistics (com.commercetools.sync.states.helpers.StateSyncStatistics)2 CustomObject (io.sphere.sdk.customobjects.CustomObject)2