Search in sources :

Example 16 with ResourceKeyIdGraphQlResult

use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.

the class ShoppingListTransformServiceImplTest method transform_ShoppingListReferences_ShouldResolveReferencesUsingCacheAndMapToShoppingListDraft.

@Test
void transform_ShoppingListReferences_ShouldResolveReferencesUsingCacheAndMapToShoppingListDraft() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
    final ShoppingListTransformService shoppingListTransformService = new ShoppingListTransformServiceImpl(sourceClient, referenceIdToKeyCache);
    final String customTypeId = UUID.randomUUID().toString();
    final String lineItemCustomTypeId = UUID.randomUUID().toString();
    final String textLineItemCustomTypeId = UUID.randomUUID().toString();
    final String customTypeKey = "customTypeKey";
    final String lineItemCustomTypeKey = "lineItemCustomTypeKey";
    final String textLineItemCustomTypeKey = "textLineItemCustomTypeKey";
    final String shoppingListKey = "shoppingListKeyValue";
    final String textLineItemName = "textLineItemName";
    final ProductVariant mockProductVariant = mock(ProductVariant.class);
    when(mockProductVariant.getSku()).thenReturn("variant-sku");
    final List<ShoppingList> mockShoppingListPage = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final ShoppingList mockShoppingList = mock(ShoppingList.class);
        final CustomFields mockCustomFields = mock(CustomFields.class);
        final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
        when(mockCustomFields.getType()).thenReturn(typeReference);
        when(mockShoppingList.getCustom()).thenReturn(mockCustomFields);
        when(mockShoppingList.getKey()).thenReturn(shoppingListKey);
        final CustomFields mockLineItemCustomFields = mock(CustomFields.class);
        final Reference<Type> lineItemTypeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", lineItemCustomTypeId);
        when(mockLineItemCustomFields.getType()).thenReturn(lineItemTypeReference);
        final LineItem mockLineItem = mock(LineItem.class);
        when(mockLineItem.getVariant()).thenReturn(mockProductVariant);
        when(mockLineItem.getCustom()).thenReturn(mockLineItemCustomFields);
        final CustomFields mockTextLineItemCustomFields = mock(CustomFields.class);
        final Reference<Type> textLineItemTypeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", textLineItemCustomTypeId);
        when(mockTextLineItemCustomFields.getType()).thenReturn(textLineItemTypeReference);
        final TextLineItem mockTextLineItem = mock(TextLineItem.class);
        when(mockTextLineItem.getName()).thenReturn(LocalizedString.ofEnglish(textLineItemName));
        when(mockTextLineItem.getCustom()).thenReturn(mockTextLineItemCustomFields);
        when(mockShoppingList.getLineItems()).thenReturn(Collections.singletonList(mockLineItem));
        when(mockShoppingList.getTextLineItems()).thenReturn(Collections.singletonList(mockTextLineItem));
        mockShoppingListPage.add(mockShoppingList);
    }
    final String jsonStringCustomTypes = "{\"results\":[{\"id\":\"" + customTypeId + "\"," + "\"key\":\"" + customTypeKey + "\"}, " + " {\"id\":\"" + lineItemCustomTypeId + "\"," + "\"key\":\"" + lineItemCustomTypeKey + "\"}, " + " {\"id\":\"" + textLineItemCustomTypeId + "\"," + "\"key\":\"" + textLineItemCustomTypeKey + "\"} ]}";
    final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
    when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(customTypesResult));
    // test
    final List<ShoppingListDraft> shoppingListDrafts = shoppingListTransformService.toShoppingListDrafts(mockShoppingListPage).toCompletableFuture().join();
    // assertions
    assertThat(shoppingListDrafts).hasSize(10);
    shoppingListDrafts.forEach(draft -> {
        assertThat(draft.getCustom().getType().getKey()).isEqualTo(customTypeKey);
        assertThat(draft.getLineItems()).containsExactly(LineItemDraftBuilder.ofSku("variant-sku", 0L).custom(CustomFieldsDraft.ofTypeKeyAndJson(lineItemCustomTypeKey, emptyMap())).build());
        assertThat(draft.getTextLineItems()).containsExactly(TextLineItemDraftBuilder.of(LocalizedString.ofEnglish(textLineItemName), 0L).custom(CustomFieldsDraft.ofTypeKeyAndJson(textLineItemCustomTypeKey, emptyMap())).build());
    });
}
Also used : ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) ShoppingListTransformService(com.commercetools.sync.shoppinglists.service.ShoppingListTransformService) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ArrayList(java.util.ArrayList) LineItem(io.sphere.sdk.shoppinglists.LineItem) TextLineItem(io.sphere.sdk.shoppinglists.TextLineItem) LocalizedString(io.sphere.sdk.models.LocalizedString) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) CustomFields(io.sphere.sdk.types.CustomFields) Type(io.sphere.sdk.types.Type) SphereClient(io.sphere.sdk.client.SphereClient) TextLineItem(io.sphere.sdk.shoppinglists.TextLineItem) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) ProductVariant(io.sphere.sdk.products.ProductVariant) Test(org.junit.jupiter.api.Test)

