Search in sources :

Example 1 with ShoppingListTransformService

use of com.commercetools.sync.shoppinglists.service.ShoppingListTransformService in project commercetools-sync-java by commercetools.

the class ShoppingListTransformServiceImplTest method transform_ShoppingListReferences_ShouldResolveReferencesUsingCacheAndMapToShoppingListDraft.

@Test
void transform_ShoppingListReferences_ShouldResolveReferencesUsingCacheAndMapToShoppingListDraft() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
    final ShoppingListTransformService shoppingListTransformService = new ShoppingListTransformServiceImpl(sourceClient, referenceIdToKeyCache);
    final String customTypeId = UUID.randomUUID().toString();
    final String lineItemCustomTypeId = UUID.randomUUID().toString();
    final String textLineItemCustomTypeId = UUID.randomUUID().toString();
    final String customTypeKey = "customTypeKey";
    final String lineItemCustomTypeKey = "lineItemCustomTypeKey";
    final String textLineItemCustomTypeKey = "textLineItemCustomTypeKey";
    final String shoppingListKey = "shoppingListKeyValue";
    final String textLineItemName = "textLineItemName";
    final ProductVariant mockProductVariant = mock(ProductVariant.class);
    when(mockProductVariant.getSku()).thenReturn("variant-sku");
    final List<ShoppingList> mockShoppingListPage = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final ShoppingList mockShoppingList = mock(ShoppingList.class);
        final CustomFields mockCustomFields = mock(CustomFields.class);
        final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
        when(mockCustomFields.getType()).thenReturn(typeReference);
        when(mockShoppingList.getCustom()).thenReturn(mockCustomFields);
        when(mockShoppingList.getKey()).thenReturn(shoppingListKey);
        final CustomFields mockLineItemCustomFields = mock(CustomFields.class);
        final Reference<Type> lineItemTypeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", lineItemCustomTypeId);
        when(mockLineItemCustomFields.getType()).thenReturn(lineItemTypeReference);
        final LineItem mockLineItem = mock(LineItem.class);
        when(mockLineItem.getVariant()).thenReturn(mockProductVariant);
        when(mockLineItem.getCustom()).thenReturn(mockLineItemCustomFields);
        final CustomFields mockTextLineItemCustomFields = mock(CustomFields.class);
        final Reference<Type> textLineItemTypeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", textLineItemCustomTypeId);
        when(mockTextLineItemCustomFields.getType()).thenReturn(textLineItemTypeReference);
        final TextLineItem mockTextLineItem = mock(TextLineItem.class);
        when(mockTextLineItem.getName()).thenReturn(LocalizedString.ofEnglish(textLineItemName));
        when(mockTextLineItem.getCustom()).thenReturn(mockTextLineItemCustomFields);
        when(mockShoppingList.getLineItems()).thenReturn(Collections.singletonList(mockLineItem));
        when(mockShoppingList.getTextLineItems()).thenReturn(Collections.singletonList(mockTextLineItem));
        mockShoppingListPage.add(mockShoppingList);
    }
    final String jsonStringCustomTypes = "{\"results\":[{\"id\":\"" + customTypeId + "\"," + "\"key\":\"" + customTypeKey + "\"}, " + " {\"id\":\"" + lineItemCustomTypeId + "\"," + "\"key\":\"" + lineItemCustomTypeKey + "\"}, " + " {\"id\":\"" + textLineItemCustomTypeId + "\"," + "\"key\":\"" + textLineItemCustomTypeKey + "\"} ]}";
    final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
    when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(customTypesResult));
    // test
    final List<ShoppingListDraft> shoppingListDrafts = shoppingListTransformService.toShoppingListDrafts(mockShoppingListPage).toCompletableFuture().join();
    // assertions
    assertThat(shoppingListDrafts).hasSize(10);
    shoppingListDrafts.forEach(draft -> {
        assertThat(draft.getCustom().getType().getKey()).isEqualTo(customTypeKey);
        assertThat(draft.getLineItems()).containsExactly(LineItemDraftBuilder.ofSku("variant-sku", 0L).custom(CustomFieldsDraft.ofTypeKeyAndJson(lineItemCustomTypeKey, emptyMap())).build());
        assertThat(draft.getTextLineItems()).containsExactly(TextLineItemDraftBuilder.of(LocalizedString.ofEnglish(textLineItemName), 0L).custom(CustomFieldsDraft.ofTypeKeyAndJson(textLineItemCustomTypeKey, emptyMap())).build());
    });
}
Also used : ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) ShoppingListTransformService(com.commercetools.sync.shoppinglists.service.ShoppingListTransformService) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ArrayList(java.util.ArrayList) LineItem(io.sphere.sdk.shoppinglists.LineItem) TextLineItem(io.sphere.sdk.shoppinglists.TextLineItem) LocalizedString(io.sphere.sdk.models.LocalizedString) 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) TextLineItem(io.sphere.sdk.shoppinglists.TextLineItem) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) ProductVariant(io.sphere.sdk.products.ProductVariant) Test(org.junit.jupiter.api.Test)

Aggregations

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 ShoppingListTransformService (com.commercetools.sync.shoppinglists.service.ShoppingListTransformService)1 SphereClient (io.sphere.sdk.client.SphereClient)1 LocalizedString (io.sphere.sdk.models.LocalizedString)1 ProductVariant (io.sphere.sdk.products.ProductVariant)1 LineItem (io.sphere.sdk.shoppinglists.LineItem)1 ShoppingList (io.sphere.sdk.shoppinglists.ShoppingList)1 ShoppingListDraft (io.sphere.sdk.shoppinglists.ShoppingListDraft)1 TextLineItem (io.sphere.sdk.shoppinglists.TextLineItem)1 CustomFields (io.sphere.sdk.types.CustomFields)1 Type (io.sphere.sdk.types.Type)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1