Search in sources :

Example 6 with AssetCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithEmptyArrayAsSameValue_ShouldBuildSetAction.

@Test
void buildSetCustomFieldsUpdateActions_WithEmptyArrayAsSameValue_ShouldBuildSetAction() {
    // preparation
    final Map<String, JsonNode> oldCustomFieldsMap = new HashMap<>();
    oldCustomFieldsMap.put("setOfBooleans", JsonNodeFactory.instance.arrayNode());
    final CustomFields oldCustomFields = mock(CustomFields.class);
    when(oldCustomFields.getFieldsJsonMap()).thenReturn(oldCustomFieldsMap);
    final Asset oldAsset = mock(Asset.class);
    when(oldAsset.getCustom()).thenReturn(oldCustomFields);
    final Map<String, JsonNode> newCustomFieldsMap = new HashMap<>();
    newCustomFieldsMap.put("setOfBooleans", JsonNodeFactory.instance.arrayNode());
    // test
    final List<UpdateAction<Product>> updateActions = buildSetCustomFieldsUpdateActions(new HashMap<>(), newCustomFieldsMap, mock(Asset.class), new AssetCustomActionBuilder(), 1, Asset::getId);
    // assertion
    assertThat(updateActions).containsExactly(SetAssetCustomField.ofVariantIdUsingJsonAndAssetKey(1, oldAsset.getKey(), "setOfBooleans", JsonNodeFactory.instance.arrayNode(), true));
}
Also used : CustomFields(io.sphere.sdk.types.CustomFields) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Asset(io.sphere.sdk.models.Asset) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test)

Example 7 with AssetCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildCustomUpdateActions_WithNonNullCustomFieldsWithDifferentTypes_ShouldBuildUpdateActions.

@Test
void buildCustomUpdateActions_WithNonNullCustomFieldsWithDifferentTypes_ShouldBuildUpdateActions() {
    final Asset oldAsset = mock(Asset.class);
    final CustomFields oldAssetCustomFields = mock(CustomFields.class);
    final Reference<Type> oldAssetCustomFieldsDraftTypeReference = Type.referenceOfId("2");
    when(oldAssetCustomFields.getType()).thenReturn(oldAssetCustomFieldsDraftTypeReference);
    when(oldAsset.getCustom()).thenReturn(oldAssetCustomFields);
    final AssetDraft newAssetDraft = mock(AssetDraft.class);
    final CustomFieldsDraft newAssetCustomFieldsDraft = mock(CustomFieldsDraft.class);
    final ResourceIdentifier<Type> typeResourceIdentifier = Type.referenceOfId("1");
    when(newAssetCustomFieldsDraft.getType()).thenReturn(typeResourceIdentifier);
    when(newAssetDraft.getCustom()).thenReturn(newAssetCustomFieldsDraft);
    final List<UpdateAction<Product>> updateActions = buildCustomUpdateActions(maiNewResource, oldAsset, newAssetDraft, new AssetCustomActionBuilder(), 10, Asset::getId, asset -> Asset.resourceTypeId(), Asset::getKey, ProductSyncOptionsBuilder.of(CTP_CLIENT).build());
    // Should set custom type of old asset.
    assertThat(updateActions).isNotNull();
    assertThat(updateActions).hasSize(1);
    assertThat(updateActions.get(0)).isInstanceOf(SetAssetCustomType.class);
}
Also used : CustomFields(io.sphere.sdk.types.CustomFields) SetProductPriceCustomType(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomType) SetAssetCustomType(io.sphere.sdk.products.commands.updateactions.SetAssetCustomType) Type(io.sphere.sdk.types.Type) AssetDraft(io.sphere.sdk.models.AssetDraft) UpdateAction(io.sphere.sdk.commands.UpdateAction) AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Asset(io.sphere.sdk.models.Asset) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) Test(org.junit.jupiter.api.Test)

Example 8 with AssetCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildCustomUpdateActions_WithNullOldCustomFieldsAndBlankNewTypeId_ShouldCallErrorCallBack.

