Search in sources :

Example 16 with AssetCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithBothNullCustomFields_ShouldNotBuildUpdateActions.

@Test
void buildNonNullCustomFieldsUpdateActions_WithBothNullCustomFields_ShouldNotBuildUpdateActions() {
    final Asset oldAsset = mock(Asset.class);
    when(oldAsset.getCustom()).thenReturn(null);
    final AssetDraft newAssetDraft = mock(AssetDraft.class);
    when(newAssetDraft.getCustom()).thenReturn(null);
    // 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).isEmpty();
    assertThat(callBackResponses).isEmpty();
}
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 17 with AssetCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithEmptyArrayAndNullNodeOldValue_ShouldBuildSetAction.

@Test
void buildSetCustomFieldsUpdateActions_WithEmptyArrayAndNullNodeOldValue_ShouldBuildSetAction() {
    // preparation
    final Map<String, JsonNode> oldCustomFieldsMap = new HashMap<>();
    oldCustomFieldsMap.put("setOfBooleans", JsonNodeFactory.instance.nullNode());
    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 18 with AssetCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithNullNewValueOfNewField_ShouldNotBuildAction.

@Test
void buildSetCustomFieldsUpdateActions_WithNullNewValueOfNewField_ShouldNotBuildAction() {
    // preparation
    final CustomFields oldCustomFields = mock(CustomFields.class);
    when(oldCustomFields.getFieldsJsonMap()).thenReturn(new HashMap<>());
    final Asset oldAsset = mock(Asset.class);
    when(oldAsset.getCustom()).thenReturn(oldCustomFields);
    final Map<String, JsonNode> newCustomFieldsMap = new HashMap<>();
    newCustomFieldsMap.put("setOfBooleans", null);
    // test
    final List<UpdateAction<Product>> updateActions = buildSetCustomFieldsUpdateActions(new HashMap<>(), newCustomFieldsMap, mock(Asset.class), new AssetCustomActionBuilder(), 1, Asset::getId);
    // assertion
    assertThat(updateActions).isEmpty();
}
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 19 with AssetCustomActionBuilder

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

the class ProductAssetCustomUpdateActionUtilsTest method buildTypedSetCustomTypeUpdateAction_WithProductAsset_ShouldBuildProductUpdateAction.

@Test
void buildTypedSetCustomTypeUpdateAction_WithProductAsset_ShouldBuildProductUpdateAction() {
    final Asset asset = mock(Asset.class);
    when(asset.getKey()).thenReturn("assetKey");
    final String newCustomTypeId = UUID.randomUUID().toString();
    final int variantId = 1;
    final UpdateAction<Product> updateAction = buildTypedSetCustomTypeUpdateAction(newCustomTypeId, new HashMap<>(), asset, new AssetCustomActionBuilder(), variantId, Asset::getId, assetResource -> Asset.resourceTypeId(), Asset::getKey, ProductSyncOptionsBuilder.of(mock(SphereClient.class)).build()).orElse(null);
    assertThat(updateAction).isInstanceOf(SetAssetCustomType.class);
    assertThat((SetAssetCustomType) updateAction).hasValues("setAssetCustomType", asset.getId(), null, variantId, true, emptyMap(), ofId(newCustomTypeId));
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UpdateAction(io.sphere.sdk.commands.UpdateAction) HashMap(java.util.HashMap) Mockito.when(org.mockito.Mockito.when) Product(io.sphere.sdk.products.Product) UUID(java.util.UUID) GenericUpdateActionUtils.buildTypedSetCustomTypeUpdateAction(com.commercetools.sync.commons.utils.GenericUpdateActionUtils.buildTypedSetCustomTypeUpdateAction) ResourceIdentifier.ofId(io.sphere.sdk.models.ResourceIdentifier.ofId) Test(org.junit.jupiter.api.Test) SetAssetCustomField(io.sphere.sdk.products.commands.updateactions.SetAssetCustomField) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) SetAssetCustomType(io.sphere.sdk.products.commands.updateactions.SetAssetCustomType) AssertionsForUpdateActions.assertThat(com.commercetools.sync.commons.asserts.actions.AssertionsForUpdateActions.assertThat) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) SphereClient(io.sphere.sdk.client.SphereClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) Asset(io.sphere.sdk.models.Asset) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) SetAssetCustomType(io.sphere.sdk.products.commands.updateactions.SetAssetCustomType) AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Asset(io.sphere.sdk.models.Asset) Product(io.sphere.sdk.products.Product) Test(org.junit.jupiter.api.Test)

Example 20 with AssetCustomActionBuilder

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

the class ProductAssetCustomUpdateActionUtilsTest method buildSetCustomFieldAction_WithProductAsset_ShouldBuildProductUpdateAction.

@Test
void buildSetCustomFieldAction_WithProductAsset_ShouldBuildProductUpdateAction() {
    final JsonNode customFieldValue = JsonNodeFactory.instance.textNode("foo");
    final String customFieldName = "name";
    final String assetKey = "assetKey";
    final int variantId = 1;
    final UpdateAction<Product> updateAction = new AssetCustomActionBuilder().buildSetCustomFieldAction(variantId, assetKey, customFieldName, customFieldValue);
    assertThat(updateAction).isInstanceOf(SetAssetCustomField.class);
    assertThat((SetAssetCustomField) updateAction).hasValues("setAssetCustomField", null, null, variantId, true, customFieldName, customFieldValue);
}
Also used : AssetCustomActionBuilder(com.commercetools.sync.products.helpers.AssetCustomActionBuilder) Product(io.sphere.sdk.products.Product) JsonNode(com.fasterxml.jackson.databind.JsonNode) SetAssetCustomField(io.sphere.sdk.products.commands.updateactions.SetAssetCustomField) 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