Search in sources :

Example 26 with ResourceKeyIdGraphQlResult

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);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) TestLoggerFactory(uk.org.lidalia.slf4jtest.TestLoggerFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.mockResourceIdsGraphQlRequest(com.commercetools.project.sync.util.TestUtils.mockResourceIdsGraphQlRequest) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) CompletableFuture(java.util.concurrent.CompletableFuture) InventoryTransformUtils.toInventoryEntryDrafts(com.commercetools.sync.inventories.utils.InventoryTransformUtils.toInventoryEntryDrafts) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) Arrays.asList(java.util.Arrays.asList) TestLogger(uk.org.lidalia.slf4jtest.TestLogger) SphereClient(io.sphere.sdk.client.SphereClient) InventoryEntryQuery(io.sphere.sdk.inventory.queries.InventoryEntryQuery) LoggingEvent(uk.org.lidalia.slf4jtest.LoggingEvent) TestUtils.getMockedClock(com.commercetools.project.sync.util.TestUtils.getMockedClock) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) SphereApiConfig(io.sphere.sdk.client.SphereApiConfig) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) Mockito.when(org.mockito.Mockito.when) InventorySync(com.commercetools.sync.inventories.InventorySync) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) Clock(java.time.Clock) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 27 with ResourceKeyIdGraphQlResult

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

Example 28 with ResourceKeyIdGraphQlResult

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();
}
Also used : ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) HttpResponse(io.sphere.sdk.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 29 with ResourceKeyIdGraphQlResult

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();
}
Also used : ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) HttpResponse(io.sphere.sdk.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 30 with ResourceKeyIdGraphQlResult

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");
}
Also used : ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) HttpResponse(io.sphere.sdk.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceKeyIdGraphQlResult (com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult)36 Test (org.junit.jupiter.api.Test)35 SphereClient (io.sphere.sdk.client.SphereClient)22 List (java.util.List)18 ResourceIdsGraphQlRequest (com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)16 Mockito.mock (org.mockito.Mockito.mock)16 Mockito.when (org.mockito.Mockito.when)16 CompletableFuture (java.util.concurrent.CompletableFuture)15 ResourceKeyId (com.commercetools.sync.commons.models.ResourceKeyId)14 SphereJsonUtils (io.sphere.sdk.json.SphereJsonUtils)13 CaffeineReferenceIdToKeyCacheImpl (com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl)12 ReferenceIdToKeyCache (com.commercetools.sync.commons.utils.ReferenceIdToKeyCache)12 ResourceKeyIdGraphQlRequest (com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest)11 Arrays.asList (java.util.Arrays.asList)11 Optional (java.util.Optional)11 CompletionStage (java.util.concurrent.CompletionStage)11 SphereJsonUtils.readObjectFromResource (io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource)10 PagedQueryResult (io.sphere.sdk.queries.PagedQueryResult)10