Search in sources :

Example 6 with ShoppingListService

use of com.commercetools.sync.services.ShoppingListService in project commercetools-sync-java by commercetools.

the class ShoppingListSyncTest method sync_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate.

@Test
void sync_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate() {
    // 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 SphereException(new ConcurrentModificationException()))).thenReturn(completedFuture(mockShoppingList));
    when(mockShoppingListService.fetchShoppingList("shoppingListKey")).thenReturn(exceptionallyCompletedFuture(new SphereException()));
    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("Failed to fetch from CTP while retrying after concurrency modification.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
}
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) SyncException(com.commercetools.sync.commons.exceptions.SyncException) SphereException(io.sphere.sdk.models.SphereException) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 7 with ShoppingListService

use of com.commercetools.sync.services.ShoppingListService in project commercetools-sync-java by commercetools.

the class ShoppingListSyncTest method sync_WithExceptionOnReferenceResolution_ShouldFailToUpdateAndIncreaseFailedCounter.

@Test
void sync_WithExceptionOnReferenceResolution_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)));
    final ShoppingListSyncOptions spySyncOptions = spy(syncOptions);
    final TypeService typeService = mock(TypeService.class);
    when(typeService.fetchCachedTypeId(anyString())).thenReturn(CompletableFutureUtils.failed(new SphereException("CTP error on fetch")));
    final ShoppingListSync shoppingListSync = new ShoppingListSync(spySyncOptions, mockShoppingListService, mock(CustomerService.class), typeService);
    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("Failed to process the ShoppingListDraft with key:'shoppingListKey'");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.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) 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 8 with ShoppingListService

use of com.commercetools.sync.services.ShoppingListService in project commercetools-sync-java by commercetools.

the class ShoppingListSyncTest method sync_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate.

@Test
void sync_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate() {
    // 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 SphereException(new ConcurrentModificationException()))).thenReturn(completedFuture(mockShoppingList));
    when(mockShoppingListService.fetchShoppingList("shoppingListKey")).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("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("Not found when attempting to fetch while retrying after concurrency modification.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasNoCause();
}
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) SyncException(com.commercetools.sync.commons.exceptions.SyncException) SphereException(io.sphere.sdk.models.SphereException) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 9 with ShoppingListService

use of com.commercetools.sync.services.ShoppingListService in project commercetools-sync-java by commercetools.

the class ShoppingListSyncTest method sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback.

@Test
void sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback() {
    // 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(completedFuture(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, 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) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 10 with ShoppingListService

use of com.commercetools.sync.services.ShoppingListService in project commercetools-sync-java by commercetools.

the class ShoppingListServiceImplTest method createShoppingList_WithUnsuccessfulMockCtpResponse_ShouldNotCreateShoppingList.

@Test
void createShoppingList_WithUnsuccessfulMockCtpResponse_ShouldNotCreateShoppingList() {
    // preparation
    final ShoppingListDraft shoppingListDraft = mock(ShoppingListDraft.class);
    final Map<String, Throwable> errors = new HashMap<>();
    when(shoppingListDraft.getKey()).thenReturn("key");
    final ShoppingListSyncOptions options = ShoppingListSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> errors.put(exception.getMessage(), exception)).build();
    final ShoppingListService shoppingListService = new ShoppingListServiceImpl(options);
    when(options.getCtpClient().execute(any())).thenReturn(CompletableFutureUtils.failed(new InternalServerErrorException()));
    // test
    final CompletionStage<Optional<ShoppingList>> result = shoppingListService.createShoppingList(shoppingListDraft);
    // assertions
    assertThat(result).isCompletedWithValue(Optional.empty());
    assertThat(errors.keySet()).hasSize(1).singleElement(as(STRING)).contains("Failed to create draft with key: 'key'.");
    assertThat(errors.values()).hasSize(1).singleElement().satisfies(exception -> {
        assertThat(exception).isExactlyInstanceOf(SyncException.class);
        assertThat(exception.getCause()).isExactlyInstanceOf(InternalServerErrorException.class);
    });
}
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) Optional(java.util.Optional) HashMap(java.util.HashMap) LocalizedString(io.sphere.sdk.models.LocalizedString) ShoppingListService(com.commercetools.sync.services.ShoppingListService) SphereClient(io.sphere.sdk.client.SphereClient) ShoppingListSyncOptions(com.commercetools.sync.shoppinglists.ShoppingListSyncOptions) InternalServerErrorException(io.sphere.sdk.client.InternalServerErrorException) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) 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