Search in sources :

Example 1 with ShoppingListServiceImpl

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

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

use of com.commercetools.sync.services.impl.ShoppingListServiceImpl 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)

Aggregations

ProductTypeITUtils.createProductType (com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.createProductType)3 ShoppingListITUtils.createShoppingList (com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.createShoppingList)3 ShoppingListITUtils.deleteShoppingListTestData (com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.deleteShoppingListTestData)3 ShoppingListITUtils.deleteShoppingLists (com.commercetools.sync.integration.commons.utils.ShoppingListITUtils.deleteShoppingLists)3 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)3 PRODUCT_TYPE_RESOURCE_PATH (com.commercetools.sync.products.ProductSyncMockUtils.PRODUCT_TYPE_RESOURCE_PATH)3 ShoppingListService (com.commercetools.sync.services.ShoppingListService)3 ShoppingListServiceImpl (com.commercetools.sync.services.impl.ShoppingListServiceImpl)3 ShoppingListSyncOptions (com.commercetools.sync.shoppinglists.ShoppingListSyncOptions)3 ShoppingListSyncOptionsBuilder (com.commercetools.sync.shoppinglists.ShoppingListSyncOptionsBuilder)3 CompletionStageUtil.executeBlocking (com.commercetools.tests.utils.CompletionStageUtil.executeBlocking)3 SphereClient (io.sphere.sdk.client.SphereClient)3 LocalizedString (io.sphere.sdk.models.LocalizedString)3 ResourceIdentifier (io.sphere.sdk.models.ResourceIdentifier)3 ProductDraft (io.sphere.sdk.products.ProductDraft)3 ProductDraftBuilder (io.sphere.sdk.products.ProductDraftBuilder)3 ProductVariantDraftBuilder (io.sphere.sdk.products.ProductVariantDraftBuilder)3 ProductCreateCommand (io.sphere.sdk.products.commands.ProductCreateCommand)3 ProductType (io.sphere.sdk.producttypes.ProductType)3 LineItemDraft (io.sphere.sdk.shoppinglists.LineItemDraft)3