Example 17 with ResourceKeyIdGraphQlResult

use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.

the class BaseServiceImplTest method cacheKeysToIdsUsingGraphQl_WithBadGateWayException_ShouldCompleteExceptionally.

@Test
void cacheKeysToIdsUsingGraphQl_WithBadGateWayException_ShouldCompleteExceptionally() {
    // preparation
    final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
    final ResourceKeyId mockResourceKeyId = mock(ResourceKeyId.class);
    final String key = "testKey";
    final String id = "testId";
    when(mockResourceKeyId.getKey()).thenReturn(key);
    when(mockResourceKeyId.getId()).thenReturn(id);
    when(resourceKeyIdGraphQlResult.getResults()).thenReturn(singleton(mockResourceKeyId));
    when(client.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException()));
    // test
    final CompletionStage<Map<String, String>> result = service.cacheKeysToIds(singleton("testKey"));
    // assertions
    assertThat(result).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
    verify(client, times(1)).execute(any(ResourceKeyIdGraphQlRequest.class));
}
Also used : ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with ResourceKeyIdGraphQlResult

use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.

the class BaseServiceImplTest method cacheKeysToIdsUsingGraphQl_With500Keys_ShouldChunkAndMakeRequestAndReturnCachedEntry.

@Test
void cacheKeysToIdsUsingGraphQl_With500Keys_ShouldChunkAndMakeRequestAndReturnCachedEntry() {
    // preparation
    Set<String> randomKeys = new HashSet<>();
    IntStream.range(0, 500).forEach(ignore -> randomKeys.add(RandomStringUtils.random(15)));
    final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
    final ResourceKeyId mockResourceKeyId = mock(ResourceKeyId.class);
    final String key = randomKeys.stream().findFirst().get();
    final String id = "testId";
    when(mockResourceKeyId.getKey()).thenReturn(key);
    when(mockResourceKeyId.getId()).thenReturn(id);
    when(resourceKeyIdGraphQlResult.getResults()).thenReturn(singleton(mockResourceKeyId));
    when(client.execute(any())).thenReturn(completedFuture(resourceKeyIdGraphQlResult));
    // test
    final Map<String, String> optional = service.cacheKeysToIds(randomKeys).toCompletableFuture().join();
    // assertions
    assertThat(optional).containsExactly(MapEntry.entry(key, id));
    verify(client, times(2)).execute(any(ResourceKeyIdGraphQlRequest.class));
}
Also used : ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with ResourceKeyIdGraphQlResult

use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.

the class InventoryEntryTransformServiceImplTest method transform_InventoryReferences_ShouldResolveReferencesUsingCacheAndMapToInventoryEntryDraft.

@Test
void transform_InventoryReferences_ShouldResolveReferencesUsingCacheAndMapToInventoryEntryDraft() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
    final InventoryEntryTransformService inventoryEntryTransformService = new InventoryEntryTransformServiceImpl(sourceClient, referenceIdToKeyCache);
    final String customTypeId = UUID.randomUUID().toString();
    final String customTypeKey = "customTypeKey";
    final String inventoryEntrySku = "inventoryEntrySkuValue";
    final List<InventoryEntry> mockInventoryEntriesPage = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final InventoryEntry mockInventoryEntry = mock(InventoryEntry.class);
        final CustomFields mockCustomFields = mock(CustomFields.class);
        final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
        when(mockCustomFields.getType()).thenReturn(typeReference);
        when(mockInventoryEntry.getCustom()).thenReturn(mockCustomFields);
        when(mockInventoryEntry.getSku()).thenReturn(inventoryEntrySku);
        mockInventoryEntriesPage.add(mockInventoryEntry);
    }
    final String jsonStringCustomTypes = "{\"results\":[{\"id\":\"" + customTypeId + "\"," + "\"key\":\"" + customTypeKey + "\"}]}";
    final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
    when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(customTypesResult));
    // test
    final List<InventoryEntryDraft> inventoryEntryDraftsResolved = inventoryEntryTransformService.toInventoryEntryDrafts(mockInventoryEntriesPage).toCompletableFuture().join();
    // assertions
    final Optional<InventoryEntryDraft> inventoryEntryDraft1 = inventoryEntryDraftsResolved.stream().filter(inventoryEntryDraft -> inventoryEntrySku.equals(inventoryEntryDraft.getSku())).findFirst();
    assertThat(inventoryEntryDraft1).hasValueSatisfying(inventoryEntryDraft -> assertThat(inventoryEntryDraft.getCustom().getType().getKey()).isEqualTo(customTypeKey));
}
Also used : InventoryEntryTransformService(com.commercetools.sync.inventories.service.InventoryEntryTransformService) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) CustomFields(io.sphere.sdk.types.CustomFields) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) Reference(io.sphere.sdk.models.Reference) InventoryEntryTransformService(com.commercetools.sync.inventories.service.InventoryEntryTransformService) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) List(java.util.List) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) SphereClient(io.sphere.sdk.client.SphereClient) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) Type(io.sphere.sdk.types.Type) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) ArrayList(java.util.ArrayList) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) CustomFields(io.sphere.sdk.types.CustomFields) Type(io.sphere.sdk.types.Type) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Test(org.junit.jupiter.api.Test)

