Search in sources :

Example 21 with CategoryCustomActionBuilder

use of com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder in project commercetools-sync-java by commercetools.

the class ResourceCustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithNullJsonNodeNewValue_ShouldBuildAction.

@Test
void buildSetCustomFieldsUpdateActions_WithNullJsonNodeNewValue_ShouldBuildAction() {
    // preparation
    final Map<String, JsonNode> oldCustomFieldsMap = new HashMap<>();
    oldCustomFieldsMap.put("setOfBooleans", JsonNodeFactory.instance.arrayNode().add(JsonNodeFactory.instance.booleanNode(false)));
    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.nullNode());
    // test
    final List<UpdateAction<Category>> updateActions = buildSetCustomFieldsUpdateActions(oldCustomFieldsMap, newCustomFieldsMap, mock(Asset.class), new CategoryCustomActionBuilder(), null, category -> null);
    // assertion
    assertThat(updateActions).containsExactly(SetCustomField.ofJson("setOfBooleans", null));
}
Also used : CustomFields(io.sphere.sdk.types.CustomFields) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) Asset(io.sphere.sdk.models.Asset) JsonNode(com.fasterxml.jackson.databind.JsonNode) LocalizedString(io.sphere.sdk.models.LocalizedString) CategoryCustomActionBuilder(com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder) Test(org.junit.jupiter.api.Test)

Example 22 with CategoryCustomActionBuilder

use of com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder in project commercetools-sync-java by commercetools.

the class ResourceCustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithNullNewCategoryTypeId_TriggersErrorCallbackAndNoAction.

@Test
void buildNonNullCustomFieldsUpdateActions_WithNullNewCategoryTypeId_TriggersErrorCallbackAndNoAction() 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);
    final Reference<Type> value = Type.referenceOfId(null);
    when(newCustomFieldsMock.getType()).thenReturn(value);
    final String categoryId = UUID.randomUUID().toString();
    final Category category = mock(Category.class);
    when(category.getId()).thenReturn(categoryId);
    when(category.toReference()).thenReturn(Category.referenceOfId(categoryId));
    final List<UpdateAction<Category>> updateActions = buildNonNullCustomFieldsUpdateActions(oldCustomFieldsMock, newCustomFieldsMock, category, new CategoryCustomActionBuilder(), null, Category::getId, categoryRes -> category.toReference().getTypeId(), categoryRes -> null, categorySyncOptions);
    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).", category.toReference().getTypeId(), categoryId));
    assertThat(exceptions).hasSize(1);
    assertThat(updateActions).isEmpty();
}
Also used : CustomFields(io.sphere.sdk.types.CustomFields) SetCustomType(io.sphere.sdk.categories.commands.updateactions.SetCustomType) Type(io.sphere.sdk.types.Type) Category(io.sphere.sdk.categories.Category) UpdateAction(io.sphere.sdk.commands.UpdateAction) LocalizedString(io.sphere.sdk.models.LocalizedString) CategoryCustomActionBuilder(com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) Test(org.junit.jupiter.api.Test)

Example 23 with CategoryCustomActionBuilder

use of com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder in project commercetools-sync-java by commercetools.

the class ResourceCustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithSameIdsButNullNewCustomFields_ShouldBuildUpdateActions.

@Test
void buildNonNullCustomFieldsUpdateActions_WithSameIdsButNullNewCustomFields_ShouldBuildUpdateActions() throws BuildUpdateActionException {
    final Reference<Type> categoryTypeReference = Type.referenceOfId("categoryCustomTypeId");
    // 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(categoryTypeReference);
    when(oldCustomFieldsMock.getFieldsJsonMap()).thenReturn(oldCustomFieldsJsonMapMock);
    // Mock new CustomFieldsDraft
    final CustomFieldsDraft newCustomFieldsMock = mock(CustomFieldsDraft.class);
    when(newCustomFieldsMock.getType()).thenReturn(categoryTypeReference);
    when(newCustomFieldsMock.getFields()).thenReturn(null);
    final List<UpdateAction<Category>> updateActions = buildNonNullCustomFieldsUpdateActions(oldCustomFieldsMock, newCustomFieldsMock, mock(Category.class), new CategoryCustomActionBuilder(), null, Category::getId, category -> category.toReference().getTypeId(), category -> null, categorySyncOptions);
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(updateActions).isNotNull();
    assertThat(updateActions).hasSize(1);
    assertThat(updateActions.get(0)).isInstanceOf(SetCustomType.class);
}
Also used : SetCustomType(io.sphere.sdk.categories.commands.updateactions.SetCustomType) Type(io.sphere.sdk.types.Type) CustomFields(io.sphere.sdk.types.CustomFields) Category(io.sphere.sdk.categories.Category) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) JsonNode(com.fasterxml.jackson.databind.JsonNode) LocalizedString(io.sphere.sdk.models.LocalizedString) CategoryCustomActionBuilder(com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) Test(org.junit.jupiter.api.Test)

