Search in sources :

Example 16 with CustomerSyncStatistics

use of com.commercetools.sync.customers.helpers.CustomerSyncStatistics 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)

Example 17 with CustomerSyncStatistics

use of com.commercetools.sync.customers.helpers.CustomerSyncStatistics 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 18 with CustomerSyncStatistics

use of com.commercetools.sync.customers.helpers.CustomerSyncStatistics 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 19 with CustomerSyncStatistics

use of com.commercetools.sync.customers.helpers.CustomerSyncStatistics in project commercetools-sync-java by commercetools.

the class CustomerSyncTest method sync_WithoutKey_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithoutKey_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    final CustomerSync customerSync = new CustomerSync(syncOptions);
    // test
    final CustomerSyncStatistics customerSyncStatistics = customerSync.sync(singletonList(CustomerDraftBuilder.of("email", "pass").build())).toCompletableFuture().join();
    AssertionsForStatistics.assertThat(customerSyncStatistics).hasValues(1, 0, 0, 1);
    // assertions
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo(format(CUSTOMER_DRAFT_KEY_NOT_SET, "email"));
}
Also used : CustomerSyncStatistics(com.commercetools.sync.customers.helpers.CustomerSyncStatistics) Test(org.junit.jupiter.api.Test)

Example 20 with CustomerSyncStatistics

use of com.commercetools.sync.customers.helpers.CustomerSyncStatistics 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)

Aggregations

CustomerSyncStatistics (com.commercetools.sync.customers.helpers.CustomerSyncStatistics)20 Test (org.junit.jupiter.api.Test)20 CustomerDraft (io.sphere.sdk.customers.CustomerDraft)16 CustomerGroupService (com.commercetools.sync.services.CustomerGroupService)11 CustomerService (com.commercetools.sync.services.CustomerService)11 TypeService (com.commercetools.sync.services.TypeService)11 Customer (io.sphere.sdk.customers.Customer)10 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 StoreITUtils.createStore (com.commercetools.sync.integration.commons.utils.StoreITUtils.createStore)2 AddStore (io.sphere.sdk.customers.commands.updateactions.AddStore)2 Store (io.sphere.sdk.stores.Store)2 CustomerSync (com.commercetools.sync.customers.CustomerSync)1 CustomerSyncOptions (com.commercetools.sync.customers.CustomerSyncOptions)1 CustomerGroupITUtils.createCustomerGroup (com.commercetools.sync.integration.commons.utils.CustomerGroupITUtils.createCustomerGroup)1 TypeServiceImpl (com.commercetools.sync.services.impl.TypeServiceImpl)1 BadRequestException (io.sphere.sdk.client.BadRequestException)1