Search in sources :

Example 1 with ResourceKeyIdGraphQlRequest

use of com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest in project commercetools-sync-java by commercetools.

the class ChunkUtilsTest method executeChunks_withGraphqlRequests_ShouldReturnResults.

@Test
void executeChunks_withGraphqlRequests_ShouldReturnResults() {
    final SphereClient client = mock(SphereClient.class);
    final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
    when(resourceKeyIdGraphQlResult.getResults()).thenReturn(new HashSet<>(Arrays.asList(new ResourceKeyId("coKey1", "coId1"), new ResourceKeyId("coKey2", "coId2"))));
    when(client.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(resourceKeyIdGraphQlResult));
    final ResourceKeyIdGraphQlRequest request = new ResourceKeyIdGraphQlRequest(singleton("key-1"), GraphQlQueryResources.CATEGORIES);
    final List<ResourceKeyIdGraphQlResult> results = ChunkUtils.executeChunks(client, asList(request, request, request)).join();
    assertThat(results).hasSize(3);
    final Set<ResourceKeyId> resourceKeyIds = ChunkUtils.flattenGraphQLBaseResults(results);
    assertThat(resourceKeyIds).hasSize(2);
}
Also used : 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)

Example 2 with ResourceKeyIdGraphQlRequest

use of com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest in project commercetools-sync-java by commercetools.

the class CtpQueryUtilsIT method queryAll_WithIdCollectorConsumerWithSinglePage_ShouldCollectIds.

@Test
void queryAll_WithIdCollectorConsumerWithSinglePage_ShouldCollectIds() {
    final int numberOfCategories = 100;
    List<CategoryDraft> categoryDrafts = getCategoryDraftsWithPrefix(Locale.ENGLISH, "new", null, numberOfCategories);
    List<Category> categories = createCategories(CTP_TARGET_CLIENT, categoryDrafts);
    Set<String> allCategoryKeys = categories.stream().map(category -> category.getKey()).collect(Collectors.toSet());
    final List<String> categoryIds = new ArrayList<>();
    final Consumer<Set<ResourceKeyId>> categoryPageConsumer = categoryPageResults -> categoryPageResults.forEach(category -> categoryIds.add(category.getId()));
    ResourceKeyIdGraphQlRequest resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(allCategoryKeys, GraphQlQueryResources.CATEGORIES);
    queryAll(CTP_TARGET_CLIENT, resourceKeyIdGraphQlRequest, categoryPageConsumer).toCompletableFuture().join();
    assertThat(categoryIds).hasSize(numberOfCategories);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) CtpQueryUtils.queryAll(com.commercetools.sync.commons.utils.CtpQueryUtils.queryAll) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ArrayList(java.util.ArrayList) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) Collections.singleton(java.util.Collections.singleton) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) GraphQlQueryResources(com.commercetools.sync.commons.models.GraphQlQueryResources) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes) CategoryITUtils.deleteAllCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) CategoryITUtils.getCategoryDraftsWithPrefix(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDraftsWithPrefix) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) Category(io.sphere.sdk.categories.Category) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) Test(org.junit.jupiter.api.Test)

Example 3 with ResourceKeyIdGraphQlRequest

use of com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest 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)

Example 4 with ResourceKeyIdGraphQlRequest

use of com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest in project commercetools-sync-java by commercetools.

the class CtpQueryUtilsIT method queryAll_WithIdCollectorConsumerWithMultiplePages_ShouldCollectIds.