Example 20 with ResourceKeyIdGraphQlResult

use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.

the class CtpQueryUtilsTest method queryAll_WithGraphQlRequest_ShouldFetchPagedResult.

@Test
void queryAll_WithGraphQlRequest_ShouldFetchPagedResult() {
    // preparation
    final SphereClient sphereClient = mock(SphereClient.class);
    final List<ResourceKeyId> resultPage = IntStream.range(0, 500).mapToObj(i -> mock(ResourceKeyId.class)).collect(Collectors.toList());
    final ResourceKeyId lastCategory = resultPage.get(resultPage.size() - 1);
    when(lastCategory.getId()).thenReturn(UUID.randomUUID().toString());
    final Set<ResourceKeyId> mockPage = resultPage.stream().collect(Collectors.toSet());
    final ResourceKeyIdGraphQlResult pagedResourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
    when(pagedResourceKeyIdGraphQlResult.getResults()).thenReturn(mockPage).thenReturn(mockPage).thenReturn(mockPage).thenReturn(mockPage).thenReturn(mockPage.stream().limit(10).collect(Collectors.toSet()));
    when(sphereClient.execute(any())).thenReturn(completedFuture(pagedResourceKeyIdGraphQlResult));
    final Set<String> keysToQuery = IntStream.range(1, 2010).mapToObj(i -> "key" + i).collect(Collectors.toSet());
    ResourceKeyIdGraphQlRequest resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(keysToQuery, GraphQlQueryResources.CATEGORIES);
    // test
    queryAll(sphereClient, resourceKeyIdGraphQlRequest, results -> identity()).toCompletableFuture().join();
    // assertions
    verify(sphereClient, times(5)).execute(graphQlRequestArgumentCaptor.capture());
    assertThat(graphQlRequestArgumentCaptor.getAllValues()).containsExactly(resourceKeyIdGraphQlRequest.withLimit(500), resourceKeyIdGraphQlRequest.withLimit(500).withPredicate(format("id > \\\\\\\"%s\\\\\\\"", "id")), resourceKeyIdGraphQlRequest.withLimit(500).withPredicate(format("id > \\\\\\\"%s\\\\\\\"", "id")), resourceKeyIdGraphQlRequest.withLimit(500).withPredicate(format("id > \\\\\\\"%s\\\\\\\"", "id")), resourceKeyIdGraphQlRequest.withLimit(500).withPredicate(format("id > \\\\\\\"%s\\\\\\\"", "id")));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) CtpQueryUtils.queryAll(com.commercetools.sync.commons.utils.CtpQueryUtils.queryAll) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Function(java.util.function.Function) Captor(org.mockito.Captor) GraphQlBaseRequest(com.commercetools.sync.commons.models.GraphQlBaseRequest) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) MockitoAnnotations(org.mockito.MockitoAnnotations) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) SphereClient(io.sphere.sdk.client.SphereClient) QuerySort(io.sphere.sdk.queries.QuerySort) GraphQlQueryResources(com.commercetools.sync.commons.models.GraphQlQueryResources) Nonnull(javax.annotation.Nonnull) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Category(io.sphere.sdk.categories.Category) QueryPredicate(io.sphere.sdk.queries.QueryPredicate) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) CategoryQueryModel(io.sphere.sdk.categories.queries.CategoryQueryModel) List(java.util.List) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) Function.identity(java.util.function.Function.identity) Mockito.mock(org.mockito.Mockito.mock) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) 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