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);
}
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);
}
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);
}
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());
}
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()));
}
Aggregations