use of com.commercetools.sync.producttypes.ProductTypeSyncOptions in project commercetools-sync-java by commercetools.
the class ProductTypeWithNestedAttributeSyncIT method sync_WithUpdatedProductType_WithNewNestedAttributeInSeparateBatch_ShouldUpdateProductTypeAddingAttribute.
@Test
void sync_WithUpdatedProductType_WithNewNestedAttributeInSeparateBatch_ShouldUpdateProductTypeAddingAttribute() {
// preparation
final AttributeDefinitionDraft nestedTypeAttr = AttributeDefinitionDraftBuilder.of(AttributeDefinitionBuilder.of("nestedattr", ofEnglish("nestedattr"), SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(NestedAttributeType.of(ProductType.referenceOfId(PRODUCT_TYPE_KEY_1))))))).build()).searchable(false).build();
final ProductTypeDraft newProductTypeDraft = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_1, PRODUCT_TYPE_NAME_1, PRODUCT_TYPE_DESCRIPTION_1, asList(ATTRIBUTE_DEFINITION_DRAFT_1, ATTRIBUTE_DEFINITION_DRAFT_2, nestedTypeAttr));
final ProductTypeDraft productTypeDraft5 = ProductTypeDraft.ofAttributeDefinitionDrafts(PRODUCT_TYPE_KEY_5, PRODUCT_TYPE_NAME_5, PRODUCT_TYPE_DESCRIPTION_5, singletonList(ATTRIBUTE_DEFINITION_DRAFT_3));
productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(1).beforeUpdateCallback((actions, draft, oldProductType) -> {
builtUpdateActions.addAll(actions);
return actions;
}).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// tests
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(asList(newProductTypeDraft, productTypeDraft5)).toCompletableFuture().join();
// assertions
final Optional<ProductType> productType1 = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_1);
assert productType1.isPresent();
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(builtUpdateActions).containsExactly(AddAttributeDefinition.of(AttributeDefinitionDraftBuilder.of(AttributeDefinitionBuilder.of("nestedattr", ofEnglish("nestedattr"), SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(SetAttributeType.of(NestedAttributeType.of(productType1.get())))))).build()).searchable(false).build()));
assertThat(productTypeSyncStatistics).hasValues(2, 1, 1, 0, 0);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 product types were processed in total" + " (1 created, 1 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).isEmpty();
final Optional<ProductType> productType5 = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_5);
assertThat(productType5).hasValueSatisfying(productType -> {
assertThat(productType.getName()).isEqualTo(PRODUCT_TYPE_NAME_5);
assertThat(productType.getDescription()).isEqualTo(PRODUCT_TYPE_DESCRIPTION_5);
assertThat(productType.getAttributes()).hasSize(1);
});
assertThat(productType1).hasValueSatisfying(productType -> assertThat(productType.getAttributes()).hasSize(3));
}
use of com.commercetools.sync.producttypes.ProductTypeSyncOptions in project commercetools-sync-java by commercetools.
the class ProductTypeWithNestedAttributeSyncIT method sync_WithUpdates_ShouldReturnProperStatistics.
@Test
void sync_WithUpdates_ShouldReturnProperStatistics() {
// preparation
populateProjectWithNestedAttributes(CTP_TARGET_CLIENT);
final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
// only update the nested types
final List<ProductTypeDraft> productTypeDrafts = ProductTypeTransformUtils.toProductTypeDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, productTypes).join().stream().map(productType -> {
final List<AttributeDefinitionDraft> attributeDefinitionDrafts = productType.getAttributes().stream().map(attribute -> {
if (attribute.getAttributeType() instanceof NestedAttributeType) {
return AttributeDefinitionDraftBuilder.of(attribute).label(ofEnglish("new-label")).build();
}
return AttributeDefinitionDraftBuilder.of(attribute).build();
}).collect(Collectors.toList());
return ProductTypeDraftBuilder.of(productType).attributes(attributeDefinitionDrafts).build();
}).collect(Collectors.toList());
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(productTypeDrafts).toCompletableFuture().join();
// assertion
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(builtUpdateActions).containsExactly(ChangeAttributeDefinitionLabel.of("nestedattr2", ofEnglish("new-label")));
assertThat(productTypeSyncStatistics).hasValues(4, 0, 1, 0, 0);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 4 product types were processed in total" + " (0 created, 1 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
}
use of com.commercetools.sync.producttypes.ProductTypeSyncOptions in project commercetools-sync-java by commercetools.
the class ProductTypeSyncIT method sync_WithoutUpdates_ShouldReturnProperStatistics.
@Test
void sync_WithoutUpdates_ShouldReturnProperStatistics() {
// preparation
final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
final List<ProductTypeDraft> productTypeDrafts = productTypes.stream().map(ProductTypeDraftBuilder::of).map(ProductTypeDraftBuilder::build).collect(Collectors.toList());
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final ProductTypeSyncOptions productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(productTypeDrafts).toCompletableFuture().join();
// assertion
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(productTypeSyncStatistics).hasValues(2, 1, 0, 0, 0);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 product types were processed in total" + " (1 created, 0 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
}
use of com.commercetools.sync.producttypes.ProductTypeSyncOptions in project commercetools-sync-java by commercetools.
the class ProductTypeWithNestedAttributeSyncIT method sync_WithOneDraftPerBatchOnEmptyProject_ShouldReturnProperStatistics.
@Test
void sync_WithOneDraftPerBatchOnEmptyProject_ShouldReturnProperStatistics() {
// preparation
final List<ProductType> productTypes = CTP_SOURCE_CLIENT.execute(ProductTypeQuery.of()).toCompletableFuture().join().getResults();
final List<ProductTypeDraft> productTypeDrafts = ProductTypeTransformUtils.toProductTypeDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, productTypes).join();
productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(1).beforeUpdateCallback((actions, draft, oldProductType) -> {
builtUpdateActions.addAll(actions);
return actions;
}).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
final ProductTypeSync productTypeSync = new ProductTypeSync(productTypeSyncOptions);
// test
final ProductTypeSyncStatistics productTypeSyncStatistics = productTypeSync.sync(productTypeDrafts).toCompletableFuture().join();
// assertion
assertThat(errorMessages).isEmpty();
assertThat(exceptions).isEmpty();
assertThat(builtUpdateActions).isEmpty();
assertThat(productTypeSyncStatistics).hasValues(4, 4, 0, 0, 0);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 4 product types were processed in total" + " (4 created, 0 updated, 0 failed to sync and 0 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
}
use of com.commercetools.sync.producttypes.ProductTypeSyncOptions in project commercetools-sync-java by commercetools.
the class BuildAttributeDefinitionUpdateActionsTest method buildAttributesUpdateActions_WithNewEnumAndOldAsNonEnum_ShouldNotBuildActionsAndTriggerErrorCb.
@Test
void buildAttributesUpdateActions_WithNewEnumAndOldAsNonEnum_ShouldNotBuildActionsAndTriggerErrorCb() {
// preparation
final AttributeDefinitionDraft newDefinition = AttributeDefinitionDraftBuilder.of(EnumAttributeType.of(EnumValue.of("foo", "bar")), "a", ofEnglish("new_label"), true).build();
final ProductTypeDraft productTypeDraft = ProductTypeDraftBuilder.of("foo", "name", "desc", singletonList(newDefinition)).build();
final AttributeDefinition oldDefinition = AttributeDefinitionBuilder.of("a", ofEnglish("new_label"), StringAttributeType.of()).build();
final ProductType productType = mock(ProductType.class);
when(productType.getAttributes()).thenReturn(singletonList(oldDefinition));
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final ProductTypeSyncOptions syncOptions = ProductTypeSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
// test
final List<UpdateAction<ProductType>> updateActions = buildAttributesUpdateActions(productType, productTypeDraft, syncOptions);
// assertions
assertThat(updateActions).isEmpty();
assertThat(errorMessages).hasSize(1);
assertThat(exceptions.get(0)).isExactlyInstanceOf(BuildUpdateActionException.class);
assertThat(exceptions.get(0).getMessage()).contains("changing the attribute definition type (attribute name='a') is not supported programmatically");
assertThat(exceptions.get(0).getCause()).isExactlyInstanceOf(UnsupportedOperationException.class);
}
Aggregations