Search in sources :

Example 1 with CartDiscountTransformService

use of com.commercetools.sync.cartdiscounts.service.CartDiscountTransformService 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)

Aggregations

CartDiscountTransformService (com.commercetools.sync.cartdiscounts.service.CartDiscountTransformService)1 ResourceIdsGraphQlRequest (com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest)1 ResourceKeyIdGraphQlResult (com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult)1 CaffeineReferenceIdToKeyCacheImpl (com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl)1 ReferenceIdToKeyCache (com.commercetools.sync.commons.utils.ReferenceIdToKeyCache)1 CartDiscount (io.sphere.sdk.cartdiscounts.CartDiscount)1 CartDiscountDraft (io.sphere.sdk.cartdiscounts.CartDiscountDraft)1 SphereClient (io.sphere.sdk.client.SphereClient)1 SphereJsonUtils (io.sphere.sdk.json.SphereJsonUtils)1 Reference (io.sphere.sdk.models.Reference)1 CustomFields (io.sphere.sdk.types.CustomFields)1 Type (io.sphere.sdk.types.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1