Search in sources :

Example 16 with CategoryCustomActionBuilder

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

the class ResourceCustomUpdateActionUtilsTest method buildResourceCustomUpdateActions_WithNonNullCustomFieldsWithDifferentTypes_ShouldBuildUpdateActions.

@Test
void buildResourceCustomUpdateActions_WithNonNullCustomFieldsWithDifferentTypes_ShouldBuildUpdateActions() {
    final Category oldCategory = mock(Category.class);
    final CustomFields oldCategoryCustomFields = mock(CustomFields.class);
    final Reference<Type> oldCategoryCustomFieldsDraftTypeReference = Type.referenceOfId("2");
    when(oldCategoryCustomFields.getType()).thenReturn(oldCategoryCustomFieldsDraftTypeReference);
    when(oldCategory.getCustom()).thenReturn(oldCategoryCustomFields);
    final CategoryDraft newCategoryDraft = mock(CategoryDraft.class);
    final CustomFieldsDraft newCategoryCustomFieldsDraft = mock(CustomFieldsDraft.class);
    final ResourceIdentifier<Type> typeResourceIdentifier = Type.referenceOfId("1");
    when(newCategoryCustomFieldsDraft.getType()).thenReturn(typeResourceIdentifier);
    when(newCategoryDraft.getCustom()).thenReturn(newCategoryCustomFieldsDraft);
    final List<UpdateAction<Category>> updateActions = buildPrimaryResourceCustomUpdateActions(oldCategory, newCategoryDraft, new CategoryCustomActionBuilder(), categorySyncOptions);
    // Should set custom type of old category.
    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) 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) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategoryCustomActionBuilder(com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) Test(org.junit.jupiter.api.Test)

Example 17 with CategoryCustomActionBuilder

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

the class ResourceCustomUpdateActionUtilsTest 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<Category>> setCustomFieldsUpdateActions = buildSetCustomFieldsUpdateActions(oldCustomFields, newCustomFields, mock(Category.class), new CategoryCustomActionBuilder(), null, category -> null);
    assertThat(setCustomFieldsUpdateActions).isNotNull();
    assertThat(setCustomFieldsUpdateActions).isNotEmpty();
    assertThat(setCustomFieldsUpdateActions).hasSize(2);
    final UpdateAction<Category> categoryUpdateAction = setCustomFieldsUpdateActions.get(0);
    assertThat(categoryUpdateAction).isNotNull();
    assertThat(categoryUpdateAction).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)

Example 18 with CategoryCustomActionBuilder

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

the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithSameCustomFieldValues_ShouldNotBuildUpdateActions.

@Test
void buildSetCustomFieldsUpdateActions_WithSameCustomFieldValues_ShouldNotBuildUpdateActions() {
    final Map<String, JsonNode> oldCustomFields = new HashMap<>();
    oldCustomFields.put("invisibleInShop", JsonNodeFactory.instance.booleanNode(true));
    oldCustomFields.put("backgroundColor", JsonNodeFactory.instance.objectNode().put("de", "rot"));
    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<Category>> updateActions = buildSetCustomFieldsUpdateActions(oldCustomFields, newCustomFields, mock(Asset.class), new CategoryCustomActionBuilder(), 1, Asset::getId);
    assertThat(updateActions).isNotNull();
    assertThat(updateActions).isEmpty();
}
Also used : HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) Asset(io.sphere.sdk.models.Asset) JsonNode(com.fasterxml.jackson.databind.JsonNode) CategoryCustomActionBuilder(com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder) Test(org.junit.jupiter.api.Test)

Example 19 with CategoryCustomActionBuilder

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

the class CategoryCustomUpdateActionUtilsTest method buildRemoveCustomTypeAction_WithCategoryResource_ShouldBuildCategoryUpdateAction.

@Test
void buildRemoveCustomTypeAction_WithCategoryResource_ShouldBuildCategoryUpdateAction() {
    final UpdateAction<Category> updateAction = new CategoryCustomActionBuilder().buildRemoveCustomTypeAction(null, null);
    assertThat(updateAction).isInstanceOf(SetCustomType.class);
    assertThat((SetCustomType) updateAction).hasValues("setCustomType", null, ofId(null));
}
Also used : SetCustomType(io.sphere.sdk.categories.commands.updateactions.SetCustomType) Category(io.sphere.sdk.categories.Category) CategoryCustomActionBuilder(com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder) Test(org.junit.jupiter.api.Test)

Example 20 with CategoryCustomActionBuilder

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

the class ResourceCustomUpdateActionUtilsTest method buildResourceCustomUpdateActions_WithNullOldCustomFields_ShouldBuildUpdateActions.

@Test
void buildResourceCustomUpdateActions_WithNullOldCustomFields_ShouldBuildUpdateActions() {
    final Category oldCategory = mock(Category.class);
    when(oldCategory.getCustom()).thenReturn(null);
    final CategoryDraft newCategoryDraft = CategoryDraftBuilder.of(LocalizedString.ofEnglish("name"), LocalizedString.ofEnglish("testSlug")).key("key").parent(ResourceIdentifier.ofId("parentId")).custom(CustomFieldsDraft.ofTypeIdAndJson("customTypeId", new HashMap<>())).build();
    final List<UpdateAction<Category>> updateActions = buildPrimaryResourceCustomUpdateActions(oldCategory, newCategoryDraft, new CategoryCustomActionBuilder(), categorySyncOptions);
    // Should add custom type to old category.
    assertThat(errorMessages).isEmpty();
    assertThat(exceptions).isEmpty();
    assertThat(updateActions).isNotNull();
    assertThat(updateActions).hasSize(1);
    assertThat(updateActions.get(0)).isInstanceOf(SetCustomType.class);
}
Also used : Category(io.sphere.sdk.categories.Category) UpdateAction(io.sphere.sdk.commands.UpdateAction) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) 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