use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.
the class ProductSyncWithNestedReferencedProductsIT method sync_withNestedProductReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct.
@Test
void sync_withNestedProductReferenceSetContainingANonExistingReference_shouldFailCreatingTheProduct() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueSetOfReferences("product-reference-set", createReferenceObject(testProduct1.getKey(), Product.referenceTypeId()), createReferenceObject("nonExistingKey", Product.referenceTypeId()));
final AttributeDraft productReferenceAttribute = AttributeDraft.of("nestedAttribute", createArrayNode(nestedAttributeValue));
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productReferenceAttribute).build();
final ProductDraft productDraftWithProductReference = ProductDraftBuilder.of(productType, ofEnglish("productName"), ofEnglish("productSlug"), masterVariant).key("new-product").build();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(productDraftWithProductReference)).toCompletableFuture().join();
// assertion
assertThat(syncStatistics).hasValues(1, 0, 0, 0, 1);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
final UnresolvedReferencesServiceImpl<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
final Set<WaitingToBeResolvedProducts> waitingToBeResolvedDrafts = unresolvedReferencesService.fetch(asSet(productDraftWithProductReference.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
assertThat(waitingToBeResolvedDrafts).singleElement().matches(waitingToBeResolvedDraft -> {
assertThat(waitingToBeResolvedDraft.getProductDraft().getKey()).isEqualTo(productDraftWithProductReference.getKey());
assertThat(waitingToBeResolvedDraft.getMissingReferencedProductKeys()).containsExactly("nonExistingKey");
return true;
});
}
use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.
the class CleanupUnresolvedReferenceCustomObjectsIT method createSampleUnresolvedReferences.
void createSampleUnresolvedReferences() {
final ProductDraft sampleProductDraft = SphereJsonUtils.readObjectFromResource(PRODUCT_KEY_1_RESOURCE_PATH, ProductDraft.class);
final CategoryDraft sampleCategoryDraft = SphereJsonUtils.readObjectFromResource(CATEGORY_KEY_1_RESOURCE_PATH, CategoryDraft.class);
final Set<Reference<State>> sampleTransitions = new HashSet<>(Arrays.asList(State.referenceOfId("id1"), State.referenceOfId("id2")));
final List<WaitingToBeResolvedProducts> productUnresolvedReferences = new ArrayList<>();
final List<WaitingToBeResolvedCategories> categoryUnresolvedReferences = new ArrayList<>();
final List<WaitingToBeResolvedTransitions> transitionUnresolvedReferences = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
productUnresolvedReferences.add(new WaitingToBeResolvedProducts(ProductDraftBuilder.of(sampleProductDraft).key(format("productKey%s", i)).build(), asSet("foo", "bar")));
categoryUnresolvedReferences.add(new WaitingToBeResolvedCategories(CategoryDraftBuilder.of(sampleCategoryDraft).key(format("categoryKey%s", i)).build(), asSet("foo", "bar")));
transitionUnresolvedReferences.add(new WaitingToBeResolvedTransitions(StateDraftBuilder.of(format("stateKeys%s", i), StateType.LINE_ITEM_STATE).transitions(sampleTransitions).build(), asSet("foo", "bar")));
}
CompletableFuture.allOf(CompletableFuture.allOf(productUnresolvedReferences.stream().map(draft -> unresolvedReferencesServiceForProducts.save(draft, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class)).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new)), CompletableFuture.allOf(categoryUnresolvedReferences.stream().map(draft -> unresolvedReferencesServiceForCategories.save(draft, CUSTOM_OBJECT_CATEGORY_CONTAINER_KEY, WaitingToBeResolvedCategories.class)).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new)), CompletableFuture.allOf(transitionUnresolvedReferences.stream().map(draft -> unresolvedReferencesServiceForTransitions.save(draft, CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY, WaitingToBeResolvedTransitions.class)).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new))).join();
}
use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.
the class ProductSync method resolveNowReadyReferences.
@Nonnull
private CompletionStage<Void> resolveNowReadyReferences(final Map<String, String> keyToIdCache) {
// We delete anyways the keys from the statistics before we attempt resolution, because even if
// resolution fails
// the products that failed to be synced would be counted as failed.
final Set<String> referencingDraftKeys = readyToResolve.stream().map(statistics::removeAndGetReferencingKeys).filter(Objects::nonNull).flatMap(Set::stream).collect(Collectors.toSet());
if (referencingDraftKeys.isEmpty()) {
return CompletableFuture.completedFuture(null);
}
final Set<ProductDraft> readyToSync = new HashSet<>();
final Set<WaitingToBeResolvedProducts> waitingDraftsToBeUpdated = new HashSet<>();
return unresolvedReferencesService.fetch(referencingDraftKeys, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).handle(ImmutablePair::new).thenCompose(fetchResponse -> {
final Set<WaitingToBeResolvedProducts> waitingDrafts = fetchResponse.getKey();
final Throwable fetchException = fetchResponse.getValue();
if (fetchException != null) {
final String errorMessage = format(UNRESOLVED_REFERENCES_STORE_FETCH_FAILED, referencingDraftKeys);
handleError(new SyncException(errorMessage, fetchException), referencingDraftKeys.size());
return CompletableFuture.completedFuture(null);
}
waitingDrafts.forEach(waitingDraft -> {
final Set<String> missingReferencedProductKeys = waitingDraft.getMissingReferencedProductKeys();
missingReferencedProductKeys.removeAll(readyToResolve);
if (missingReferencedProductKeys.isEmpty()) {
readyToSync.add(waitingDraft.getProductDraft());
} else {
waitingDraftsToBeUpdated.add(waitingDraft);
}
});
return updateWaitingDrafts(waitingDraftsToBeUpdated).thenCompose(aVoid -> syncBatch(readyToSync, keyToIdCache)).thenCompose(aVoid -> removeFromWaiting(readyToSync));
});
}
use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.
the class UnresolvedReferencesServiceImplIT method save_ExistingProductDraftWithoutException_overwritesOldCustomObjectValue.
@Test
void save_ExistingProductDraftWithoutException_overwritesOldCustomObjectValue() {
// preparation
final ProductDraft productDraft = SphereJsonUtils.readObjectFromResource(PRODUCT_KEY_1_RESOURCE_PATH, ProductDraft.class);
final WaitingToBeResolvedProducts productDraftWithUnresolvedRefs = new WaitingToBeResolvedProducts(productDraft, asSet("foo", "bar"));
unresolvedReferencesService.save(productDraftWithUnresolvedRefs, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
final WaitingToBeResolvedProducts productDraftWithUnresolvedNewRefs = new WaitingToBeResolvedProducts(productDraft, asSet("foo123", "bar123"));
// test
final Optional<WaitingToBeResolvedProducts> latestResult = unresolvedReferencesService.save(productDraftWithUnresolvedNewRefs, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
// assertions
assertThat(latestResult).hasValueSatisfying(waitingToBeResolved -> {
assertThat(waitingToBeResolved.getProductDraft()).isEqualTo(productDraft);
assertThat(waitingToBeResolved.getMissingReferencedProductKeys()).isEqualTo(productDraftWithUnresolvedNewRefs.getMissingReferencedProductKeys());
});
final CustomObjectByKeyGet<WaitingToBeResolvedProducts> customObjectByKeyGet = CustomObjectByKeyGet.of(CUSTOM_OBJECT_CONTAINER_KEY, sha1Hex(productDraft.getKey()), WaitingToBeResolvedProducts.class);
final CustomObject<WaitingToBeResolvedProducts> createdCustomObject = CTP_TARGET_CLIENT.execute(customObjectByKeyGet).toCompletableFuture().join();
assertThat(createdCustomObject.getValue()).isEqualTo(productDraftWithUnresolvedNewRefs);
assertThat(errorCallBackMessages).isEmpty();
assertThat(errorCallBackExceptions).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.
the class UnresolvedReferencesServiceImplIT method saveFetchAndDelete_WithoutExceptions_shouldWorkCorrectly.
@Test
void saveFetchAndDelete_WithoutExceptions_shouldWorkCorrectly() {
// preparation
final ProductDraft productDraft = SphereJsonUtils.readObjectFromResource(PRODUCT_KEY_1_RESOURCE_PATH, ProductDraft.class);
final WaitingToBeResolvedProducts productDraftWithUnresolvedRefs = new WaitingToBeResolvedProducts(productDraft, asSet("foo", "bar"));
// test
final Optional<WaitingToBeResolvedProducts> result = unresolvedReferencesService.save(productDraftWithUnresolvedRefs, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
// assertions
assertThat(result).hasValueSatisfying(waitingToBeResolved -> assertThat(waitingToBeResolved.getProductDraft()).isEqualTo(productDraft));
// test
final Set<WaitingToBeResolvedProducts> waitingDrafts = unresolvedReferencesService.fetch(singleton(productDraft.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
// assertions
assertThat(waitingDrafts).containsExactly(productDraftWithUnresolvedRefs);
// test
final Optional<WaitingToBeResolvedProducts> deletionResult = unresolvedReferencesService.delete(productDraft.getKey(), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
// assertions
assertThat(deletionResult).hasValueSatisfying(waitingToBeResolved -> assertThat(waitingToBeResolved.getProductDraft()).isEqualTo(productDraft));
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
assertThat(errorCallBackExceptions).isEmpty();
}
Aggregations