Search in sources :

Example 1 with ShoppingListService

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

the class ShoppingListServiceImplIT method setup.

/**
 * Deletes shopping list and products from the target CTP projects, then it populates the project
 * with test data.
 */
@BeforeEach
void setup() {
    deleteShoppingListTestData(CTP_TARGET_CLIENT);
    errorCallBackMessages = new ArrayList<>();
    errorCallBackExceptions = new ArrayList<>();
    shoppingList = createShoppingList(CTP_TARGET_CLIENT, "name", "key");
    final ShoppingListSyncOptions options = ShoppingListSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception);
    }).build();
    shoppingListService = new ShoppingListServiceImpl(options);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) TextLineItemDraftBuilder(io.sphere.sdk.shoppinglists.TextLineItemDraftBuilder) PRODUCT_TYPE_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ShoppingListSyncOptions(com.commercetools.sync.shoppinglists.ShoppingListSyncOptions) ProductType(io.sphere.sdk.producttypes.ProductType) TextLineItemDraft(io.sphere.sdk.shoppinglists.TextLineItemDraft) ShoppingListITUtils.deleteShoppingLists(com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.deleteShoppingLists) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) Mockito.spy(org.mockito.Mockito.spy) ShoppingListQuery(io.sphere.sdk.shoppinglists.queries.ShoppingListQuery) ShoppingListSyncOptionsBuilder(com.commercetools.sync.shoppinglists.ShoppingListSyncOptionsBuilder) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) ShoppingListITUtils.createShoppingList(com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.createShoppingList) Collections.singleton(java.util.Collections.singleton) ShoppingListDraftDsl(io.sphere.sdk.shoppinglists.ShoppingListDraftDsl) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) LineItemDraft(io.sphere.sdk.shoppinglists.LineItemDraft) ShoppingListDraftBuilder(io.sphere.sdk.shoppinglists.ShoppingListDraftBuilder) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) Collections.emptySet(java.util.Collections.emptySet) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) LineItemDraftBuilder(io.sphere.sdk.shoppinglists.LineItemDraftBuilder) ShoppingListITUtils.deleteShoppingListTestData(com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.deleteShoppingListTestData) ChangeName(io.sphere.sdk.shoppinglists.commands.updateactions.ChangeName) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) ShoppingListService(com.commercetools.sync.services.ShoppingListService) ShoppingListServiceImpl(com.commercetools.sync.services.impl.ShoppingListServiceImpl) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) List(java.util.List) Optional(java.util.Optional) ProductCreateCommand(io.sphere.sdk.products.commands.ProductCreateCommand) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) ShoppingListSyncOptions(com.commercetools.sync.shoppinglists.ShoppingListSyncOptions) ShoppingListServiceImpl(com.commercetools.sync.services.impl.ShoppingListServiceImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with ShoppingListService

use of com.commercetools.sync.services.ShoppingListService 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 3 with ShoppingListService

use of com.commercetools.sync.services.ShoppingListService 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 ShoppingListService

use of com.commercetools.sync.services.ShoppingListService 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 ShoppingListService

use of com.commercetools.sync.services.ShoppingListService 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

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