use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategoryReferenceResolverTest method resolveCategoryReferences_WithNonExistentCategoryReferences_ShouldNotResolveReferences.
@Test
void resolveCategoryReferences_WithNonExistentCategoryReferences_ShouldNotResolveReferences() {
// preparation
final CategoryService mockCategoryService = mockCategoryService(emptySet(), null);
final Set<ResourceIdentifier<Category>> categories = new HashSet<>();
categories.add(ResourceIdentifier.ofKey("non-existent-category-1"));
categories.add(ResourceIdentifier.ofKey("non-existent-category-2"));
final ProductDraftBuilder productBuilder = getBuilderWithRandomProductType().key("dummyKey").categories(categories);
final ProductReferenceResolver productReferenceResolver = createProductReferenceResolver(mockCategoryService);
// test and assertion
final String expectedMessageWithCause = format(FAILED_TO_RESOLVE_REFERENCE, Category.resourceTypeId(), "dummyKey", format(CATEGORIES_DO_NOT_EXIST, "non-existent-category-1, non-existent-category-2"));
productReferenceResolver.resolveCategoryReferences(productBuilder).exceptionally(exception -> {
assertThat(exception).hasCauseExactlyInstanceOf(ReferenceResolutionException.class);
assertThat(exception.getCause().getMessage()).isEqualTo(expectedMessageWithCause);
return null;
}).toCompletableFuture().join();
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategoryReferenceResolverTest method resolveCategoryReferences_WithCategoryKeysAndSomeCategoryOrderHints_ShouldResolveReferences.
@Test
void resolveCategoryReferences_WithCategoryKeysAndSomeCategoryOrderHints_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 Map<String, String> categoryOrderHintValues = categories.stream().limit(3).collect(Collectors.toMap(Category::getKey, (category -> "0.00" + category.getId())));
final CategoryOrderHints categoryOrderHints = CategoryOrderHints.of(categoryOrderHintValues);
final ProductDraftBuilder productBuilder = getBuilderWithRandomProductType().categories(categoryResourceIdentifiers).categoryOrderHints(categoryOrderHints);
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()).hasSize(3);
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategoryReferenceResolverTest method resolveCategoryReferences_WithNullKeyOnCategoryReference_ShouldNotResolveReference.
@Test
void resolveCategoryReferences_WithNullKeyOnCategoryReference_ShouldNotResolveReference() {
// preparation
final CategoryService mockCategoryService = mockCategoryService(emptySet(), null);
final ProductDraftBuilder productBuilder = getBuilderWithRandomProductType().categories(singleton(ResourceIdentifier.ofKey(null)));
final ProductReferenceResolver productReferenceResolver = createProductReferenceResolver(mockCategoryService);
// test and assertion
assertThat(productReferenceResolver.resolveCategoryReferences(productBuilder)).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(ReferenceResolutionException.class).withMessageContaining(format("Failed to resolve 'category' resource identifier on ProductDraft with " + "key:'%s'. Reason: %s", productBuilder.getKey(), BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER));
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategoryReferenceResolverTest method resolveCategoryReferences_WithCategoryKeysAndCategoryOrderHints_ShouldResolveReferences.
@Test
void resolveCategoryReferences_WithCategoryKeysAndCategoryOrderHints_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 Map<String, String> categoryOrderHintValues = categories.stream().collect(Collectors.toMap(Category::getKey, (category -> "0.00" + category.getId())));
final ProductDraftBuilder productBuilder = getBuilderWithRandomProductType().categories(categoryResourceIdentifiers).categoryOrderHints(CategoryOrderHints.of(categoryOrderHintValues));
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(Collectors.toSet()));
assertThat(resolvedDraft.getCategoryOrderHints()).isNotNull();
assertThat(resolvedDraft.getCategoryOrderHints().getAsMap()).hasSize(categoryOrderHintValues.size());
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class ProductSyncMockUtils method getMockCategoryService.
/**
* Creates a mock {@link CategoryService} that returns a completed {@link CompletableFuture}
* containing an {@link Optional} containing the id of the supplied value whenever the following
* method is called on the service:
*
* <ul>
* <li>{@link CategoryService#fetchCachedCategoryId(String)}
* </ul>
*
* @return the created mock of the {@link CategoryService}.
*/
public static CategoryService getMockCategoryService(@Nonnull final String id) {
final CategoryService categoryService = mock(CategoryService.class);
when(categoryService.fetchCachedCategoryId(any())).thenReturn(CompletableFuture.completedFuture(Optional.of(id)));
return categoryService;
}
Aggregations