Search in sources :

Example 11 with ShoppingListService

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

the class ShoppingListServiceImplTest method updateShoppingList_WithMockSuccessfulCtpResponse_ShouldCallShoppingListUpdateCommand.

@Test
void updateShoppingList_WithMockSuccessfulCtpResponse_ShouldCallShoppingListUpdateCommand() {
    // preparation
    final ShoppingList shoppingList = mock(ShoppingList.class);
    final ShoppingListSyncOptions options = ShoppingListSyncOptionsBuilder.of(mock(SphereClient.class)).build();
    when(options.getCtpClient().execute(any())).thenReturn(completedFuture(shoppingList));
    final ShoppingListService shoppingListService = new ShoppingListServiceImpl(options);
    final List<UpdateAction<ShoppingList>> updateActions = singletonList(ChangeName.of(LocalizedString.ofEnglish("new_name")));
    // test
    final CompletionStage<ShoppingList> result = shoppingListService.updateShoppingList(shoppingList, updateActions);
    // assertions
    assertThat(result).isCompletedWithValue(shoppingList);
    verify(options.getCtpClient()).execute(eq(ShoppingListUpdateCommand.of(shoppingList, updateActions)));
}
Also used : ShoppingListService(com.commercetools.sync.services.ShoppingListService) UpdateAction(io.sphere.sdk.commands.UpdateAction) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ShoppingListSyncOptions(com.commercetools.sync.shoppinglists.ShoppingListSyncOptions) Test(org.junit.jupiter.api.Test)

Example 12 with ShoppingListService

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

the class ShoppingListServiceImplIT method createShoppingList_WithValidShoppingList_ShouldCreateShoppingList.

@Test
void createShoppingList_WithValidShoppingList_ShouldCreateShoppingList() {
    // preparation
    ProductType productType = createProductType(PRODUCT_TYPE_RESOURCE_PATH, CTP_TARGET_CLIENT);
    final ProductDraft productDraft = ProductDraftBuilder.of(ResourceIdentifier.ofKey(productType.getKey()), LocalizedString.ofEnglish("newProduct"), LocalizedString.ofEnglish("foo"), ProductVariantDraftBuilder.of().key("foo-new").sku("sku-new").build()).key("newProduct").build();
    executeBlocking(CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(productDraft)));
    LineItemDraft lineItemDraft = LineItemDraftBuilder.ofSku("sku-new", Long.valueOf(1)).build();
    TextLineItemDraft textLineItemDraft = TextLineItemDraftBuilder.of(LocalizedString.ofEnglish("text"), 1L).build();
    final ShoppingListDraftDsl newShoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("new_name")).key("new_key").plusLineItems(lineItemDraft).plusTextLineItems(textLineItemDraft).build();
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    final ShoppingListSyncOptions options = ShoppingListSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, actions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception);
    }).build();
    ShoppingListService spyShoppingListService = new ShoppingListServiceImpl(options);
    // test
    final Optional<ShoppingList> createdShoppingList = spyShoppingListService.createShoppingList(newShoppingListDraft).toCompletableFuture().join();
    final Optional<ShoppingList> queriedOptional = CTP_TARGET_CLIENT.execute(ShoppingListQuery.of().withPredicates(shoppingListQueryModel -> shoppingListQueryModel.key().is("new_key"))).toCompletableFuture().join().head();
    assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdShoppingList).hasValueSatisfying(created -> {
        assertThat(created.getKey()).isEqualTo(queried.getKey());
        assertThat(created.getName()).isEqualTo(queried.getName());
        assertThat(created.getLineItems()).hasSize(1);
        assertThat(created.getTextLineItems()).hasSize(1);
    }));
}
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) ShoppingListDraftDsl(io.sphere.sdk.shoppinglists.ShoppingListDraftDsl) ShoppingListService(com.commercetools.sync.services.ShoppingListService) ProductDraft(io.sphere.sdk.products.ProductDraft) TextLineItemDraft(io.sphere.sdk.shoppinglists.TextLineItemDraft) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ShoppingListITUtils.createShoppingList(com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.createShoppingList) SphereClient(io.sphere.sdk.client.SphereClient) ProductType(io.sphere.sdk.producttypes.ProductType) ProductTypeITUtils.createProductType(com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType) ShoppingListSyncOptions(com.commercetools.sync.shoppinglists.ShoppingListSyncOptions) TextLineItemDraft(io.sphere.sdk.shoppinglists.TextLineItemDraft) LineItemDraft(io.sphere.sdk.shoppinglists.LineItemDraft) ShoppingListServiceImpl(com.commercetools.sync.services.impl.ShoppingListServiceImpl) Test(org.junit.jupiter.api.Test)

