Search in sources :

Example 16 with AttributeMetaData

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());
}
Also used : ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) LocalizedString(io.sphere.sdk.models.LocalizedString) Test(org.junit.jupiter.api.Test)

Example 17 with AttributeMetaData

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()));
}
Also used : AddExternalImage(io.sphere.sdk.products.commands.updateactions.AddExternalImage) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductUpdateActionUtils.buildChangeMasterVariantUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildChangeMasterVariantUpdateAction) RemoveImage(io.sphere.sdk.products.commands.updateactions.RemoveImage) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) AttributeConstraint(io.sphere.sdk.products.attributes.AttributeConstraint) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraft(io.sphere.sdk.products.ProductDraft) SphereClient(io.sphere.sdk.client.SphereClient) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Test(org.junit.jupiter.api.Test)

Example 18 with AttributeMetaData

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));
}
Also used : ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductUpdateActionUtils.buildChangeMasterVariantUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildChangeMasterVariantUpdateAction) SphereClient(io.sphere.sdk.client.SphereClient) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Test(org.junit.jupiter.api.Test)

Example 19 with AttributeMetaData

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();
}
Also used : Attribute(io.sphere.sdk.products.attributes.Attribute) SetAttribute(io.sphere.sdk.products.commands.updateactions.SetAttribute) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) HashMap(java.util.HashMap) ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction(com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction) UpdateAction(io.sphere.sdk.commands.UpdateAction) AttributeDefinition(io.sphere.sdk.products.attributes.AttributeDefinition) Test(org.junit.jupiter.api.Test)

Example 20 with AttributeMetaData

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));
}
Also used : Attribute(io.sphere.sdk.products.attributes.Attribute) SetAttribute(io.sphere.sdk.products.commands.updateactions.SetAttribute) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) AttributeDefinition(io.sphere.sdk.products.attributes.AttributeDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

AttributeMetaData (com.commercetools.sync.products.AttributeMetaData)30 HashMap (java.util.HashMap)28 UpdateAction (io.sphere.sdk.commands.UpdateAction)26 Test (org.junit.jupiter.api.Test)26 AttributeDraft (io.sphere.sdk.products.attributes.AttributeDraft)21 SetAttribute (io.sphere.sdk.products.commands.updateactions.SetAttribute)21 Attribute (io.sphere.sdk.products.attributes.Attribute)20 AttributeDefinition (io.sphere.sdk.products.attributes.AttributeDefinition)18 ProductDraft (io.sphere.sdk.products.ProductDraft)8 ProductSyncOptions (com.commercetools.sync.products.ProductSyncOptions)6 ProductProjection (io.sphere.sdk.products.ProductProjection)6 ProductVariantDraft (io.sphere.sdk.products.ProductVariantDraft)6 SyncFilter (com.commercetools.sync.products.SyncFilter)4 ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction (com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction)4 LocalizedString (io.sphere.sdk.models.LocalizedString)4 CollectionUtils.emptyIfNull (com.commercetools.sync.commons.utils.CollectionUtils.emptyIfNull)3 ActionGroup (com.commercetools.sync.products.ActionGroup)3 Product (io.sphere.sdk.products.Product)3 ProductVariant (io.sphere.sdk.products.ProductVariant)3 RemoveVariant (io.sphere.sdk.products.commands.updateactions.RemoveVariant)3