Search in sources :

Example 16 with ShoppingListSyncStatistics

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

the class ShoppingListSyncTest method sync_WithUnchangedShoppingListDraftAndUpdatedLineItemDraft_ShouldIncrementUpdated.

@Test
void sync_WithUnchangedShoppingListDraftAndUpdatedLineItemDraft_ShouldIncrementUpdated() {
    // preparation
    final ShoppingListService mockShoppingListService = mock(ShoppingListService.class);
    final ShoppingList mockShoppingList = mock(ShoppingList.class);
    final LineItem mockLineItem = mock(LineItem.class);
    when(mockShoppingList.getKey()).thenReturn("shoppingListKey");
    when(mockShoppingList.getName()).thenReturn(LocalizedString.ofEnglish("shoppingListName"));
    final ProductVariant mockProductVariant = mock(ProductVariant.class);
    when(mockProductVariant.getSku()).thenReturn("dummy-sku");
    when(mockLineItem.getVariant()).thenReturn(mockProductVariant);
    when(mockLineItem.getQuantity()).thenReturn(10L);
    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 List<LineItemDraft> lineItemDrafts = singletonList(LineItemDraftBuilder.ofSku("dummy-sku", 5L).build());
    final ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("shoppingListName")).key("shoppingListKey").lineItems(lineItemDrafts).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) LineItem(io.sphere.sdk.shoppinglists.LineItem) TextLineItem(io.sphere.sdk.shoppinglists.TextLineItem) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) ProductVariant(io.sphere.sdk.products.ProductVariant) LineItemDraft(io.sphere.sdk.shoppinglists.LineItemDraft) TextLineItemDraft(io.sphere.sdk.shoppinglists.TextLineItemDraft) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 17 with ShoppingListSyncStatistics

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

the class ShoppingListSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    final ShoppingListService shoppingListService = mock(ShoppingListService.class);
    when(shoppingListService.fetchMatchingShoppingListsByKeys(singleton("shopping-list-key"))).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final ShoppingListSync shoppingListSync = new ShoppingListSync(syncOptions, shoppingListService, mock(CustomerService.class), mock(TypeService.class));
    ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("shopping-list-name")).key("shopping-list-key").customer(ResourceIdentifier.ofKey("customer-key")).custom(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap())).build();
    // test
    final ShoppingListSyncStatistics statistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(statistics).hasValues(1, 0, 0, 1);
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to fetch existing shopping lists with keys: '[shopping-list-key]'.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
}
Also used : ShoppingListSyncStatistics(com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics) ShoppingListService(com.commercetools.sync.services.ShoppingListService) CustomerService(com.commercetools.sync.services.CustomerService) CompletionException(java.util.concurrent.CompletionException) 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 18 with ShoppingListSyncStatistics

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

the class ShoppingListSyncTest method sync_WithConcurrentModificationException_ShouldRetryToUpdateNewCustomerWithSuccess.

@Test
void sync_WithConcurrentModificationException_ShouldRetryToUpdateNewCustomerWithSuccess() {
    // preparation
    final ShoppingListService mockShoppingListService = mock(ShoppingListService.class);
    final ShoppingList mockShoppingList = mock(ShoppingList.class);
    when(mockShoppingList.getName()).thenReturn(LocalizedString.ofEnglish("shoppingListName"));
    when(mockShoppingList.getKey()).thenReturn("shoppingListKey");
    when(mockShoppingList.getDescription()).thenReturn(LocalizedString.ofEnglish("shoppingListDesc"));
    when(mockShoppingList.getSlug()).thenReturn(LocalizedString.ofEnglish("shoppingListSlug"));
    when(mockShoppingList.getAnonymousId()).thenReturn("shoppingListAnonymousId");
    when(mockShoppingList.getDeleteDaysAfterLastModification()).thenReturn(360);
    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.of(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("shoppingListName")).key("shoppingListKey").description(LocalizedString.ofEnglish("newShoppingListDesc")).slug(mockShoppingList.getSlug()).anonymousId(mockShoppingList.getAnonymousId()).deleteDaysAfterLastModification(mockShoppingList.getDeleteDaysAfterLastModification()).build();
    // test
    final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(shoppingListSyncStatistics).hasValues(1, 0, 1, 0);
}
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) SphereException(io.sphere.sdk.models.SphereException) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 19 with ShoppingListSyncStatistics

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

the class ShoppingListSyncTest method sync_FailedOnCreation_ShouldCallBeforeCreateCallbackAndIncrementFailed.

@Test
void sync_FailedOnCreation_ShouldCallBeforeCreateCallbackAndIncrementFailed() {
    // preparation
    final ShoppingListService mockShoppingListService = mock(ShoppingListService.class);
    final ShoppingList mockShoppingList = mock(ShoppingList.class);
    when(mockShoppingListService.fetchMatchingShoppingListsByKeys(singleton("shoppingListKey"))).thenReturn(completedFuture(new HashSet<>(singletonList(mockShoppingList))));
    // simulate an error during create, service will return an empty optional.
    when(mockShoppingListService.createShoppingList(any())).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("NAME")).key("shoppingListKey").build();
    // test
    final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(shoppingListSyncStatistics).hasValues(1, 0, 0, 1);
    verify(spySyncOptions).applyBeforeCreateCallback(shoppingListDraft);
    verify(spySyncOptions, never()).applyBeforeUpdateCallback(any(), any(), any());
}
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) HashSet(java.util.HashSet) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 20 with ShoppingListSyncStatistics

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

the class ShoppingListSyncTest method sync_WithNullKeyDraft_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithNullKeyDraft_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    // preparation
    ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("shopping-list-name")).build();
    ShoppingListSync shoppingListSync = new ShoppingListSync(syncOptions);
    // test
    ShoppingListSyncStatistics statistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
    AssertionsForStatistics.assertThat(statistics).hasValues(1, 0, 0, 1);
    // assertions
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo(format(SHOPPING_LIST_DRAFT_KEY_NOT_SET, shoppingListDraft.getName()));
}
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