Search in sources :

Example 21 with AttributeMetaData

use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.

the class BuildProductVariantAttributesUpdateActionsTest method withNullNewAttributes_ShouldBuildUnSetAttributeActions.

@Test
void withNullNewAttributes_ShouldBuildUnSetAttributeActions() {
    // Preparation
    final String productKey = "foo";
    final String variantKey = "foo";
    when(oldProduct.getKey()).thenReturn(productKey);
    when(newProductVariant.getAttributes()).thenReturn(null);
    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)

Example 22 with AttributeMetaData

use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.

the class BuildProductVariantAttributesUpdateActionsTest method withSomeNonChangedMatchingAttributesAndNoNewAttributes_ShouldBuildUnSetAttributeActions.

@Test
void withSomeNonChangedMatchingAttributesAndNoNewAttributes_ShouldBuildUnSetAttributeActions() {
    // Preparation
    final String productKey = "foo";
    final String variantKey = "foo";
    when(oldProduct.getKey()).thenReturn(productKey);
    final List<AttributeDraft> newAttributes = singletonList(AttributeDraftBuilder.of(BOOLEAN_ATTRIBUTE_TRUE).build());
    when(newProductVariant.getAttributes()).thenReturn(newAttributes);
    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(), TEXT_ATTRIBUTE_BAR.getName(), true));
}
Also used : AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) 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)

Example 23 with AttributeMetaData

use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.

the class BuildProductVariantAttributesUpdateActionsTest method withAllChangedMatchingAttributes_ShouldBuildSetActions.

@Test
void withAllChangedMatchingAttributes_ShouldBuildSetActions() {
    // Preparation
    final String productKey = "foo";
    final String variantKey = "foo";
    when(oldProduct.getKey()).thenReturn(productKey);
    final List<AttributeDraft> newAttributes = asList(AttributeDraftBuilder.of(BOOLEAN_ATTRIBUTE_TRUE).build(), AttributeDraftBuilder.of(TEXT_ATTRIBUTE_FOO).build());
    when(newProductVariant.getAttributes()).thenReturn(newAttributes);
    when(newProductVariant.getKey()).thenReturn(variantKey);
    final List<Attribute> oldAttributes = asList(BOOLEAN_ATTRIBUTE_FALSE, 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).containsExactlyInAnyOrder(SetAttribute.of(oldProductVariant.getId(), AttributeDraftBuilder.of(TEXT_ATTRIBUTE_FOO).build(), true), SetAttribute.of(oldProductVariant.getId(), AttributeDraftBuilder.of(BOOLEAN_ATTRIBUTE_TRUE).build(), true));
}
Also used : AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) 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)

Example 24 with AttributeMetaData

use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.

the class BuildProductVariantAttributesUpdateActionsTest method withSomeChangedMatchingAttributes_ShouldBuildSetActions.

@Test
void withSomeChangedMatchingAttributes_ShouldBuildSetActions() {
    // Preparation
    final String productKey = "foo";
    final String variantKey = "foo";
    when(oldProduct.getKey()).thenReturn(productKey);
    final List<AttributeDraft> newAttributes = asList(AttributeDraftBuilder.of(BOOLEAN_ATTRIBUTE_TRUE).build(), AttributeDraftBuilder.of(TEXT_ATTRIBUTE_FOO).build());
    when(newProductVariant.getAttributes()).thenReturn(newAttributes);
    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.of(oldProductVariant.getId(), AttributeDraftBuilder.of(TEXT_ATTRIBUTE_FOO).build(), true));
}
Also used : AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) 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)

Example 25 with AttributeMetaData

use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.

the class BuildProductVariantAttributesUpdateActionsTest method withNoMatchingAttributes_ShouldBuildUnsetAndSetActionsInCorrectOrder.

@Test
void withNoMatchingAttributes_ShouldBuildUnsetAndSetActionsInCorrectOrder() {
    // Preparation
    final String productKey = "foo";
    final String variantKey = "foo";
    when(oldProduct.getKey()).thenReturn(productKey);
    final List<AttributeDraft> newAttributes = singletonList(AttributeDraftBuilder.of(BOOLEAN_ATTRIBUTE_TRUE).build());
    when(newProductVariant.getAttributes()).thenReturn(newAttributes);
    when(newProductVariant.getKey()).thenReturn(variantKey);
    final List<Attribute> oldAttributes = singletonList(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(), TEXT_ATTRIBUTE_BAR.getName(), true), SetAttribute.of(oldProductVariant.getId(), AttributeDraftBuilder.of(BOOLEAN_ATTRIBUTE_TRUE).build(), true));
}
Also used : AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) 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