Search in sources :

Example 31 with ResourceKeyIdGraphQlResult

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

Example 32 with ResourceKeyIdGraphQlResult

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");
        });
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) STAGED(io.sphere.sdk.products.ProductProjectionType.STAGED) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CustomObject(io.sphere.sdk.customobjects.CustomObject) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) KEY_IS_NOT_SET_PLACE_HOLDER(com.commercetools.sync.services.impl.BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) Collections.singletonList(java.util.Collections.singletonList) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) Arrays.asList(java.util.Arrays.asList) SphereClient(io.sphere.sdk.client.SphereClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) ProductTransformService(com.commercetools.sync.products.service.ProductTransformService) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Product(io.sphere.sdk.products.Product) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) CompletionStage(java.util.concurrent.CompletionStage) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ReferenceTransformException(com.commercetools.sync.commons.exceptions.ReferenceTransformException) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductTransformService(com.commercetools.sync.products.service.ProductTransformService) ProductDraft(io.sphere.sdk.products.ProductDraft) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) Test(org.junit.jupiter.api.Test)

Example 33 with ResourceKeyIdGraphQlResult

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"));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) STAGED(io.sphere.sdk.products.ProductProjectionType.STAGED) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CustomObject(io.sphere.sdk.customobjects.CustomObject) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) KEY_IS_NOT_SET_PLACE_HOLDER(com.commercetools.sync.services.impl.BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) Collections.singletonList(java.util.Collections.singletonList) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) Arrays.asList(java.util.Arrays.asList) SphereClient(io.sphere.sdk.client.SphereClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) ProductTransformService(com.commercetools.sync.products.service.ProductTransformService) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Product(io.sphere.sdk.products.Product) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) CompletionStage(java.util.concurrent.CompletionStage) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ReferenceTransformException(com.commercetools.sync.commons.exceptions.ReferenceTransformException) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductTransformService(com.commercetools.sync.products.service.ProductTransformService) ProductDraft(io.sphere.sdk.products.ProductDraft) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) Test(org.junit.jupiter.api.Test)

Example 34 with ResourceKeyIdGraphQlResult

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");
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Collections.emptySet(java.util.Collections.emptySet) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.when(org.mockito.Mockito.when) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) BeforeAll(org.junit.jupiter.api.BeforeAll) DEFAULT_PAGE_SIZE(io.sphere.sdk.queries.QueryExecutionUtils.DEFAULT_PAGE_SIZE) SphereClient(io.sphere.sdk.client.SphereClient) Mockito.mock(org.mockito.Mockito.mock) Collections.emptySet(java.util.Collections.emptySet) Set(java.util.Set) HashSet(java.util.HashSet) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ArrayList(java.util.ArrayList) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 35 with ResourceKeyIdGraphQlResult

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 } " + "}}\"}");
}
Also used : StringHttpRequestBody(io.sphere.sdk.http.StringHttpRequestBody) HttpRequestIntent(io.sphere.sdk.client.HttpRequestIntent) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) 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