Search in sources :

Example 6 with PriceCustomActionBuilder

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

the class ProductPriceCustomUpdateActionUtilsTest method buildRemoveCustomTypeAction_WithProductPrice_ShouldBuildChannelUpdateAction.

@Test
void buildRemoveCustomTypeAction_WithProductPrice_ShouldBuildChannelUpdateAction() {
    final String priceId = "1";
    final UpdateAction<Product> updateAction = new PriceCustomActionBuilder().buildRemoveCustomTypeAction(1, priceId);
    assertThat(updateAction).isInstanceOf(SetProductPriceCustomType.class);
    assertThat((SetProductPriceCustomType) updateAction).hasValues("setProductPriceCustomType", priceId, true, null, ofId(null));
}
Also used : SetProductPriceCustomType(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomType) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) Product(io.sphere.sdk.products.Product) Test(org.junit.jupiter.api.Test)

Example 7 with PriceCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithNewCustomFields_ShouldBuildUpdateActions.

@Test
void buildSetCustomFieldsUpdateActions_WithNewCustomFields_ShouldBuildUpdateActions() {
    final Map<String, JsonNode> oldCustomFields = new HashMap<>();
    final Map<String, JsonNode> newCustomFields = new HashMap<>();
    newCustomFields.put("invisibleInShop", JsonNodeFactory.instance.booleanNode(true));
    newCustomFields.put("backgroundColor", JsonNodeFactory.instance.objectNode().put("de", "rot"));
    newCustomFields.put("url", JsonNodeFactory.instance.objectNode().put("domain", "domain.com"));
    newCustomFields.put("size", JsonNodeFactory.instance.objectNode().put("cm", 34));
    final List<UpdateAction<Product>> updateActions = buildSetCustomFieldsUpdateActions(oldCustomFields, newCustomFields, mock(Price.class), new PriceCustomActionBuilder(), 1, Price::getId);
    assertThat(updateActions).hasSize(4);
}
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) Test(org.junit.jupiter.api.Test)

Example 8 with PriceCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithNullNewCategoryTypeId_ShouldBuildUpdateActions.

@Test
void buildNonNullCustomFieldsUpdateActions_WithNullNewCategoryTypeId_ShouldBuildUpdateActions() throws BuildUpdateActionException {
    // Mock old CustomFields
    final CustomFields oldCustomFieldsMock = mock(CustomFields.class);
    when(oldCustomFieldsMock.getType()).thenReturn(Type.referenceOfId("1"));
    // Mock new CustomFieldsDraft
    final CustomFieldsDraft newCustomFieldsMock = mock(CustomFieldsDraft.class);
    when(newCustomFieldsMock.getType()).thenReturn(Type.referenceOfId(null));
    // Mock custom options error callback
    final ArrayList<String> errorMessages = new ArrayList<>();
    final QuadConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>, List<UpdateAction<Product>>> errorCallback = (exception, newResource, oldResource, updateActions) -> errorMessages.add(exception.getMessage());
    // Mock sync options
    final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(CTP_CLIENT).errorCallback(errorCallback).build();
    final Price price = mock(Price.class);
    when(price.getId()).thenReturn(UUID.randomUUID().toString());
    final List<UpdateAction<Product>> updateActions = buildNonNullCustomFieldsUpdateActions(oldCustomFieldsMock, newCustomFieldsMock, price, new PriceCustomActionBuilder(), 1, Price::getId, priceResource -> Price.resourceTypeId(), Price::getId, productSyncOptions);
    assertThat(errorMessages).hasSize(1);
    assertThat(errorMessages.get(0)).isEqualTo(format("Failed to build 'setCustomType' update action on the " + "%s with id '%s'. Reason: New Custom Type id is blank (null/empty).", Price.resourceTypeId(), price.getId()));
    assertThat(updateActions).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) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) ArrayList(java.util.ArrayList) Product(io.sphere.sdk.products.Product) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CustomFields(io.sphere.sdk.types.CustomFields) Price(io.sphere.sdk.products.Price) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) Test(org.junit.jupiter.api.Test)

Example 9 with PriceCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithNullOldCategoryTypeId_ShouldBuildUpdateActions.

@Test
void buildNonNullCustomFieldsUpdateActions_WithNullOldCategoryTypeId_ShouldBuildUpdateActions() throws BuildUpdateActionException {
    // Mock old CustomFields
    final CustomFields oldCustomFieldsMock = mock(CustomFields.class);
    when(oldCustomFieldsMock.getType()).thenReturn(Type.referenceOfId(null));
    // Mock new CustomFieldsDraft
    final CustomFieldsDraft newCustomFieldsMock = mock(CustomFieldsDraft.class);
    when(newCustomFieldsMock.getType()).thenReturn(Type.referenceOfId("priceCustomTypeId"));
    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 : CustomFields(io.sphere.sdk.types.CustomFields) UpdateAction(io.sphere.sdk.commands.UpdateAction) Price(io.sphere.sdk.products.Price) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) Test(org.junit.jupiter.api.Test)

Example 10 with PriceCustomActionBuilder

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

the class ProductPriceCustomUpdateActionUtilsTest method buildTypedSetCustomTypeUpdateAction_WithProductPrice_ShouldBuildProductUpdateAction.

@Test
void buildTypedSetCustomTypeUpdateAction_WithProductPrice_ShouldBuildProductUpdateAction() {
    final Price price = mock(Price.class);
    when(price.getId()).thenReturn("priceId");
    final String newCustomTypeId = UUID.randomUUID().toString();
    final UpdateAction<Product> updateAction = buildTypedSetCustomTypeUpdateAction(newCustomTypeId, new HashMap<>(), price, new PriceCustomActionBuilder(), 1, Price::getId, priceResource -> Price.resourceTypeId(), Price::getId, ProductSyncOptionsBuilder.of(mock(SphereClient.class)).build()).orElse(null);
    assertThat(updateAction).isInstanceOf(SetProductPriceCustomType.class);
    assertThat((SetProductPriceCustomType) updateAction).hasValues("setProductPriceCustomType", price.getId(), true, emptyMap(), ofId(newCustomTypeId));
}
Also used : SetProductPriceCustomType(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomType) Collections.emptyMap(java.util.Collections.emptyMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Price(io.sphere.sdk.products.Price) 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) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) AssertionsForUpdateActions.assertThat(com.commercetools.sync.commons.asserts.actions.AssertionsForUpdateActions.assertThat) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) SphereClient(io.sphere.sdk.client.SphereClient) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) JsonNode(com.fasterxml.jackson.databind.JsonNode) SetProductPriceCustomField(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomField) Mockito.mock(org.mockito.Mockito.mock) Price(io.sphere.sdk.products.Price) HashMap(java.util.HashMap) SetProductPriceCustomType(io.sphere.sdk.products.commands.updateactions.SetProductPriceCustomType) PriceCustomActionBuilder(com.commercetools.sync.products.helpers.PriceCustomActionBuilder) Product(io.sphere.sdk.products.Product) 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