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