use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-project-sync by commercetools.
the class InventoryEntrySyncerTest method transform_ShouldReplaceInventoryEntryReferenceIdsWithKeys.
@Test
void transform_ShouldReplaceInventoryEntryReferenceIdsWithKeys() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final InventoryEntrySyncer inventoryEntrySyncer = InventoryEntrySyncer.of(sourceClient, mock(SphereClient.class), getMockedClock());
final List<InventoryEntry> inventoryPage = asList(readObjectFromResource("inventory-sku-1.json", InventoryEntry.class), readObjectFromResource("inventory-sku-2.json", InventoryEntry.class));
final List<String> referenceIds = inventoryPage.stream().filter(inventoryEntry -> inventoryEntry.getSupplyChannel() != null).filter(inventoryEntry -> inventoryEntry.getCustom() != null).flatMap(inventoryEntry -> Stream.of(inventoryEntry.getCustom().getType().getId(), inventoryEntry.getSupplyChannel().getId())).collect(Collectors.toList());
final String jsonStringCustomTypes = "{\"results\":[{\"id\":\"02e915e7-7763-48d1-83bd-d4e940a1a368\"," + "\"key\":\"test-custom-type-key\"} ]}";
final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
final String jsonStringSupplyChannels = "{\"results\":[{\"id\":\"5c0516b5-f506-4b6a-b4d1-c06ca29ab7e1\"," + "\"key\":\"test-channel-key\"} ]}";
final ResourceKeyIdGraphQlResult supplyChannelsResult = SphereJsonUtils.readObject(jsonStringSupplyChannels, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(customTypesResult)).thenReturn(CompletableFuture.completedFuture(supplyChannelsResult));
// test
final CompletionStage<List<InventoryEntryDraft>> draftsFromPageStage = inventoryEntrySyncer.transform(inventoryPage);
// assertions
final List<InventoryEntryDraft> expectedResult = toInventoryEntryDrafts(sourceClient, referenceIdToKeyCache, inventoryPage).join();
final List<String> referenceKeys = expectedResult.stream().filter(inventoryEntry -> inventoryEntry.getSupplyChannel() != null).filter(inventoryEntry -> inventoryEntry.getCustom() != null).flatMap(inventoryEntry -> Stream.of(inventoryEntry.getCustom().getType().getId(), inventoryEntry.getSupplyChannel().getId())).collect(Collectors.toList());
assertThat(referenceKeys).doesNotContainAnyElementsOf(referenceIds);
assertThat(draftsFromPageStage).isCompletedWithValue(expectedResult);
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult 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);
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ResourceKeyIdGraphQlRequestTest method deserialize_WithEmptyResult_ShouldDeserializeCorrectly.
@Test
void deserialize_WithEmptyResult_ShouldDeserializeCorrectly() throws JsonProcessingException {
// preparation
String jsonAsString = "{\"data\":{\"categories\":{\"results\":[]}}}";
final HttpResponse httpResponse = HttpResponse.of(200, jsonAsString);
final ResourceKeyIdGraphQlRequest resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(singleton("key-1"), GraphQlQueryResources.CATEGORIES);
// test
final ResourceKeyIdGraphQlResult result = resourceKeyIdGraphQlRequest.deserialize(httpResponse);
// assertions
assertThat(result).isNotNull();
assertThat(result.getResults()).isEmpty();
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ResourceKeyIdGraphQlRequestTest method deserialize_WithEmptyResult_ShouldReturnNull.
@Test
void deserialize_WithEmptyResult_ShouldReturnNull() throws JsonProcessingException {
// preparation
final HttpResponse httpResponse = HttpResponse.of(200, "null");
final ResourceKeyIdGraphQlRequest resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(singleton("key-1"), GraphQlQueryResources.CATEGORIES);
// test
final ResourceKeyIdGraphQlResult result = resourceKeyIdGraphQlRequest.deserialize(httpResponse);
// assertions
assertThat(result).isNull();
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ResourceKeyIdGraphQlRequestTest method deserialize_WithSingleResult_ShouldReturnSingletonMap.
@Test
void deserialize_WithSingleResult_ShouldReturnSingletonMap() throws JsonProcessingException {
// preparation
String jsonAsString = "{\"data\":{\"categories\":{\"results\":[{\"id\":\"id-1\",\"key\":\"key-1\"}]}}}";
final HttpResponse httpResponse = HttpResponse.of(200, jsonAsString);
final ResourceKeyIdGraphQlRequest resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(singleton("key-1"), GraphQlQueryResources.CATEGORIES);
// test
final ResourceKeyIdGraphQlResult result = resourceKeyIdGraphQlRequest.deserialize(httpResponse);
// assertions
assertThat(result).isNotNull();
assertThat(result.getResults()).hasSize(1);
assertThat(result.getResults()).extracting("key").containsExactly("key-1");
assertThat(result.getResults()).extracting("id").containsExactly("id-1");
}
Aggregations