use of com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithDeletedTransition_ShouldRemoveTransitions.
@Test
void sync_WithDeletedTransition_ShouldRemoveTransitions() {
final StateDraft stateCDraft = createStateDraft(keyC);
final State stateC = createStateInSource(stateCDraft);
final StateDraft tagetStateCDraft = createStateDraft(keyC);
final State targetStateC = createStateInTarget(tagetStateCDraft);
final StateDraft stateBDraft = createStateDraft(keyB, stateC);
final State stateB = createStateInSource(stateBDraft);
final StateDraft tagetStateBDraft = createStateDraft(keyB, targetStateC);
final State targetStateB = createStateInTarget(tagetStateBDraft);
final StateDraft stateADraft = createStateDraft(keyA, stateB);
final State stateA = createStateInSource(stateADraft);
final StateDraft tagetStateADraft = createStateDraft(keyA, targetStateB, targetStateC);
final State targetStateA = createStateInTarget(tagetStateADraft);
Assertions.assertThat(targetStateB.getTransitions().size()).isEqualTo(1);
Assertions.assertThat(targetStateA.getTransitions().size()).isEqualTo(2);
final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(3).build();
final StateSync stateSync = new StateSync(stateSyncOptions);
final List<StateDraft> stateDrafts = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, Arrays.asList(stateA, stateB, stateC)).join();
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(3, 0, 1, 0, 0);
CtpQueryUtils.queryAll(CTP_TARGET_CLIENT, StateQueryBuilder.of().plusPredicates(q -> q.key().is(keyA)).build(), Function.identity()).thenApply(fetchedCategories -> fetchedCategories.stream().flatMap(List::stream).collect(Collectors.toList())).thenAccept(resultStates -> {
Assertions.assertThat(resultStates.size()).isEqualTo(1);
Assertions.assertThat(resultStates.get(0).getTransitions().size()).isEqualTo(1);
}).toCompletableFuture().join();
}
use of com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withChangedAttributes_ShouldUpdateProducts.
@Test
void sync_withChangedAttributes_ShouldUpdateProducts() {
// Preparation
// Create custom options with whitelisting and action filter callback..
final ProductSyncOptions customSyncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> errorCallback(exception.getMessage(), exception.getCause())).warningCallback((exception, oldResource, newResources) -> warningCallBackMessages.add(exception.getMessage())).beforeUpdateCallback(this::beforeUpdateCallback).syncFilter(SyncFilter.ofWhiteList(ATTRIBUTES)).build();
final ProductSync customSync = new ProductSync(customSyncOptions);
// Create existing products in target project with keys (productKey1)
final ProductDraft existingProductDraft = createProductDraft(PRODUCT_KEY_1_RESOURCE_PATH, targetProductType.toReference(), targetTaxCategory.toReference(), targetProductState.toReference(), targetCategoryReferencesWithIds, createRandomCategoryOrderHints(targetCategoryReferencesWithIds));
CTP_TARGET_CLIENT.execute(ProductCreateCommand.of(existingProductDraft)).toCompletableFuture().join();
// Create existing product with productKey1 in source project with changed attributes
final ProductDraft newProductDraftWithProductReference = createProductDraftBuilder(PRODUCT_KEY_1_CHANGED_ATTRIBUTES_RESOURCE_PATH, sourceProductType.toReference()).taxCategory(sourceTaxCategory.toReference()).state(sourceProductState.toReference()).categories(Collections.emptySet()).categoryOrderHints(null).build();
CTP_SOURCE_CLIENT.execute(ProductCreateCommand.of(newProductDraftWithProductReference)).toCompletableFuture().join();
// Test
final List<ProductProjection> products = CTP_SOURCE_CLIENT.execute(ProductProjectionQuery.ofStaged()).toCompletableFuture().join().getResults();
final List<ProductDraft> productDrafts = ProductTransformUtils.toProductDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, products).join();
final ProductSyncStatistics syncStatistics = customSync.sync(productDrafts).toCompletableFuture().join();
// Assertion
assertThat(syncStatistics).hasValues(1, 0, 1, 0);
assertThat(errorCallBackMessages).isEmpty();
assertThat(errorCallBackExceptions).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
final AttributeDraft priceInfoAttrDraft = AttributeDraft.of("priceInfo", JsonNodeFactory.instance.textNode("100/kg"));
final AttributeDraft angebotAttrDraft = AttributeDraft.of("angebot", JsonNodeFactory.instance.textNode("big discount"));
assertThat(updateActions).containsExactlyInAnyOrder(SetAttributeInAllVariants.of(priceInfoAttrDraft, true), SetAttribute.of(1, angebotAttrDraft, true), SetAttributeInAllVariants.ofUnsetAttribute("size", true), SetAttributeInAllVariants.ofUnsetAttribute("rinderrasse", true), SetAttributeInAllVariants.ofUnsetAttribute("herkunft", true), SetAttributeInAllVariants.ofUnsetAttribute("teilstueck", true), SetAttributeInAllVariants.ofUnsetAttribute("fuetterung", true), SetAttributeInAllVariants.ofUnsetAttribute("reifung", true), SetAttributeInAllVariants.ofUnsetAttribute("haltbarkeit", true), SetAttributeInAllVariants.ofUnsetAttribute("verpackung", true), SetAttributeInAllVariants.ofUnsetAttribute("anlieferung", true), SetAttributeInAllVariants.ofUnsetAttribute("zubereitung", true), SetAttribute.ofUnsetAttribute(1, "localisedText", true), Publish.of());
}
use of com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT 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.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT in project commercetools-sync-java by commercetools.
the class ShoppingListSyncIT method sync_WithUpdatedCustomerOnShoppingList_ShouldReturnProperStatistics.
@Test
void sync_WithUpdatedCustomerOnShoppingList_ShouldReturnProperStatistics() {
final List<ShoppingList> shoppingLists = CTP_SOURCE_CLIENT.execute(buildShoppingListQuery()).toCompletableFuture().join().getResults();
createSampleCustomerJaneDoe(CTP_SOURCE_CLIENT);
final Customer sampleCustomerJaneDoe = createSampleCustomerJaneDoe(CTP_TARGET_CLIENT);
final List<ShoppingListDraft> updatedShoppingListDrafts = ShoppingListTransformUtils.toShoppingListDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, shoppingLists).join().stream().map(shoppingListDraft -> ShoppingListDraftBuilder.of(shoppingListDraft).name(LocalizedString.ofEnglish("second-shopping-list")).anonymousId(null).customer(ResourceIdentifier.ofKey(sampleCustomerJaneDoe.getKey())).build()).collect(Collectors.toList());
final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(updatedShoppingListDrafts).toCompletableFuture().join();
assertThat(errorMessages).isEmpty();
assertThat(warningMessages).isEmpty();
assertThat(exceptions).isEmpty();
// order is important, otherwise the error below could occur:
// "message" : "The resource was already claimed by a customer..
// "action" : {
// "action" : "setAnonymousId"
// }
assertThat(updateActionList).containsExactly(ChangeName.of(LocalizedString.ofEnglish("second-shopping-list")), SetAnonymousId.of(null), SetCustomer.of(Reference.of(Customer.referenceTypeId(), sampleCustomerJaneDoe.getId())));
AssertionsForStatistics.assertThat(shoppingListSyncStatistics).hasValues(2, 1, 1, 0);
assertThat(shoppingListSyncStatistics.getReportMessage()).isEqualTo("Summary: 2 shopping lists were processed in total " + "(1 created, 1 updated and 0 failed to sync).");
}
use of com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT 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).");
}
Aggregations