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);
}
Aggregations