use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.
the class ProductSyncUtilsTest method buildActions_FromDraftsWithDifferentAttributes_ShouldBuildUpdateActions.
@Test
void buildActions_FromDraftsWithDifferentAttributes_ShouldBuildUpdateActions() {
// Reloading the oldProduct object with a specific file for this test
oldProduct = readObjectFromResource(SIMPLE_PRODUCT_WITH_MULTIPLE_VARIANTS_RESOURCE_PATH, Product.class).toProjection(STAGED);
final AttributeDraft brandNameAttribute = AttributeDraft.of("brandName", "myBrand");
final AttributeDraft orderLimitAttribute = AttributeDraft.of("orderLimit", "5");
final AttributeDraft priceInfoAttribute = AttributeDraft.of("priceInfo", "80,20/kg");
final ProductVariantDraft variant = ProductVariantDraftBuilder.of().key("v3").sku("1065834").plusAttribute(orderLimitAttribute).plusAttribute(priceInfoAttribute).plusAttribute(brandNameAttribute).build();
final ProductDraft newProductDraft = createProductDraftBuilder(SIMPLE_PRODUCT_WITH_MASTER_VARIANT_RESOURCE_PATH, ProductType.referenceOfId("anyProductType")).plusVariants(variant).build();
final Map<String, AttributeMetaData> attributesMetaData = new HashMap<>();
final AttributeMetaData brandName = AttributeMetaData.of(AttributeDefinitionBuilder.of("brandName", null, null).build());
final AttributeMetaData orderLimit = AttributeMetaData.of(AttributeDefinitionBuilder.of("orderLimit", null, null).build());
final AttributeMetaData priceInfo = AttributeMetaData.of(AttributeDefinitionBuilder.of("priceInfo", null, null).build());
final AttributeMetaData size = AttributeMetaData.of(AttributeDefinitionBuilder.of("size", null, null).build());
attributesMetaData.put("brandName", brandName);
attributesMetaData.put("orderLimit", orderLimit);
attributesMetaData.put("priceInfo", priceInfo);
attributesMetaData.put("size", size);
final List<UpdateAction<Product>> updateActions = ProductSyncUtils.buildActions(oldProduct, newProductDraft, productSyncOptions, attributesMetaData);
// check the generated attribute update actions
assertThat(updateActions.size()).isEqualTo(9);
assertThat(updateActions).containsSequence(RemoveVariant.ofVariantId(5, true), AddVariant.of(Arrays.asList(AttributeDraft.of("priceInfo", "64,90/kg"), AttributeDraft.of("size", "ca. 1 x 1000 g")), Collections.emptyList(), "1065833", true).withKey("v2").withImages(Collections.emptyList()).withAssetDrafts(Collections.emptyList()), ChangeMasterVariant.ofSku("1065833", true), RemoveVariant.ofVariantId(1), SetAttribute.of(2, AttributeDraft.of("size", null), true), SetAttribute.of(2, AttributeDraft.of("orderLimit", "5"), true), SetAttribute.of(2, AttributeDraft.of("priceInfo", "80,20/kg"), true), SetAttribute.of(2, AttributeDraft.of("brandName", "myBrand"), true), Publish.of());
}
use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.
the class ProductUpdateActionUtilsTest method buildVariantsUpdateActions_updatesVariants.
@Test
void buildVariantsUpdateActions_updatesVariants() {
// preparation
final ProductProjection productOld = createProductFromJson(OLD_PROD_WITH_VARIANTS);
final ProductDraft productDraftNew = createProductDraftFromJson(NEW_PROD_DRAFT_WITH_VARIANTS_REMOVE_MASTER);
final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(mock(SphereClient.class)).syncFilter(SyncFilter.of()).build();
final Map<String, AttributeMetaData> attributesMetaData = new HashMap<>();
final AttributeMetaData priceInfo = AttributeMetaData.of(AttributeDefinitionBuilder.of("priceInfo", null, null).build());
attributesMetaData.put("priceInfo", priceInfo);
final List<UpdateAction<Product>> updateActions = buildVariantsUpdateActions(productOld, productDraftNew, productSyncOptions, attributesMetaData);
// check remove variants are the first in the list, but not the master variant
assertThat(updateActions.subList(0, 2)).containsExactlyInAnyOrder(RemoveVariant.ofVariantId(2, true), RemoveVariant.ofVariantId(3, true));
// check add actions
final ProductVariantDraft draftMaster = productDraftNew.getMasterVariant();
final ProductVariantDraft draft5 = productDraftNew.getVariants().get(1);
final ProductVariantDraft draft6 = productDraftNew.getVariants().get(2);
final ProductVariantDraft draft7 = productDraftNew.getVariants().get(3);
assertThat(updateActions).contains(AddVariant.of(draftMaster.getAttributes(), draftMaster.getPrices(), draftMaster.getSku(), true).withKey(draftMaster.getKey()).withImages(draftMaster.getImages()), AddVariant.of(draft5.getAttributes(), draft5.getPrices(), draft5.getSku(), true).withKey(draft5.getKey()).withImages(draft5.getImages()), AddVariant.of(draft6.getAttributes(), draft6.getPrices(), draft6.getSku(), true).withKey(draft6.getKey()).withImages(draft6.getImages()), AddVariant.of(draft7.getAttributes(), draft7.getPrices(), draft7.getSku(), true).withKey(draft7.getKey()).withImages(draft7.getImages()).withAssetDrafts(draft7.getAssets()));
// variant 4 sku change
assertThat(updateActions).containsOnlyOnce(SetSku.of(4, "var-44-sku", true));
// verify image update of variant 4
RemoveImage removeImage = RemoveImage.ofVariantId(4, "https://xxx.ggg/4.png", true);
AddExternalImage addExternalImage = AddExternalImage.ofVariantId(4, productDraftNew.getVariants().get(0).getImages().get(0), true);
assertThat(updateActions).containsOnlyOnce(removeImage);
assertThat(updateActions).containsOnlyOnce(addExternalImage);
assertThat(updateActions.indexOf(removeImage)).withFailMessage("Remove image action must be executed before add image action").isLessThan(updateActions.indexOf(addExternalImage));
// verify attributes changes
assertThat(updateActions).contains(SetAttribute.ofVariantId(4, "priceInfo", "44/kg", true));
// change master variant must be always after variants are added/updated,
// because it is set by SKU and we should be sure the master variant is already added and SKUs
// are actual.
// Also, master variant should be removed because it is missing in
// NEW_PROD_DRAFT_WITH_VARIANTS_REMOVE_MASTER
final int size = updateActions.size();
assertThat(updateActions.subList(size - 2, size)).containsExactly(ChangeMasterVariant.ofSku("var-7-sku", true), RemoveVariant.of(productOld.getMasterVariant()));
}
use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.
the class ProductUpdateActionUtilsTest method buildVariantsUpdateActions_updateVariantsWithSameForAll.
@Test
void buildVariantsUpdateActions_updateVariantsWithSameForAll() {
// preparation
final ProductProjection productOld = createProductFromJson(OLD_PROD_WITH_VARIANTS);
final ProductDraft productDraftNew = createProductDraftFromJson(NEW_PROD_DRAFT_WITH_MATCHING_VARIANTS_WITH_UPDATED_ATTR_VALUES);
final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(mock(SphereClient.class)).syncFilter(SyncFilter.of()).build();
final Map<String, AttributeMetaData> attributesMetaData = new HashMap<>();
final AttributeMetaData priceInfo = AttributeMetaData.of(AttributeDefinitionBuilder.of("priceInfo", null, null).attributeConstraint(AttributeConstraint.SAME_FOR_ALL).build());
final AttributeMetaData size = AttributeMetaData.of(AttributeDefinitionBuilder.of("size", null, null).build());
attributesMetaData.put("priceInfo", priceInfo);
attributesMetaData.put("size", size);
final List<UpdateAction<Product>> updateActions = buildVariantsUpdateActions(productOld, productDraftNew, productSyncOptions, attributesMetaData);
// check that we only have one generated action for all the variants and no duplicates
assertThat(updateActions.size()).isEqualTo(3);
assertThat(updateActions).containsOnlyOnce(SetAttributeInAllVariants.of(AttributeDraft.of("priceInfo", "74,90/kg"), true));
// Other update actions can be duplicated per variant
assertThat(updateActions).containsOnlyOnce(SetAttribute.of(2, AttributeDraft.of("size", "ca. 1 x 1200 g"), true));
assertThat(updateActions).containsOnlyOnce(SetAttribute.of(3, AttributeDraft.of("size", "ca. 1 x 1200 g"), true));
}
use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.
the class BuildProductVariantAttributeUpdateActionsTest method withSameValues_ShouldNotBuildAction.
@Test
void withSameValues_ShouldNotBuildAction() throws BuildUpdateActionException {
// Preparation
final Attribute oldAttribute = Attribute.of("foo", JsonNodeFactory.instance.textNode("foo"));
final AttributeDraft newAttribute = AttributeDraft.of("foo", JsonNodeFactory.instance.textNode("foo"));
final Map<String, AttributeMetaData> attributesMetaData = new HashMap<>();
final AttributeDefinition attributeDefinition = AttributeDefinitionBuilder.of(newAttribute.getName(), ofEnglish("foo"), StringAttributeType.of()).build();
attributesMetaData.put(newAttribute.getName(), AttributeMetaData.of(attributeDefinition));
// Test
final Optional<UpdateAction<Product>> actionOptional = buildProductVariantAttributeUpdateAction(1, oldAttribute, newAttribute, attributesMetaData);
// Assertion
assertThat(actionOptional).isEmpty();
}
use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.
the class BuildProductVariantAttributesUpdateActionsTest method withEmptyNewAttributes_ShouldBuildUnSetAttributeActions.
@Test
void withEmptyNewAttributes_ShouldBuildUnSetAttributeActions() {
// Preparation
final String productKey = "foo";
final String variantKey = "foo";
when(oldProduct.getKey()).thenReturn(productKey);
when(newProductVariant.getAttributes()).thenReturn(emptyList());
when(newProductVariant.getKey()).thenReturn(variantKey);
final List<Attribute> oldAttributes = asList(BOOLEAN_ATTRIBUTE_TRUE, TEXT_ATTRIBUTE_BAR);
when(oldProductVariant.getAttributes()).thenReturn(oldAttributes);
when(oldProductVariant.getKey()).thenReturn(variantKey);
final HashMap<String, AttributeMetaData> attributesMetaData = new HashMap<>();
final AttributeDefinition booleanAttributeDefinition = AttributeDefinitionBuilder.of(BOOLEAN_ATTRIBUTE_TRUE.getName(), ofEnglish("label"), BooleanAttributeType.of()).build();
final AttributeDefinition textAttributeDefinition = AttributeDefinitionBuilder.of(TEXT_ATTRIBUTE_BAR.getName(), ofEnglish("label"), StringAttributeType.of()).build();
attributesMetaData.put(BOOLEAN_ATTRIBUTE_TRUE.getName(), AttributeMetaData.of(booleanAttributeDefinition));
attributesMetaData.put(TEXT_ATTRIBUTE_BAR.getName(), AttributeMetaData.of(textAttributeDefinition));
// Test
final List<UpdateAction<Product>> updateActions = buildProductVariantAttributesUpdateActions(oldProduct, newProductDraft, oldProductVariant, newProductVariant, attributesMetaData, syncOptions);
// Assertion
assertThat(updateActions).containsExactly(SetAttribute.ofUnsetAttribute(oldProductVariant.getId(), BOOLEAN_ATTRIBUTE_TRUE.getName(), true), SetAttribute.ofUnsetAttribute(oldProductVariant.getId(), TEXT_ATTRIBUTE_BAR.getName(), true));
}
Aggregations