Search in sources :

Example 1 with ProductReferenceResolver

use of com.commercetools.sync.products.helpers.ProductReferenceResolver 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();
}
Also used : ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) ProductReferenceResolver(com.commercetools.sync.products.helpers.ProductReferenceResolver) CategoryService(com.commercetools.sync.services.CategoryService) ProductSyncMockUtils.getMockTaxCategoryService(com.commercetools.sync.products.ProductSyncMockUtils.getMockTaxCategoryService) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with ProductReferenceResolver

use of com.commercetools.sync.products.helpers.ProductReferenceResolver 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);
}
Also used : ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProductSyncMockUtils.getMockProductService(com.commercetools.sync.products.ProductSyncMockUtils.getMockProductService) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) ProductSyncMockUtils.getMockCustomObjectService(com.commercetools.sync.products.ProductSyncMockUtils.getMockCustomObjectService) SphereClient(io.sphere.sdk.client.SphereClient) InventorySyncMockUtils.getMockSupplyChannel(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockSupplyChannel) Collectors.toSet(java.util.stream.Collectors.toSet) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CategoryService(com.commercetools.sync.services.CategoryService) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) IntStream(java.util.stream.IntStream) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) InventorySyncMockUtils.getMockChannelService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService) CategoryOrderHints(io.sphere.sdk.products.CategoryOrderHints) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) ProductSyncMockUtils.getBuilderWithRandomProductType(com.commercetools.sync.products.ProductSyncMockUtils.getBuilderWithRandomProductType) CompletableFuture(java.util.concurrent.CompletableFuture) ProductReferenceResolver(com.commercetools.sync.products.helpers.ProductReferenceResolver) HashSet(java.util.HashSet) ProductSyncMockUtils.getMockTaxCategoryService(com.commercetools.sync.products.ProductSyncMockUtils.getMockTaxCategoryService) ProductSyncMockUtils.getMockProductTypeService(com.commercetools.sync.products.ProductSyncMockUtils.getMockProductTypeService) SphereException(io.sphere.sdk.models.SphereException) ProductSyncMockUtils.getMockStateService(com.commercetools.sync.products.ProductSyncMockUtils.getMockStateService) ProductSyncMockUtils.getMockCustomerService(com.commercetools.sync.products.ProductSyncMockUtils.getMockCustomerService) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) Nonnull(javax.annotation.Nonnull) Collections.emptySet(java.util.Collections.emptySet) FAILED_TO_RESOLVE_REFERENCE(com.commercetools.sync.products.helpers.ProductReferenceResolver.FAILED_TO_RESOLVE_REFERENCE) Mockito.when(org.mockito.Mockito.when) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MockUtils.getMockTypeService(com.commercetools.sync.commons.MockUtils.getMockTypeService) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) CATEGORIES_DO_NOT_EXIST(com.commercetools.sync.products.helpers.ProductReferenceResolver.CATEGORIES_DO_NOT_EXIST) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) Collections(java.util.Collections) BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER(com.commercetools.sync.commons.helpers.BaseReferenceResolver.BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER) ProductReferenceResolver(com.commercetools.sync.products.helpers.ProductReferenceResolver) Category(io.sphere.sdk.categories.Category) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) CategoryOrderHints(io.sphere.sdk.products.CategoryOrderHints) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) CategoryService(com.commercetools.sync.services.CategoryService) ProductSyncMockUtils.getMockTaxCategoryService(com.commercetools.sync.products.ProductSyncMockUtils.getMockTaxCategoryService) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) Test(org.junit.jupiter.api.Test)

Example 3 with ProductReferenceResolver

use of com.commercetools.sync.products.helpers.ProductReferenceResolver 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));
}
Also used : ProductReferenceResolver(com.commercetools.sync.products.helpers.ProductReferenceResolver) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) CategoryService(com.commercetools.sync.services.CategoryService) ProductSyncMockUtils.getMockTaxCategoryService(com.commercetools.sync.products.ProductSyncMockUtils.getMockTaxCategoryService) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) Test(org.junit.jupiter.api.Test)

Example 4 with ProductReferenceResolver

