Search in sources :

Example 1 with KEY_IS_NOT_SET_PLACE_HOLDER

use of com.commercetools.sync.services.impl.BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER 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 2 with KEY_IS_NOT_SET_PLACE_HOLDER

use of com.commercetools.sync.services.impl.BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER in project commercetools-sync-java by commercetools.

the class ProductTransformServiceImplTest method transform_ProductWithProductTypeReferencesWithNullKeyAlreadyInCache_ShouldFetchAndReplaceReferencesKeyValue.

@Test
void transform_ProductWithProductTypeReferencesWithNullKeyAlreadyInCache_ShouldFetchAndReplaceReferencesKeyValue() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    referenceIdToKeyCache.add("cda0dbf7-b42e-40bf-8453-241d5b587f93", KEY_IS_NOT_SET_PLACE_HOLDER);
    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).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("productTypeKey"));
}
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)

Aggregations

ReferenceTransformException (com.commercetools.sync.commons.exceptions.ReferenceTransformException)2 ResourceIdsGraphQlRequest (com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest)2 ResourceKeyIdGraphQlResult (com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult)2 CaffeineReferenceIdToKeyCacheImpl (com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl)2 ReferenceIdToKeyCache (com.commercetools.sync.commons.utils.ReferenceIdToKeyCache)2 ProductTransformService (com.commercetools.sync.products.service.ProductTransformService)2 KEY_IS_NOT_SET_PLACE_HOLDER (com.commercetools.sync.services.impl.BaseTransformServiceImpl.KEY_IS_NOT_SET_PLACE_HOLDER)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 BadGatewayException (io.sphere.sdk.client.BadGatewayException)2 SphereClient (io.sphere.sdk.client.SphereClient)2 CustomObject (io.sphere.sdk.customobjects.CustomObject)2 CustomObjectQuery (io.sphere.sdk.customobjects.queries.CustomObjectQuery)2 SphereJsonUtils (io.sphere.sdk.json.SphereJsonUtils)2 SphereJsonUtils.readObjectFromResource (io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource)2 Product (io.sphere.sdk.products.Product)2 ProductDraft (io.sphere.sdk.products.ProductDraft)2 ProductProjection (io.sphere.sdk.products.ProductProjection)2 STAGED (io.sphere.sdk.products.ProductProjectionType.STAGED)2 PagedQueryResult (io.sphere.sdk.queries.PagedQueryResult)2 CompletableFutureUtils (io.sphere.sdk.utils.CompletableFutureUtils)2