Search in sources :

Example 1 with WaitingToBeResolvedTransitions

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

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

use of com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions 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 4 with WaitingToBeResolvedTransitions

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

the class StateSync method resolveNowReadyReferences.

@Nonnull
private CompletionStage<Void> resolveNowReadyReferences(@Nonnull final Map<String, String> keyToIdCache) {
    // We delete anyways the keys from the statistics before we attempt resolution, because even if
    // resolution fails
    // the states 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<StateDraft> readyToSync = new HashSet<>();
    final Set<WaitingToBeResolvedTransitions> waitingDraftsToBeUpdated = new HashSet<>();
    return unresolvedReferencesService.fetch(referencingDraftKeys, CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY, WaitingToBeResolvedTransitions.class).handle(ImmutablePair::new).thenCompose(fetchResponse -> {
        final Set<WaitingToBeResolvedTransitions> waitingDrafts = fetchResponse.getKey();
        final Throwable fetchException = fetchResponse.getValue();
        if (fetchException != null) {
            final String errorMessage = format(UNRESOLVED_TRANSITIONS_STORE_FETCH_FAILED, referencingDraftKeys);
            handleError(new SyncException(errorMessage, fetchException), null, null, null, referencingDraftKeys.size());
            return CompletableFuture.completedFuture(null);
        }
        waitingDrafts.forEach(waitingDraft -> {
            final Set<String> missingTransitionStateKeys = waitingDraft.getMissingTransitionStateKeys();
            missingTransitionStateKeys.removeAll(readyToResolve);
            if (missingTransitionStateKeys.isEmpty()) {
                readyToSync.add(waitingDraft.getStateDraft());
            } else {
                waitingDraftsToBeUpdated.add(waitingDraft);
            }
        });
        return updateWaitingDrafts(waitingDraftsToBeUpdated).thenCompose(aVoid -> syncBatch(readyToSync, keyToIdCache)).thenCompose(aVoid -> removeFromWaiting(readyToSync));
    });
}
Also used : StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) Reference(io.sphere.sdk.models.Reference) SyncException(com.commercetools.sync.commons.exceptions.SyncException) BaseSync(com.commercetools.sync.commons.BaseSync) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) StateBatchValidator(com.commercetools.sync.states.helpers.StateBatchValidator) UpdateAction(io.sphere.sdk.commands.UpdateAction) CompletableFuture(java.util.concurrent.CompletableFuture) HashSet(java.util.HashSet) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) Collectors.toMap(java.util.stream.Collectors.toMap) StateSyncUtils.buildActions(com.commercetools.sync.states.utils.StateSyncUtils.buildActions) StateReferenceResolver(com.commercetools.sync.states.helpers.StateReferenceResolver) Map(java.util.Map) SyncUtils.batchElements(com.commercetools.sync.commons.utils.SyncUtils.batchElements) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) Optional.ofNullable(java.util.Optional.ofNullable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) StateService(com.commercetools.sync.services.StateService) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) State(io.sphere.sdk.states.State) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Objects(java.util.Objects) 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) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) StateDraft(io.sphere.sdk.states.StateDraft) Collections(java.util.Collections) StateDraft(io.sphere.sdk.states.StateDraft) WaitingToBeResolvedTransitions(com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions) Objects(java.util.Objects) SyncException(com.commercetools.sync.commons.exceptions.SyncException) HashSet(java.util.HashSet) Nonnull(javax.annotation.Nonnull)

Aggregations

WaitingToBeResolvedTransitions (com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions)4 UnresolvedReferencesServiceImpl (com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl)4 State (io.sphere.sdk.states.State)4 StateSyncStatistics (com.commercetools.sync.states.helpers.StateSyncStatistics)3 StateDraft (io.sphere.sdk.states.StateDraft)3 UnresolvedReferencesService (com.commercetools.sync.services.UnresolvedReferencesService)2 CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY (com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY)2 StateSync (com.commercetools.sync.states.StateSync)2 StateSyncOptions (com.commercetools.sync.states.StateSyncOptions)2 Reference (io.sphere.sdk.models.Reference)2 String.format (java.lang.String.format)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 Test (org.junit.jupiter.api.Test)2 CategorySyncOptionsBuilder (com.commercetools.sync.categories.CategorySyncOptionsBuilder)1 BaseSync (com.commercetools.sync.commons.BaseSync)1 CleanupUnresolvedReferenceCustomObjects (com.commercetools.sync.commons.CleanupUnresolvedReferenceCustomObjects)1