@Test
void queryAll_WithIdCollectorConsumerWithMultiplePages_ShouldCollectIds() {
    final int numberOfCategories = 600;
    final List<CategoryDraft> categoryDrafts = getCategoryDraftsWithPrefix(Locale.ENGLISH, "new", null, numberOfCategories);
    List<Category> categories = createCategories(CTP_TARGET_CLIENT, categoryDrafts);
    Set<String> allCategoryKeys = categories.stream().map(category -> category.getKey()).collect(Collectors.toSet());
    final List<String> categoryIds = new ArrayList<>();
    final Consumer<Set<ResourceKeyId>> categoryPageConsumer = categoryPageResults -> categoryPageResults.forEach(category -> categoryIds.add(category.getId()));
    ResourceKeyIdGraphQlRequest resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(allCategoryKeys, GraphQlQueryResources.CATEGORIES);
    queryAll(CTP_TARGET_CLIENT, resourceKeyIdGraphQlRequest, categoryPageConsumer).toCompletableFuture().join();
    assertThat(categoryIds).hasSize(numberOfCategories);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) CtpQueryUtils.queryAll(com.commercetools.sync.commons.utils.CtpQueryUtils.queryAll) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ArrayList(java.util.ArrayList) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) Collections.singleton(java.util.Collections.singleton) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) GraphQlQueryResources(com.commercetools.sync.commons.models.GraphQlQueryResources) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes) CategoryITUtils.deleteAllCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) CategoryITUtils.getCategoryDraftsWithPrefix(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDraftsWithPrefix) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) Category(io.sphere.sdk.categories.Category) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) Test(org.junit.jupiter.api.Test)

Example 5 with ResourceKeyIdGraphQlRequest

use of com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest in project commercetools-sync-java by commercetools.

the class CtpQueryUtilsIT method queryAll_WithIdCollectorConsumerWithRequestedKeySubset_ShouldCollectIds.

@Test
void queryAll_WithIdCollectorConsumerWithRequestedKeySubset_ShouldCollectIds() {
    final List<CategoryDraft> categoryDrafts = getCategoryDraftsWithPrefix(Locale.ENGLISH, "new", null, 5);
    List<Category> categories = createCategories(CTP_TARGET_CLIENT, categoryDrafts);
    final List<String> categoryIds = new ArrayList<>();
    final Consumer<Set<ResourceKeyId>> categoryPageConsumer = categoryPageResults -> categoryPageResults.forEach(category -> categoryIds.add(category.getId()));
    ResourceKeyIdGraphQlRequest resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(singleton(categories.get(0).getKey()), GraphQlQueryResources.CATEGORIES);
    queryAll(CTP_TARGET_CLIENT, resourceKeyIdGraphQlRequest, categoryPageConsumer).toCompletableFuture().join();
    assertThat(categoryIds).hasSize(1);
    assertThat(categoryIds).containsExactly(categories.get(0).getId());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) CtpQueryUtils.queryAll(com.commercetools.sync.commons.utils.CtpQueryUtils.queryAll) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ArrayList(java.util.ArrayList) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) Collections.singleton(java.util.Collections.singleton) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) GraphQlQueryResources(com.commercetools.sync.commons.models.GraphQlQueryResources) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes) CategoryITUtils.deleteAllCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) CategoryITUtils.getCategoryDraftsWithPrefix(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDraftsWithPrefix) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) Category(io.sphere.sdk.categories.Category) HashSet(java.util.HashSet) Set(java.util.Set) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ArrayList(java.util.ArrayList) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceKeyIdGraphQlRequest (com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest)5 ResourceKeyId (com.commercetools.sync.commons.models.ResourceKeyId)5 Test (org.junit.jupiter.api.Test)5 GraphQlQueryResources (com.commercetools.sync.commons.models.GraphQlQueryResources)4 CtpQueryUtils.queryAll (com.commercetools.sync.commons.utils.CtpQueryUtils.queryAll)4 Category (io.sphere.sdk.categories.Category)4 CategoryQuery (io.sphere.sdk.categories.queries.CategoryQuery)4 List (java.util.List)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 OLD_CATEGORY_CUSTOM_TYPE_KEY (com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY)3 CategoryITUtils.createCategories (com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories)3 CategoryITUtils.createCategoriesCustomType (com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType)3 CategoryITUtils.deleteAllCategories (com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories)3 CategoryITUtils.getCategoryDraftsWithPrefix (com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDraftsWithPrefix)3 ITUtils.deleteTypes (com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes)3 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)3 CategoryDraft (io.sphere.sdk.categories.CategoryDraft)3