use of com.commercetools.sync.products.ProductSync 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.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsIT method sync_withProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingProducts.
@Test
void sync_withProductReferenceSetAsAttribute_shouldCreateProductReferencingExistingProducts() {
// preparation
final AttributeDraft productReferenceAttribute = AttributeDraft.of("product-reference", Reference.of(Product.referenceTypeId(), product.getKey()));
final HashSet<Reference<Product>> references = new HashSet<>();
references.add(Reference.of(Product.referenceTypeId(), product.getKey()));
references.add(Reference.of(Product.referenceTypeId(), product2.getKey()));
final AttributeDraft productReferenceSetAttribute = AttributeDraft.of("product-reference-set", references);
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().sku("sku").key("new-product-master-variant").attributes(productReferenceAttribute, productReferenceSetAttribute).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, 1, 0, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
final Product createdProduct = CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(productDraftWithProductReference.getKey())).toCompletableFuture().join();
final Optional<Attribute> createdProductReferenceAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceAttribute.getName());
assertThat(createdProductReferenceAttribute).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(attribute.getValueAsJsonNode().get(REFERENCE_ID_FIELD).asText()).isEqualTo(product.getId());
});
final Optional<Attribute> createdProductReferenceSetAttribute = createdProduct.getMasterData().getStaged().getMasterVariant().findAttribute(productReferenceSetAttribute.getName());
assertThat(createdProductReferenceSetAttribute).hasValueSatisfying(attribute -> {
assertThat(attribute.getValueAsJsonNode()).isInstanceOf(ArrayNode.class);
final ArrayNode referenceSet = (ArrayNode) attribute.getValueAsJsonNode();
assertThat(referenceSet).hasSize(2).anySatisfy(reference -> {
assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(product.getId());
}).anySatisfy(reference -> {
assertThat(reference.get(REFERENCE_TYPE_ID_FIELD).asText()).isEqualTo(Product.referenceTypeId());
assertThat(reference.get(REFERENCE_ID_FIELD).asText()).isEqualTo(product2.getId());
});
});
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withMissingPriceChannel_shouldCreateProductDistributionPriceChannel.
@Test
void sync_withMissingPriceChannel_shouldCreateProductDistributionPriceChannel() {
PriceDraftDsl priceDraftWithMissingChannelRef = PriceDraftBuilder.of(MoneyImpl.of("20", "EUR")).channel(ResourceIdentifier.ofKey("missingKey")).build();
ProductVariantDraftDsl masterVariantDraft = ProductVariantDraftBuilder.of(ProductVariantDraftDsl.of().withKey("v2").withSku("1065833").withPrices(Collections.singletonList(priceDraftWithMissingChannelRef))).build();
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).masterVariant(masterVariantDraft).taxCategory(null).state(null).build();
final TriConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>> warningCallBack = (exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage());
ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> collectErrors(exception.getMessage(), exception.getCause())).warningCallback(warningCallBack).ensurePriceChannels(true).build();
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraft)));
assertThat(syncStatistics).hasValues(1, 1, 0, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
Product productFromTargetProject = executeBlocking(CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(productDraft.getKey())));
List<Price> prices = productFromTargetProject.getMasterData().getStaged().getMasterVariant().getPrices();
assertThat(prices.size()).isEqualTo(1);
Channel channel = executeBlocking(CTP_TARGET_CLIENT.execute(ChannelByIdGet.of(Objects.requireNonNull(Objects.requireNonNull(prices.get(0).getChannel()).getId()))));
assertThat(channel.getRoles()).containsOnly(ChannelRole.PRODUCT_DISTRIBUTION);
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withProductContainingAttributeChanges_shouldSyncProductCorrectly.
@Test
void sync_withProductContainingAttributeChanges_shouldSyncProductCorrectly() {
// preparation
final List<UpdateAction<Product>> updateActions = new ArrayList<>();
final TriConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>> warningCallBack = (exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage());
final ProductSyncOptions customOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> collectErrors(exception.getMessage(), exception.getCause())).warningCallback(warningCallBack).beforeUpdateCallback((actions, draft, old) -> {
updateActions.addAll(actions);
return actions;
}).build();
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_1_RESOURCE_PATH, ProductType.referenceOfId(productType.getKey())).categories(emptyList()).taxCategory(null).state(null).build();
// Creating the attribute draft with the changes
final AttributeDraft priceInfoAttrDraft = AttributeDraft.of("priceInfo", JsonNodeFactory.instance.textNode("100/kg"));
final AttributeDraft angebotAttrDraft = AttributeDraft.of("angebot", JsonNodeFactory.instance.textNode("big discount"));
final AttributeDraft unknownAttrDraft = AttributeDraft.of("unknown", JsonNodeFactory.instance.textNode("unknown"));
// Creating the product variant draft with the product reference attribute
final List<AttributeDraft> attributes = asList(priceInfoAttrDraft, angebotAttrDraft, unknownAttrDraft);
final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of(productDraft.getMasterVariant()).attributes(attributes).build();
final ProductDraft productDraftWithChangedAttributes = ProductDraftBuilder.of(productDraft).masterVariant(masterVariant).build();
// test
final ProductSync productSync = new ProductSync(customOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraftWithChangedAttributes)));
// assertion
assertThat(syncStatistics).hasValues(1, 0, 1, 0, 0);
final String causeErrorMessage = format(ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA, unknownAttrDraft.getName());
final String expectedErrorMessage = format(FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION, unknownAttrDraft.getName(), productDraft.getMasterVariant().getKey(), productDraft.getKey(), causeErrorMessage);
assertThat(errorCallBackExceptions).hasSize(1);
assertThat(errorCallBackExceptions.get(0).getMessage()).isEqualTo(expectedErrorMessage);
assertThat(errorCallBackExceptions.get(0).getCause().getMessage()).isEqualTo(causeErrorMessage);
assertThat(errorCallBackMessages).containsExactly(expectedErrorMessage);
assertThat(warningCallBackMessages).isEmpty();
assertThat(updateActions).filteredOn(updateAction -> !(updateAction instanceof SetTaxCategory)).filteredOn(updateAction -> !(updateAction instanceof RemoveFromCategory)).containsExactlyInAnyOrder(SetAttributeInAllVariants.of(priceInfoAttrDraft, true), SetAttribute.of(1, angebotAttrDraft, true), SetAttributeInAllVariants.ofUnsetAttribute("size", true), SetAttributeInAllVariants.ofUnsetAttribute("rinderrasse", true), SetAttributeInAllVariants.ofUnsetAttribute("herkunft", true), SetAttributeInAllVariants.ofUnsetAttribute("teilstueck", true), SetAttributeInAllVariants.ofUnsetAttribute("fuetterung", true), SetAttributeInAllVariants.ofUnsetAttribute("reifung", true), SetAttributeInAllVariants.ofUnsetAttribute("haltbarkeit", true), SetAttributeInAllVariants.ofUnsetAttribute("verpackung", true), SetAttributeInAllVariants.ofUnsetAttribute("anlieferung", true), SetAttributeInAllVariants.ofUnsetAttribute("zubereitung", true), SetAttribute.ofUnsetAttribute(1, "localisedText", true), Publish.of());
}
use of com.commercetools.sync.products.ProductSync in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withSingleBatchSyncing_ShouldSync.
@Test
void sync_withSingleBatchSyncing_ShouldSync() {
// Prepare batches from external source
final ProductDraft productDraft = createProductDraft(PRODUCT_KEY_1_CHANGED_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey()), ResourceIdentifier.ofKey(targetTaxCategory.getKey()), ResourceIdentifier.ofKey(targetProductState.getKey()), categoryResourceIdentifiersWithKeys, categoryOrderHintsWithKeys);
final ProductDraft key3Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(ResourceIdentifier.ofKey(targetTaxCategory.getKey())).state(ResourceIdentifier.ofKey(targetProductState.getKey())).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey3").slug(LocalizedString.of(Locale.ENGLISH, "slug3")).masterVariant(ProductVariantDraftBuilder.of().key("mv3").sku("sku3").build()).build();
final ProductDraft key4Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(ResourceIdentifier.ofKey(targetTaxCategory.getKey())).state(ResourceIdentifier.ofKey(targetProductState.getKey())).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey4").slug(LocalizedString.of(Locale.ENGLISH, "slug4")).masterVariant(ProductVariantDraftBuilder.of().key("mv4").sku("sku4").build()).build();
final ProductDraft key5Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(ResourceIdentifier.ofKey(targetTaxCategory.getKey())).state(ResourceIdentifier.ofKey(targetProductState.getKey())).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey5").slug(LocalizedString.of(Locale.ENGLISH, "slug5")).masterVariant(ProductVariantDraftBuilder.of().key("mv5").sku("sku5").build()).build();
final ProductDraft key6Draft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).taxCategory(ResourceIdentifier.ofKey(targetTaxCategory.getKey())).state(ResourceIdentifier.ofKey(targetProductState.getKey())).categories(new ArrayList<>()).categoryOrderHints(CategoryOrderHints.of(new HashMap<>())).key("productKey6").slug(LocalizedString.of(Locale.ENGLISH, "slug6")).masterVariant(ProductVariantDraftBuilder.of().key("mv6").sku("sku6").build()).build();
final List<ProductDraft> batch = new ArrayList<>();
batch.add(productDraft);
batch.add(key3Draft);
batch.add(key4Draft);
batch.add(key5Draft);
batch.add(key6Draft);
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(batch));
assertThat(syncStatistics).hasValues(5, 4, 1, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
}
Aggregations