Search in sources :

Example 6 with CustomObjectSyncStatistics

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

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

the class CustomObjectSyncTest method sync_WitEmptyValidDrafts_ShouldFailed.

@Test
void sync_WitEmptyValidDrafts_ShouldFailed() {
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final CustomObjectSyncOptions spyCustomObjectSyncOptions = initCustomObjectSyncOptions(errorMessages, exceptions);
    final CustomObjectService customObjectService = mock(CustomObjectService.class);
    when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(emptySet()));
    when(customObjectService.upsertCustomObject(any())).thenReturn(completedFuture(Optional.empty()));
    // test
    CustomObjectSyncStatistics syncStatistics = new CustomObjectSync(spyCustomObjectSyncOptions, customObjectService).sync(singletonList(null)).toCompletableFuture().join();
    // assertion
    assertThat(exceptions).hasSize(1);
    assertThat(errorMessages).hasSize(1);
    assertAll(() -> assertThat(syncStatistics.getProcessed().get()).isEqualTo(1), () -> assertThat(syncStatistics.getCreated().get()).isEqualTo(0), () -> assertThat(syncStatistics.getUpdated().get()).isEqualTo(0), () -> assertThat(syncStatistics.getFailed().get()).isEqualTo(1));
}
Also used : CustomObjectService(com.commercetools.sync.services.CustomObjectService) ArrayList(java.util.ArrayList) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) Test(org.junit.jupiter.api.Test)

Example 8 with CustomObjectSyncStatistics

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

the class CustomObjectSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final CustomObjectSyncOptions spyCustomObjectSyncOptions = initCustomObjectSyncOptions(errorMessages, exceptions);
    final CustomObjectService mockCustomObjectService = mock(CustomObjectService.class);
    when(mockCustomObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final CustomObjectSync customObjectSync = new CustomObjectSync(spyCustomObjectSyncOptions, mockCustomObjectService);
    // test
    final CustomObjectSyncStatistics customObjectSyncStatistics = customObjectSync.sync(singletonList(newCustomObjectDraft)).toCompletableFuture().join();
    // assertion
    assertThat(errorMessages).hasSize(1).singleElement().asString().isEqualTo("Failed to fetch existing custom objects with keys: " + "'[someContainer|someKey]'.");
    assertThat(exceptions).hasSize(1).singleElement().isInstanceOfSatisfying(Throwable.class, throwable -> {
        assertThat(throwable).isExactlyInstanceOf(CompletionException.class);
        assertThat(throwable).hasCauseExactlyInstanceOf(SphereException.class);
    });
    assertThat(customObjectSyncStatistics).hasValues(1, 0, 0, 1);
}
Also used : CustomObjectService(com.commercetools.sync.services.CustomObjectService) ArrayList(java.util.ArrayList) SphereException(io.sphere.sdk.models.SphereException) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) Test(org.junit.jupiter.api.Test)

Example 9 with CustomObjectSyncStatistics

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

the class CustomObjectSyncTest method sync_WithSameIdentifiersAndDifferentValues_ShouldUpdateSuccessfully.

@Test
void sync_WithSameIdentifiersAndDifferentValues_ShouldUpdateSuccessfully() {
    final CustomObjectSyncOptions spyCustomObjectSyncOptions = initCustomObjectSyncOptions(emptyList(), emptyList());
    final CustomObject<JsonNode> existingCustomObject = mock(CustomObject.class);
    when(existingCustomObject.getContainer()).thenReturn("someContainer");
    when(existingCustomObject.getKey()).thenReturn("someKey");
    when(existingCustomObject.getValue()).thenReturn(JsonNodeFactory.instance.numberNode(2020));
    final CustomObject<JsonNode> updatedCustomObject = mock(CustomObject.class);
    when(updatedCustomObject.getContainer()).thenReturn("someContainer");
    when(updatedCustomObject.getKey()).thenReturn("someKey");
    when(updatedCustomObject.getValue()).thenReturn(newCustomObjectDraft.getValue());
    final Set<CustomObject<JsonNode>> existingCustomObjectSet = new HashSet<CustomObject<JsonNode>>();
    existingCustomObjectSet.add(existingCustomObject);
    final CustomObjectService customObjectService = mock(CustomObjectService.class);
    when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(existingCustomObjectSet));
    when(customObjectService.upsertCustomObject(any())).thenReturn(completedFuture(Optional.of(updatedCustomObject)));
    // test
    CustomObjectSyncStatistics syncStatistics = new CustomObjectSync(spyCustomObjectSyncOptions, customObjectService).sync(singletonList(newCustomObjectDraft)).toCompletableFuture().join();
    // assertion
    assertAll(() -> assertThat(syncStatistics.getProcessed().get()).isEqualTo(1), () -> assertThat(syncStatistics.getUpdated().get()).isEqualTo(1), () -> assertThat(syncStatistics.getCreated().get()).isEqualTo(0), () -> assertThat(syncStatistics.getFailed().get()).isEqualTo(0));
}
Also used : CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObject(io.sphere.sdk.customobjects.CustomObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 10 with CustomObjectSyncStatistics

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

the class CustomObjectSyncTest method sync_WithSameKeysAndDifferentContainers_ShouldCreateSuccessfully.

@Test
void sync_WithSameKeysAndDifferentContainers_ShouldCreateSuccessfully() {
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final CustomObjectSyncOptions spyCustomObjectSyncOptions = initCustomObjectSyncOptions(errorMessages, exceptions);
    final CustomObject<JsonNode> existingCustomObject = mock(CustomObject.class);
    when(existingCustomObject.getContainer()).thenReturn("otherContainer");
    when(existingCustomObject.getKey()).thenReturn("someKey");
    when(existingCustomObject.getValue()).thenReturn(JsonNodeFactory.instance.numberNode(2020));
    final CustomObject<JsonNode> updatedCustomObject = mock(CustomObject.class);
    when(updatedCustomObject.getContainer()).thenReturn("someContainer");
    when(updatedCustomObject.getKey()).thenReturn("someKey");
    when(updatedCustomObject.getValue()).thenReturn(newCustomObjectDraft.getValue());
    final Set<CustomObject<JsonNode>> existingCustomObjectSet = new HashSet<CustomObject<JsonNode>>();
    existingCustomObjectSet.add(existingCustomObject);
    final CustomObjectService customObjectService = mock(CustomObjectService.class);
    when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(existingCustomObjectSet));
    when(customObjectService.upsertCustomObject(any())).thenReturn(completedFuture(Optional.of(updatedCustomObject)));
    // test
    CustomObjectSyncStatistics syncStatistics = new CustomObjectSync(spyCustomObjectSyncOptions, customObjectService).sync(singletonList(newCustomObjectDraft)).toCompletableFuture().join();
    // assertion
    assertThat(exceptions).hasSize(0);
    assertThat(errorMessages).hasSize(0);
    assertAll(() -> assertThat(syncStatistics.getProcessed().get()).isEqualTo(1), () -> assertThat(syncStatistics.getCreated().get()).isEqualTo(1), () -> assertThat(syncStatistics.getUpdated().get()).isEqualTo(0), () -> assertThat(syncStatistics.getFailed().get()).isEqualTo(0));
    verify(spyCustomObjectSyncOptions).applyBeforeCreateCallback(newCustomObjectDraft);
}
Also used : CustomObject(io.sphere.sdk.customobjects.CustomObject) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) HashSet(java.util.HashSet) 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