Search in sources :

Example 16 with ShoppingListService

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());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) SyncException(com.commercetools.sync.commons.exceptions.SyncException) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ShoppingListSyncOptions(com.commercetools.sync.shoppinglists.ShoppingListSyncOptions) UpdateAction(io.sphere.sdk.commands.UpdateAction) HashMap(java.util.HashMap) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ShoppingListQuery(io.sphere.sdk.shoppinglists.queries.ShoppingListQuery) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) ShoppingListSyncOptionsBuilder(com.commercetools.sync.shoppinglists.ShoppingListSyncOptionsBuilder) Collections.singletonList(java.util.Collections.singletonList) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) ArrayList(java.util.ArrayList) Collections.singleton(java.util.Collections.singleton) VerificationModeFactory.only(org.mockito.internal.verification.VerificationModeFactory.only) Assertions.as(org.assertj.core.api.Assertions.as) ShoppingListUpdateCommand(io.sphere.sdk.shoppinglists.commands.ShoppingListUpdateCommand) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) BadGatewayException(io.sphere.sdk.client.BadGatewayException) Collections.emptySet(java.util.Collections.emptySet) InternalServerErrorException(io.sphere.sdk.client.InternalServerErrorException) ChangeName(io.sphere.sdk.shoppinglists.commands.updateactions.ChangeName) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ShoppingListService(com.commercetools.sync.services.ShoppingListService) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) Mockito.never(org.mockito.Mockito.never) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Mockito.mock(org.mockito.Mockito.mock) ShoppingListService(com.commercetools.sync.services.ShoppingListService) Optional(java.util.Optional) HashMap(java.util.HashMap) SphereClient(io.sphere.sdk.client.SphereClient) ShoppingListSyncOptions(com.commercetools.sync.shoppinglists.ShoppingListSyncOptions) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) LocalizedString(io.sphere.sdk.models.LocalizedString) Test(org.junit.jupiter.api.Test)

Example 17 with ShoppingListService

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);
}
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 18 with ShoppingListService

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);
}
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 19 with ShoppingListService

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);
}
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 20 with ShoppingListService

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

Aggregations

ShoppingListService (com.commercetools.sync.services.ShoppingListService)20 Test (org.junit.jupiter.api.Test)20 ShoppingList (io.sphere.sdk.shoppinglists.ShoppingList)19 ShoppingListDraft (io.sphere.sdk.shoppinglists.ShoppingListDraft)18 CustomerService (com.commercetools.sync.services.CustomerService)12 TypeService (com.commercetools.sync.services.TypeService)12 ShoppingListSyncStatistics (com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics)12 ShoppingListSyncOptions (com.commercetools.sync.shoppinglists.ShoppingListSyncOptions)8 SyncException (com.commercetools.sync.commons.exceptions.SyncException)6 ShoppingListSyncOptionsBuilder (com.commercetools.sync.shoppinglists.ShoppingListSyncOptionsBuilder)6 SphereClient (io.sphere.sdk.client.SphereClient)6 LocalizedString (io.sphere.sdk.models.LocalizedString)6 ChangeName (io.sphere.sdk.shoppinglists.commands.updateactions.ChangeName)6 ShoppingListQuery (io.sphere.sdk.shoppinglists.queries.ShoppingListQuery)6 ArrayList (java.util.ArrayList)6 Collections.emptySet (java.util.Collections.emptySet)6 Collections.singleton (java.util.Collections.singleton)6 Collections.singletonList (java.util.Collections.singletonList)6 List (java.util.List)6 Map (java.util.Map)6