use of com.commercetools.sync.products.helpers.ProductReferenceResolver 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());
}
Also used : ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProductSyncMockUtils.getMockProductService(com.commercetools.sync.products.ProductSyncMockUtils.getMockProductService) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) ProductSyncMockUtils.getMockCustomObjectService(com.commercetools.sync.products.ProductSyncMockUtils.getMockCustomObjectService) SphereClient(io.sphere.sdk.client.SphereClient) InventorySyncMockUtils.getMockSupplyChannel(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockSupplyChannel) Collectors.toSet(java.util.stream.Collectors.toSet) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CategoryService(com.commercetools.sync.services.CategoryService) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) IntStream(java.util.stream.IntStream) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) InventorySyncMockUtils.getMockChannelService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService) CategoryOrderHints(io.sphere.sdk.products.CategoryOrderHints) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) ProductSyncMockUtils.getBuilderWithRandomProductType(com.commercetools.sync.products.ProductSyncMockUtils.getBuilderWithRandomProductType) CompletableFuture(java.util.concurrent.CompletableFuture) ProductReferenceResolver(com.commercetools.sync.products.helpers.ProductReferenceResolver) HashSet(java.util.HashSet) ProductSyncMockUtils.getMockTaxCategoryService(com.commercetools.sync.products.ProductSyncMockUtils.getMockTaxCategoryService) ProductSyncMockUtils.getMockProductTypeService(com.commercetools.sync.products.ProductSyncMockUtils.getMockProductTypeService) SphereException(io.sphere.sdk.models.SphereException) ProductSyncMockUtils.getMockStateService(com.commercetools.sync.products.ProductSyncMockUtils.getMockStateService) ProductSyncMockUtils.getMockCustomerService(com.commercetools.sync.products.ProductSyncMockUtils.getMockCustomerService) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) Nonnull(javax.annotation.Nonnull) Collections.emptySet(java.util.Collections.emptySet) FAILED_TO_RESOLVE_REFERENCE(com.commercetools.sync.products.helpers.ProductReferenceResolver.FAILED_TO_RESOLVE_REFERENCE) Mockito.when(org.mockito.Mockito.when) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MockUtils.getMockTypeService(com.commercetools.sync.commons.MockUtils.getMockTypeService) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) CATEGORIES_DO_NOT_EXIST(com.commercetools.sync.products.helpers.ProductReferenceResolver.CATEGORIES_DO_NOT_EXIST) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) Collections(java.util.Collections) BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER(com.commercetools.sync.commons.helpers.BaseReferenceResolver.BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) ProductReferenceResolver(com.commercetools.sync.products.helpers.ProductReferenceResolver) Category(io.sphere.sdk.categories.Category) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) CategoryService(com.commercetools.sync.services.CategoryService) ProductSyncMockUtils.getMockTaxCategoryService(com.commercetools.sync.products.ProductSyncMockUtils.getMockTaxCategoryService) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) Test(org.junit.jupiter.api.Test)

Example 5 with ProductReferenceResolver

use of com.commercetools.sync.products.helpers.ProductReferenceResolver in project commercetools-sync-java by commercetools.

the class ProductTypeReferenceResolverTest method setup.

/**
 * Sets up the services and the options needed for reference resolution.
 */
@BeforeEach
void setup() {
    productTypeService = getMockProductTypeService(PRODUCT_TYPE_ID);
    final ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(mock(SphereClient.class)).build();
    referenceResolver = new ProductReferenceResolver(syncOptions, productTypeService, mock(CategoryService.class), getMockTypeService(), getMockChannelService(getMockSupplyChannel(CHANNEL_ID, CHANNEL_KEY)), mock(CustomerGroupService.class), getMockTaxCategoryService(TAX_CATEGORY_ID), getMockStateService(STATE_ID), getMockProductService(PRODUCT_ID), getMockCustomObjectService(CUSTOM_OBJECT_ID), getMockCustomerService(CUSTOMER_ID));
}
Also used : ProductReferenceResolver(com.commercetools.sync.products.helpers.ProductReferenceResolver) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ProductReferenceResolver (com.commercetools.sync.products.helpers.ProductReferenceResolver)13 MockUtils.mockCategoryService (com.commercetools.sync.commons.MockUtils.mockCategoryService)10 ProductSyncMockUtils.getMockTaxCategoryService (com.commercetools.sync.products.ProductSyncMockUtils.getMockTaxCategoryService)10 CategoryService (com.commercetools.sync.services.CategoryService)10 ProductDraftBuilder (io.sphere.sdk.products.ProductDraftBuilder)10 Test (org.junit.jupiter.api.Test)10 ProductSyncOptions (com.commercetools.sync.products.ProductSyncOptions)9 ReferenceResolutionException (com.commercetools.sync.commons.exceptions.ReferenceResolutionException)8 ResourceIdentifier (io.sphere.sdk.models.ResourceIdentifier)7 HashSet (java.util.HashSet)7 CategorySyncMockUtils.getMockCategory (com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory)6 MockUtils.getMockTypeService (com.commercetools.sync.commons.MockUtils.getMockTypeService)6 BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER (com.commercetools.sync.commons.helpers.BaseReferenceResolver.BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER)6 InventorySyncMockUtils.getMockChannelService (com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService)6 InventorySyncMockUtils.getMockSupplyChannel (com.commercetools.sync.inventories.InventorySyncMockUtils.getMockSupplyChannel)6 ProductSyncMockUtils.getBuilderWithRandomProductType (com.commercetools.sync.products.ProductSyncMockUtils.getBuilderWithRandomProductType)6 ProductSyncMockUtils.getMockCustomObjectService (com.commercetools.sync.products.ProductSyncMockUtils.getMockCustomObjectService)6 ProductSyncMockUtils.getMockCustomerService (com.commercetools.sync.products.ProductSyncMockUtils.getMockCustomerService)6 ProductSyncMockUtils.getMockProductService (com.commercetools.sync.products.ProductSyncMockUtils.getMockProductService)6 ProductSyncMockUtils.getMockProductTypeService (com.commercetools.sync.products.ProductSyncMockUtils.getMockProductTypeService)6