Search in sources :

Example 1 with ShoppingListSyncStatistics

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

the class ShoppingListSyncTest method sync_WithUnchangedShoppingListDraftAndUpdatedTextLineItemDraft_ShouldIncrementUpdated.

@Test
void sync_WithUnchangedShoppingListDraftAndUpdatedTextLineItemDraft_ShouldIncrementUpdated() {
    // preparation
    final ShoppingList mockShoppingList = mock(ShoppingList.class);
    when(mockShoppingList.getKey()).thenReturn("shoppingListKey");
    when(mockShoppingList.getName()).thenReturn(LocalizedString.ofEnglish("shoppingListName"));
    final TextLineItem mockTextLineItem = mock(TextLineItem.class);
    when(mockTextLineItem.getName()).thenReturn(LocalizedString.ofEnglish("textLineItemName"));
    when(mockTextLineItem.getQuantity()).thenReturn(10L);
    final ShoppingListService mockShoppingListService = mock(ShoppingListService.class);
    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<TextLineItemDraft> textLineItemDrafts = singletonList(TextLineItemDraftBuilder.of(LocalizedString.ofEnglish("textLineItemName"), 5L).build());
    final ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("shoppingListName")).key("shoppingListKey").textLineItems(textLineItemDrafts).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) TextLineItemDraft(io.sphere.sdk.shoppinglists.TextLineItemDraft) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) TextLineItem(io.sphere.sdk.shoppinglists.TextLineItem) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 2 with ShoppingListSyncStatistics

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

the class ShoppingListSyncTest method sync_WithEmptyKeyDraft_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithEmptyKeyDraft_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    // preparation
    ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("shopping-list-name")).key("").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)

Example 3 with ShoppingListSyncStatistics

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

the class ShoppingListSyncTest method sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallbackAndIncrementCreated.

@Test
void sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallbackAndIncrementCreated() {
    // preparation
    final ShoppingListService mockShoppingListService = mock(ShoppingListService.class);
    final ShoppingList mockShoppingList = mock(ShoppingList.class);
    when(mockShoppingListService.fetchMatchingShoppingListsByKeys(singleton("shoppingListKey"))).thenReturn(completedFuture(new HashSet<>(singletonList(mockShoppingList))));
    when(mockShoppingListService.createShoppingList(any())).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("NAME")).key("shoppingListKey").build();
    // test
    final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(shoppingListSyncStatistics).hasValues(1, 1, 0, 0);
    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 4 with ShoppingListSyncStatistics

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

the class ShoppingListSyncTest method sync_WithoutUpdateActions_ShouldNotIncrementUpdated.

@Test
void sync_WithoutUpdateActions_ShouldNotIncrementUpdated() {
    // preparation
    final ShoppingListService mockShoppingListService = mock(ShoppingListService.class);
    final ShoppingList mockShoppingList = mock(ShoppingList.class);
    when(mockShoppingList.getKey()).thenReturn("shoppingListKey");
    when(mockShoppingList.getName()).thenReturn(LocalizedString.ofEnglish("shoppingListName"));
    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)));
    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(mockShoppingList.getDescription()).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, 0, 0);
    verify(spySyncOptions).applyBeforeUpdateCallback(emptyList(), shoppingListDraft, mockShoppingList);
    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 5 with ShoppingListSyncStatistics

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

the class ShoppingListSyncTest method sync_WithBadRequestException_ShouldFailToUpdateAndIncreaseFailedCounter.

@Test
void sync_WithBadRequestException_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)));
    when(mockShoppingListService.updateShoppingList(any(), anyList())).thenReturn(exceptionallyCompletedFuture(new BadRequestException("Invalid request")));
    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("Invalid request");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasRootCauseExactlyInstanceOf(BadRequestException.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) BadRequestException(io.sphere.sdk.client.BadRequestException) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) SyncException(com.commercetools.sync.commons.exceptions.SyncException) TypeService(com.commercetools.sync.services.TypeService) 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