Search in sources :

Example 11 with CustomObjectSyncStatistics

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

the class CustomObjectSyncTest method sync_UpdateWithConcurrentModificationExceptionAndRetryWithFetchException_ShouldIncrementFailed.

@Test
void sync_UpdateWithConcurrentModificationExceptionAndRetryWithFetchException_ShouldIncrementFailed() {
    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("someContainer");
    when(existingCustomObject.getKey()).thenReturn("someKey");
    when(existingCustomObject.getValue()).thenReturn(JsonNodeFactory.instance.numberNode(2020));
    final Set<CustomObject<JsonNode>> existingCustomObjectSet = new HashSet<CustomObject<JsonNode>>();
    existingCustomObjectSet.add(existingCustomObject);
    final CustomObject<JsonNode> updatedCustomObject = mock(CustomObject.class);
    when(updatedCustomObject.getContainer()).thenReturn("someContainer");
    when(updatedCustomObject.getKey()).thenReturn("someKey");
    when(updatedCustomObject.getValue()).thenReturn(newCustomObjectDraft.getValue());
    final CustomObjectService customObjectService = mock(CustomObjectService.class);
    when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(existingCustomObjectSet));
    when(customObjectService.upsertCustomObject(any())).thenReturn(supplyAsync(() -> {
        throw new ConcurrentModificationException();
    }));
    when(customObjectService.fetchCustomObject(any(CustomObjectCompositeIdentifier.class))).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    // test
    CustomObjectSyncStatistics syncStatistics = new CustomObjectSync(spyCustomObjectSyncOptions, customObjectService).sync(singletonList(newCustomObjectDraft)).toCompletableFuture().join();
    // assertion
    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));
    assertThat(exceptions).hasSize(1);
    assertThat(errorMessages).hasSize(1).singleElement().isEqualTo(format("Failed to update custom object with key: '%s'. Reason: %s", CustomObjectCompositeIdentifier.of(newCustomObjectDraft).toString(), "Failed to fetch from CTP while retrying after concurrency modification."));
    verify(customObjectService).fetchCustomObject(any(CustomObjectCompositeIdentifier.class));
    verify(customObjectService).upsertCustomObject(any());
    verify(customObjectService).fetchMatchingCustomObjects(any());
}
Also used : ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) CustomObject(io.sphere.sdk.customobjects.CustomObject) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) SphereException(io.sphere.sdk.models.SphereException) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 12 with CustomObjectSyncStatistics

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

the class CustomObjectSyncTest method sync_WithDifferentKeysAndSameContainers_ShouldCreateSuccessfully.

@Test
void sync_WithDifferentKeysAndSameContainers_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("someContainer");
    when(existingCustomObject.getKey()).thenReturn("otherKey");
    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)

Example 13 with CustomObjectSyncStatistics

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

the class CustomObjectSyncTest method sync_WithSameIdentifiersAndIdenticalValues_ShouldProcessedAndNotUpdated.

@Test
void sync_WithSameIdentifiersAndIdenticalValues_ShouldProcessedAndNotUpdated() {
    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(newCustomObjectDraft.getValue());
    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(0), () -> 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 14 with CustomObjectSyncStatistics

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

the class CustomObjectSyncTest method sync_UpdateWithSphereExceptionAndRetryWithFetchException_ShouldIncrementFailed.

@Test
void sync_UpdateWithSphereExceptionAndRetryWithFetchException_ShouldIncrementFailed() {
    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("someContainer");
    when(existingCustomObject.getKey()).thenReturn("someKey");
    when(existingCustomObject.getValue()).thenReturn(JsonNodeFactory.instance.numberNode(2020));
    final Set<CustomObject<JsonNode>> existingCustomObjectSet = new HashSet<CustomObject<JsonNode>>();
    existingCustomObjectSet.add(existingCustomObject);
    final CustomObject<JsonNode> updatedCustomObject = mock(CustomObject.class);
    when(updatedCustomObject.getContainer()).thenReturn("someContainer");
    when(updatedCustomObject.getKey()).thenReturn("someKey");
    when(updatedCustomObject.getValue()).thenReturn(newCustomObjectDraft.getValue());
    final CustomObjectService customObjectService = mock(CustomObjectService.class);
    when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(existingCustomObjectSet));
    when(customObjectService.upsertCustomObject(any())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    when(customObjectService.fetchCustomObject(any(CustomObjectCompositeIdentifier.class))).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    // test
    CustomObjectSyncStatistics syncStatistics = new CustomObjectSync(spyCustomObjectSyncOptions, customObjectService).sync(singletonList(newCustomObjectDraft)).toCompletableFuture().join();
    // assertion
    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));
    assertThat(exceptions).hasSize(1);
    assertThat(errorMessages).hasSize(1).singleElement().isEqualTo(format("Failed to update custom object with key: '%s'. Reason: %s", CustomObjectCompositeIdentifier.of(newCustomObjectDraft).toString(), exceptions.get(0).getMessage()));
    verify(customObjectService).upsertCustomObject(any());
    verify(customObjectService).fetchMatchingCustomObjects(any());
}
Also used : CustomObject(io.sphere.sdk.customobjects.CustomObject) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) SphereException(io.sphere.sdk.models.SphereException) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 15 with CustomObjectSyncStatistics

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

the class CustomObjectSyncTest method sync_WithDifferentIdentifiers_ShouldCreateSuccessfully.

@Test
void sync_WithDifferentIdentifiers_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("otherKey");
    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