Search in sources :

Example 6 with CustomerService

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);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) CustomerServiceImpl(com.commercetools.sync.services.impl.CustomerServiceImpl) CustomerCreateCommand(io.sphere.sdk.customers.commands.CustomerCreateCommand) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.spy(org.mockito.Mockito.spy) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) CustomerQuery(io.sphere.sdk.customers.queries.CustomerQuery) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) CustomerService(com.commercetools.sync.services.CustomerService) Collections.emptySet(java.util.Collections.emptySet) ChangeEmail(io.sphere.sdk.customers.commands.updateactions.ChangeEmail) QueryPredicate(io.sphere.sdk.queries.QueryPredicate) CustomerDraftBuilder(io.sphere.sdk.customers.CustomerDraftBuilder) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Customer(io.sphere.sdk.customers.Customer) String.format(java.lang.String.format) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) CustomerSyncOptionsBuilder(com.commercetools.sync.customers.CustomerSyncOptionsBuilder) List(java.util.List) CustomerITUtils.deleteCustomers(com.commercetools.sync.integration.commons.utils.CustomerITUtils.deleteCustomers) CustomerSyncOptions(com.commercetools.sync.customers.CustomerSyncOptions) Optional(java.util.Optional) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CustomerSyncOptions(com.commercetools.sync.customers.CustomerSyncOptions) CustomerServiceImpl(com.commercetools.sync.services.impl.CustomerServiceImpl) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with CustomerService

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);
}
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 8 with CustomerService

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);
}
Also used : CustomerService(com.commercetools.sync.services.CustomerService) Customer(io.sphere.sdk.customers.Customer) BadRequestException(io.sphere.sdk.client.BadRequestException) SyncException(com.commercetools.sync.commons.exceptions.SyncException) 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 9 with CustomerService

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());
}
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 10 with CustomerService

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);
}
Also used : CustomerService(com.commercetools.sync.services.CustomerService) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Customer(io.sphere.sdk.customers.Customer) 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)

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