Search in sources :

Example 1 with ProductTypeTransformService

use of com.commercetools.sync.producttypes.service.ProductTypeTransformService in project commercetools-sync-java by commercetools.

the class ProductTypeTransformServiceImplTest method transform_ShouldReplaceProductTypeNestedAttributeReferenceIdsWithKeys.

@Test
void transform_ShouldReplaceProductTypeNestedAttributeReferenceIdsWithKeys() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    final ProductTypeTransformService productTypeTransformService = new ProductTypeTransformServiceImpl(sourceClient, referenceIdToKeyCache);
    final String referencedProductTypeId = UUID.randomUUID().toString();
    final String referencedProductTypeKey = "referencedProductTypeKey";
    final ProductType referencedProductType = mock(ProductType.class);
    when(referencedProductType.getKey()).thenReturn(referencedProductTypeKey);
    final Reference<ProductType> productTypeReference = spy(ProductType.reference(referencedProductType));
    when(productTypeReference.getId()).thenReturn(referencedProductTypeId);
    final AttributeDefinition nestedTypeAttr = AttributeDefinitionBuilder.of("nestedattr", ofEnglish("nestedattr"), SetAttributeType.of(NestedAttributeType.of(productTypeReference))).build();
    final ProductType productType = mock(ProductType.class);
    when(productType.getKey()).thenReturn("withNestedTypeAttr");
    when(productType.getAttributes()).thenReturn(singletonList(nestedTypeAttr));
    final List<ProductType> productTypes = singletonList(productType);
    String jsonStringProductTypes = "{\"results\":[{\"id\":\"" + referencedProductTypeId + "\"," + "\"key\":\"" + referencedProductTypeKey + "\"}]}";
    final ResourceKeyIdGraphQlResult productTypesResult = SphereJsonUtils.readObject(jsonStringProductTypes, ResourceKeyIdGraphQlResult.class);
    when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(productTypesResult));
    // test
    final List<ProductTypeDraft> productTypeDrafts = productTypeTransformService.toProductTypeDrafts(productTypes).join();
    // assertion
    assertThat(productTypeDrafts).satisfies(productTypeDraft -> {
        final SetAttributeType setAttributeType = (SetAttributeType) productTypeDraft.get(0).getAttributes().get(0).getAttributeType();
        final NestedAttributeType nestedAttributeType = (NestedAttributeType) setAttributeType.getElementType();
        assertThat(nestedAttributeType.getTypeReference().getId()).isEqualTo(referencedProductType.getKey());
    });
}
Also used : ProductTypeTransformService(com.commercetools.sync.producttypes.service.ProductTypeTransformService) ProductType(io.sphere.sdk.producttypes.ProductType) AttributeDefinition(io.sphere.sdk.products.attributes.AttributeDefinition) NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) SetAttributeType(io.sphere.sdk.products.attributes.SetAttributeType) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Test(org.junit.jupiter.api.Test)

Example 2 with ProductTypeTransformService

use of com.commercetools.sync.producttypes.service.ProductTypeTransformService in project commercetools-sync-java by commercetools.

the class ProductTypeTransformServiceImplTest method mapToProductDrafts_WithProductTypeWithNoAttributeDefs_ShouldReturnProductType.

@Test
void mapToProductDrafts_WithProductTypeWithNoAttributeDefs_ShouldReturnProductType() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    final ProductTypeTransformService productTypeTransformService = new ProductTypeTransformServiceImpl(sourceClient, referenceIdToKeyCache);
    final ProductType productTypeFoo = mock(ProductType.class);
    when(productTypeFoo.getKey()).thenReturn("foo");
    when(productTypeFoo.getAttributes()).thenReturn(emptyList());
    final List<ProductType> productTypes = singletonList(productTypeFoo);
    // test
    final List<ProductTypeDraft> productTypeDrafts = productTypeTransformService.toProductTypeDrafts(productTypes).join();
    // assertion
    assertThat(productTypeDrafts).containsExactly(ProductTypeDraftBuilder.of(productTypeFoo).build());
}
Also used : ProductTypeTransformService(com.commercetools.sync.producttypes.service.ProductTypeTransformService) SphereClient(io.sphere.sdk.client.SphereClient) ProductType(io.sphere.sdk.producttypes.ProductType) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Test(org.junit.jupiter.api.Test)

Example 3 with ProductTypeTransformService

use of com.commercetools.sync.producttypes.service.ProductTypeTransformService in project commercetools-sync-java by commercetools.

the class ProductTypeTransformServiceImplTest method mapToProductDrafts_WithNoReferences_ShouldReturnCorrectProductTypeDrafts.

@Test
void mapToProductDrafts_WithNoReferences_ShouldReturnCorrectProductTypeDrafts() {
    final SphereClient sourceClient = mock(SphereClient.class);
    final ProductTypeTransformService productTypeTransformService = new ProductTypeTransformServiceImpl(sourceClient, referenceIdToKeyCache);
    final AttributeDefinition stringAttr = AttributeDefinitionBuilder.of("a", ofEnglish("a"), StringAttributeType.of()).build();
    final AttributeDefinition numberAttr = AttributeDefinitionBuilder.of("b", ofEnglish("b"), NumberAttributeType.of()).build();
    final ProductType productTypeFoo = mock(ProductType.class);
    when(productTypeFoo.getKey()).thenReturn("ProductTypeFoo");
    when(productTypeFoo.getAttributes()).thenReturn(singletonList(stringAttr));
    final ProductType productTypeBar = mock(ProductType.class);
    when(productTypeBar.getKey()).thenReturn("ProductTypeBar");
    when(productTypeBar.getAttributes()).thenReturn(singletonList(numberAttr));
    final List<ProductType> productTypes = asList(productTypeFoo, productTypeBar);
    // test
    final List<ProductTypeDraft> productTypeDrafts = productTypeTransformService.toProductTypeDrafts(productTypes).join();
    // assertion
    assertThat(productTypeDrafts).containsExactly(ProductTypeDraftBuilder.of(productTypeFoo).build(), ProductTypeDraftBuilder.of(productTypeBar).build());
}
Also used : ProductTypeTransformService(com.commercetools.sync.producttypes.service.ProductTypeTransformService) SphereClient(io.sphere.sdk.client.SphereClient) ProductType(io.sphere.sdk.producttypes.ProductType) AttributeDefinition(io.sphere.sdk.products.attributes.AttributeDefinition) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) Test(org.junit.jupiter.api.Test)

Aggregations

ProductTypeTransformService (com.commercetools.sync.producttypes.service.ProductTypeTransformService)3 SphereClient (io.sphere.sdk.client.SphereClient)3 ProductType (io.sphere.sdk.producttypes.ProductType)3 ProductTypeDraft (io.sphere.sdk.producttypes.ProductTypeDraft)3 Test (org.junit.jupiter.api.Test)3 AttributeDefinition (io.sphere.sdk.products.attributes.AttributeDefinition)2 ResourceIdsGraphQlRequest (com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest)1 ResourceKeyIdGraphQlResult (com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult)1 NestedAttributeType (io.sphere.sdk.products.attributes.NestedAttributeType)1 SetAttributeType (io.sphere.sdk.products.attributes.SetAttributeType)1