Search in sources :

Example 6 with ShoppingListSyncStatistics

use of com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics in project commercetools-sync-java by commercetools.

the class ShoppingListSyncTest method sync_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate.

@Test
void sync_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate() {
    // preparation
    final ShoppingListService mockShoppingListService = mock(ShoppingListService.class);
    final ShoppingList mockShoppingList = mock(ShoppingList.class);
    when(mockShoppingList.getKey()).thenReturn("shoppingListKey");
    when(mockShoppingListService.fetchMatchingShoppingListsByKeys(anySet())).thenReturn(completedFuture(singleton(mockShoppingList)));
    when(mockShoppingListService.updateShoppingList(any(), anyList())).thenReturn(exceptionallyCompletedFuture(new SphereException(new ConcurrentModificationException()))).thenReturn(completedFuture(mockShoppingList));
    when(mockShoppingListService.fetchShoppingList("shoppingListKey")).thenReturn(exceptionallyCompletedFuture(new SphereException()));
    final ShoppingListSyncOptions spySyncOptions = spy(syncOptions);
    final ShoppingListSync shoppingListSync = new ShoppingListSync(spySyncOptions, mockShoppingListService, mock(CustomerService.class), mock(TypeService.class));
    final ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("shoppingListName")).key("shoppingListKey").build();
    // test
    final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(shoppingListSyncStatistics).hasValues(1, 0, 0, 1);
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to fetch from CTP while retrying after concurrency modification.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
}
Also used : ShoppingListSyncStatistics(com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics) ShoppingListService(com.commercetools.sync.services.ShoppingListService) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) CustomerService(com.commercetools.sync.services.CustomerService) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) SyncException(com.commercetools.sync.commons.exceptions.SyncException) SphereException(io.sphere.sdk.models.SphereException) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 7 with ShoppingListSyncStatistics

use of com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics in project commercetools-sync-java by commercetools.

the class ShoppingListSyncTest method sync_WithExceptionOnReferenceResolution_ShouldFailToUpdateAndIncreaseFailedCounter.

@Test
void sync_WithExceptionOnReferenceResolution_ShouldFailToUpdateAndIncreaseFailedCounter() {
    // preparation
    final ShoppingListService mockShoppingListService = mock(ShoppingListService.class);
    final ShoppingList mockShoppingList = mock(ShoppingList.class);
    when(mockShoppingList.getKey()).thenReturn("shoppingListKey");
    when(mockShoppingListService.fetchMatchingShoppingListsByKeys(anySet())).thenReturn(completedFuture(singleton(mockShoppingList)));
    final ShoppingListSyncOptions spySyncOptions = spy(syncOptions);
    final TypeService typeService = mock(TypeService.class);
    when(typeService.fetchCachedTypeId(anyString())).thenReturn(CompletableFutureUtils.failed(new SphereException("CTP error on fetch")));
    final ShoppingListSync shoppingListSync = new ShoppingListSync(spySyncOptions, mockShoppingListService, mock(CustomerService.class), typeService);
    final ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("shoppingListName")).key("shoppingListKey").build();
    // test
    final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(shoppingListSyncStatistics).hasValues(1, 0, 0, 1);
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to process the ShoppingListDraft with key:'shoppingListKey'");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class);
}
Also used : ShoppingListSyncStatistics(com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics) ShoppingListService(com.commercetools.sync.services.ShoppingListService) CustomerService(com.commercetools.sync.services.CustomerService) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) SphereException(io.sphere.sdk.models.SphereException) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 8 with ShoppingListSyncStatistics

use of com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics in project commercetools-sync-java by commercetools.

the class ShoppingListSyncTest method sync_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate.

@Test
void sync_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate() {
    // preparation
    final ShoppingListService mockShoppingListService = mock(ShoppingListService.class);
    final ShoppingList mockShoppingList = mock(ShoppingList.class);
    when(mockShoppingList.getKey()).thenReturn("shoppingListKey");
    when(mockShoppingListService.fetchMatchingShoppingListsByKeys(anySet())).thenReturn(completedFuture(singleton(mockShoppingList)));
    when(mockShoppingListService.updateShoppingList(any(), anyList())).thenReturn(exceptionallyCompletedFuture(new SphereException(new ConcurrentModificationException()))).thenReturn(completedFuture(mockShoppingList));
    when(mockShoppingListService.fetchShoppingList("shoppingListKey")).thenReturn(completedFuture(Optional.empty()));
    final ShoppingListSyncOptions spySyncOptions = spy(syncOptions);
    final ShoppingListSync shoppingListSync = new ShoppingListSync(spySyncOptions, mockShoppingListService, mock(CustomerService.class), mock(TypeService.class));
    final ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("shoppingListName")).key("shoppingListKey").build();
    // test
    final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(shoppingListSyncStatistics).hasValues(1, 0, 0, 1);
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Not found when attempting to fetch while retrying after concurrency modification.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasNoCause();
}
Also used : ShoppingListSyncStatistics(com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics) ShoppingListService(com.commercetools.sync.services.ShoppingListService) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) CustomerService(com.commercetools.sync.services.CustomerService) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) SyncException(com.commercetools.sync.commons.exceptions.SyncException) SphereException(io.sphere.sdk.models.SphereException) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 9 with ShoppingListSyncStatistics

