Search in sources :

Example 1 with CategoryTransformService

use of com.commercetools.sync.categories.service.CategoryTransformService in project commercetools-sync-java by commercetools.

the class CategoryTransformServiceImplTest method transform_ShouldReplaceCategoryReferenceIdsWithKeys.

@Test
void transform_ShouldReplaceCategoryReferenceIdsWithKeys() {
    // preparation
    final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
    final SphereClient sourceClient = mock(SphereClient.class);
    final CategoryTransformService categoryTransformService = new CategoryTransformServiceImpl(sourceClient, referenceIdToKeyCache);
    final List<Category> categoryPage = asList(readObjectFromResource("category-key-1.json", Category.class), readObjectFromResource("category-key-2.json", Category.class));
    final List<String> referenceIds = categoryPage.stream().filter(category -> category.getCustom() != null).map(category -> category.getCustom().getType().getId()).collect(Collectors.toList());
    String jsonStringCategories = "{\"results\":[{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0c3\",\"key\":\"cat1\"}]}";
    final ResourceKeyIdGraphQlResult categoriesResult = SphereJsonUtils.readObject(jsonStringCategories, ResourceKeyIdGraphQlResult.class);
    when(sourceClient.execute(any())).thenReturn(CompletableFuture.completedFuture(categoriesResult));
    // test
    final CompletionStage<List<CategoryDraft>> draftsFromPageStage = categoryTransformService.toCategoryDrafts(categoryPage);
    // assertions
    final List<CategoryDraft> expectedResult = mapToCategoryDrafts(categoryPage, referenceIdToKeyCache);
    final List<String> referenceKeys = expectedResult.stream().filter(category -> category.getCustom() != null).map(category -> category.getCustom().getType().getId()).collect(Collectors.toList());
    assertThat(referenceKeys).doesNotContainSequence(referenceIds);
    assertThat(draftsFromPageStage).isCompletedWithValue(expectedResult);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) CategoryTransformService(com.commercetools.sync.categories.service.CategoryTransformService) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Category(io.sphere.sdk.categories.Category) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) Test(org.junit.jupiter.api.Test) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) Arrays.asList(java.util.Arrays.asList) CategoryReferenceResolutionUtils.mapToCategoryDrafts(com.commercetools.sync.categories.utils.CategoryReferenceResolutionUtils.mapToCategoryDrafts) SphereClient(io.sphere.sdk.client.SphereClient) Mockito.mock(org.mockito.Mockito.mock) CategoryTransformService(com.commercetools.sync.categories.service.CategoryTransformService) Category(io.sphere.sdk.categories.Category) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.jupiter.api.Test)

Aggregations

CategoryTransformService (com.commercetools.sync.categories.service.CategoryTransformService)1 CategoryReferenceResolutionUtils.mapToCategoryDrafts (com.commercetools.sync.categories.utils.CategoryReferenceResolutionUtils.mapToCategoryDrafts)1 ResourceKeyIdGraphQlResult (com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult)1 CaffeineReferenceIdToKeyCacheImpl (com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl)1 ReferenceIdToKeyCache (com.commercetools.sync.commons.utils.ReferenceIdToKeyCache)1 Category (io.sphere.sdk.categories.Category)1 CategoryDraft (io.sphere.sdk.categories.CategoryDraft)1 SphereClient (io.sphere.sdk.client.SphereClient)1 SphereJsonUtils (io.sphere.sdk.json.SphereJsonUtils)1 SphereJsonUtils.readObjectFromResource (io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.when (org.mockito.Mockito.when)1