use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsInAnyOrderIT method sync_withMissingParent_shouldSyncCorrectly.
@Test
void sync_withMissingParent_shouldSyncCorrectly() {
// preparation
final String productReferenceAttributeName = "product-reference";
final String parentProductKey = "parent-product-key";
final String parentProductKey2 = "parent-product-key2";
final AttributeDraft productReferenceAttribute = AttributeDraft.of(productReferenceAttributeName, asSet(Reference.of(Product.referenceTypeId(), parentProductKey), Reference.of(Product.referenceTypeId(), parentProductKey2)));
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 childDraft2 = ProductDraftBuilder.of(productType, ofEnglish("foo-2"), ofEnglish("foo-slug-2"), ProductVariantDraftBuilder.of().key("foo-2").sku("foo-2").attributes(productReferenceAttribute).build()).key("foo-2").build();
final ProductDraft parentDraft = ProductDraftBuilder.of(productType, ofEnglish(parentProductKey2), ofEnglish(parentProductKey2), ProductVariantDraftBuilder.of().sku(parentProductKey2).key(parentProductKey2).build()).key(parentProductKey2).build();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(asList(childDraft1, childDraft2, parentDraft)).toCompletableFuture().join();
final Product syncedParent = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(parentProductKey)).toCompletableFuture().join();
// assertion
assertThat(syncedParent).isNull();
assertThat(syncStatistics).hasValues(3, 1, 0, 0, 2);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
final UnresolvedReferencesService<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
final Set<WaitingToBeResolvedProducts> waitingDrafts = unresolvedReferencesService.fetch(asSet(childDraft1.getKey(), childDraft2.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
assertThat(waitingDrafts).containsExactlyInAnyOrder(new WaitingToBeResolvedProducts(childDraft1, singleton(parentProductKey)), new WaitingToBeResolvedProducts(childDraft2, singleton(parentProductKey)));
}
use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts 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)));
}
use of com.commercetools.sync.commons.models.WaitingToBeResolvedProducts in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsIT method sync_withNonExistingProductReferenceAsAttribute_ShouldFailCreatingTheProduct.
@Test
void sync_withNonExistingProductReferenceAsAttribute_ShouldFailCreatingTheProduct() {
// preparation
final AttributeDraft productReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), "nonExistingKey"));
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 ProductSyncWithNestedReferencedProductsIT method sync_withNonExistingNestedProductReferenceAsAttribute_ShouldFailCreatingTheProduct.
@Test
void sync_withNonExistingNestedProductReferenceAsAttribute_ShouldFailCreatingTheProduct() {
// preparation
final ObjectNode nestedAttributeValue = createNestedAttributeValueReferences("product-reference", 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 UnresolvedReferencesServiceImplTest method fetch_OnSuccess_ShouldReturnMock.
@SuppressWarnings("unchecked")
@Test
void fetch_OnSuccess_ShouldReturnMock() {
// preparation
final CustomObject customObjectMock = mock(CustomObject.class);
final ProductDraft productDraftMock = mock(ProductDraft.class);
when(productDraftMock.getKey()).thenReturn("product-draft-key");
final WaitingToBeResolvedProducts waitingToBeResolved = new WaitingToBeResolvedProducts(productDraftMock, singleton("test-ref"));
when(customObjectMock.getValue()).thenReturn(waitingToBeResolved);
final PagedQueryResult result = getMockPagedQueryResult(singletonList(customObjectMock));
when(productSyncOptions.getCtpClient().execute(any(CustomObjectQuery.class))).thenReturn(completedFuture(result));
// test
final Set<WaitingToBeResolvedProducts> toBeResolvedOptional = service.fetch(singleton("product-draft-key"), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
// assertions
assertThat(toBeResolvedOptional).containsOnly(waitingToBeResolved);
}
Aggregations