use of com.commercetools.sync.services.ShoppingListService in project commercetools-sync-java by commercetools.
the class ShoppingListServiceImplTest method createShoppingList_WithEmptyShoppingListKey_ShouldHaveEmptyOptionalAsAResult.
@Test
void createShoppingList_WithEmptyShoppingListKey_ShouldHaveEmptyOptionalAsAResult() {
// preparation
final SphereClient sphereClient = mock(SphereClient.class);
final ShoppingListDraft shoppingListDraft = mock(ShoppingListDraft.class);
final Map<String, Throwable> errors = new HashMap<>();
when(shoppingListDraft.getKey()).thenReturn("");
final ShoppingListSyncOptions options = ShoppingListSyncOptionsBuilder.of(sphereClient).errorCallback((exception, oldResource, newResource, actions) -> errors.put(exception.getMessage(), exception)).build();
final ShoppingListService shoppingListService = new ShoppingListServiceImpl(options);
// test
final CompletionStage<Optional<ShoppingList>> result = shoppingListService.createShoppingList(shoppingListDraft);
// assertion
assertThat(result).isCompletedWithValue(Optional.empty());
assertThat(errors.keySet()).containsExactly("Failed to create draft with key: ''. Reason: Draft key is blank!");
verify(options.getCtpClient(), times(0)).execute(any());
}
use of com.commercetools.sync.services.ShoppingListService 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.services.ShoppingListService 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.services.ShoppingListService 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.services.ShoppingListService 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());
}
Aggregations