use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class ProductTransformServiceImplTest method transform_WithNonCachedCustomObjectAttributeReference_ShouldFetchAndTransformProduct.
@Test
void transform_WithNonCachedCustomObjectAttributeReference_ShouldFetchAndTransformProduct() {
// 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\":\"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).join();
final Optional<ProductDraft> productKey1 = productsResolved.stream().filter(productDraft -> "productKeyResolved".equals(productDraft.getKey())).findFirst();
assertThat(productKey1).hasValueSatisfying(product -> assertThat(product.getMasterVariant().getAttributes()).anySatisfy(attribute -> {
assertThat(attribute.getName()).isEqualTo("customObjectReference");
}));
verify(sourceClient, times(1)).execute(any(CustomObjectQuery.class));
}
use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache 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));
}
use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class ShoppingListTransformServiceImplTest method transform_ShoppingListReferences_ShouldResolveReferencesUsingCacheAndMapToShoppingListDraft.
@Test
void transform_ShoppingListReferences_ShouldResolveReferencesUsingCacheAndMapToShoppingListDraft() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
final ShoppingListTransformService shoppingListTransformService = new ShoppingListTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final String customTypeId = UUID.randomUUID().toString();
final String lineItemCustomTypeId = UUID.randomUUID().toString();
final String textLineItemCustomTypeId = UUID.randomUUID().toString();
final String customTypeKey = "customTypeKey";
final String lineItemCustomTypeKey = "lineItemCustomTypeKey";
final String textLineItemCustomTypeKey = "textLineItemCustomTypeKey";
final String shoppingListKey = "shoppingListKeyValue";
final String textLineItemName = "textLineItemName";
final ProductVariant mockProductVariant = mock(ProductVariant.class);
when(mockProductVariant.getSku()).thenReturn("variant-sku");
final List<ShoppingList> mockShoppingListPage = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final ShoppingList mockShoppingList = mock(ShoppingList.class);
final CustomFields mockCustomFields = mock(CustomFields.class);
final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
when(mockCustomFields.getType()).thenReturn(typeReference);
when(mockShoppingList.getCustom()).thenReturn(mockCustomFields);
when(mockShoppingList.getKey()).thenReturn(shoppingListKey);
final CustomFields mockLineItemCustomFields = mock(CustomFields.class);
final Reference<Type> lineItemTypeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", lineItemCustomTypeId);
when(mockLineItemCustomFields.getType()).thenReturn(lineItemTypeReference);
final LineItem mockLineItem = mock(LineItem.class);
when(mockLineItem.getVariant()).thenReturn(mockProductVariant);
when(mockLineItem.getCustom()).thenReturn(mockLineItemCustomFields);
final CustomFields mockTextLineItemCustomFields = mock(CustomFields.class);
final Reference<Type> textLineItemTypeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", textLineItemCustomTypeId);
when(mockTextLineItemCustomFields.getType()).thenReturn(textLineItemTypeReference);
final TextLineItem mockTextLineItem = mock(TextLineItem.class);
when(mockTextLineItem.getName()).thenReturn(LocalizedString.ofEnglish(textLineItemName));
when(mockTextLineItem.getCustom()).thenReturn(mockTextLineItemCustomFields);
when(mockShoppingList.getLineItems()).thenReturn(Collections.singletonList(mockLineItem));
when(mockShoppingList.getTextLineItems()).thenReturn(Collections.singletonList(mockTextLineItem));
mockShoppingListPage.add(mockShoppingList);
}
final String jsonStringCustomTypes = "{\"results\":[{\"id\":\"" + customTypeId + "\"," + "\"key\":\"" + customTypeKey + "\"}, " + " {\"id\":\"" + lineItemCustomTypeId + "\"," + "\"key\":\"" + lineItemCustomTypeKey + "\"}, " + " {\"id\":\"" + textLineItemCustomTypeId + "\"," + "\"key\":\"" + textLineItemCustomTypeKey + "\"} ]}";
final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(customTypesResult));
// test
final List<ShoppingListDraft> shoppingListDrafts = shoppingListTransformService.toShoppingListDrafts(mockShoppingListPage).toCompletableFuture().join();
// assertions
assertThat(shoppingListDrafts).hasSize(10);
shoppingListDrafts.forEach(draft -> {
assertThat(draft.getCustom().getType().getKey()).isEqualTo(customTypeKey);
assertThat(draft.getLineItems()).containsExactly(LineItemDraftBuilder.ofSku("variant-sku", 0L).custom(CustomFieldsDraft.ofTypeKeyAndJson(lineItemCustomTypeKey, emptyMap())).build());
assertThat(draft.getTextLineItems()).containsExactly(TextLineItemDraftBuilder.of(LocalizedString.ofEnglish(textLineItemName), 0L).custom(CustomFieldsDraft.ofTypeKeyAndJson(textLineItemCustomTypeKey, emptyMap())).build());
});
}
use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithExceptionOnResolvingTransition_ShouldUpdateTransitions.
@Test
void sync_WithExceptionOnResolvingTransition_ShouldUpdateTransitions() {
final StateDraft stateCDraft = createStateDraft(keyC);
final State stateC = createStateInSource(stateCDraft);
final StateDraft stateBDraft = createStateDraft(keyB, stateC);
final State stateB = createStateInSource(stateBDraft);
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any())).thenCallRealMethod().thenReturn(exceptionallyCompletedFuture(new BadGatewayException()));
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(spyClient).batchSize(3).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).warningCallback((exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage())).build();
final StateSync stateSync = new StateSync(stateSyncOptions);
final List<StateDraft> stateDrafts = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, Arrays.asList(stateB, stateC)).join();
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(2, 0, 0, 2, 0);
Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
Assertions.assertThat(errorCallBackMessages).isNotEmpty();
Assertions.assertThat(errorCallBackMessages.get(0)).contains("Failed to fetch existing states with keys");
Assertions.assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class InventoryEntryTransformServiceImplTest method transform_InventoryReferences_ShouldResolveReferencesUsingCacheAndMapToInventoryEntryDraft.
@Test
void transform_InventoryReferences_ShouldResolveReferencesUsingCacheAndMapToInventoryEntryDraft() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
final InventoryEntryTransformService inventoryEntryTransformService = new InventoryEntryTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final String customTypeId = UUID.randomUUID().toString();
final String customTypeKey = "customTypeKey";
final String inventoryEntrySku = "inventoryEntrySkuValue";
final List<InventoryEntry> mockInventoryEntriesPage = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final InventoryEntry mockInventoryEntry = mock(InventoryEntry.class);
final CustomFields mockCustomFields = mock(CustomFields.class);
final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
when(mockCustomFields.getType()).thenReturn(typeReference);
when(mockInventoryEntry.getCustom()).thenReturn(mockCustomFields);
when(mockInventoryEntry.getSku()).thenReturn(inventoryEntrySku);
mockInventoryEntriesPage.add(mockInventoryEntry);
}
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<InventoryEntryDraft> inventoryEntryDraftsResolved = inventoryEntryTransformService.toInventoryEntryDrafts(mockInventoryEntriesPage).toCompletableFuture().join();
// assertions
final Optional<InventoryEntryDraft> inventoryEntryDraft1 = inventoryEntryDraftsResolved.stream().filter(inventoryEntryDraft -> inventoryEntrySku.equals(inventoryEntryDraft.getSku())).findFirst();
assertThat(inventoryEntryDraft1).hasValueSatisfying(inventoryEntryDraft -> assertThat(inventoryEntryDraft.getCustom().getType().getKey()).isEqualTo(customTypeKey));
}
Aggregations