Search in sources :

Example 1 with CustomerService

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

the class MockUtils method getMockCustomerService.

/**
 * Creates a mock {@link CustomerService} that returns a dummy customer id of value "customerId"
 * instance whenever the following method is called on the service:
 *
 * <ul>
 *   <li>{@link CustomerService#fetchCachedCustomerId(String)}
 * </ul>
 *
 * @return the created mock of the {@link CustomerService}.
 */
public static CustomerService getMockCustomerService() {
    final CustomerService customerService = mock(CustomerService.class);
    when(customerService.fetchCachedCustomerId(anyString())).thenReturn(completedFuture(Optional.of("customerId")));
    when(customerService.cacheKeysToIds(anySet())).thenReturn(completedFuture(Collections.singletonMap("customerKey", "customerId")));
    return customerService;
}
Also used : CustomerService(com.commercetools.sync.services.CustomerService)

Example 2 with CustomerService

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

the class ProductSyncMockUtils method getMockCustomerService.

/**
 * Creates a mock {@link CustomerService} that returns a completed {@link CompletableFuture}
 * containing an {@link Optional} containing the id of the supplied value whenever the following
 * method is called on the service:
 *
 * <ul>
 *   <li>{@link CustomerService#fetchCachedCustomerId(String)}
 * </ul>
 *
 * @return the created mock of the {@link CustomerService}.
 */
public static CustomerService getMockCustomerService(@Nonnull final String id) {
    final CustomerService customerService = mock(CustomerService.class);
    when(customerService.fetchCachedCustomerId(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.of(id)));
    return customerService;
}
Also used : CustomerService(com.commercetools.sync.services.CustomerService)

Example 3 with CustomerService

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

the class CustomerSyncTest method sync_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate.

@Test
void sync_WithConcurrentModificationExceptionAndUnexpectedDelete_ShouldFailToReFetchAndUpdate() {
    // preparation
    final CustomerService mockCustomerService = mock(CustomerService.class);
    final Customer mockCustomer = mock(Customer.class);
    when(mockCustomer.getKey()).thenReturn("customerKey");
    when(mockCustomerService.fetchMatchingCustomersByKeys(anySet())).thenReturn(completedFuture(singleton(mockCustomer)));
    when(mockCustomerService.updateCustomer(any(), anyList())).thenReturn(exceptionallyCompletedFuture(new SphereException(new ConcurrentModificationException()))).thenReturn(completedFuture(mockCustomer));
    when(mockCustomerService.fetchCustomerByKey("customerKey")).thenReturn(completedFuture(Optional.empty()));
    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, 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 : CustomerService(com.commercetools.sync.services.CustomerService) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Customer(io.sphere.sdk.customers.Customer) SyncException(com.commercetools.sync.commons.exceptions.SyncException) 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) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) Test(org.junit.jupiter.api.Test)

Example 4 with CustomerService

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

the class CustomerSyncTest method sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallbackAndIncrementCreated.

@Test
void sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallbackAndIncrementCreated() {
    // preparation
    final CustomerService mockCustomerService = mock(CustomerService.class);
    final Customer mockCustomer = mock(Customer.class);
    when(mockCustomerService.fetchMatchingCustomersByKeys(singleton("customerKey"))).thenReturn(completedFuture(new HashSet<>(singletonList(mockCustomer))));
    when(mockCustomerService.createCustomer(any())).thenReturn(completedFuture(Optional.of(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, 1, 0, 0);
    verify(spyCustomerSyncOptions).applyBeforeCreateCallback(customerDraft);
    verify(spyCustomerSyncOptions, never()).applyBeforeUpdateCallback(any(), any(), any());
}
Also used : CustomerService(com.commercetools.sync.services.CustomerService) Customer(io.sphere.sdk.customers.Customer) CustomerSyncStatistics(com.commercetools.sync.customers.helpers.CustomerSyncStatistics) HashSet(java.util.HashSet) 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 5 with CustomerService

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

the class CustomerSyncTest method sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback.

@Test
void sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback() {
    // preparation
    final CustomerService mockCustomerService = mock(CustomerService.class);
    final Customer mockCustomer = mock(Customer.class);
    when(mockCustomer.getKey()).thenReturn("customerKey");
    when(mockCustomerService.fetchMatchingCustomersByKeys(anySet())).thenReturn(completedFuture(singleton(mockCustomer)));
    when(mockCustomerService.updateCustomer(any(), anyList())).thenReturn(completedFuture(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, 1, 0);
    verify(spyCustomerSyncOptions).applyBeforeUpdateCallback(any(), any(), any());
    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)

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