Example 13 with ShoppingListService

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

the class ShoppingListServiceImplIT method cacheKeysToIds_WithCachedKeys_ShouldReturnCachedKeysWithoutRequest.

@Test
void cacheKeysToIds_WithCachedKeys_ShouldReturnCachedKeysWithoutRequest() {
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    final ShoppingListSyncOptions shoppingListSyncOptions = ShoppingListSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final ShoppingListService shoppingListService = new ShoppingListServiceImpl(shoppingListSyncOptions);
    Map<String, String> cache = shoppingListService.cacheKeysToIds(singleton(shoppingList.getKey())).toCompletableFuture().join();
    assertThat(cache).hasSize(1);
    cache = shoppingListService.cacheKeysToIds(singleton(shoppingList.getKey())).toCompletableFuture().join();
    assertThat(cache).hasSize(1);
    verify(spyClient, times(1)).execute(any());
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
}
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) ShoppingListService(com.commercetools.sync.services.ShoppingListService) SphereClient(io.sphere.sdk.client.SphereClient) ShoppingListSyncOptions(com.commercetools.sync.shoppinglists.ShoppingListSyncOptions) LocalizedString(io.sphere.sdk.models.LocalizedString) ShoppingListServiceImpl(com.commercetools.sync.services.impl.ShoppingListServiceImpl) Test(org.junit.jupiter.api.Test)

Example 14 with ShoppingListService

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

the class ShoppingListServiceImplTest method updateShoppingList_WithMockUnsuccessfulCtpResponse_ShouldCompleteExceptionally.

@Test
void updateShoppingList_WithMockUnsuccessfulCtpResponse_ShouldCompleteExceptionally() {
    // preparation
    final ShoppingList shoppingList = mock(ShoppingList.class);
    final ShoppingListSyncOptions shoppingListSyncOptions = ShoppingListSyncOptionsBuilder.of(mock(SphereClient.class)).build();
    when(shoppingListSyncOptions.getCtpClient().execute(any())).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new InternalServerErrorException()));
    final ShoppingListService shoppingListService = new ShoppingListServiceImpl(shoppingListSyncOptions);
    final List<UpdateAction<ShoppingList>> updateActions = singletonList(ChangeName.of(LocalizedString.ofEnglish("new_name")));
    // test
    final CompletionStage<ShoppingList> result = shoppingListService.updateShoppingList(shoppingList, updateActions);
    // assertions
    assertThat(result).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(InternalServerErrorException.class);
}
Also used : ShoppingListService(com.commercetools.sync.services.ShoppingListService) UpdateAction(io.sphere.sdk.commands.UpdateAction) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ShoppingListSyncOptions(com.commercetools.sync.shoppinglists.ShoppingListSyncOptions) InternalServerErrorException(io.sphere.sdk.client.InternalServerErrorException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 15 with ShoppingListService

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

the class ShoppingListServiceImplTest method createShoppingList_WithNullShoppingListKey_ShouldNotCreateShoppingList.

@Test
void createShoppingList_WithNullShoppingListKey_ShouldNotCreateShoppingList() {
    // preparation
    final ShoppingListDraft mockShoppingListDraft = mock(ShoppingListDraft.class);
    final Map<String, Throwable> errors = new HashMap<>();
    when(mockShoppingListDraft.getKey()).thenReturn(null);
    final ShoppingListSyncOptions shoppingListSyncOptions = ShoppingListSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> errors.put(exception.getMessage(), exception)).build();
    final ShoppingListService shoppingListService = new ShoppingListServiceImpl(shoppingListSyncOptions);
    // test
    final CompletionStage<Optional<ShoppingList>> result = shoppingListService.createShoppingList(mockShoppingListDraft);
    // assertions
    assertThat(result).isCompletedWithValue(Optional.empty());
    assertThat(errors.keySet()).containsExactly("Failed to create draft with key: 'null'. Reason: Draft key is blank!");
    verify(shoppingListSyncOptions.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)

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