Search in sources :

Example 16 with ReferenceIdToKeyCache

use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.

the class ProductTransformServiceImplTest method transform_WithNonCachedCustomObjectAttributeReference_ShouldFetchAndTransformProduct.

@Test
void transform_WithNonCachedCustomObjectAttributeReference_ShouldFetchAndTransformProduct() {
    // 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);
    when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(productTypesResult));
    mockAttributeCustomObjectReference(sourceClient);
    // test
    final List<ProductDraft> productsResolved = productTransformService.toProductDrafts(productPage).join();
    final Optional<ProductDraft> productKey1 = productsResolved.stream().filter(productDraft -> "productKeyResolved".equals(productDraft.getKey())).findFirst();
    assertThat(productKey1).hasValueSatisfying(product -> assertThat(product.getMasterVariant().getAttributes()).anySatisfy(attribute -> {
        assertThat(attribute.getName()).isEqualTo("customObjectReference");
    }));
    verify(sourceClient, times(1)).execute(any(CustomObjectQuery.class));
}
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) ProductTransformService(com.commercetools.sync.products.service.ProductTransformService) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraft(io.sphere.sdk.products.ProductDraft) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) Test(org.junit.jupiter.api.Test)

Example 17 with ReferenceIdToKeyCache

use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.

the class ProductTransformServiceImplTest method transform_ProductWithProductTypeReferencesWithNullKey_ShouldReplaceReferencesKeyValueWithPlaceHolder.

@Test
void transform_ProductWithProductTypeReferencesWithNullKey_ShouldReplaceReferencesKeyValueWithPlaceHolder() {
    // 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\":" + null + "}]}";
    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(KEY_IS_NOT_SET_PLACE_HOLDER));
}
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 18 with ReferenceIdToKeyCache

use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache 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)

Example 19 with ReferenceIdToKeyCache

use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.

the class StateSyncIT method sync_WithExceptionOnResolvingTransition_ShouldUpdateTransitions.

@Test
void sync_WithExceptionOnResolvingTransition_ShouldUpdateTransitions() {
    final StateDraft stateCDraft = createStateDraft(keyC);
    final State stateC = createStateInSource(stateCDraft);
    final StateDraft stateBDraft = createStateDraft(keyB, stateC);
    final State stateB = createStateInSource(stateBDraft);
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    when(spyClient.execute(any())).thenCallRealMethod().thenReturn(exceptionallyCompletedFuture(new BadGatewayException()));
    final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(spyClient).batchSize(3).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).warningCallback((exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage())).build();
    final StateSync stateSync = new StateSync(stateSyncOptions);
    final List<StateDraft> stateDrafts = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, Arrays.asList(stateB, stateC)).join();
    // test
    final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
    assertThat(stateSyncStatistics).hasValues(2, 0, 0, 2, 0);
    Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
    Assertions.assertThat(errorCallBackMessages).isNotEmpty();
    Assertions.assertThat(errorCallBackMessages.get(0)).contains("Failed to fetch existing states with keys");
    Assertions.assertThat(warningCallBackMessages).isEmpty();
}
Also used : CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) Arrays(java.util.Arrays) Reference(io.sphere.sdk.models.Reference) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) StateTransformUtils(com.commercetools.sync.states.utils.StateTransformUtils) StateCreateCommand(io.sphere.sdk.states.commands.StateCreateCommand) Arrays.asList(java.util.Arrays.asList) StateITUtils.getStateByKey(com.commercetools.sync.integration.commons.utils.StateITUtils.getStateByKey) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) StateType(io.sphere.sdk.states.StateType) Assertions(org.assertj.core.api.Assertions) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ThreadLocalRandom.current(java.util.concurrent.ThreadLocalRandom.current) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) StateUpdateCommand(io.sphere.sdk.states.commands.StateUpdateCommand) Set(java.util.Set) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) State(io.sphere.sdk.states.State) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) StateITUtils.deleteStatesFromTargetAndSource(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStatesFromTargetAndSource) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) List(java.util.List) CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) CompletionStage(java.util.concurrent.CompletionStage) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) Optional(java.util.Optional) StateDraft(io.sphere.sdk.states.StateDraft) StateITUtils.deleteStates(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStates) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) StateQuery(io.sphere.sdk.states.queries.StateQuery) BadRequestException(io.sphere.sdk.client.BadRequestException) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) Mockito.spy(org.mockito.Mockito.spy) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) State.referenceOfId(io.sphere.sdk.states.State.referenceOfId) StateReferenceResolver(com.commercetools.sync.states.helpers.StateReferenceResolver) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) StateRole(io.sphere.sdk.states.StateRole) Nonnull(javax.annotation.Nonnull) StateDraftBuilder(io.sphere.sdk.states.StateDraftBuilder) StateQueryBuilder(io.sphere.sdk.states.queries.StateQueryBuilder) StateSyncOptionsBuilder(com.commercetools.sync.states.StateSyncOptionsBuilder) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) StateSync(com.commercetools.sync.states.StateSync) Mockito.when(org.mockito.Mockito.when) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) CtpQueryUtils(com.commercetools.sync.commons.utils.CtpQueryUtils) WaitingToBeResolvedTransitions(com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Collections(java.util.Collections) StateSync(com.commercetools.sync.states.StateSync) StateDraft(io.sphere.sdk.states.StateDraft) State(io.sphere.sdk.states.State) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) SphereClient(io.sphere.sdk.client.SphereClient) BadGatewayException(io.sphere.sdk.client.BadGatewayException) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Test(org.junit.jupiter.api.Test)

Example 20 with ReferenceIdToKeyCache

use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache 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

CaffeineReferenceIdToKeyCacheImpl (com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl)35 ReferenceIdToKeyCache (com.commercetools.sync.commons.utils.ReferenceIdToKeyCache)35 Test (org.junit.jupiter.api.Test)35 List (java.util.List)34 ArrayList (java.util.ArrayList)26 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)26 BeforeEach (org.junit.jupiter.api.BeforeEach)24 CTP_SOURCE_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT)22 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)22 SphereClient (io.sphere.sdk.client.SphereClient)22 AfterAll (org.junit.jupiter.api.AfterAll)21 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)20 Collectors (java.util.stream.Collectors)20 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)20 Mockito.when (org.mockito.Mockito.when)20 Arrays.asList (java.util.Arrays.asList)19 Collections.singletonList (java.util.Collections.singletonList)18 Collections (java.util.Collections)17 Optional (java.util.Optional)17 CompletionStage (java.util.concurrent.CompletionStage)17