Search in sources :

Example 1 with PriceCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithDifferentCustomFieldValues_ShouldBuildUpdateActions.

@Test
void buildSetCustomFieldsUpdateActions_WithDifferentCustomFieldValues_ShouldBuildUpdateActions() {
    final Map<String, JsonNode> oldCustomFields = new HashMap<>();
    oldCustomFields.put("invisibleInShop", JsonNodeFactory.instance.booleanNode(false));
    oldCustomFields.put("backgroundColor", JsonNodeFactory.instance.objectNode().put("de", "rot").put("en", "red"));
    final Map<String, JsonNode> newCustomFields = new HashMap<>();
    newCustomFields.put("invisibleInShop", JsonNodeFactory.instance.booleanNode(true));
    newCustomFields.put("backgroundColor", JsonNodeFactory.instance.objectNode().put("de", "rot"));
    final List<UpdateAction<Product>> updateActions = buildSetCustomFieldsUpdateActions(oldCustomFields, newCustomFields, mock(Price.class), new PriceCustomActionBuilder(), 1, Price::getId);
    assertThat(updateActions).isNotNull();
    assertThat(updateActions).isNotEmpty();
    assertThat(updateActions).hasSize(2);
    final UpdateAction<Product> categoryUpdateAction = updateActions.get(0);
    assertThat(categoryUpdateAction).isNotNull();
    assertThat(categoryUpdateAction).isInstanceOf(SetProductPriceCustomField.class);
}
Also used : HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) Price(io.sphere.sdk.products.Price) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) Product(io.sphere.sdk.products.Product) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test)

Example 2 with PriceCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithRemovedCustomFields_ShouldBuildUpdateActions.

@Test
void buildSetCustomFieldsUpdateActions_WithRemovedCustomFields_ShouldBuildUpdateActions() {
    final Map<String, JsonNode> oldCustomFields = new HashMap<>();
    oldCustomFields.put("invisibleInShop", JsonNodeFactory.instance.booleanNode(true));
    oldCustomFields.put("backgroundColor", JsonNodeFactory.instance.objectNode().put("de", "rot").put("en", "red"));
    final Map<String, JsonNode> newCustomFields = new HashMap<>();
    newCustomFields.put("invisibleInShop", JsonNodeFactory.instance.booleanNode(true));
    final List<UpdateAction<Product>> updateActions = buildSetCustomFieldsUpdateActions(oldCustomFields, newCustomFields, mock(Price.class), new PriceCustomActionBuilder(), 1, Price::getId);
    assertThat(updateActions).hasSize(1).singleElement().matches(action -> {
        assertThat(action).isInstanceOf(SetProductPriceCustomField.class);
        final SetProductPriceCustomField setProductPriceCustomFieldAction = (SetProductPriceCustomField) action;
        assertThat(setProductPriceCustomFieldAction.getName()).isEqualTo("backgroundColor");
        assertThat(setProductPriceCustomFieldAction.getValue()).isEqualTo(null);
        return true;
    });
}
Also used : HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) Price(io.sphere.sdk.products.Price) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) JsonNode(com.fasterxml.jackson.databind.JsonNode) SetProductPriceCustomField(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomField) Test(org.junit.jupiter.api.Test)

Example 3 with PriceCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithNullIds_ShouldThrowBuildUpdateActionException.

@Test
void buildNonNullCustomFieldsUpdateActions_WithNullIds_ShouldThrowBuildUpdateActionException() {
    final Reference<Type> productPriceTypeReference = Type.referenceOfId(null);
    // Mock old CustomFields
    final CustomFields oldCustomFieldsMock = mock(CustomFields.class);
    when(oldCustomFieldsMock.getType()).thenReturn(productPriceTypeReference);
    // Mock new CustomFieldsDraft
    final CustomFieldsDraft newCustomFieldsMock = mock(CustomFieldsDraft.class);
    when(newCustomFieldsMock.getType()).thenReturn(productPriceTypeReference);
    assertThatThrownBy(() -> buildNonNullCustomFieldsUpdateActions(oldCustomFieldsMock, newCustomFieldsMock, mock(Price.class), new PriceCustomActionBuilder(), 1, Price::getId, priceResource -> Price.resourceTypeId(), Price::getId, ProductSyncOptionsBuilder.of(CTP_CLIENT).build())).isInstanceOf(BuildUpdateActionException.class).hasMessageMatching("Custom type ids are not set for both the old and new product-price.");
}
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) SetProductPriceCustomType(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomType) SetAssetCustomType(io.sphere.sdk.products.commands.updateactions.SetAssetCustomType) Type(io.sphere.sdk.types.Type) CustomFields(io.sphere.sdk.types.CustomFields) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) Test(org.junit.jupiter.api.Test)

