use of com.commercetools.sync.inventories.InventorySync in project commercetools-sync-java by commercetools.
the class InventorySyncIT method sync_WithBatchProcessing_ShouldCreateAllGivenInventories.
@Disabled
@Test
void sync_WithBatchProcessing_ShouldCreateAllGivenInventories() {
// Ensure inventory entries amount in target project before sync.
final List<InventoryEntry> oldEntriesBeforeSync = CTP_TARGET_CLIENT.execute(InventoryEntryQuery.of()).toCompletableFuture().join().getResults();
assertThat(oldEntriesBeforeSync).hasSize(2);
// Create 10 drafts of new inventories.
final List<InventoryEntryDraft> newInventories = LongStream.range(0, 10).mapToObj(l -> InventoryEntryDraftBuilder.of(String.valueOf(l), l).build()).collect(toList());
// Set batch size of number less that new inventories size.
final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(3).build();
final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
// Perform sync and ensure its results.
final InventorySyncStatistics inventorySyncStatistics = inventorySync.sync(newInventories).toCompletableFuture().join();
assertThat(inventorySyncStatistics).hasValues(10, 10, 0, 0);
// Ensure inventory entries amount in target project after sync.
final List<InventoryEntry> oldEntriesAfterSync = CTP_TARGET_CLIENT.execute(InventoryEntryQuery.of()).toCompletableFuture().join().getResults();
assertThat(oldEntriesAfterSync).hasSize(12);
}
use of com.commercetools.sync.inventories.InventorySync in project commercetools-sync-java by commercetools.
the class InventorySyncIT method sync_WithUpdatedInventory_ShouldUpdateInventory.
@Test
void sync_WithUpdatedInventory_ShouldUpdateInventory() {
// Ensure that old entry has correct values before sync.
final Optional<InventoryEntry> oldInventoryBeforeSync = getInventoryEntryBySkuAndSupplyChannel(CTP_TARGET_CLIENT, SKU_1, null);
assertThat(oldInventoryBeforeSync).isNotEmpty();
assertValues(oldInventoryBeforeSync.get(), QUANTITY_ON_STOCK_1, EXPECTED_DELIVERY_1, RESTOCKABLE_IN_DAYS_1);
// Prepare sync data.
final InventoryEntryDraft newInventoryDraft = InventoryEntryDraftBuilder.of(SKU_1, QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2, null).build();
final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
// Sync and ensure that proper statistics were returned.
final InventorySyncStatistics inventorySyncStatistics = inventorySync.sync(singletonList(newInventoryDraft)).toCompletableFuture().join();
assertThat(inventorySyncStatistics).hasValues(1, 0, 1, 0);
// Ensure that old entry has correct values after sync.
final Optional<InventoryEntry> oldInventoryAfterSync = getInventoryEntryBySkuAndSupplyChannel(CTP_TARGET_CLIENT, SKU_1, null);
assertThat(oldInventoryAfterSync).isNotEmpty();
assertValues(oldInventoryAfterSync.get(), QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2);
}
use of com.commercetools.sync.inventories.InventorySync in project commercetools-project-sync by commercetools.
the class InventoryEntrySyncer method of.
public static InventoryEntrySyncer of(@Nonnull final SphereClient sourceClient, @Nonnull final SphereClient targetClient, @Nonnull final Clock clock) {
final QuadConsumer<SyncException, Optional<InventoryEntryDraft>, Optional<InventoryEntry>, List<UpdateAction<InventoryEntry>>> logErrorCallback = (exception, newResourceDraft, oldResource, updateActions) -> logErrorCallback(LOGGER, "inventory entry", exception, oldResource.map(InventoryEntry::getSku).orElse(IDENTIFIER_NOT_PRESENT), updateActions);
final TriConsumer<SyncException, Optional<InventoryEntryDraft>, Optional<InventoryEntry>> logWarningCallback = (exception, newResourceDraft, oldResource) -> logWarningCallback(LOGGER, "inventory entry", exception, oldResource.map(InventoryEntry::getSku).orElse(IDENTIFIER_NOT_PRESENT));
final InventorySyncOptions syncOptions = InventorySyncOptionsBuilder.of(targetClient).errorCallback(logErrorCallback).warningCallback(logWarningCallback).build();
final InventorySync inventorySync = new InventorySync(syncOptions);
final CustomObjectService customObjectService = new CustomObjectServiceImpl(targetClient);
return new InventoryEntrySyncer(inventorySync, sourceClient, targetClient, customObjectService, clock);
}
use of com.commercetools.sync.inventories.InventorySync in project commercetools-sync-java by commercetools.
the class InventorySyncBenchmark method sync_NewInventories_ShouldCreateInventories.
@Test
void sync_NewInventories_ShouldCreateInventories() throws IOException {
// preparation
final List<InventoryEntryDraft> inventoryEntryDrafts = buildInventoryDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
// benchmark
final long beforeSync = System.currentTimeMillis();
final InventorySyncStatistics inventorySyncStatistics = executeBlocking(inventorySync.sync(inventoryEntryDrafts));
final long totalTime = System.currentTimeMillis() - beforeSync;
// assert on threshold (based on history of benchmarks; highest was ~9 seconds)
// double of the highest benchmark
final int threshold = 18000;
assertThat(totalTime).withFailMessage(format(THRESHOLD_EXCEEDED_ERROR, totalTime, threshold)).isLessThan(threshold);
// Assert actual state of CTP project (total number of existing inventories)
final CompletableFuture<Integer> totalNumberOfInventories = CTP_TARGET_CLIENT.execute(InventoryEntryQuery.of()).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
executeBlocking(totalNumberOfInventories);
assertThat(totalNumberOfInventories).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
// Assert on sync statistics
assertThat(inventorySyncStatistics).hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, NUMBER_OF_RESOURCE_UNDER_TEST, 0, 0);
saveNewResult(INVENTORY_SYNC, CREATES_ONLY, totalTime);
}
use of com.commercetools.sync.inventories.InventorySync in project commercetools-sync-java by commercetools.
the class InventorySyncIT method sync_WithReferencesToSourceChannels_ShouldUpdateInventoriesWithoutChannelCreation.
@Test
void sync_WithReferencesToSourceChannels_ShouldUpdateInventoriesWithoutChannelCreation() {
// Ensure channels in target project before sync
final ChannelQuery targetChannelsQuery = ChannelQuery.of().withPredicates(channelQueryModel -> channelQueryModel.roles().containsAny(singletonList(ChannelRole.INVENTORY_SUPPLY)));
final List<Channel> targetChannelsBeforeSync = CTP_TARGET_CLIENT.execute(targetChannelsQuery).toCompletableFuture().join().getResults();
assertThat(targetChannelsBeforeSync).hasSize(1);
assertThat(targetChannelsBeforeSync.get(0).getKey()).isEqualTo(SUPPLY_CHANNEL_KEY_1);
// Prepare InventoryEntryDraft of sku SKU_1 and reference to above supply channel key.
final InventoryEntryDraft newInventoryDraft = InventoryEntryDraftBuilder.of(SKU_1, QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2, ResourceIdentifier.ofKey(SUPPLY_CHANNEL_KEY_1)).build();
// Fetch existing Channel of key SUPPLY_CHANNEL_KEY_1 from target project.
final Optional<Channel> targetSupplyChannel = getChannelByKey(CTP_TARGET_CLIENT, SUPPLY_CHANNEL_KEY_1);
assertThat(targetSupplyChannel).isNotEmpty();
final Reference<Channel> targetChannelReference = targetSupplyChannel.get().toReference();
// Ensure old entry values before sync.
final Optional<InventoryEntry> oldInventoryBeforeSync = getInventoryEntryBySkuAndSupplyChannel(CTP_TARGET_CLIENT, SKU_1, targetChannelReference);
assertThat(oldInventoryBeforeSync).isPresent();
assertValues(oldInventoryBeforeSync.get(), QUANTITY_ON_STOCK_1, EXPECTED_DELIVERY_1, RESTOCKABLE_IN_DAYS_1);
assertThat(oldInventoryBeforeSync.get().getSupplyChannel().getId()).isEqualTo(targetChannelReference.getId());
// 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, targetChannelReference);
assertThat(oldInventoryAfterSync).isPresent();
assertValues(oldInventoryAfterSync.get(), QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2);
assertThat(oldInventoryAfterSync.get().getSupplyChannel().getId()).isEqualTo(targetChannelReference.getId());
// Ensure channels in target project after sync.
final List<Channel> targetChannelsAfterSync = CTP_TARGET_CLIENT.execute(targetChannelsQuery).toCompletableFuture().join().getResults();
assertThat(targetChannelsAfterSync).isNotEmpty();
assertThat(targetChannelsAfterSync).hasSize(1);
assertThat(targetChannelsAfterSync.get(0)).isEqualTo(targetChannelsBeforeSync.get(0));
}
Aggregations