use of com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics in project commercetools-sync-java by commercetools.

the class ShoppingListSyncTest method sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback.

@Test
void sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback() {
    // preparation
    final ShoppingListService mockShoppingListService = mock(ShoppingListService.class);
    final ShoppingList mockShoppingList = mock(ShoppingList.class);
    when(mockShoppingList.getKey()).thenReturn("shoppingListKey");
    when(mockShoppingListService.fetchMatchingShoppingListsByKeys(anySet())).thenReturn(completedFuture(singleton(mockShoppingList)));
    when(mockShoppingListService.updateShoppingList(any(), anyList())).thenReturn(completedFuture(mockShoppingList));
    final ShoppingListSyncOptions spySyncOptions = spy(syncOptions);
    final ShoppingListSync shoppingListSync = new ShoppingListSync(spySyncOptions, mockShoppingListService, mock(CustomerService.class), mock(TypeService.class));
    final ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("NAME")).key("shoppingListKey").build();
    // test
    final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(shoppingListSyncStatistics).hasValues(1, 0, 1, 0);
    verify(spySyncOptions).applyBeforeUpdateCallback(any(), any(), any());
    verify(spySyncOptions, never()).applyBeforeCreateCallback(shoppingListDraft);
}
Also used : ShoppingListSyncStatistics(com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics) ShoppingListService(com.commercetools.sync.services.ShoppingListService) CustomerService(com.commercetools.sync.services.CustomerService) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 10 with ShoppingListSyncStatistics

use of com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics in project commercetools-sync-java by commercetools.

the class ShoppingListSyncIT method sync_WithModifiedShoppingList_ShouldUpdateShoppingList.

@Test
void sync_WithModifiedShoppingList_ShouldUpdateShoppingList() {
    final ShoppingListDraft modifiedShoppingListDraft = prepareUpdatedDraft();
    final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(singletonList(modifiedShoppingListDraft)).toCompletableFuture().join();
    assertThat(errorMessages).isEmpty();
    assertThat(warningMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertUpdateActions();
    assertThat(shoppingListSyncStatistics).hasValues(1, 0, 1, 0);
    assertThat(shoppingListSyncStatistics.getReportMessage()).isEqualTo("Summary: 1 shopping lists were processed in total " + "(0 created, 1 updated and 0 failed to sync).");
    assertShoppingListUpdatedCorrectly(modifiedShoppingListDraft);
}
Also used : ShoppingListSyncStatistics(com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) Test(org.junit.jupiter.api.Test)

Aggregations

ShoppingListSyncStatistics (com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics)21 Test (org.junit.jupiter.api.Test)21 ShoppingListDraft (io.sphere.sdk.shoppinglists.ShoppingListDraft)19 CustomerService (com.commercetools.sync.services.CustomerService)13 ShoppingListService (com.commercetools.sync.services.ShoppingListService)13 TypeService (com.commercetools.sync.services.TypeService)13 ShoppingList (io.sphere.sdk.shoppinglists.ShoppingList)13 SphereException (io.sphere.sdk.models.SphereException)6 SyncException (com.commercetools.sync.commons.exceptions.SyncException)3 ConcurrentModificationException (io.sphere.sdk.client.ConcurrentModificationException)3 ShoppingListITUtils.createShoppingList (com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.createShoppingList)2 TextLineItem (io.sphere.sdk.shoppinglists.TextLineItem)2 TextLineItemDraft (io.sphere.sdk.shoppinglists.TextLineItemDraft)2 CompletionException (java.util.concurrent.CompletionException)2 AssertionsForStatistics (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics)1 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)1 CaffeineReferenceIdToKeyCacheImpl (com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl)1 ReferenceIdToKeyCache (com.commercetools.sync.commons.utils.ReferenceIdToKeyCache)1 CustomerITUtils.createSampleCustomerJaneDoe (com.commercetools.sync.integration.commons.utils.CustomerITUtils.createSampleCustomerJaneDoe)1 ShoppingListITUtils.createSampleShoppingListCarrotCake (com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.createSampleShoppingListCarrotCake)1