Search in sources :

Example 6 with CustomObjectSync

use of com.commercetools.sync.customobjects.CustomObjectSync in project commercetools-sync-java by commercetools.

the class CustomObjectSyncIT method sync_withConcurrentModificationExceptionAndUnexpectedDelete_shouldFailToReFetchAndUpdate.

@Test
void sync_withConcurrentModificationExceptionAndUnexpectedDelete_shouldFailToReFetchAndUpdate() {
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    final CustomObjectUpsertCommand customObjectUpsertCommand = any(CustomObjectUpsertCommand.class);
    when(spyClient.execute(customObjectUpsertCommand)).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new ConcurrentModificationException())).thenCallRealMethod();
    final CustomObjectQuery customObjectQuery = any(CustomObjectQuery.class);
    when(spyClient.execute(customObjectQuery)).thenCallRealMethod().thenReturn(CompletableFuture.completedFuture(PagedQueryResult.empty()));
    final ObjectNode newCustomObjectValue = JsonNodeFactory.instance.objectNode().put("name", "value2");
    List<String> errorCallBackMessages = new ArrayList<>();
    List<Throwable> errorCallBackExceptions = new ArrayList<>();
    final CustomObjectSyncOptions spyOptions = CustomObjectSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final CustomObjectSync customObjectSync = new CustomObjectSync(spyOptions);
    final CustomObjectDraft<JsonNode> customObjectDraft = CustomObjectDraft.ofUnversionedUpsert("container1", "key1", newCustomObjectValue);
    final CustomObjectSyncStatistics customObjectSyncStatistics = customObjectSync.sync(Collections.singletonList(customObjectDraft)).toCompletableFuture().join();
    assertThat(customObjectSyncStatistics).hasValues(1, 0, 0, 1);
    Assertions.assertThat(errorCallBackMessages).hasSize(1);
    Assertions.assertThat(errorCallBackExceptions).hasSize(1);
    Assertions.assertThat(errorCallBackMessages.get(0)).contains(format("Failed to update custom object with key: '%s'. Reason: Not found when attempting to fetch while" + " retrying after concurrency modification.", CustomObjectCompositeIdentifier.of(customObjectDraft)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CustomObjectITUtils.deleteCustomObject(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteCustomObject) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) BadRequestException(io.sphere.sdk.client.BadRequestException) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) SphereClient(io.sphere.sdk.client.SphereClient) Assertions(org.assertj.core.api.Assertions) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CustomObjectDraft(io.sphere.sdk.customobjects.CustomObjectDraft) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) CustomObjectSyncOptionsBuilder(com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder) CustomObjectUpsertCommand(io.sphere.sdk.customobjects.commands.CustomObjectUpsertCommand) Test(org.junit.jupiter.api.Test) CustomObjectSync(com.commercetools.sync.customobjects.CustomObjectSync) List(java.util.List) CustomObjectITUtils.createCustomObject(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.createCustomObject) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Collections(java.util.Collections) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CustomObjectUpsertCommand(io.sphere.sdk.customobjects.commands.CustomObjectUpsertCommand) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectSync(com.commercetools.sync.customobjects.CustomObjectSync) CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) SphereClient(io.sphere.sdk.client.SphereClient) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) Test(org.junit.jupiter.api.Test)

Example 7 with CustomObjectSync

use of com.commercetools.sync.customobjects.CustomObjectSync in project commercetools-project-sync by commercetools.

the class CustomObjectSyncer method of.

@Nonnull
public static CustomObjectSyncer of(@Nonnull final SphereClient sourceClient, @Nonnull final SphereClient targetClient, @Nonnull final Clock clock, @Nullable final String runnerName, final boolean isSyncProjectSyncCustomObjects) {
    final QuadConsumer<SyncException, Optional<CustomObjectDraft<JsonNode>>, Optional<CustomObject<JsonNode>>, List<UpdateAction<CustomObject<JsonNode>>>> logErrorCallback = (exception, newResourceDraft, oldResource, updateActions) -> {
        final String resourceIdentifier = getCustomObjectResourceIdentifier(oldResource);
        updateActions = updateActions == null ? Collections.emptyList() : updateActions;
        logErrorCallback(LOGGER, "customObject", exception, resourceIdentifier, updateActions);
    };
    final TriConsumer<SyncException, Optional<CustomObjectDraft<JsonNode>>, Optional<CustomObject<JsonNode>>> logWarningCallback = (exception, newResourceDraft, oldResource) -> {
        final String resourceIdentifier = getCustomObjectResourceIdentifier(oldResource);
        logWarningCallback(LOGGER, "customObject", exception, resourceIdentifier);
    };
    final CustomObjectSyncOptions syncOptions = CustomObjectSyncOptionsBuilder.of(targetClient).errorCallback(logErrorCallback).warningCallback(logWarningCallback).build();
    final CustomObjectSync customObjectSyncer = new CustomObjectSync(syncOptions);
    final CustomObjectService customObjectService = new CustomObjectServiceImpl(targetClient);
    return new CustomObjectSyncer(customObjectSyncer, sourceClient, targetClient, customObjectService, clock, runnerName, isSyncProjectSyncCustomObjects);
}
Also used : CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) CustomObject(io.sphere.sdk.customobjects.CustomObject) LoggerFactory(org.slf4j.LoggerFactory) UpdateAction(io.sphere.sdk.commands.UpdateAction) CompletableFuture(java.util.concurrent.CompletableFuture) QuadConsumer(com.commercetools.sync.commons.utils.QuadConsumer) SyncUtils.logWarningCallback(com.commercetools.project.sync.util.SyncUtils.logWarningCallback) SphereClient(io.sphere.sdk.client.SphereClient) TriConsumer(com.commercetools.sync.commons.utils.TriConsumer) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) SyncUtils.logErrorCallback(com.commercetools.project.sync.util.SyncUtils.logErrorCallback) SyncModuleOption(com.commercetools.project.sync.SyncModuleOption) Logger(org.slf4j.Logger) CustomObjectDraft(io.sphere.sdk.customobjects.CustomObjectDraft) IDENTIFIER_NOT_PRESENT(com.commercetools.project.sync.util.SyncUtils.IDENTIFIER_NOT_PRESENT) CustomObjectService(com.commercetools.project.sync.service.CustomObjectService) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) Collectors(java.util.stream.Collectors) CustomObjectSyncOptionsBuilder(com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder) CustomObjectSync(com.commercetools.sync.customobjects.CustomObjectSync) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) Syncer(com.commercetools.project.sync.Syncer) Clock(java.time.Clock) Optional(java.util.Optional) CustomObjectServiceImpl(com.commercetools.project.sync.service.impl.CustomObjectServiceImpl) Collections(java.util.Collections) SyncUtils(com.commercetools.project.sync.util.SyncUtils) CustomObject(io.sphere.sdk.customobjects.CustomObject) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CustomObjectSync(com.commercetools.sync.customobjects.CustomObjectSync) CustomObjectService(com.commercetools.project.sync.service.CustomObjectService) CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) CustomObjectServiceImpl(com.commercetools.project.sync.service.impl.CustomObjectServiceImpl) List(java.util.List) Nonnull(javax.annotation.Nonnull)