@Test
void buildCustomUpdateActions_WithNullOldCustomFieldsAndBlankNewTypeId_ShouldCallErrorCallBack() {
    final Asset oldAsset = mock(Asset.class);
    final String oldAssetId = "oldAssetId";
    when(oldAsset.getId()).thenReturn(oldAssetId);
    when(oldAsset.getCustom()).thenReturn(null);
    final AssetDraft newAssetDraft = AssetDraftBuilder.of(emptyList(), ofEnglish("assetName")).custom(ofTypeKeyAndJson("key", new HashMap<>())).build();
    // Mock custom options error callback
    final ArrayList<Object> callBackResponses = new ArrayList<>();
    final QuadConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>, List<UpdateAction<Product>>> errorCallback = (exception, newResource, oldResource, updateActions) -> {
        callBackResponses.add(exception.getMessage());
        callBackResponses.add(exception.getCause());
    };
    // Mock sync options
    final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(CTP_CLIENT).errorCallback(errorCallback).build();
    final List<UpdateAction<Product>> updateActions = buildCustomUpdateActions(maiNewResource, oldAsset, newAssetDraft, new AssetCustomActionBuilder(), 10, Asset::getId, asset -> Asset.resourceTypeId(), Asset::getKey, productSyncOptions);
    assertThat(updateActions).isNotNull();
    assertThat(updateActions).hasSize(0);
    assertThat(callBackResponses.get(0)).isEqualTo(format("Failed to build custom fields update actions on the " + "asset with id '%s'. Reason: New resource's custom type id is blank (empty/null).", oldAssetId));
    assertThat(callBackResponses.get(1)).isNull();
}
Also used : CustomUpdateActionUtils.buildCustomUpdateActions(com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildCustomUpdateActions) SetProductPriceCustomType(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomType) AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Reference(io.sphere.sdk.models.Reference) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) Map(java.util.Map) CategoryCustomActionBuilder(com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder) SphereClient(io.sphere.sdk.client.SphereClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) AssetDraft(io.sphere.sdk.models.AssetDraft) AssetDraftBuilder(io.sphere.sdk.models.AssetDraftBuilder) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) Price(io.sphere.sdk.products.Price) CustomFieldsDraft.ofTypeKeyAndJson(io.sphere.sdk.types.CustomFieldsDraft.ofTypeKeyAndJson) Product(io.sphere.sdk.products.Product) UUID(java.util.UUID) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Optional(java.util.Optional) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) CustomFields(io.sphere.sdk.types.CustomFields) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CustomUpdateActionUtils.buildNonNullCustomFieldsUpdateActions(com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildNonNullCustomFieldsUpdateActions) SetAssetCustomField(io.sphere.sdk.products.commands.updateactions.SetAssetCustomField) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SetAssetCustomType(io.sphere.sdk.products.commands.updateactions.SetAssetCustomType) CustomUpdateActionUtils.buildSetCustomFieldsUpdateActions(com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildSetCustomFieldsUpdateActions) Mockito.when(org.mockito.Mockito.when) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) Resource(io.sphere.sdk.models.Resource) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) SetProductPriceCustomField(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomField) Asset(io.sphere.sdk.models.Asset) Type(io.sphere.sdk.types.Type) Optional(java.util.Optional) UpdateAction(io.sphere.sdk.commands.UpdateAction) ArrayList(java.util.ArrayList) AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Product(io.sphere.sdk.products.Product) SyncException(com.commercetools.sync.commons.exceptions.SyncException) AssetDraft(io.sphere.sdk.models.AssetDraft) Asset(io.sphere.sdk.models.Asset) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Test(org.junit.jupiter.api.Test)

Example 9 with AssetCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithEmptyArrayAndNullOldValue_ShouldBuildSetAction.

@Test
void buildSetCustomFieldsUpdateActions_WithEmptyArrayAndNullOldValue_ShouldBuildSetAction() {
    // preparation
    final Map<String, JsonNode> oldCustomFieldsMap = new HashMap<>();
    oldCustomFieldsMap.put("setOfBooleans", null);
    final CustomFields oldCustomFields = mock(CustomFields.class);
    when(oldCustomFields.getFieldsJsonMap()).thenReturn(oldCustomFieldsMap);
    final Asset oldAsset = mock(Asset.class);
    when(oldAsset.getCustom()).thenReturn(oldCustomFields);
    final Map<String, JsonNode> newCustomFieldsMap = new HashMap<>();
    newCustomFieldsMap.put("setOfBooleans", JsonNodeFactory.instance.arrayNode());
    // test
    final List<UpdateAction<Product>> updateActions = buildSetCustomFieldsUpdateActions(oldCustomFieldsMap, newCustomFieldsMap, mock(Asset.class), new AssetCustomActionBuilder(), 1, Asset::getId);
    // assertion
    assertThat(updateActions).containsExactly(SetAssetCustomField.ofVariantIdUsingJsonAndAssetKey(1, oldAsset.getKey(), "setOfBooleans", JsonNodeFactory.instance.arrayNode(), true));
}
Also used : CustomFields(io.sphere.sdk.types.CustomFields) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Asset(io.sphere.sdk.models.Asset) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test)

Example 10 with AssetCustomActionBuilder

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

the class ProductAssetCustomUpdateActionUtilsTest method buildRemoveCustomTypeAction_WithProductAsset_ShouldBuildChannelUpdateAction.

@Test
void buildRemoveCustomTypeAction_WithProductAsset_ShouldBuildChannelUpdateAction() {
    final int variantId = 1;
    final UpdateAction<Product> updateAction = new AssetCustomActionBuilder().buildRemoveCustomTypeAction(variantId, "assetKey");
    assertThat(updateAction).isInstanceOf(SetAssetCustomType.class);
    assertThat((SetAssetCustomType) updateAction).hasValues("setAssetCustomType", null, null, variantId, true, null, ofId(null));
}
Also used : SetAssetCustomType(io.sphere.sdk.products.commands.updateactions.SetAssetCustomType) AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Product(io.sphere.sdk.products.Product) Test(org.junit.jupiter.api.Test)

Aggregations

AssetCustomActionBuilder (com.commercetools.sync.products.helpers.AssetCustomActionBuilder)20 Test (org.junit.jupiter.api.Test)20 UpdateAction (io.sphere.sdk.commands.UpdateAction)18 Asset (io.sphere.sdk.models.Asset)18 JsonNode (com.fasterxml.jackson.databind.JsonNode)16 HashMap (java.util.HashMap)15 CustomFields (io.sphere.sdk.types.CustomFields)14 SetAssetCustomType (io.sphere.sdk.products.commands.updateactions.SetAssetCustomType)7 AssetDraft (io.sphere.sdk.models.AssetDraft)6 Product (io.sphere.sdk.products.Product)6 SetAssetCustomField (io.sphere.sdk.products.commands.updateactions.SetAssetCustomField)5 SetProductPriceCustomType (io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomType)5 CustomFieldsDraft (io.sphere.sdk.types.CustomFieldsDraft)5 Type (io.sphere.sdk.types.Type)5 ProductSyncOptionsBuilder (com.commercetools.sync.products.ProductSyncOptionsBuilder)4 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)4 SphereClient (io.sphere.sdk.client.SphereClient)4 CategorySyncOptions (com.commercetools.sync.categories.CategorySyncOptions)3 CategorySyncOptionsBuilder (com.commercetools.sync.categories.CategorySyncOptionsBuilder)3 CategoryCustomActionBuilder (com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder)3