Search in sources :

Example 11 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions 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);
}
Also used : SyncException(com.commercetools.sync.commons.exceptions.SyncException) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) LoggerFactory(org.slf4j.LoggerFactory) InventorySyncOptionsBuilder(com.commercetools.sync.inventories.InventorySyncOptionsBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) QuadConsumer(com.commercetools.sync.commons.utils.QuadConsumer) InventoryTransformUtils.toInventoryEntryDrafts(com.commercetools.sync.inventories.utils.InventoryTransformUtils.toInventoryEntryDrafts) SyncUtils.logWarningCallback(com.commercetools.project.sync.util.SyncUtils.logWarningCallback) SphereClient(io.sphere.sdk.client.SphereClient) TriConsumer(com.commercetools.sync.commons.utils.TriConsumer) InventoryEntryQuery(io.sphere.sdk.inventory.queries.InventoryEntryQuery) Nonnull(javax.annotation.Nonnull) SyncUtils.logErrorCallback(com.commercetools.project.sync.util.SyncUtils.logErrorCallback) Logger(org.slf4j.Logger) IDENTIFIER_NOT_PRESENT(com.commercetools.project.sync.util.SyncUtils.IDENTIFIER_NOT_PRESENT) CustomObjectService(com.commercetools.project.sync.service.CustomObjectService) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) InventorySync(com.commercetools.sync.inventories.InventorySync) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) Syncer(com.commercetools.project.sync.Syncer) Clock(java.time.Clock) Optional(java.util.Optional) CustomObjectServiceImpl(com.commercetools.project.sync.service.impl.CustomObjectServiceImpl) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) CustomObjectService(com.commercetools.project.sync.service.CustomObjectService) Optional(java.util.Optional) InventorySync(com.commercetools.sync.inventories.InventorySync) CustomObjectServiceImpl(com.commercetools.project.sync.service.impl.CustomObjectServiceImpl) List(java.util.List) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) SyncException(com.commercetools.sync.commons.exceptions.SyncException) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions)

Example 12 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions 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);
}
Also used : PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) InventorySync(com.commercetools.sync.inventories.InventorySync) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) Test(org.junit.jupiter.api.Test)

Example 13 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions 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));
}
Also used : InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) ChannelQuery(io.sphere.sdk.channels.queries.ChannelQuery) InventorySync(com.commercetools.sync.inventories.InventorySync) Channel(io.sphere.sdk.channels.Channel) InventoryITUtils.getInventoryEntryBySkuAndSupplyChannel(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.getInventoryEntryBySkuAndSupplyChannel) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) Test(org.junit.jupiter.api.Test)

