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