Search in sources :

Example 6 with AttributeMetaData

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

the class BuildProductVariantAttributeUpdateActionsTest method withNullOldAndNonNullNew_WithNoExistingAttributeInMetaData_ShouldThrowException.

@Test
void withNullOldAndNonNullNew_WithNoExistingAttributeInMetaData_ShouldThrowException() {
    // Preparation
    final Attribute oldAttribute = null;
    final AttributeDraft newAttribute = AttributeDraft.of("foo", JsonNodeFactory.instance.objectNode());
    final Map<String, AttributeMetaData> attributesMetaData = new HashMap<>();
    // Test and assertion
    assertThatThrownBy(() -> buildProductVariantAttributeUpdateAction(1, oldAttribute, newAttribute, attributesMetaData)).hasMessage(format(ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA, newAttribute.getName())).isExactlyInstanceOf(BuildUpdateActionException.class);
}
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) Test(org.junit.jupiter.api.Test)

Example 7 with AttributeMetaData

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

the class BuildProductVariantAttributesUpdateActionsTest method withAllChangedMatchingAttrs_WithSomeNoExistingMetaData_ShouldBuildSomeActionsAndTriggerErrorCallback.

@Test
void withAllChangedMatchingAttrs_WithSomeNoExistingMetaData_ShouldBuildSomeActionsAndTriggerErrorCallback() {
    // 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 textAttributeDefinition = AttributeDefinitionBuilder.of(TEXT_ATTRIBUTE_BAR.getName(), ofEnglish("label"), StringAttributeType.of()).build();
    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));
    assertThat(errorMessages).containsExactly(format(FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION, BOOLEAN_ATTRIBUTE_TRUE.getName(), variantKey, productKey, format(ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA, BOOLEAN_ATTRIBUTE_TRUE.getName())));
}
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 8 with AttributeMetaData

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

the class BuildProductVariantAttributesUpdateActionsTest method withNullNewAttributes_WithSameForAllAttributes_ShouldBuildUnSetAllAttributeActions.

@Test
void withNullNewAttributes_WithSameForAllAttributes_ShouldBuildUnSetAllAttributeActions() {
    // 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()).attributeConstraint(AttributeConstraint.SAME_FOR_ALL).build();
    final AttributeDefinition textAttributeDefinition = AttributeDefinitionBuilder.of(TEXT_ATTRIBUTE_BAR.getName(), ofEnglish("label"), StringAttributeType.of()).attributeConstraint(AttributeConstraint.SAME_FOR_ALL).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(SetAttributeInAllVariants.ofUnsetAttribute(BOOLEAN_ATTRIBUTE_TRUE.getName(), true), SetAttributeInAllVariants.ofUnsetAttribute(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 9 with AttributeMetaData

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

the class BuildProductVariantAttributesUpdateActionsTest method withSomeNullNewAttributesAndExistingAttributes_ShouldBuildActionsAndTriggerErrorCallback.

@Test
void withSomeNullNewAttributesAndExistingAttributes_ShouldBuildActionsAndTriggerErrorCallback() {
    // 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(), null);
    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));
    assertThat(errorMessages).containsExactly(format(FAILED_TO_BUILD_ATTRIBUTE_UPDATE_ACTION, null, variantKey, productKey, NULL_PRODUCT_VARIANT_ATTRIBUTE));
}
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 10 with AttributeMetaData

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

the class BuildProductVariantAttributesUpdateActionsTest method withNonEmptyNewAttributesButEmptyExistingAttributes_ShouldBuildSetAttributeActions.

@Test
void withNonEmptyNewAttributesButEmptyExistingAttributes_ShouldBuildSetAttributeActions() {
    // 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_BAR).build());
    when(newProductVariant.getAttributes()).thenReturn(newAttributes);
    when(newProductVariant.getKey()).thenReturn(variantKey);
    when(oldProductVariant.getAttributes()).thenReturn(emptyList());
    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(), newAttributes.get(0), true), SetAttribute.of(oldProductVariant.getId(), newAttributes.get(1), true));
}
Also used : AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) 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