use of com.commercetools.sync.services.CustomerService in project commercetools-sync-java by commercetools.
the class CustomerServiceImplIT method setupTest.
/**
* Deletes Customers from target CTP projects, then it populates target CTP project with customer
* test data.
*/
@BeforeEach
void setupTest() {
errorCallBackMessages = new ArrayList<>();
errorCallBackExceptions = new ArrayList<>();
warningCallBackMessages = new ArrayList<>();
deleteCustomers(CTP_TARGET_CLIENT);
final CustomerSyncOptions customerSyncOptions = CustomerSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).build();
// Create a mock new customer in the target project.
CustomerDraft customerDraft = CustomerDraftBuilder.of("mail@mail.com", "password").key(EXISTING_CUSTOMER_KEY).build();
customer = CTP_TARGET_CLIENT.execute(CustomerCreateCommand.of(customerDraft)).toCompletableFuture().join().getCustomer();
customerService = new CustomerServiceImpl(customerSyncOptions);
}
use of com.commercetools.sync.services.CustomerService in project commercetools-sync-java by commercetools.
the class CustomerSyncTest method sync_WithConcurrentModificationExceptionAndFailedFetch_ShouldFailToReFetchAndUpdate.
@Test
void sync_WithConcurrentModificationExceptionAndFailedFetch_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(exceptionallyCompletedFuture(new SphereException()));
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("Failed to fetch from CTP while retrying after concurrency modification.");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
}
use of com.commercetools.sync.services.CustomerService in project commercetools-sync-java by commercetools.
the class CustomerSyncTest method sync_WithBadRequestException_ShouldFailToUpdateAndIncreaseFailedCounter.
@Test
void sync_WithBadRequestException_ShouldFailToUpdateAndIncreaseFailedCounter() {
// 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 BadRequestException("Invalid request")));
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("Invalid request");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasRootCauseExactlyInstanceOf(BadRequestException.class);
}
use of com.commercetools.sync.services.CustomerService in project commercetools-sync-java by commercetools.
the class CustomerSyncTest method sync_FailedOnCreation_ShouldCallBeforeCreateCallbackAndIncrementFailed.
@Test
void sync_FailedOnCreation_ShouldCallBeforeCreateCallbackAndIncrementFailed() {
// preparation
final CustomerService mockCustomerService = mock(CustomerService.class);
final Customer mockCustomer = mock(Customer.class);
when(mockCustomerService.fetchMatchingCustomersByKeys(singleton("customerKey"))).thenReturn(completedFuture(new HashSet<>(singletonList(mockCustomer))));
// simulate an error during create, service will return an empty optional.
when(mockCustomerService.createCustomer(any())).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);
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_WithConcurrentModificationException_ShouldRetryToUpdateNewCustomerWithSuccess.
@Test
void sync_WithConcurrentModificationException_ShouldRetryToUpdateNewCustomerWithSuccess() {
// 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.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, 0, 1, 0);
}
Aggregations