Example 14 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions 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);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SUPPLY_CHANNEL_KEY_2(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.SUPPLY_CHANNEL_KEY_2) SUPPLY_CHANNEL_KEY_1(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.SUPPLY_CHANNEL_KEY_1) Reference(io.sphere.sdk.models.Reference) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) SyncException(com.commercetools.sync.commons.exceptions.SyncException) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) ITUtils.deleteTypesFromTargetAndSource(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypesFromTargetAndSource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ZonedDateTime(java.time.ZonedDateTime) InventorySyncOptionsBuilder(com.commercetools.sync.inventories.InventorySyncOptionsBuilder) UpdateAction(io.sphere.sdk.commands.UpdateAction) QuadConsumer(com.commercetools.sync.commons.utils.QuadConsumer) Disabled(org.junit.jupiter.api.Disabled) Channel(io.sphere.sdk.channels.Channel) Collections.singletonList(java.util.Collections.singletonList) ExpansionPath(io.sphere.sdk.expansion.ExpansionPath) AfterAll(org.junit.jupiter.api.AfterAll) SKU_2(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.SKU_2) SKU_1(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.SKU_1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelQuery(io.sphere.sdk.channels.queries.ChannelQuery) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) RESTOCKABLE_IN_DAYS_1(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.RESTOCKABLE_IN_DAYS_1) RESTOCKABLE_IN_DAYS_2(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.RESTOCKABLE_IN_DAYS_2) InventoryITUtils.populateSourceProject(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.populateSourceProject) Test(org.junit.jupiter.api.Test) InventoryITUtils.populateTargetProject(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.populateTargetProject) InventoryEntryDraftBuilder(io.sphere.sdk.inventory.InventoryEntryDraftBuilder) List(java.util.List) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) Optional(java.util.Optional) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) IntStream(java.util.stream.IntStream) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CompletableFuture(java.util.concurrent.CompletableFuture) InventoryEntryQuery(io.sphere.sdk.inventory.queries.InventoryEntryQuery) Nonnull(javax.annotation.Nonnull) InventoryITUtils.getChannelByKey(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.getChannelByKey) Nullable(javax.annotation.Nullable) InventoryITUtils.getInventoryEntryBySkuAndSupplyChannel(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.getInventoryEntryBySkuAndSupplyChannel) ChannelRole(io.sphere.sdk.channels.ChannelRole) LongStream(java.util.stream.LongStream) EXPECTED_DELIVERY_1(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.EXPECTED_DELIVERY_1) EXPECTED_DELIVERY_2(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.EXPECTED_DELIVERY_2) ChannelITUtils.deleteChannelsFromTargetAndSource(com.commercetools.sync.integration.commons.utils.ChannelITUtils.deleteChannelsFromTargetAndSource) REFERENCE_ID_TO_KEY_CACHE(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.REFERENCE_ID_TO_KEY_CACHE) InventoryITUtils.deleteInventoryEntriesFromTargetAndSource(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.deleteInventoryEntriesFromTargetAndSource) InventorySync(com.commercetools.sync.inventories.InventorySync) QUANTITY_ON_STOCK_1(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.QUANTITY_ON_STOCK_1) Collectors.toList(java.util.stream.Collectors.toList) InventoryEntryExpansionModel(io.sphere.sdk.inventory.expansion.InventoryEntryExpansionModel) InventoryTransformUtils(com.commercetools.sync.inventories.utils.InventoryTransformUtils) QUANTITY_ON_STOCK_2(com.commercetools.sync.integration.inventories.utils.InventoryITUtils.QUANTITY_ON_STOCK_2) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) InventorySync(com.commercetools.sync.inventories.InventorySync) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) Test(org.junit.jupiter.api.Test)

Example 15 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions 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);
}
Also used : InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) InventorySync(com.commercetools.sync.inventories.InventorySync) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) Test(org.junit.jupiter.api.Test)

Aggregations

InventorySyncOptions (com.commercetools.sync.inventories.InventorySyncOptions)16 InventoryEntryDraft (io.sphere.sdk.inventory.InventoryEntryDraft)15 Test (org.junit.jupiter.api.Test)14 InventorySync (com.commercetools.sync.inventories.InventorySync)13 InventorySyncStatistics (com.commercetools.sync.inventories.helpers.InventorySyncStatistics)13 InventoryEntry (io.sphere.sdk.inventory.InventoryEntry)12 InventoryITUtils.getInventoryEntryBySkuAndSupplyChannel (com.commercetools.sync.integration.inventories.utils.InventoryITUtils.getInventoryEntryBySkuAndSupplyChannel)6 Channel (io.sphere.sdk.channels.Channel)6 UpdateAction (io.sphere.sdk.commands.UpdateAction)5 SyncException (com.commercetools.sync.commons.exceptions.SyncException)4 QuadConsumer (com.commercetools.sync.commons.utils.QuadConsumer)4 InventorySyncOptionsBuilder (com.commercetools.sync.inventories.InventorySyncOptionsBuilder)4 ChannelQuery (io.sphere.sdk.channels.queries.ChannelQuery)4 InventoryEntryQuery (io.sphere.sdk.inventory.queries.InventoryEntryQuery)4 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)3 ChannelITUtils.deleteChannelsFromTargetAndSource (com.commercetools.sync.integration.commons.utils.ChannelITUtils.deleteChannelsFromTargetAndSource)3 ITUtils.deleteTypesFromTargetAndSource (com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypesFromTargetAndSource)3 CTP_SOURCE_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT)3 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)3 EXPECTED_DELIVERY_1 (com.commercetools.sync.integration.inventories.utils.InventoryITUtils.EXPECTED_DELIVERY_1)3