Search in sources :

Example 11 with CustomObjectSyncOptions

use of com.commercetools.sync.customobjects.CustomObjectSyncOptions 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 12 with CustomObjectSyncOptions

use of com.commercetools.sync.customobjects.CustomObjectSyncOptions 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 13 with CustomObjectSyncOptions

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

the class CustomObjectServiceImplIT method fetchMatchingCustomObjectsByCompositeIdentifiers_WithBadGateWayExceptionAlways_ShouldFail.

@Test
void fetchMatchingCustomObjectsByCompositeIdentifiers_WithBadGateWayExceptionAlways_ShouldFail() {
    // Mock sphere client to return BadGatewayException on any request.
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    when(spyClient.execute(any(CustomObjectQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
    final CustomObjectSyncOptions spyOptions = CustomObjectSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final CustomObjectService spyCustomObjectService = new CustomObjectServiceImpl(spyOptions);
    final Set<CustomObjectCompositeIdentifier> customObjectCompositeIdentifiers = new HashSet<>();
    customObjectCompositeIdentifiers.add(CustomObjectCompositeIdentifier.of(OLD_CUSTOM_OBJECT_KEY, OLD_CUSTOM_OBJECT_CONTAINER));
    // test and assert
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(spyCustomObjectService.fetchMatchingCustomObjects(customObjectCompositeIdentifiers)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
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) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) 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) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) SphereClient(io.sphere.sdk.client.SphereClient) 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) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) CustomObjectSyncOptionsBuilder(com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) CustomObjectITUtils.createCustomObject(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.createCustomObject) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Optional(java.util.Optional) CustomObjectServiceImpl(com.commercetools.sync.services.impl.CustomObjectServiceImpl) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) SphereClient(io.sphere.sdk.client.SphereClient) CustomObjectServiceImpl(com.commercetools.sync.services.impl.CustomObjectServiceImpl) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 14 with CustomObjectSyncOptions

use of com.commercetools.sync.customobjects.CustomObjectSyncOptions 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)

Example 15 with CustomObjectSyncOptions

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

the class BaseServiceImplTest method cacheKeysToIds_WithEmptySetOfKeys_ShouldNotMakeRequestAndReturnEmpty.

@Test
void cacheKeysToIds_WithEmptySetOfKeys_ShouldNotMakeRequestAndReturnEmpty() {
    // preparation
    CustomObjectSyncOptions customObjectSyncOptions = CustomObjectSyncOptionsBuilder.of(client).build();
    CustomObjectServiceImpl serviceImpl = new CustomObjectServiceImpl(customObjectSyncOptions);
    // test
    final Map<String, String> optional = serviceImpl.cacheKeysToIds(emptySet()).toCompletableFuture().join();
    // assertions
    assertThat(optional).isEmpty();
    verify(client, never()).execute(any());
}
Also used : CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

CustomObjectSyncOptions (com.commercetools.sync.customobjects.CustomObjectSyncOptions)15 Test (org.junit.jupiter.api.Test)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 CustomObjectQuery (io.sphere.sdk.customobjects.queries.CustomObjectQuery)10 CustomObjectCompositeIdentifier (com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier)9 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 CustomObjectSync (com.commercetools.sync.customobjects.CustomObjectSync)8 CustomObjectSyncOptionsBuilder (com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder)8 CustomObjectSyncStatistics (com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics)8 SphereClient (io.sphere.sdk.client.SphereClient)8 CustomObjectDraft (io.sphere.sdk.customobjects.CustomObjectDraft)8 List (java.util.List)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 CustomObjectITUtils.createCustomObject (com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.createCustomObject)7 CustomObjectITUtils.deleteCustomObject (com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteCustomObject)7 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)7 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)7 BadGatewayException (io.sphere.sdk.client.BadGatewayException)7 CompletableFutureUtils (io.sphere.sdk.utils.CompletableFutureUtils)7 ArrayList (java.util.ArrayList)7