use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategoryReferenceResolverTest method resolveCategoryReferences_WithCategoryKeysAndNoCategoryOrderHints_ShouldResolveReferences.
@Test
void resolveCategoryReferences_WithCategoryKeysAndNoCategoryOrderHints_ShouldResolveReferences() {
// preparation
final int nCategories = 10;
final List<Category> categories = IntStream.range(0, nCategories).mapToObj(i -> i + "").map(key -> getMockCategory(key, key)).collect(Collectors.toList());
final CategoryService mockCategoryService = mockCategoryService(new HashSet<>(categories), null);
final Set<ResourceIdentifier<Category>> categoryResourceIdentifiers = categories.stream().map(category -> ResourceIdentifier.<Category>ofKey(category.getKey())).collect(toSet());
final ProductDraftBuilder productBuilder = getBuilderWithRandomProductType().categories(categoryResourceIdentifiers);
final ProductReferenceResolver productReferenceResolver = createProductReferenceResolver(mockCategoryService);
// test
final ProductDraftBuilder resolvedDraft = productReferenceResolver.resolveCategoryReferences(productBuilder).toCompletableFuture().join();
// assertion
assertThat(resolvedDraft.getCategories()).hasSameElementsAs(categoryResourceIdentifiers.stream().map(categoryResourceIdentifier -> Category.referenceOfId(categoryResourceIdentifier.getKey())).collect(toSet()));
assertThat(resolvedDraft.getCategoryOrderHints()).isNotNull();
assertThat(resolvedDraft.getCategoryOrderHints().getAsMap()).isEmpty();
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategoryReferenceResolverTest method resolveCategoryReferences_WithExceptionCategoryFetch_ShouldNotResolveReference.
@Test
void resolveCategoryReferences_WithExceptionCategoryFetch_ShouldNotResolveReference() {
final Category category = getMockCategory("categoryKey", "categoryKey");
final List<Category> categories = Collections.singletonList(category);
final Set<ResourceIdentifier<Category>> categoryResourceIdentifiers = categories.stream().map(cat -> ResourceIdentifier.<Category>ofKey(cat.getKey())).collect(toSet());
final CategoryService mockCategoryService = mockCategoryService(new HashSet<>(categories), null);
final ProductDraftBuilder productBuilder = getBuilderWithRandomProductType().categories(categoryResourceIdentifiers);
final CompletableFuture<Set<Category>> futureThrowingSphereException = new CompletableFuture<>();
futureThrowingSphereException.completeExceptionally(new SphereException("CTP error on fetch"));
when(mockCategoryService.fetchMatchingCategoriesByKeys(anySet())).thenReturn(futureThrowingSphereException);
final ProductReferenceResolver productReferenceResolver = createProductReferenceResolver(mockCategoryService);
// test and assertion
assertThat(productReferenceResolver.resolveCategoryReferences(productBuilder)).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 CategoryReferenceResolverTest method resolveCategoryReferences_WithNullCategoryReferences_ShouldNotResolveReferences.
@Test
void resolveCategoryReferences_WithNullCategoryReferences_ShouldNotResolveReferences() {
// preparation
final CategoryService mockCategoryService = mockCategoryService(emptySet(), null);
final ProductDraftBuilder productBuilder = getBuilderWithRandomProductType();
final ProductReferenceResolver productReferenceResolver = createProductReferenceResolver(mockCategoryService);
// test and assertion
assertThat(productReferenceResolver.resolveCategoryReferences(productBuilder).toCompletableFuture()).matches(f -> !f.isCompletedExceptionally() || f.isCancelled()).isCompletedWithValueMatching(resolvedDraft -> resolvedDraft.getCategories().isEmpty());
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategoryReferenceResolverTest method resolveCategoryReferences_WithCategoryReferencesWithId_ShouldResolveReference.
@Test
void resolveCategoryReferences_WithCategoryReferencesWithId_ShouldResolveReference() {
// preparation
final CategoryService mockCategoryService = mockCategoryService(emptySet(), null);
final ProductDraftBuilder productBuilder = getBuilderWithRandomProductType().categories(singleton(ResourceIdentifier.ofId("existing-category-id")));
final ProductReferenceResolver productReferenceResolver = createProductReferenceResolver(mockCategoryService);
final ProductDraftBuilder resolvedDraft = productReferenceResolver.resolveCategoryReferences(productBuilder).toCompletableFuture().join();
// assertion
assertThat(resolvedDraft.getCategories()).isEqualTo(singleton(ResourceIdentifier.ofId("existing-category-id")));
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategoryReferenceResolverTest method resolveCategoryReferences_WithIdOnCategoryReference_ShouldNotResolveReference.
@Test
void resolveCategoryReferences_WithIdOnCategoryReference_ShouldNotResolveReference() {
// preparation
final CategoryService mockCategoryService = mockCategoryService(emptySet(), null);
final ProductDraftBuilder productBuilder = getBuilderWithRandomProductType().categories(asSet(ResourceIdentifier.ofId("existing-id"), null));
final ProductReferenceResolver productReferenceResolver = createProductReferenceResolver(mockCategoryService);
// test and assertion
assertThat(productReferenceResolver.resolveCategoryReferences(productBuilder).toCompletableFuture()).matches(f -> !f.isCompletedExceptionally() || f.isCancelled()).isCompletedWithValueMatching(resolvedDraft -> Objects.equals(resolvedDraft.getCategories(), productBuilder.getCategories()));
}
Aggregations