use of com.commercetools.sync.cartdiscounts.service.CartDiscountTransformService in project commercetools-sync-java by commercetools.
the class CartDiscountTransformServiceImplTest method transform_CartDiscountReferences_ShouldResolveReferencesUsingCacheAndMapToCartDiscountDraft.
@Test
void transform_CartDiscountReferences_ShouldResolveReferencesUsingCacheAndMapToCartDiscountDraft() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
final CartDiscountTransformService cartDiscountTransformService = new CartDiscountTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final String cartDiscountKey = "cartDiscountKey";
final String customTypeId = UUID.randomUUID().toString();
final String customTypeKey = "customTypeKey";
final List<CartDiscount> mockCartDiscountsPage = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final CartDiscount mockCartDiscount = mock(CartDiscount.class);
final CustomFields mockCustomFields = mock(CustomFields.class);
final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
when(mockCustomFields.getType()).thenReturn(typeReference);
when(mockCartDiscount.getCustom()).thenReturn(mockCustomFields);
when(mockCartDiscount.getKey()).thenReturn(cartDiscountKey);
mockCartDiscountsPage.add(mockCartDiscount);
}
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<CartDiscountDraft> cartDiscountsResolved = cartDiscountTransformService.toCartDiscountDrafts(mockCartDiscountsPage).toCompletableFuture().join();
// assertions
final Optional<CartDiscountDraft> cartDiscountKey1 = cartDiscountsResolved.stream().filter(cartDiscountDraft -> cartDiscountKey.equals(cartDiscountDraft.getKey())).findFirst();
assertThat(cartDiscountKey1).hasValueSatisfying(cartDiscountDraft -> assertThat(cartDiscountDraft.getCustom().getType().getKey()).isEqualTo(customTypeKey));
}
Aggregations