Search in sources :

Example 6 with ResourceKeyIdGraphQlResult

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

the class CartDiscountTransformServiceImplTest method transform_CartDiscountReferences_ShouldResolveReferencesUsingCacheAndMapToCartDiscountDraft.

@Test
void transform_CartDiscountReferences_ShouldResolveReferencesUsingCacheAndMapToCartDiscountDraft() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
    final CartDiscountTransformService cartDiscountTransformService = new CartDiscountTransformServiceImpl(sourceClient, referenceIdToKeyCache);
    final String cartDiscountKey = "cartDiscountKey";
    final String customTypeId = UUID.randomUUID().toString();
    final String customTypeKey = "customTypeKey";
    final List<CartDiscount> mockCartDiscountsPage = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final CartDiscount mockCartDiscount = mock(CartDiscount.class);
        final CustomFields mockCustomFields = mock(CustomFields.class);
        final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
        when(mockCustomFields.getType()).thenReturn(typeReference);
        when(mockCartDiscount.getCustom()).thenReturn(mockCustomFields);
        when(mockCartDiscount.getKey()).thenReturn(cartDiscountKey);
        mockCartDiscountsPage.add(mockCartDiscount);
    }
    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<CartDiscountDraft> cartDiscountsResolved = cartDiscountTransformService.toCartDiscountDrafts(mockCartDiscountsPage).toCompletableFuture().join();
    // assertions
    final Optional<CartDiscountDraft> cartDiscountKey1 = cartDiscountsResolved.stream().filter(cartDiscountDraft -> cartDiscountKey.equals(cartDiscountDraft.getKey())).findFirst();
    assertThat(cartDiscountKey1).hasValueSatisfying(cartDiscountDraft -> assertThat(cartDiscountDraft.getCustom().getType().getKey()).isEqualTo(customTypeKey));
}
Also used : 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) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) 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) CartDiscountTransformService(com.commercetools.sync.cartdiscounts.service.CartDiscountTransformService) SphereClient(io.sphere.sdk.client.SphereClient) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) Type(io.sphere.sdk.types.Type) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) CartDiscountDraft(io.sphere.sdk.cartdiscounts.CartDiscountDraft) ArrayList(java.util.ArrayList) CartDiscountTransformService(com.commercetools.sync.cartdiscounts.service.CartDiscountTransformService) 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) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) CartDiscount(io.sphere.sdk.cartdiscounts.CartDiscount) Test(org.junit.jupiter.api.Test)

Example 7 with ResourceKeyIdGraphQlResult

use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult 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 8 with ResourceKeyIdGraphQlResult

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

the class ResourceKeyIdGraphQlRequestTest method deserialize_WithMultipleResults_ShouldReturnCorrectResult.

@Test
void deserialize_WithMultipleResults_ShouldReturnCorrectResult() throws JsonProcessingException {
    // preparation
    String jsonAsString = "{\"data\":{\"categories\":{\"results\":[{\"id\":\"id-1\",\"key\":\"key-1\"}," + "{\"id\":\"id-2\",\"key\":\"key-2\"},{\"id\":\"id-3\",\"key\":\"key-3\"}]}}}";
    final HttpResponse httpResponse = HttpResponse.of(200, jsonAsString);
    final ResourceKeyIdGraphQlRequest resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(singleton("key-1"), GraphQlQueryResources.CATEGORIES);
    // test
    final ResourceKeyIdGraphQlResult result = resourceKeyIdGraphQlRequest.deserialize(httpResponse);
    // assertions
    assertThat(result).isNotNull();
    assertThat(result.getResults()).hasSize(3);
    assertThat(result.getResults()).extracting("key").containsExactlyInAnyOrder("key-1", "key-2", "key-3");
    assertThat(result.getResults()).extracting("id").containsExactlyInAnyOrder("id-1", "id-2", "id-3");
}
Also used : ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) HttpResponse(io.sphere.sdk.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 9 with ResourceKeyIdGraphQlResult

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

the class ResourceKeyIdGraphQlRequestTest method httpRequestIntent_WithKeyAndPredicate_ShouldReturnCorrectQueryString.

@Test
void httpRequestIntent_WithKeyAndPredicate_ShouldReturnCorrectQueryString() {
    // preparation
    final GraphQlBaseRequest<ResourceKeyIdGraphQlResult> resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(singleton("key1"), GraphQlQueryResources.CATEGORIES).withPredicate("id > \\\\\\\"id" + "\\\\\\\"");
    // 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: 500, where: \\\"key" + " in (\\\\\\\"key1\\\\\\\") AND id > \\\\\\\"id\\\\\\\"\\\", 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)

Example 10 with ResourceKeyIdGraphQlResult

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

the class CleanupUnresolvedReferenceCustomObjectsTest method cleanup_withNotFound404Exception_ShouldNotIncrementFailedCounter.

@Test
void cleanup_withNotFound404Exception_ShouldNotIncrementFailedCounter() {
    final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
    when(resourceKeyIdGraphQlResult.getResults()).thenReturn(Collections.singleton(new ResourceKeyId("coKey1", "coId1")));
    when(mockClient.execute(any(FetchCustomObjectsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(resourceKeyIdGraphQlResult));
    when(mockClient.execute(any(CustomObjectDeleteCommand.class))).thenReturn(CompletableFuture.completedFuture(mock(CustomObject.class))).thenReturn(CompletableFutureUtils.failed(new NotFoundException()));
    final CleanupUnresolvedReferenceCustomObjects.Statistics statistics = CleanupUnresolvedReferenceCustomObjects.of(mockClient).cleanup(deleteDaysAfterLastModification).join();
    assertThat(statistics.getTotalDeleted()).isEqualTo(1);
    assertThat(statistics.getTotalFailed()).isEqualTo(0);
    assertThat(statistics.getReportMessage()).isEqualTo("Summary: 1 custom objects were deleted in total (0 failed to delete).");
}
Also used : CustomObject(io.sphere.sdk.customobjects.CustomObject) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) NotFoundException(io.sphere.sdk.client.NotFoundException) FetchCustomObjectsGraphQlRequest(com.commercetools.sync.commons.models.FetchCustomObjectsGraphQlRequest) 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