Search in sources :

Example 1 with CustomerTransformService

use of com.commercetools.sync.customers.service.CustomerTransformService in project commercetools-sync-java by commercetools.

the class CustomerTransformServiceImplTest method transform_CustomerReferences_ShouldResolveReferencesUsingCacheAndMapToCustomerDraft.

@Test
void transform_CustomerReferences_ShouldResolveReferencesUsingCacheAndMapToCustomerDraft() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
    final CustomerTransformService CustomerTransformService = new CustomerTransformServiceImpl(sourceClient, referenceIdToKeyCache);
    final String customerKey = "customerKey";
    final String customTypeId = UUID.randomUUID().toString();
    final String customTypeKey = "customTypeKey";
    final String customerGroupId = UUID.randomUUID().toString();
    final String customerGroupKey = "customerGroupKey";
    final List<Customer> mockCustomersPage = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final Customer mockCustomer = mock(Customer.class);
        final CustomFields mockCustomFields = mock(CustomFields.class);
        final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
        when(mockCustomFields.getType()).thenReturn(typeReference);
        when(mockCustomer.getCustom()).thenReturn(mockCustomFields);
        when(mockCustomer.getKey()).thenReturn(customerKey);
        final Reference<CustomerGroup> customerGroupReference = Reference.ofResourceTypeIdAndId("resourceCustomerGroupId", customerGroupId);
        when(mockCustomer.getCustomerGroup()).thenReturn(customerGroupReference);
        mockCustomersPage.add(mockCustomer);
    }
    final String jsonStringCustomTypes = "{\"results\":[{\"id\":\"" + customTypeId + "\"," + "\"key\":\"" + customTypeKey + "\"}]}";
    final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
    final String jsonStringCustomerGroups = "{\"results\":[{\"id\":\"" + customerGroupId + "\"," + "\"key\":\"" + customerGroupKey + "\"}]}";
    final ResourceKeyIdGraphQlResult customerGroupsResult = SphereJsonUtils.readObject(jsonStringCustomerGroups, ResourceKeyIdGraphQlResult.class);
    when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(customTypesResult)).thenReturn(CompletableFuture.completedFuture(customerGroupsResult));
    // test
    final List<CustomerDraft> customersResolved = CustomerTransformService.toCustomerDrafts(mockCustomersPage).toCompletableFuture().join();
    // assertions
    final Optional<CustomerDraft> customerKey1 = customersResolved.stream().filter(customerDraft -> customerKey.equals(customerDraft.getKey())).findFirst();
    assertThat(customerKey1).hasValueSatisfying(customerDraft -> {
        assertThat(customerDraft.getCustom().getType().getKey()).isEqualTo(customTypeKey);
        assertThat(customerDraft.getCustomerGroup().getKey()).isEqualTo(customerGroupKey);
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) CustomFields(io.sphere.sdk.types.CustomFields) CustomerGroup(io.sphere.sdk.customergroups.CustomerGroup) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) Reference(io.sphere.sdk.models.Reference) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Customer(io.sphere.sdk.customers.Customer) CustomerTransformService(com.commercetools.sync.customers.service.CustomerTransformService) 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) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) Mockito.mock(org.mockito.Mockito.mock) Type(io.sphere.sdk.types.Type) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) CustomerTransformService(com.commercetools.sync.customers.service.CustomerTransformService) Customer(io.sphere.sdk.customers.Customer) CustomerGroup(io.sphere.sdk.customergroups.CustomerGroup) ArrayList(java.util.ArrayList) 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) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) 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 CustomerTransformService (com.commercetools.sync.customers.service.CustomerTransformService)1 SphereClient (io.sphere.sdk.client.SphereClient)1 CustomerGroup (io.sphere.sdk.customergroups.CustomerGroup)1 Customer (io.sphere.sdk.customers.Customer)1 CustomerDraft (io.sphere.sdk.customers.CustomerDraft)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