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);
});
}
Aggregations