use of com.commercetools.sync.products.helpers.AssetCustomActionBuilder in project commercetools-sync-java by commercetools.
the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithEmptyArrayAsSameValue_ShouldBuildSetAction.
@Test
void buildSetCustomFieldsUpdateActions_WithEmptyArrayAsSameValue_ShouldBuildSetAction() {
// preparation
final Map<String, JsonNode> oldCustomFieldsMap = new HashMap<>();
oldCustomFieldsMap.put("setOfBooleans", JsonNodeFactory.instance.arrayNode());
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.arrayNode());
// test
final List<UpdateAction<Product>> updateActions = buildSetCustomFieldsUpdateActions(new HashMap<>(), newCustomFieldsMap, mock(Asset.class), new AssetCustomActionBuilder(), 1, Asset::getId);
// assertion
assertThat(updateActions).containsExactly(SetAssetCustomField.ofVariantIdUsingJsonAndAssetKey(1, oldAsset.getKey(), "setOfBooleans", JsonNodeFactory.instance.arrayNode(), true));
}
use of com.commercetools.sync.products.helpers.AssetCustomActionBuilder in project commercetools-sync-java by commercetools.
the class CustomUpdateActionUtilsTest method buildCustomUpdateActions_WithNonNullCustomFieldsWithDifferentTypes_ShouldBuildUpdateActions.
@Test
void buildCustomUpdateActions_WithNonNullCustomFieldsWithDifferentTypes_ShouldBuildUpdateActions() {
final Asset oldAsset = mock(Asset.class);
final CustomFields oldAssetCustomFields = mock(CustomFields.class);
final Reference<Type> oldAssetCustomFieldsDraftTypeReference = Type.referenceOfId("2");
when(oldAssetCustomFields.getType()).thenReturn(oldAssetCustomFieldsDraftTypeReference);
when(oldAsset.getCustom()).thenReturn(oldAssetCustomFields);
final AssetDraft newAssetDraft = mock(AssetDraft.class);
final CustomFieldsDraft newAssetCustomFieldsDraft = mock(CustomFieldsDraft.class);
final ResourceIdentifier<Type> typeResourceIdentifier = Type.referenceOfId("1");
when(newAssetCustomFieldsDraft.getType()).thenReturn(typeResourceIdentifier);
when(newAssetDraft.getCustom()).thenReturn(newAssetCustomFieldsDraft);
final List<UpdateAction<Product>> updateActions = buildCustomUpdateActions(maiNewResource, oldAsset, newAssetDraft, new AssetCustomActionBuilder(), 10, Asset::getId, asset -> Asset.resourceTypeId(), Asset::getKey, ProductSyncOptionsBuilder.of(CTP_CLIENT).build());
// Should set custom type of old asset.
assertThat(updateActions).isNotNull();
assertThat(updateActions).hasSize(1);
assertThat(updateActions.get(0)).isInstanceOf(SetAssetCustomType.class);
}
use of com.commercetools.sync.products.helpers.AssetCustomActionBuilder in project commercetools-sync-java by commercetools.
the class CustomUpdateActionUtilsTest method buildCustomUpdateActions_WithNullOldCustomFieldsAndBlankNewTypeId_ShouldCallErrorCallBack.
@Test
void buildCustomUpdateActions_WithNullOldCustomFieldsAndBlankNewTypeId_ShouldCallErrorCallBack() {
final Asset oldAsset = mock(Asset.class);
final String oldAssetId = "oldAssetId";
when(oldAsset.getId()).thenReturn(oldAssetId);
when(oldAsset.getCustom()).thenReturn(null);
final AssetDraft newAssetDraft = AssetDraftBuilder.of(emptyList(), ofEnglish("assetName")).custom(ofTypeKeyAndJson("key", new HashMap<>())).build();
// Mock custom options error callback
final ArrayList<Object> callBackResponses = new ArrayList<>();
final QuadConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>, List<UpdateAction<Product>>> errorCallback = (exception, newResource, oldResource, updateActions) -> {
callBackResponses.add(exception.getMessage());
callBackResponses.add(exception.getCause());
};
// Mock sync options
final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(CTP_CLIENT).errorCallback(errorCallback).build();
final List<UpdateAction<Product>> updateActions = buildCustomUpdateActions(maiNewResource, oldAsset, newAssetDraft, new AssetCustomActionBuilder(), 10, Asset::getId, asset -> Asset.resourceTypeId(), Asset::getKey, productSyncOptions);
assertThat(updateActions).isNotNull();
assertThat(updateActions).hasSize(0);
assertThat(callBackResponses.get(0)).isEqualTo(format("Failed to build custom fields update actions on the " + "asset with id '%s'. Reason: New resource's custom type id is blank (empty/null).", oldAssetId));
assertThat(callBackResponses.get(1)).isNull();
}
use of com.commercetools.sync.products.helpers.AssetCustomActionBuilder in project commercetools-sync-java by commercetools.
the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithEmptyArrayAndNullOldValue_ShouldBuildSetAction.
@Test
void buildSetCustomFieldsUpdateActions_WithEmptyArrayAndNullOldValue_ShouldBuildSetAction() {
// preparation
final Map<String, JsonNode> oldCustomFieldsMap = new HashMap<>();
oldCustomFieldsMap.put("setOfBooleans", null);
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.arrayNode());
// test
final List<UpdateAction<Product>> updateActions = buildSetCustomFieldsUpdateActions(oldCustomFieldsMap, newCustomFieldsMap, mock(Asset.class), new AssetCustomActionBuilder(), 1, Asset::getId);
// assertion
assertThat(updateActions).containsExactly(SetAssetCustomField.ofVariantIdUsingJsonAndAssetKey(1, oldAsset.getKey(), "setOfBooleans", JsonNodeFactory.instance.arrayNode(), true));
}
use of com.commercetools.sync.products.helpers.AssetCustomActionBuilder in project commercetools-sync-java by commercetools.
the class ProductAssetCustomUpdateActionUtilsTest method buildRemoveCustomTypeAction_WithProductAsset_ShouldBuildChannelUpdateAction.
@Test
void buildRemoveCustomTypeAction_WithProductAsset_ShouldBuildChannelUpdateAction() {
final int variantId = 1;
final UpdateAction<Product> updateAction = new AssetCustomActionBuilder().buildRemoveCustomTypeAction(variantId, "assetKey");
assertThat(updateAction).isInstanceOf(SetAssetCustomType.class);
assertThat((SetAssetCustomType) updateAction).hasValues("setAssetCustomType", null, null, variantId, true, null, ofId(null));
}
Aggregations