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);
}
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())));
}
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));
}
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));
}
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));
}
Aggregations