use of com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder in project commercetools-sync-java by commercetools.
the class ResourceCustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithNullNewValue_ShouldBuildSetAction.
@Test
void buildSetCustomFieldsUpdateActions_WithNullNewValue_ShouldBuildSetAction() {
// 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", null);
// 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));
}
use of com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder in project commercetools-sync-java by commercetools.
the class ResourceCustomUpdateActionUtilsTest 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<Category>> updateActions = buildSetCustomFieldsUpdateActions(new HashMap<>(), newCustomFieldsMap, mock(Asset.class), new CategoryCustomActionBuilder(), null, category -> null);
// assertion
assertThat(updateActions).isEmpty();
}
use of com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder in project commercetools-sync-java by commercetools.
the class ResourceCustomUpdateActionUtilsTest method buildResourceCustomUpdateActions_WithNullOldCustomFieldsAndBlankNewTypeId_ShouldCallErrorCallBack.
@Test
void buildResourceCustomUpdateActions_WithNullOldCustomFieldsAndBlankNewTypeId_ShouldCallErrorCallBack() {
final Category oldCategory = mock(Category.class);
when(oldCategory.getCustom()).thenReturn(null);
final String oldCategoryId = "oldCategoryId";
when(oldCategory.getId()).thenReturn(oldCategoryId);
when(oldCategory.toReference()).thenReturn(Category.referenceOfId(oldCategoryId));
final CategoryDraft newCategoryDraft = CategorySyncMockUtils.getMockCategoryDraft(Locale.ENGLISH, "name", "slug", "key");
final CustomFieldsDraft mockCustomFieldsDraft = CustomFieldsDraft.ofTypeKeyAndJson("key", new HashMap<>());
when(newCategoryDraft.getCustom()).thenReturn(mockCustomFieldsDraft);
final List<UpdateAction<Category>> updateActions = buildPrimaryResourceCustomUpdateActions(oldCategory, newCategoryDraft, new CategoryCustomActionBuilder(), categorySyncOptions);
// Should add custom type to old category.
assertThat(updateActions).isNotNull();
assertThat(updateActions).hasSize(0);
assertThat(errorMessages.get(0)).isEqualTo(format("Failed to build custom fields update actions on the " + "category with id '%s'. Reason: New resource's custom type id is blank (empty/null).", oldCategoryId));
assertThat(exceptions.get(0)).isNull();
}
use of com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder in project commercetools-sync-java by commercetools.
the class ResourceCustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithBothNullCustomFields_ShouldNotBuildUpdateActions.
@Test
void buildNonNullCustomFieldsUpdateActions_WithBothNullCustomFields_ShouldNotBuildUpdateActions() {
final Category oldCategory = mock(Category.class);
when(oldCategory.getCustom()).thenReturn(null);
final CategoryDraft newCategoryDraft = mock(CategoryDraft.class);
when(newCategoryDraft.getCustom()).thenReturn(null);
final List<UpdateAction<Category>> updateActions = buildPrimaryResourceCustomUpdateActions(oldCategory, newCategoryDraft, new CategoryCustomActionBuilder(), categorySyncOptions);
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(updateActions).isNotNull();
assertThat(updateActions).isEmpty();
}
use of com.commercetools.sync.categories.helpers.CategoryCustomActionBuilder in project commercetools-sync-java by commercetools.
the class ResourceCustomUpdateActionUtilsTest method buildNonNullCustomFieldsUpdateActions_WithSameCategoryTypesButDifferentFieldValues_ShouldBuildUpdateActions.
@Test
void buildNonNullCustomFieldsUpdateActions_WithSameCategoryTypesButDifferentFieldValues_ShouldBuildUpdateActions() throws BuildUpdateActionException {
// 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(Type.referenceOfId("categoryCustomTypeId"));
when(oldCustomFieldsMock.getFieldsJsonMap()).thenReturn(oldCustomFieldsJsonMapMock);
// Mock new CustomFieldsDraft
final CustomFieldsDraft newCustomFieldsMock = mock(CustomFieldsDraft.class);
final Map<String, JsonNode> newCustomFieldsJsonMapMock = new HashMap<>();
newCustomFieldsJsonMapMock.put("invisibleInShop", JsonNodeFactory.instance.booleanNode(false));
when(newCustomFieldsMock.getType()).thenReturn(Type.referenceOfId("categoryCustomTypeId"));
when(newCustomFieldsMock.getFields()).thenReturn(newCustomFieldsJsonMapMock);
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(SetCustomField.class);
}
Aggregations