Search in sources :

Example 11 with CustomerSyncStatistics

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

the class CustomerSyncTest method sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats.

@Test
void sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats() {
    // preparation
    final TypeService typeService = spy(new TypeServiceImpl(syncOptions));
    when(typeService.cacheKeysToIds(anySet())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final CustomerSync customerSync = new CustomerSync(syncOptions, mock(CustomerService.class), typeService, mock(CustomerGroupService.class));
    final CustomerDraft customerDraft = CustomerDraftBuilder.of("email", "pass").key("customerKey").customerGroup(ResourceIdentifier.ofKey("customerGroupKey")).custom(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap())).stores(asList(ResourceIdentifier.ofKey("storeKey1"), ResourceIdentifier.ofKey("storeKey2"), ResourceIdentifier.ofId("storeId3"))).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)).isEqualTo("Failed to build a cache of keys to ids.");
    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) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) 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 12 with CustomerSyncStatistics

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

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

the class CustomerSyncTest method sync_WithNullDraft_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithNullDraft_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    final CustomerSync customerSync = new CustomerSync(syncOptions);
    // test
    final CustomerSyncStatistics customerSyncStatistics = customerSync.sync(singletonList(null)).toCompletableFuture().join();
    AssertionsForStatistics.assertThat(customerSyncStatistics).hasValues(1, 0, 0, 1);
    // assertions
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo(CUSTOMER_DRAFT_IS_NULL);
}
Also used : CustomerSyncStatistics(com.commercetools.sync.customers.helpers.CustomerSyncStatistics) Test(org.junit.jupiter.api.Test)

Example 14 with CustomerSyncStatistics

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

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

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