use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class LineItemListUpdateActionUtilsTest method getMockTypeService.
@Nonnull
private static TypeService getMockTypeService() {
final TypeService typeService = mock(TypeService.class);
when(typeService.fetchCachedTypeId(anyString())).thenReturn(completedFuture(Optional.of("custom_type_id")));
return typeService;
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class CategoryReferenceResolverTest method resolveCustomTypeReference_WithExceptionOnCustomTypeFetch_ShouldNotResolveReferences.
@Test
void resolveCustomTypeReference_WithExceptionOnCustomTypeFetch_ShouldNotResolveReferences() {
// Preparation
final SphereClient ctpClient = mock(SphereClient.class);
final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(ctpClient).build();
final TypeService typeService = new TypeServiceImpl(categorySyncOptions);
final CompletableFuture<PagedQueryResult<Type>> futureThrowingSphereException = new CompletableFuture<>();
futureThrowingSphereException.completeExceptionally(new SphereException("CTP error on fetch"));
when(ctpClient.execute(any(TypeQuery.class))).thenReturn(futureThrowingSphereException);
final CategoryDraftBuilder categoryDraft = getMockCategoryDraftBuilder(Locale.ENGLISH, "myDraft", "key", null, "customTypeId", new HashMap<>());
final CategoryReferenceResolver categoryReferenceResolver = new CategoryReferenceResolver(categorySyncOptions, typeService, categoryService);
// Test and assertion
assertThat(categoryReferenceResolver.resolveCustomTypeReference(categoryDraft)).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(SphereException.class).withMessageContaining("CTP error on fetch");
}
use of com.commercetools.sync.services.TypeService 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.services.TypeService 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.services.TypeService in project commercetools-sync-java by commercetools.
the class ShoppingListSyncTest method sync_WithExceptionOnCachingKeysToIds_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithExceptionOnCachingKeysToIds_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final TypeService typeService = mock(TypeService.class);
final CustomerService customerService = mock(CustomerService.class);
when(typeService.cacheKeysToIds(any())).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
when(customerService.cacheKeysToIds(any())).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final ShoppingListSync shoppingListSync = new ShoppingListSync(syncOptions, mock(ShoppingListService.class), customerService, typeService);
ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("shopping-list-name")).key("shopping-list-key").customer(ResourceIdentifier.ofKey("customer-key")).custom(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap())).build();
// test
ShoppingListSyncStatistics statistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
AssertionsForStatistics.assertThat(statistics).hasValues(1, 0, 0, 1);
// assertions
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);
}
Aggregations