use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.
the class BuildProductVariantAttributesUpdateActionsTest method withNoMatchingAttributes_ShouldBuildUnsetAndSetActions.
@Test
void withNoMatchingAttributes_ShouldBuildUnsetAndSetActions() {
// 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).containsExactlyInAnyOrder(SetAttribute.ofUnsetAttribute(oldProductVariant.getId(), TEXT_ATTRIBUTE_BAR.getName(), true), SetAttribute.of(oldProductVariant.getId(), AttributeDraftBuilder.of(BOOLEAN_ATTRIBUTE_TRUE).build(), true));
}
use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.
the class BuildProductVariantAttributesUpdateActionsTest method withSomeNonChangedMatchingAttributesAndNewAttributes_ShouldBuildSetAttributeActions.
@Test
void withSomeNonChangedMatchingAttributesAndNewAttributes_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(), AttributeDraftBuilder.of(TIME_ATTRIBUTE_10_08_46).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();
final AttributeDefinition timeAttributeDefinition = AttributeDefinitionBuilder.of(TIME_ATTRIBUTE_10_08_46.getName(), ofEnglish("label"), TimeAttributeType.of()).build();
attributesMetaData.put(BOOLEAN_ATTRIBUTE_TRUE.getName(), AttributeMetaData.of(booleanAttributeDefinition));
attributesMetaData.put(TEXT_ATTRIBUTE_BAR.getName(), AttributeMetaData.of(textAttributeDefinition));
attributesMetaData.put(TIME_ATTRIBUTE_10_08_46.getName(), AttributeMetaData.of(timeAttributeDefinition));
// Test
final List<UpdateAction<Product>> updateActions = buildProductVariantAttributesUpdateActions(oldProduct, newProductDraft, oldProductVariant, newProductVariant, attributesMetaData, syncOptions);
// Assertion
assertThat(updateActions).containsExactly(SetAttribute.of(oldProductVariant.getId(), AttributeDraftBuilder.of(TIME_ATTRIBUTE_10_08_46).build(), true));
}
use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.
the class BuildProductVariantAttributesUpdateActionsTest method withNoMatchingAttributes_WithSomeNoExistingMetaData_ShouldBuildSomeActionsAndTriggerErrorCallback.
@Test
void withNoMatchingAttributes_WithSomeNoExistingMetaData_ShouldBuildSomeActionsAndTriggerErrorCallback() {
// 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 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.ofUnsetAttribute(oldProductVariant.getId(), TEXT_ATTRIBUTE_BAR.getName(), 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 withAllMatchingAttributes_ShouldNotBuildActions.
@Test
void withAllMatchingAttributes_ShouldNotBuildActions() {
// 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);
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).isEmpty();
}
use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.
the class ProductUpdateActionUtils method buildVariantsUpdateActions.
/**
* Compares the variants (including the master variants) of a {@link ProductDraft} and a {@link
* Product}. It returns a {@link List} of variant related update actions. For example:
*
* <ul>
* <li>{@link AddVariant}
* <li>{@link RemoveVariant}
* <li>{@link ChangeMasterVariant}
* <li>{@link io.sphere.sdk.products.commands.updateactions.SetAttribute}
* <li>{@link io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants}
* <li>{@link io.sphere.sdk.products.commands.updateactions.SetSku}
* <li>{@link io.sphere.sdk.products.commands.updateactions.AddExternalImage}
* <li>{@link io.sphere.sdk.products.commands.updateactions.RemoveImage}
* <li>{@link io.sphere.sdk.products.commands.updateactions.AddPrice}
* <li>... and more variant level update actions.
* </ul>
*
* If both the {@link ProductProjection} and the {@link ProductDraft} have identical variants,
* then no update actions are needed and hence an empty {@link List} is returned.
*
* <p>NOTE: Comparison is done against the staged projection of the old product.
*
* <p>{@link ProductProjection} which should be updated.
*
* @param oldProduct the productprojection which should be updated.
* @param newProduct the product draft where we get the new meta title.
* @param syncOptions the sync options wrapper which contains options related to the sync process
* supplied by the user. For example, custom callbacks to call in case of warnings or errors
* occurring on the build update action process. And other options (See {@link
* ProductSyncOptions} for more info).
* @param attributesMetaData a map of attribute name -> {@link AttributeMetaData}; which
* defines attribute information: its name and whether it has the constraint "SameForAll" or
* not.
* @return A list of product variant-specific update actions.
*/
@Nonnull
public static List<UpdateAction<Product>> buildVariantsUpdateActions(@Nonnull final ProductProjection oldProduct, @Nonnull final ProductDraft newProduct, @Nonnull final ProductSyncOptions syncOptions, @Nonnull final Map<String, AttributeMetaData> attributesMetaData) {
if (haveInvalidMasterVariants(oldProduct, newProduct, syncOptions)) {
return emptyList();
}
final ProductVariant oldMasterVariant = oldProduct.getMasterVariant();
final List<ProductVariant> oldProductVariantsWithoutMaster = oldProduct.getVariants();
final Map<String, ProductVariant> oldProductVariantsNoMaster = collectionToMap(oldProductVariantsWithoutMaster, ProductVariant::getKey);
final Map<String, ProductVariant> oldProductVariantsWithMaster = new HashMap<>(oldProductVariantsNoMaster);
oldProductVariantsWithMaster.put(oldMasterVariant.getKey(), oldMasterVariant);
final List<ProductVariantDraft> newAllProductVariants = new ArrayList<>(newProduct.getVariants());
newAllProductVariants.add(newProduct.getMasterVariant());
// Remove missing variants, but keep master variant (MV can't be removed)
final List<UpdateAction<Product>> updateActions = buildRemoveUpdateActions(oldProductVariantsWithoutMaster, newAllProductVariants, ProductVariant::getKey, ProductVariantDraft::getKey, variant -> RemoveVariant.ofVariantId(variant.getId(), true));
emptyIfNull(newAllProductVariants).forEach(newProductVariant -> {
if (newProductVariant == null) {
handleBuildVariantsUpdateActionsError(oldProduct, NULL_VARIANT, syncOptions);
} else {
final String newProductVariantKey = newProductVariant.getKey();
if (isBlank(newProductVariantKey)) {
handleBuildVariantsUpdateActionsError(oldProduct, BLANK_VARIANT_KEY, syncOptions);
} else {
final ProductVariant matchingOldVariant = oldProductVariantsWithMaster.get(newProductVariantKey);
final List<UpdateAction<Product>> updateOrAddVariant = ofNullable(matchingOldVariant).map(oldVariant -> collectAllVariantUpdateActions(getSameForAllUpdateActions(updateActions), oldProduct, newProduct, oldVariant, newProductVariant, attributesMetaData, syncOptions)).orElseGet(() -> singletonList(buildAddVariantUpdateActionFromDraft(newProductVariant)));
updateActions.addAll(updateOrAddVariant);
}
}
});
updateActions.addAll(buildChangeMasterVariantUpdateAction(oldProduct, newProduct, syncOptions));
return updateActions;
}
Aggregations