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;
}
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;
}
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();
}
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());
}
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);
}
Aggregations