Search in sources :

Example 16 with CustomObjectSyncStatistics

use of com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics 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

CustomObjectSyncStatistics (com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics)16 Test (org.junit.jupiter.api.Test)16 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 ArrayList (java.util.ArrayList)11 CustomObjectService (com.commercetools.sync.services.CustomObjectService)9 CustomObjectSync (com.commercetools.sync.customobjects.CustomObjectSync)7 CustomObjectSyncOptions (com.commercetools.sync.customobjects.CustomObjectSyncOptions)7 CustomObject (io.sphere.sdk.customobjects.CustomObject)7 HashSet (java.util.HashSet)7 CustomObjectCompositeIdentifier (com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 ConcurrentModificationException (io.sphere.sdk.client.ConcurrentModificationException)5 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)4 CustomObjectSyncOptionsBuilder (com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder)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 BadRequestException (io.sphere.sdk.client.BadRequestException)4