Example 4 with PriceCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithSameIdsButNullNewCustomFields_ShouldBuildUpdateActions.

@Test
void buildNonNullCustomFieldsUpdateActions_WithSameIdsButNullNewCustomFields_ShouldBuildUpdateActions() throws BuildUpdateActionException {
    final Reference<Type> productPriceTypeReference = Type.referenceOfId("productPriceCustomTypeId");
    // Mock old CustomFields
    final CustomFields oldCustomFieldsMock = mock(CustomFields.class);
    final Map<String, JsonNode> oldCustomFieldsJsonMapMock = new HashMap<>();
    oldCustomFieldsJsonMapMock.put("invisibleInShop", JsonNodeFactory.instance.booleanNode(true));
    when(oldCustomFieldsMock.getType()).thenReturn(productPriceTypeReference);
    when(oldCustomFieldsMock.getFieldsJsonMap()).thenReturn(oldCustomFieldsJsonMapMock);
    // Mock new CustomFieldsDraft
    final CustomFieldsDraft newCustomFieldsMock = mock(CustomFieldsDraft.class);
    when(newCustomFieldsMock.getType()).thenReturn(productPriceTypeReference);
    when(newCustomFieldsMock.getFields()).thenReturn(null);
    final List<UpdateAction<Product>> updateActions = buildNonNullCustomFieldsUpdateActions(oldCustomFieldsMock, newCustomFieldsMock, mock(Price.class), new PriceCustomActionBuilder(), 1, Price::getId, priceResource -> Price.resourceTypeId(), Price::getId, ProductSyncOptionsBuilder.of(CTP_CLIENT).build());
    assertThat(updateActions).isNotNull();
    assertThat(updateActions).hasSize(1);
    assertThat(updateActions.get(0)).isInstanceOf(SetProductPriceCustomType.class);
}
Also used : SetProductPriceCustomType(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomType) SetAssetCustomType(io.sphere.sdk.products.commands.updateactions.SetAssetCustomType) Type(io.sphere.sdk.types.Type) CustomFields(io.sphere.sdk.types.CustomFields) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) Price(io.sphere.sdk.products.Price) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) Test(org.junit.jupiter.api.Test)

Example 5 with PriceCustomActionBuilder

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

the class ProductPriceCustomUpdateActionUtilsTest method buildSetCustomFieldAction_WithProductPrice_ShouldBuildProductUpdateAction.

@Test
void buildSetCustomFieldAction_WithProductPrice_ShouldBuildProductUpdateAction() {
    final String priceId = "1";
    final String customFieldName = "name";
    final JsonNode customFieldValue = JsonNodeFactory.instance.textNode("foo");
    final UpdateAction<Product> updateAction = new PriceCustomActionBuilder().buildSetCustomFieldAction(1, priceId, customFieldName, customFieldValue);
    assertThat(updateAction).isInstanceOf(SetProductPriceCustomField.class);
    assertThat((SetProductPriceCustomField) updateAction).hasValues("setProductPriceCustomField", true, priceId, customFieldName, customFieldValue);
}
Also used : PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) Product(io.sphere.sdk.products.Product) JsonNode(com.fasterxml.jackson.databind.JsonNode) SetProductPriceCustomField(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomField) Test(org.junit.jupiter.api.Test)

Aggregations

PriceCustomActionBuilder (com.commercetools.sync.products.helpers.PriceCustomActionBuilder)10 Test (org.junit.jupiter.api.Test)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 UpdateAction (io.sphere.sdk.commands.UpdateAction)8 Price (io.sphere.sdk.products.Price)8 HashMap (java.util.HashMap)7 Product (io.sphere.sdk.products.Product)6 SetProductPriceCustomField (io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomField)5 SetProductPriceCustomType (io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomType)5 CustomFields (io.sphere.sdk.types.CustomFields)4 CustomFieldsDraft (io.sphere.sdk.types.CustomFieldsDraft)4 ProductSyncOptionsBuilder (com.commercetools.sync.products.ProductSyncOptionsBuilder)3 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)3 SphereClient (io.sphere.sdk.client.SphereClient)3 CategorySyncOptions (com.commercetools.sync.categories.CategorySyncOptions)2 CategorySyncOptionsBuilder (com.commercetools.sync.categories.CategorySyncOptionsBuilder)2 CategoryCustomActionBuilder (com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder)2 BuildUpdateActionException (com.commercetools.sync.commons.exceptions.BuildUpdateActionException)2 SyncException (com.commercetools.sync.commons.exceptions.SyncException)2 CustomUpdateActionUtils.buildCustomUpdateActions (com.commercetools.sync.commons.utils.CustomUpdateActionUtils.buildCustomUpdateActions)2