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));
}
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();
}
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);
}
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);
}
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);
}
Aggregations