use of com.commercetools.sync.products.helpers.AssetCustomActionBuilder in project commercetools-sync-java by commercetools.
the class CustomUpdateActionUtilsTest method buildCustomUpdateActions_WithNullOldCustomFields_ShouldBuildUpdateActions.
@Test
void buildCustomUpdateActions_WithNullOldCustomFields_ShouldBuildUpdateActions() {
final Asset oldAsset = mock(Asset.class);
when(oldAsset.getCustom()).thenReturn(null);
final CustomFieldsDraft newAssetCustomFieldsDraft = mock(CustomFieldsDraft.class);
final ResourceIdentifier<Type> typeResourceIdentifier = Type.referenceOfId("1");
when(newAssetCustomFieldsDraft.getType()).thenReturn(typeResourceIdentifier);
final AssetDraft newAssetDraft = AssetDraftBuilder.of(emptyList(), ofEnglish("assetName")).custom(newAssetCustomFieldsDraft).build();
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 add custom type to 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_WithNullIds_ShouldCallSyncOptionsCallBack.
@Test
void buildCustomUpdateActions_WithNullIds_ShouldCallSyncOptionsCallBack() {
final Reference<Type> assetCustomTypeReference = Type.referenceOfId(null);
// Mock old CustomFields
final CustomFields oldCustomFieldsMock = mock(CustomFields.class);
when(oldCustomFieldsMock.getType()).thenReturn(assetCustomTypeReference);
// Mock new CustomFieldsDraft
final CustomFieldsDraft newCustomFieldsMock = mock(CustomFieldsDraft.class);
when(newCustomFieldsMock.getType()).thenReturn(assetCustomTypeReference);
// Mock old Asset
final Asset oldAsset = mock(Asset.class);
final String oldAssetId = "oldAssetId";
when(oldAsset.getId()).thenReturn(oldAssetId);
when(oldAsset.getCustom()).thenReturn(oldCustomFieldsMock);
final AssetDraft newAssetDraft = AssetDraftBuilder.of(emptyList(), ofEnglish("assetName")).custom(newCustomFieldsMock).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(callBackResponses).hasSize(2);
assertThat(callBackResponses.get(0)).isEqualTo("Failed to build custom fields update actions on the asset" + " with id 'oldAssetId'. Reason: Custom type ids are not set for both the old and new asset.");
assertThat((Exception) callBackResponses.get(1)).isInstanceOf(BuildUpdateActionException.class);
assertThat(updateActions).isEmpty();
}
use of com.commercetools.sync.products.helpers.AssetCustomActionBuilder in project commercetools-sync-java by commercetools.
the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithEmptyCustomFields_ShouldNotBuildUpdateActions.
@Test
void buildSetCustomFieldsUpdateActions_WithEmptyCustomFields_ShouldNotBuildUpdateActions() {
final Map<String, JsonNode> oldCustomFields = new HashMap<>();
final Map<String, JsonNode> newCustomFields = new HashMap<>();
final List<UpdateAction<Product>> updateActions = buildSetCustomFieldsUpdateActions(oldCustomFields, newCustomFields, mock(Asset.class), new AssetCustomActionBuilder(), 1, Asset::getId);
assertThat(updateActions).isNotNull();
assertThat(updateActions).isEmpty();
}
use of com.commercetools.sync.products.helpers.AssetCustomActionBuilder in project commercetools-sync-java by commercetools.
the class CustomUpdateActionUtilsTest method buildSetCustomFieldsUpdateActions_WithNullJsonNodeNewValueOfNewField_ShouldNotBuildAction.
@Test
void buildSetCustomFieldsUpdateActions_WithNullJsonNodeNewValueOfNewField_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", JsonNodeFactory.instance.nullNode());
// test
final List<UpdateAction<Product>> updateActions = buildSetCustomFieldsUpdateActions(new HashMap<>(), newCustomFieldsMap, mock(Asset.class), new AssetCustomActionBuilder(), 1, Asset::getId);
// assertion
assertThat(updateActions).isEmpty();
}
use of com.commercetools.sync.products.helpers.AssetCustomActionBuilder in project commercetools-sync-java by commercetools.
the class CustomUpdateActionUtilsTest 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<Product>> updateActions = buildSetCustomFieldsUpdateActions(oldCustomFieldsMap, newCustomFieldsMap, mock(Asset.class), new AssetCustomActionBuilder(), 1, Asset::getId);
// assertion
assertThat(updateActions).containsExactly(SetAssetCustomField.ofVariantIdUsingJsonAndAssetKey(1, oldAsset.getKey(), "setOfBooleans", null, true));
}
Aggregations