Search in sources :

Example 11 with CustomerService

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

the class CustomerSyncTest method sync_WithNonExistingTypeReference_ShouldTriggerErrorCallbackAndReturnProperStats.

@Test
void sync_WithNonExistingTypeReference_ShouldTriggerErrorCallbackAndReturnProperStats() {
    // preparation
    final TypeService mockTypeService = mock(TypeService.class);
    when(mockTypeService.fetchCachedTypeId(anyString())).thenReturn(completedFuture(empty()));
    when(mockTypeService.cacheKeysToIds(anySet())).thenReturn(completedFuture(emptyMap()));
    final CustomerService mockCustomerService = mock(CustomerService.class);
    when(mockCustomerService.fetchMatchingCustomersByKeys(singleton("customerKey"))).thenReturn(completedFuture(new HashSet<>(singletonList(mock(Customer.class)))));
    final CustomerSync customerSync = new CustomerSync(syncOptions, mockCustomerService, mockTypeService, mock(CustomerGroupService.class));
    final CustomerDraft customerDraft = CustomerDraftBuilder.of("email", "pass").key("customerKey").custom(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap())).build();
    // test
    final CustomerSyncStatistics customerSyncStatistics = customerSync.sync(singletonList(customerDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(customerSyncStatistics).hasValues(1, 0, 0, 1);
    final String expectedExceptionMessage = format(FAILED_TO_RESOLVE_CUSTOM_TYPE, customerDraft.getKey());
    final String expectedMessageWithCause = format("%s Reason: %s", expectedExceptionMessage, format(TYPE_DOES_NOT_EXIST, "typeKey"));
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains(expectedMessageWithCause);
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(ReferenceResolutionException.class);
}
Also used : CustomerService(com.commercetools.sync.services.CustomerService) CompletionException(java.util.concurrent.CompletionException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CustomerSyncStatistics(com.commercetools.sync.customers.helpers.CustomerSyncStatistics) TypeService(com.commercetools.sync.services.TypeService) HashSet(java.util.HashSet) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) Test(org.junit.jupiter.api.Test)

Example 12 with CustomerService

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

the class CustomerSyncTest method sync_WithoutUpdateActions_ShouldNotIncrementUpdated.

@Test
void sync_WithoutUpdateActions_ShouldNotIncrementUpdated() {
    // preparation
    final CustomerService mockCustomerService = mock(CustomerService.class);
    final Customer mockCustomer = mock(Customer.class);
    when(mockCustomer.getKey()).thenReturn("customerKey");
    when(mockCustomer.getEmail()).thenReturn("email");
    when(mockCustomerService.fetchMatchingCustomersByKeys(anySet())).thenReturn(completedFuture(singleton(mockCustomer)));
    final CustomerSyncOptions spyCustomerSyncOptions = spy(syncOptions);
    final CustomerSync customerSync = new CustomerSync(spyCustomerSyncOptions, mockCustomerService, mock(TypeService.class), mock(CustomerGroupService.class));
    final CustomerDraft customerDraft = CustomerDraftBuilder.of("email", "pass").key("customerKey").build();
    // test
    final CustomerSyncStatistics customerSyncStatistics = customerSync.sync(singletonList(customerDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(customerSyncStatistics).hasValues(1, 0, 0, 0);
    verify(spyCustomerSyncOptions).applyBeforeUpdateCallback(emptyList(), customerDraft, mockCustomer);
    verify(spyCustomerSyncOptions, never()).applyBeforeCreateCallback(customerDraft);
}
Also used : CustomerService(com.commercetools.sync.services.CustomerService) Customer(io.sphere.sdk.customers.Customer) CustomerSyncStatistics(com.commercetools.sync.customers.helpers.CustomerSyncStatistics) TypeService(com.commercetools.sync.services.TypeService) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) Test(org.junit.jupiter.api.Test)

Example 13 with CustomerService

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

the class CustomerSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    final CustomerService mockCustomerService = mock(CustomerService.class);
    when(mockCustomerService.fetchMatchingCustomersByKeys(singleton("customer-key"))).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final CustomerSync customerSync = new CustomerSync(syncOptions, mockCustomerService, mock(TypeService.class), mock(CustomerGroupService.class));
    // test
    final CustomerSyncStatistics customerSyncStatistics = customerSync.sync(singletonList(CustomerDraftBuilder.of("email", "pass").key("customer-key").build())).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(customerSyncStatistics).hasValues(1, 0, 0, 1);
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to fetch existing customers with keys: '[customer-key]'.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
}
Also used : CustomerService(com.commercetools.sync.services.CustomerService) CompletionException(java.util.concurrent.CompletionException) SphereException(io.sphere.sdk.models.SphereException) CustomerSyncStatistics(com.commercetools.sync.customers.helpers.CustomerSyncStatistics) TypeService(com.commercetools.sync.services.TypeService) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) Test(org.junit.jupiter.api.Test)

Example 14 with CustomerService

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

the class ShoppingListSyncTest method sync_WithExceptionOnCachingKeysToIds_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithExceptionOnCachingKeysToIds_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    // preparation
    final TypeService typeService = mock(TypeService.class);
    final CustomerService customerService = mock(CustomerService.class);
    when(typeService.cacheKeysToIds(any())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    when(customerService.cacheKeysToIds(any())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final ShoppingListSync shoppingListSync = new ShoppingListSync(syncOptions, mock(ShoppingListService.class), customerService, typeService);
    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
    ShoppingListSyncStatistics statistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
    AssertionsForStatistics.assertThat(statistics).hasValues(1, 0, 0, 1);
    // assertions
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to build a cache of keys to ids.");
    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) CustomerService(com.commercetools.sync.services.CustomerService) ShoppingListService(com.commercetools.sync.services.ShoppingListService) 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)

Aggregations

CustomerService (com.commercetools.sync.services.CustomerService)14 Test (org.junit.jupiter.api.Test)12 TypeService (com.commercetools.sync.services.TypeService)11 CustomerSyncStatistics (com.commercetools.sync.customers.helpers.CustomerSyncStatistics)10 CustomerGroupService (com.commercetools.sync.services.CustomerGroupService)10 CustomerDraft (io.sphere.sdk.customers.CustomerDraft)10 Customer (io.sphere.sdk.customers.Customer)9 SphereException (io.sphere.sdk.models.SphereException)5 SyncException (com.commercetools.sync.commons.exceptions.SyncException)3 ConcurrentModificationException (io.sphere.sdk.client.ConcurrentModificationException)3 HashSet (java.util.HashSet)3 CompletionException (java.util.concurrent.CompletionException)3 CustomerSyncOptions (com.commercetools.sync.customers.CustomerSyncOptions)1 CustomerSyncOptionsBuilder (com.commercetools.sync.customers.CustomerSyncOptionsBuilder)1 CustomerITUtils.deleteCustomers (com.commercetools.sync.integration.commons.utils.CustomerITUtils.deleteCustomers)1 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)1 ShoppingListService (com.commercetools.sync.services.ShoppingListService)1 CustomerServiceImpl (com.commercetools.sync.services.impl.CustomerServiceImpl)1 ShoppingListSyncStatistics (com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics)1 BadRequestException (io.sphere.sdk.client.BadRequestException)1