use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class CleanupUnresolvedReferenceCustomObjectsTest method cleanup_withDeleteDaysAfterLastModification_ShouldDeleteAndReturnCleanupStatistics.
@Test
void cleanup_withDeleteDaysAfterLastModification_ShouldDeleteAndReturnCleanupStatistics() {
final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(new HashSet<>(Arrays.asList(new ResourceKeyId("coKey1", "coId1"), new ResourceKeyId("coKey2", "coId2"))));
when(mockClient.execute(any(FetchCustomObjectsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(resourceKeyIdGraphQlResult));
final Throwable badRequestException = new BadRequestException("key is not valid");
when(mockClient.execute(any(CustomObjectDeleteCommand.class))).thenReturn(CompletableFutureUtils.failed(badRequestException)).thenReturn(CompletableFuture.completedFuture(mock(CustomObject.class)));
final CleanupUnresolvedReferenceCustomObjects.Statistics statistics = CleanupUnresolvedReferenceCustomObjects.of(mockClient).cleanup(deleteDaysAfterLastModification).join();
assertThat(statistics.getTotalDeleted()).isEqualTo(5);
assertThat(statistics.getTotalFailed()).isEqualTo(1);
assertThat(statistics.getReportMessage()).isEqualTo("Summary: 5 custom objects were deleted in total (1 failed to delete).");
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ProductTransformServiceImplTest method transform_ProductReferences_ShouldReplaceReferencesIdsWithKeysAndMapToProductDraft.
@Test
void transform_ProductReferences_ShouldReplaceReferencesIdsWithKeysAndMapToProductDraft() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ProductTransformService productTransformService = new ProductTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final List<ProductProjection> productPage = asList(readObjectFromResource("product-with-unresolved-references.json", Product.class).toProjection(STAGED));
String jsonStringProductTypes = "{\"results\":[{\"id\":\"cda0dbf7-b42e-40bf-8453-241d5b587f93\"," + "\"key\":\"productTypeKey\"}]}";
final ResourceKeyIdGraphQlResult productTypesResult = SphereJsonUtils.readObject(jsonStringProductTypes, ResourceKeyIdGraphQlResult.class);
String jsonStringState = "{\"results\":[{\"id\":\"ste95fb-2282-4f9a-8747-fbe440e02dcs0\"," + "\"key\":\"stateKey\"}]}";
final ResourceKeyIdGraphQlResult statesResult = SphereJsonUtils.readObject(jsonStringState, ResourceKeyIdGraphQlResult.class);
String jsonStringTaxCategory = "{\"results\":[{\"id\":\"ebbe95fb-2282-4f9a-8747-fbe440e02dc0\"," + "\"key\":\"taxCategoryKey\"}]}";
final ResourceKeyIdGraphQlResult taxCategoryResult = SphereJsonUtils.readObject(jsonStringTaxCategory, ResourceKeyIdGraphQlResult.class);
String jsonStringCustomerGroup = "{\"results\":[{\"id\":\"d1229e6f-2b79-441e-b419-180311e52754\"," + "\"key\":\"customerGroupKey\"}]}";
final ResourceKeyIdGraphQlResult customerGroupResult = SphereJsonUtils.readObject(jsonStringCustomerGroup, ResourceKeyIdGraphQlResult.class);
String jsonStringChannel = "{\"results\":[{\"id\":\"cdcf8bea-48f2-54bc-b3c2-cdc94bf94f2c\"," + "\"key\":\"channelKey\"}]}";
final ResourceKeyIdGraphQlResult channelResult = SphereJsonUtils.readObject(jsonStringChannel, ResourceKeyIdGraphQlResult.class);
String jsonStringCategories = "{\"results\":[{\"id\":\"1dfc8bea-84f2-45bc-b3c2-cdc94bf96f1f\",\"key\":\"categoryKey1\"}," + "{\"id\":\"2dfc8bea-84f2-45bc-b3c2-cdc94bf96f1f\",\"key\":\"categoryKey2\"}]}";
final ResourceKeyIdGraphQlResult categoriesResult = SphereJsonUtils.readObject(jsonStringCategories, ResourceKeyIdGraphQlResult.class);
String jsonStringCustomTypes = "{\"results\":[{\"id\":\"custom_type_id\"," + "\"key\":\"customTypeId\"}]}";
final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(productTypesResult)).thenReturn(CompletableFuture.completedFuture(categoriesResult)).thenReturn(CompletableFuture.completedFuture(statesResult)).thenReturn(CompletableFuture.completedFuture(channelResult)).thenReturn(CompletableFuture.completedFuture(taxCategoryResult)).thenReturn(CompletableFuture.completedFuture(customerGroupResult)).thenReturn(CompletableFuture.completedFuture(customTypesResult));
mockAttributeCustomObjectReference(sourceClient);
// test
final List<ProductDraft> productsResolved = productTransformService.toProductDrafts(productPage).toCompletableFuture().join();
// assertions
final Optional<ProductDraft> productKey1 = productsResolved.stream().filter(productDraft -> "productKeyResolved".equals(productDraft.getKey())).findFirst();
assertThat(productKey1).hasValueSatisfying(productDraft -> assertThat(productDraft.getMasterVariant().getPrices()).anySatisfy(priceDraft -> {
assertThat(priceDraft.getChannel().getKey()).isEqualTo("channelKey");
assertThat(priceDraft.getCustomerGroup().getKey()).isEqualTo("customerGroupKey");
}));
assertThat(productKey1).hasValueSatisfying(productDraft -> {
assertThat(productDraft.getProductType().getKey()).isEqualTo("productTypeKey");
assertThat(productDraft.getState().getKey()).isEqualTo("stateKey");
assertThat(productDraft.getTaxCategory().getKey()).isEqualTo("taxCategoryKey");
assertThat(productDraft.getCategories()).anySatisfy(categoryDraft -> {
assertThat(categoryDraft.getKey()).isEqualTo("categoryKey1");
});
});
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ProductTransformServiceImplTest method transform_ProductWithProductTypeReferencesWithNullKeyAlreadyInCache_ShouldFetchAndReplaceReferencesKeyValue.
@Test
void transform_ProductWithProductTypeReferencesWithNullKeyAlreadyInCache_ShouldFetchAndReplaceReferencesKeyValue() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
referenceIdToKeyCache.add("cda0dbf7-b42e-40bf-8453-241d5b587f93", KEY_IS_NOT_SET_PLACE_HOLDER);
final ProductTransformService productTransformService = new ProductTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final List<ProductProjection> productPage = asList(readObjectFromResource("product-with-unresolved-references.json", Product.class).toProjection(STAGED));
String jsonStringProductTypes = "{\"results\":[{\"id\":\"cda0dbf7-b42e-40bf-8453-241d5b587f93\"," + "\"key\":\"productTypeKey\"}]}";
final ResourceKeyIdGraphQlResult productTypesResult = SphereJsonUtils.readObject(jsonStringProductTypes, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(productTypesResult));
mockAttributeCustomObjectReference(sourceClient);
// test
final List<ProductDraft> productsResolved = productTransformService.toProductDrafts(productPage).toCompletableFuture().join();
// assertions
final Optional<ProductDraft> productKey1 = productsResolved.stream().filter(productDraft -> "productKeyResolved".equals(productDraft.getKey())).findFirst();
assertThat(productKey1).hasValueSatisfying(productDraft -> assertThat(productDraft.getProductType().getKey()).isEqualTo("productTypeKey"));
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class GraphQlQueryAllTest method run_WithConsumer_ShouldApplyConsumer.
@Test
void run_WithConsumer_ShouldApplyConsumer() {
// preparation
ResourceKeyId resource1 = new ResourceKeyId("key1", "id1");
ResourceKeyId resource2 = new ResourceKeyId("key2", "id2");
ResourceKeyId resource3 = new ResourceKeyId("key3", "id3");
ResourceKeyId resource4 = new ResourceKeyId("key4", "id4");
Set<ResourceKeyId> results = new HashSet<>();
results.add(resource1);
results.add(resource2);
results.add(resource3);
results.add(resource4);
when(pagedGraphQlQueryResult.getResults()).thenReturn(results);
final GraphQlQueryAll<ResourceKeyIdGraphQlResult, ResourceKeyId> query = GraphQlQueryAll.of(sphereClient, mock(ResourceKeyIdGraphQlRequest.class), DEFAULT_PAGE_SIZE);
final List<String> resourceIds = new ArrayList<>();
final Consumer<Set<ResourceKeyId>> resourceIdCollector = page -> page.forEach(resource -> resourceIds.add(resource.getId()));
// test
query.run(resourceIdCollector).toCompletableFuture().join();
// assertions
assertThat(resourceIds).hasSize(4);
assertThat(resourceIds).containsExactlyInAnyOrder("id2", "id1", "id3", "id4");
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ResourceKeyIdGraphQlRequestTest method httpRequestIntent_WithKeyAndExplicitLimit_ShouldReturnCorrectQueryString.
@Test
void httpRequestIntent_WithKeyAndExplicitLimit_ShouldReturnCorrectQueryString() {
// preparation
final GraphQlBaseRequest<ResourceKeyIdGraphQlResult> resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(singleton("key1"), GraphQlQueryResources.CATEGORIES).withLimit(10);
// test
final HttpRequestIntent httpRequestIntent = resourceKeyIdGraphQlRequest.httpRequestIntent();
// assertions
assertThat(httpRequestIntent.getBody()).isExactlyInstanceOf(StringHttpRequestBody.class);
assertThat(httpRequestIntent.getHttpMethod()).isEqualByComparingTo(HttpMethod.POST);
final StringHttpRequestBody requestBody = (StringHttpRequestBody) httpRequestIntent.getBody();
assertThat(requestBody).isNotNull();
assertThat(requestBody.getString()).isEqualTo("{\"query\": \"{categories(limit: 10, where: \\\"key" + " in (\\\\\\\"key1\\\\\\\")\\\", sort: [\\\"id asc\\\"]) { results { id key } " + "}}\"}");
}
Aggregations