use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategorySyncTest method sync_WithANullDraft_ShouldBeCountedAsFailed.
@Test
void sync_WithANullDraft_ShouldBeCountedAsFailed() {
final CategoryService mockCategoryService = mockCategoryService(emptySet(), null);
final CategorySync mockCategorySync = new CategorySync(categorySyncOptions, getMockTypeService(), mockCategoryService, mockUnresolvedReferencesService);
final CategorySyncStatistics syncStatistics = mockCategorySync.sync(singletonList(null)).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(1, 0, 0, 1);
assertThat(errorCallBackMessages).hasSize(1);
assertThat(errorCallBackMessages.get(0)).isEqualTo("CategoryDraft is null.");
assertThat(errorCallBackExceptions).hasSize(1);
assertThat(errorCallBackExceptions.get(0)).isEqualTo(null);
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategorySyncTest method sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats.
@Test
void sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats() {
// preparation
final SphereClient mockClient = mock(SphereClient.class);
when(mockClient.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final CategorySyncOptions syncOptions = CategorySyncOptionsBuilder.of(mockClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final CategoryService categoryServiceSpy = spy(new CategoryServiceImpl(syncOptions));
final CategorySync mockCategorySync = new CategorySync(syncOptions, getMockTypeService(), categoryServiceSpy, mockUnresolvedReferencesService);
CategoryDraft categoryDraft = getMockCategoryDraft(Locale.ENGLISH, "name", "newKey", "parentKey", "customTypeId", new HashMap<>());
// test
final CategorySyncStatistics syncStatistics = mockCategorySync.sync(singletonList(categoryDraft)).toCompletableFuture().join();
// assertions
assertThat(syncStatistics).hasValues(1, 0, 0, 1);
assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to build a cache of keys to ids.");
assertThat(errorCallBackExceptions).hasSize(1).singleElement().matches(throwable -> throwable instanceof CompletionException && throwable.getCause() instanceof SphereException);
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategorySyncTest method sync_WithExistingCategoryButWithEmptyCustomTypeReference_ShouldFailSync.
@Test
void sync_WithExistingCategoryButWithEmptyCustomTypeReference_ShouldFailSync() {
final Category mockCategory = getMockCategory(UUID.randomUUID().toString(), "key");
final Category mockParentCategory = getMockCategory(UUID.randomUUID().toString(), "parentKey");
Set<Category> existingCategories = new HashSet<>();
existingCategories.add(mockCategory);
existingCategories.add(mockParentCategory);
final CategoryService mockCategoryService = mockCategoryService(existingCategories, null, mockCategory);
when(mockCategoryService.fetchCachedCategoryId(Mockito.eq("parentKey"))).thenReturn(CompletableFuture.completedFuture(Optional.of("parentId")));
final CategorySync categorySync = new CategorySync(categorySyncOptions, getMockTypeService(), mockCategoryService, mockUnresolvedReferencesService);
final List<CategoryDraft> categoryDrafts = singletonList(getMockCategoryDraft(Locale.ENGLISH, "name", "key", "parentKey", "", new HashMap<>()));
final CategorySyncStatistics syncStatistics = categorySync.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(1, 0, 0, 1);
assertThat(errorCallBackMessages).hasSize(1);
assertThat(errorCallBackMessages.get(0)).isEqualTo(format("Failed to process the CategoryDraft with" + " key: 'key'. Reason: %s: Failed to resolve custom type reference on CategoryDraft " + "with key:'key'. Reason: %s", ReferenceResolutionException.class.getCanonicalName(), BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER));
assertThat(errorCallBackExceptions).hasSize(1);
assertThat(errorCallBackExceptions.get(0)).isExactlyInstanceOf(CompletionException.class);
assertThat(errorCallBackExceptions.get(0).getCause()).isExactlyInstanceOf(ReferenceResolutionException.class);
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategoryReferenceResolverTest method resolveParentReference_WithExceptionOnFetch_ShouldNotResolveReferences.
@Test
void resolveParentReference_WithExceptionOnFetch_ShouldNotResolveReferences() {
// Preparation
final SphereClient ctpClient = mock(SphereClient.class);
final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(ctpClient).build();
final CategoryService categoryService = new CategoryServiceImpl(categorySyncOptions);
final CompletableFuture<PagedQueryResult<Category>> futureThrowingSphereException = new CompletableFuture<>();
futureThrowingSphereException.completeExceptionally(new SphereException("CTP error on fetch"));
when(ctpClient.execute(any(CategoryQuery.class))).thenReturn(futureThrowingSphereException);
final CategoryDraftBuilder categoryDraft = getMockCategoryDraftBuilder(Locale.ENGLISH, "myDraft", "key", "nonExistingCategoryKey", "customTypeKey", new HashMap<>());
final CategoryReferenceResolver categoryReferenceResolver = new CategoryReferenceResolver(categorySyncOptions, typeService, categoryService);
// Test and assertion
assertThat(categoryReferenceResolver.resolveParentReference(categoryDraft)).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(SphereException.class).withMessageContaining("CTP error on fetch");
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class MockUtils method mockCategoryService.
public static CategoryService mockCategoryService(@Nonnull final Set<Category> existingCategories, @Nullable final Category createdCategory) {
final CategoryService mockCategoryService = mock(CategoryService.class);
when(mockCategoryService.fetchMatchingCategoriesByKeys(any())).thenReturn(CompletableFuture.completedFuture(existingCategories));
final Map<String, String> keyToIds = existingCategories.stream().collect(Collectors.toMap(Category::getKey, Category::getId));
when(mockCategoryService.cacheKeysToIds(anySet())).thenReturn(completedFuture(keyToIds));
when(mockCategoryService.createCategory(any())).thenReturn(CompletableFuture.completedFuture(ofNullable(createdCategory)));
return mockCategoryService;
}
Aggregations