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