Search in sources :

Example 1 with InventoryEntryTransformService

use of com.commercetools.sync.inventories.service.InventoryEntryTransformService in project commercetools-sync-java by commercetools.

the class InventoryEntryTransformServiceImplTest method transform_InventoryReferences_ShouldResolveReferencesUsingCacheAndMapToInventoryEntryDraft.

@Test
void transform_InventoryReferences_ShouldResolveReferencesUsingCacheAndMapToInventoryEntryDraft() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
    final InventoryEntryTransformService inventoryEntryTransformService = new InventoryEntryTransformServiceImpl(sourceClient, referenceIdToKeyCache);
    final String customTypeId = UUID.randomUUID().toString();
    final String customTypeKey = "customTypeKey";
    final String inventoryEntrySku = "inventoryEntrySkuValue";
    final List<InventoryEntry> mockInventoryEntriesPage = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final InventoryEntry mockInventoryEntry = mock(InventoryEntry.class);
        final CustomFields mockCustomFields = mock(CustomFields.class);
        final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
        when(mockCustomFields.getType()).thenReturn(typeReference);
        when(mockInventoryEntry.getCustom()).thenReturn(mockCustomFields);
        when(mockInventoryEntry.getSku()).thenReturn(inventoryEntrySku);
        mockInventoryEntriesPage.add(mockInventoryEntry);
    }
    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<InventoryEntryDraft> inventoryEntryDraftsResolved = inventoryEntryTransformService.toInventoryEntryDrafts(mockInventoryEntriesPage).toCompletableFuture().join();
    // assertions
    final Optional<InventoryEntryDraft> inventoryEntryDraft1 = inventoryEntryDraftsResolved.stream().filter(inventoryEntryDraft -> inventoryEntrySku.equals(inventoryEntryDraft.getSku())).findFirst();
    assertThat(inventoryEntryDraft1).hasValueSatisfying(inventoryEntryDraft -> assertThat(inventoryEntryDraft.getCustom().getType().getKey()).isEqualTo(customTypeKey));
}
Also used : InventoryEntryTransformService(com.commercetools.sync.inventories.service.InventoryEntryTransformService) 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) InventoryEntryTransformService(com.commercetools.sync.inventories.service.InventoryEntryTransformService) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) 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) SphereClient(io.sphere.sdk.client.SphereClient) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) Type(io.sphere.sdk.types.Type) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) ArrayList(java.util.ArrayList) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) 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) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) 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 InventoryEntryTransformService (com.commercetools.sync.inventories.service.InventoryEntryTransformService)1 SphereClient (io.sphere.sdk.client.SphereClient)1 InventoryEntry (io.sphere.sdk.inventory.InventoryEntry)1 InventoryEntryDraft (io.sphere.sdk.inventory.InventoryEntryDraft)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