use of com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_1 in project commercetools-sync-java by commercetools.
the class ProductTypeWithNestedAttributeSyncIT method sync_WithNewProductTypeWithFailedFetchOnReferenceResolution_ShouldFail.
@Test
void sync_WithNewProductTypeWithFailedFetchOnReferenceResolution_ShouldFail() {
// preparation
final AttributeDefinitionDraft nestedTypeAttr = AttributeDefinitionDraftBuilder.of(AttributeDefinitionBuilder.of("nestedattr", ofEnglish("nestedattr"), NestedAttributeType.of(ProductType.referenceOfId(PRODUCT_TYPE_KEY_5))).build()).searchable(false).build();
final ProductTypeDraft withMissingNestedTypeRef = 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));
final SphereClient ctpClient = spy(CTP_TARGET_CLIENT);
final BadGatewayException badGatewayException = new BadGatewayException();
when(ctpClient.execute(any(ResourceKeyIdGraphQlRequest.class))).thenCallRealMethod();
when(ctpClient.execute(any(ProductTypeQuery.class))).thenCallRealMethod().thenCallRealMethod().thenReturn(// fail on fetching during resolution
exceptionallyCompletedFuture(badGatewayException)).thenCallRealMethod();
productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(ctpClient).batchSize(// this ensures the drafts are in separate batches.
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(withMissingNestedTypeRef, productTypeDraft5)).toCompletableFuture().join();
// assertions
final Optional<ProductType> productType1 = getProductTypeByKey(CTP_TARGET_CLIENT, PRODUCT_TYPE_KEY_1);
assert productType1.isPresent();
assertThat(errorMessages).containsExactly("Failed to fetch existing product types with keys: '[key_1]'.");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(BadGatewayException.class);
assertThat(builtUpdateActions).isEmpty();
assertThat(productTypeSyncStatistics).hasValues(2, 1, 0, 0, 1);
assertThat(productTypeSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 product types were processed in total" + " (1 created, 0 updated, 0 failed to sync and 1 product types with at least one NestedType or a Set" + " of NestedType attribute definition(s) referencing a missing product type).");
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).hasSize(1);
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>> children = productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get(PRODUCT_TYPE_KEY_5);
assertThat(children).hasSize(1);
assertThat(children.get(PRODUCT_TYPE_KEY_1)).containsExactly(nestedTypeAttr);
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(2));
}
use of com.commercetools.sync.integration.commons.utils.ProductTypeITUtils.PRODUCT_TYPE_KEY_1 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));
}
Aggregations