use of com.commercetools.sync.services.UnresolvedReferencesService in project commercetools-sync-java by commercetools.
the class UnresolvedReferencesServiceImplIT method setupTest.
@BeforeEach
void setupTest() {
deleteWaitingToBeResolvedCustomObjects(CTP_TARGET_CLIENT, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class);
errorCallBackMessages = new ArrayList<>();
errorCallBackExceptions = new ArrayList<>();
warningCallBackMessages = new ArrayList<>();
final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception);
}).warningCallback((syncException, productDraft, product) -> warningCallBackMessages.add(syncException.getMessage())).build();
unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(productSyncOptions);
}
use of com.commercetools.sync.services.UnresolvedReferencesService in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsInAnyOrderIT method sync_withFailToFetchCustomObject_shouldSyncCorrectly.
@SuppressWarnings("unchecked")
@Test
void sync_withFailToFetchCustomObject_shouldSyncCorrectly() {
// preparation
final String productReferenceAttributeName = "product-reference";
final String parentProductKey = "parent-product-key";
final AttributeDraft productReferenceAttribute = AttributeDraft.of(productReferenceAttributeName, Reference.of(Product.referenceTypeId(), parentProductKey));
final ProductDraft childDraft1 = ProductDraftBuilder.of(productType, ofEnglish("foo"), ofEnglish("foo-slug"), ProductVariantDraftBuilder.of().key("foo").sku("foo").attributes(productReferenceAttribute).build()).key(product.getKey()).build();
final ProductDraft parentDraft = ProductDraftBuilder.of(productType, ofEnglish(parentProductKey), ofEnglish(parentProductKey), ProductVariantDraftBuilder.of().sku(parentProductKey).key(parentProductKey).build()).key(parentProductKey).build();
final SphereClient ctpClient = spy(CTP_TARGET_CLIENT);
final BadGatewayException gatewayException = new BadGatewayException("failed to respond.");
when(ctpClient.execute(any(CustomObjectQuery.class))).thenReturn(CompletableFutureUtils.failed(gatewayException)).thenCallRealMethod();
syncOptions = ProductSyncOptionsBuilder.of(ctpClient).errorCallback((syncException, draft, product, updateActions) -> collectErrors(syncException.getMessage(), syncException)).beforeUpdateCallback(this::collectActions).build();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(childDraft1)).thenCompose(ignoredResult -> productSync.sync(singletonList(parentDraft))).toCompletableFuture().join();
// assertion
assertThat(syncStatistics).hasValues(2, 1, 0, 1, 0);
assertThat(errorCallBackMessages).containsExactly("Failed to fetch ProductDrafts waiting to be resolved with keys '[foo]'.");
assertThat(errorCallBackExceptions).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(BadGatewayException.class);
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
final UnresolvedReferencesService<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
final Set<WaitingToBeResolvedProducts> waitingDrafts = unresolvedReferencesService.fetch(asSet(childDraft1.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
assertThat(waitingDrafts).containsExactly(new WaitingToBeResolvedProducts(childDraft1, singleton(parentProductKey)));
}
Aggregations