Example 8 with CustomObjectSync

use of com.commercetools.sync.customobjects.CustomObjectSync in project commercetools-sync-java by commercetools.

the class CustomObjectSyncIT method sync_withNewCustomObjectAndBadRequest_shouldNotCreateButHandleError.

@Test
void sync_withNewCustomObjectAndBadRequest_shouldNotCreateButHandleError() {
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    final CustomObjectUpsertCommand upsertCommand = any(CustomObjectUpsertCommand.class);
    when(spyClient.execute(upsertCommand)).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadRequestException("bad request"))).thenCallRealMethod();
    final ObjectNode newCustomObjectValue = JsonNodeFactory.instance.objectNode().put("name", "value2");
    final CustomObjectDraft<JsonNode> newCustomObjectDraft = CustomObjectDraft.ofUnversionedUpsert("container2", "key2", newCustomObjectValue);
    List<String> errorCallBackMessages = new ArrayList<>();
    List<Throwable> errorCallBackExceptions = new ArrayList<>();
    final CustomObjectSyncOptions spyOptions = CustomObjectSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final CustomObjectSync customObjectSync = new CustomObjectSync(spyOptions);
    final CustomObjectSyncStatistics customObjectSyncStatistics = customObjectSync.sync(Collections.singletonList(newCustomObjectDraft)).toCompletableFuture().join();
    assertThat(customObjectSyncStatistics).hasValues(1, 0, 0, 1);
    Assertions.assertThat(errorCallBackMessages).hasSize(1);
    Assertions.assertThat(errorCallBackExceptions).hasSize(1);
    Assertions.assertThat(errorCallBackExceptions.get(0)).isExactlyInstanceOf(CompletionException.class);
    Assertions.assertThat(errorCallBackExceptions.get(0).getCause()).isExactlyInstanceOf(BadRequestException.class);
    Assertions.assertThat(errorCallBackMessages.get(0)).contains(format("Failed to create custom object with key: '%s'.", CustomObjectCompositeIdentifier.of(newCustomObjectDraft)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CustomObjectITUtils.deleteCustomObject(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteCustomObject) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) BadRequestException(io.sphere.sdk.client.BadRequestException) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) SphereClient(io.sphere.sdk.client.SphereClient) Assertions(org.assertj.core.api.Assertions) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CustomObjectDraft(io.sphere.sdk.customobjects.CustomObjectDraft) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) CustomObjectSyncOptionsBuilder(com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder) CustomObjectUpsertCommand(io.sphere.sdk.customobjects.commands.CustomObjectUpsertCommand) Test(org.junit.jupiter.api.Test) CustomObjectSync(com.commercetools.sync.customobjects.CustomObjectSync) List(java.util.List) CustomObjectITUtils.createCustomObject(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.createCustomObject) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Collections(java.util.Collections) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CustomObjectUpsertCommand(io.sphere.sdk.customobjects.commands.CustomObjectUpsertCommand) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectSync(com.commercetools.sync.customobjects.CustomObjectSync) CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) SphereClient(io.sphere.sdk.client.SphereClient) BadRequestException(io.sphere.sdk.client.BadRequestException) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) Test(org.junit.jupiter.api.Test)

Aggregations

CustomObjectSync (com.commercetools.sync.customobjects.CustomObjectSync)8 CustomObjectSyncOptions (com.commercetools.sync.customobjects.CustomObjectSyncOptions)8 CustomObjectSyncStatistics (com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 Test (org.junit.jupiter.api.Test)6 CustomObjectSyncOptionsBuilder (com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder)5 CustomObjectCompositeIdentifier (com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier)5 SphereClient (io.sphere.sdk.client.SphereClient)5 CustomObjectDraft (io.sphere.sdk.customobjects.CustomObjectDraft)5 CustomObjectQuery (io.sphere.sdk.customobjects.queries.CustomObjectQuery)5 Collections (java.util.Collections)5 List (java.util.List)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)4 CustomObjectITUtils.createCustomObject (com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.createCustomObject)4 CustomObjectITUtils.deleteCustomObject (com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteCustomObject)4 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)4 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)4 BadGatewayException (io.sphere.sdk.client.BadGatewayException)4