use of com.commercetools.sync.inventories.InventorySync in project commercetools-sync-java by commercetools.
the class InventorySyncIT method sync_WithSyncDifferentBatchesConcurrently_ShouldReturnProperStatistics.
@Test
void sync_WithSyncDifferentBatchesConcurrently_ShouldReturnProperStatistics() {
// Prepare new inventories.
final List<InventoryEntryDraft> newDrafts = IntStream.range(100, 160).mapToObj(i -> InventoryEntryDraftBuilder.of(String.valueOf(i), QUANTITY_ON_STOCK_1).build()).collect(toList());
// Split them to batches.
final List<InventoryEntryDraft> firstBatch = newDrafts.subList(0, 20);
final List<InventoryEntryDraft> secondBatch = newDrafts.subList(20, 40);
final List<InventoryEntryDraft> thirdBatch = newDrafts.subList(40, 60);
// Initialize sync.
final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
// Run batch syncing concurrently.
final CompletableFuture<InventorySyncStatistics> firstResult = inventorySync.sync(firstBatch).toCompletableFuture();
final CompletableFuture<InventorySyncStatistics> secondResult = inventorySync.sync(secondBatch).toCompletableFuture();
final CompletableFuture<InventorySyncStatistics> thirdResult = inventorySync.sync(thirdBatch).toCompletableFuture();
CompletableFuture.allOf(firstResult, secondResult, thirdResult).join();
// Ensure instance's statistics.
assertThat(inventorySync.getStatistics()).hasValues(60, 60, 0, 0);
}
use of com.commercetools.sync.inventories.InventorySync in project commercetools-sync-java by commercetools.
the class InventorySyncIT method sync_FromSourceToTargetProjectWithChannelsEnsured_ShouldReturnProperStatistics.
@Test
void sync_FromSourceToTargetProjectWithChannelsEnsured_ShouldReturnProperStatistics() {
// Fetch new inventories from source project. Convert them to drafts.
final List<InventoryEntry> inventoryEntries = CTP_SOURCE_CLIENT.execute(InventoryEntryQuery.of().withExpansionPaths(InventoryEntryExpansionModel::supplyChannel).plusExpansionPaths(ExpansionPath.of("custom.type"))).toCompletableFuture().join().getResults();
final List<InventoryEntryDraft> newInventories = InventoryTransformUtils.toInventoryEntryDrafts(CTP_SOURCE_CLIENT, REFERENCE_ID_TO_KEY_CACHE, inventoryEntries).join();
// Prepare sync options and perform sync of draft to target project.
final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).ensureChannels(true).build();
final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
final InventorySyncStatistics inventorySyncStatistics = inventorySync.sync(newInventories).toCompletableFuture().join();
assertThat(inventorySyncStatistics).hasValues(3, 1, 1, 0);
}
use of com.commercetools.sync.inventories.InventorySync in project commercetools-sync-java by commercetools.
the class InventorySyncIT method sync_WithKeyToExistingSupplyChannelInPlaceOfId_ShouldUpdateInventory.
@Test
void sync_WithKeyToExistingSupplyChannelInPlaceOfId_ShouldUpdateInventory() {
/*
* Fetch existing Channel of key SUPPLY_CHANNEL_KEY_1 from target project.
* This is done only for test assertion reasons, not necessary for sync.
*/
final Optional<Channel> supplyChannel = getChannelByKey(CTP_TARGET_CLIENT, SUPPLY_CHANNEL_KEY_1);
assertThat(supplyChannel).isNotEmpty();
/*
* Prepare InventoryEntryDraft of sku SKU_1 and reference to supply channel of key SUPPLY_CHANNEL_KEY_1.
*/
final ResourceIdentifier<Channel> supplyChannelReference = ResourceIdentifier.ofKey(SUPPLY_CHANNEL_KEY_1);
final InventoryEntryDraft newInventoryDraft = InventoryEntryDraftBuilder.of(SKU_1, QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2, supplyChannelReference).build();
// Ensure old entry values before sync.
final Optional<InventoryEntry> oldInventoryBeforeSync = getInventoryEntryBySkuAndSupplyChannel(CTP_TARGET_CLIENT, SKU_1, supplyChannel.get().toReference());
assertThat(oldInventoryBeforeSync).isPresent();
assertValues(oldInventoryBeforeSync.get(), QUANTITY_ON_STOCK_1, EXPECTED_DELIVERY_1, RESTOCKABLE_IN_DAYS_1);
// Prepare sync options and perform sync of draft to target project.
final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
final InventorySyncStatistics inventorySyncStatistics = inventorySync.sync(singletonList(newInventoryDraft)).toCompletableFuture().join();
assertThat(inventorySyncStatistics).hasValues(1, 0, 1, 0);
// Ensure old entry values after sync.
final Optional<InventoryEntry> oldInventoryAfterSync = getInventoryEntryBySkuAndSupplyChannel(CTP_TARGET_CLIENT, SKU_1, supplyChannel.get().toReference());
assertThat(oldInventoryAfterSync).isPresent();
assertValues(oldInventoryAfterSync.get(), QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2);
}
Aggregations