Example 24 with CategoryCustomActionBuilder

use of com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder in project commercetools-sync-java by commercetools.

the class ResourceCustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithDifferentCategoryTypeIds_ShouldBuildUpdateActions.

@Test
void buildNonNullCustomFieldsUpdateActions_WithDifferentCategoryTypeIds_ShouldBuildUpdateActions() throws BuildUpdateActionException {
    // Mock old CustomFields
    final CustomFields oldCustomFieldsMock = mock(CustomFields.class);
    when(oldCustomFieldsMock.getType()).thenReturn(Type.referenceOfId("categoryCustomTypeId"));
    // Mock new CustomFieldsDraft
    final CustomFieldsDraft newCustomFieldsMock = mock(CustomFieldsDraft.class);
    when(newCustomFieldsMock.getType()).thenReturn(ResourceIdentifier.ofId("newCategoryCustomTypeId"));
    final List<UpdateAction<Category>> updateActions = buildNonNullCustomFieldsUpdateActions(oldCustomFieldsMock, newCustomFieldsMock, mock(Category.class), new CategoryCustomActionBuilder(), null, Category::getId, category -> category.toReference().getTypeId(), category -> null, categorySyncOptions);
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(updateActions).isNotNull();
    assertThat(updateActions).hasSize(1);
    assertThat(updateActions.get(0)).isInstanceOf(SetCustomType.class);
}
Also used : CustomFields(io.sphere.sdk.types.CustomFields) Category(io.sphere.sdk.categories.Category) UpdateAction(io.sphere.sdk.commands.UpdateAction) CategoryCustomActionBuilder(com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) Test(org.junit.jupiter.api.Test)

Example 25 with CategoryCustomActionBuilder

use of com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder in project commercetools-sync-java by commercetools.

the class ResourceCustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithOldCustomFieldNotInNewFields_ShouldBuildUpdateActions.

@Test
void buildSetCustomFieldsUpdateActions_WithOldCustomFieldNotInNewFields_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<Category>> setCustomFieldsUpdateActions = buildSetCustomFieldsUpdateActions(oldCustomFields, newCustomFields, mock(Category.class), new CategoryCustomActionBuilder(), null, category -> null);
    assertThat(setCustomFieldsUpdateActions).isNotNull();
    assertThat(setCustomFieldsUpdateActions).isNotEmpty();
    assertThat(setCustomFieldsUpdateActions).hasSize(1);
    assertThat(setCustomFieldsUpdateActions.get(0)).isInstanceOf(SetCustomField.class);
}
Also used : Category(io.sphere.sdk.categories.Category) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) JsonNode(com.fasterxml.jackson.databind.JsonNode) LocalizedString(io.sphere.sdk.models.LocalizedString) CategoryCustomActionBuilder(com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

CategoryCustomActionBuilder (com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder)26 Test (org.junit.jupiter.api.Test)26 UpdateAction (io.sphere.sdk.commands.UpdateAction)24 Category (io.sphere.sdk.categories.Category)21 JsonNode (com.fasterxml.jackson.databind.JsonNode)16 LocalizedString (io.sphere.sdk.models.LocalizedString)16 HashMap (java.util.HashMap)15 CustomFields (io.sphere.sdk.types.CustomFields)13 CustomFieldsDraft (io.sphere.sdk.types.CustomFieldsDraft)9 CategoryDraft (io.sphere.sdk.categories.CategoryDraft)7 SetCustomType (io.sphere.sdk.categories.commands.updateactions.SetCustomType)6 Asset (io.sphere.sdk.models.Asset)6 Type (io.sphere.sdk.types.Type)5 BooleanNode (com.fasterxml.jackson.databind.node.BooleanNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 SetCustomField (io.sphere.sdk.categories.commands.updateactions.SetCustomField)2 CategorySyncMockUtils (com.commercetools.sync.categories.CategorySyncMockUtils)1 CategorySyncOptions (com.commercetools.sync.categories.CategorySyncOptions)1 CategorySyncOptionsBuilder (com.commercetools.sync.categories.CategorySyncOptionsBuilder)1 BuildUpdateActionException (com.commercetools.sync.commons.exceptions.BuildUpdateActionException)1