use of com.commercetools.sync.products.helpers.ProductReferenceResolver in project commercetools-sync-java by commercetools.
the class StateReferenceResolverTest method setup.
/**
* Sets up the services and the options needed for reference resolution.
*/
@BeforeEach
void setup() {
stateService = getMockStateService(STATE_ID);
final ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(mock(SphereClient.class)).build();
referenceResolver = new ProductReferenceResolver(syncOptions, getMockProductTypeService(PRODUCT_TYPE_ID), mock(CategoryService.class), getMockTypeService(), getMockChannelService(getMockSupplyChannel(CHANNEL_ID, CHANNEL_KEY)), mock(CustomerGroupService.class), getMockTaxCategoryService(TAX_CATEGORY_ID), stateService, getMockProductService(PRODUCT_ID), getMockCustomObjectService(CUSTOM_OBJECT_ID), getMockCustomerService(CUSTOMER_ID));
}
use of com.commercetools.sync.products.helpers.ProductReferenceResolver 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.products.helpers.ProductReferenceResolver 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.products.helpers.ProductReferenceResolver 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.products.helpers.ProductReferenceResolver 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")));
}
Aggregations