Search in sources :

Example 1 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions in project commercetools-sync-java by commercetools.

the class InventorySyncIT method sync_WithCustomErrorCallback_ShouldExecuteCallbackOnError.

@Test
void sync_WithCustomErrorCallback_ShouldExecuteCallbackOnError() {
    // 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 AtomicInteger invocationCounter = new AtomicInteger(0);
    QuadConsumer<SyncException, Optional<InventoryEntryDraft>, Optional<InventoryEntry>, List<UpdateAction<InventoryEntry>>> countingErrorCallback = (exception, newResource, oldResource, updateActions) -> invocationCounter.incrementAndGet();
    final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback(countingErrorCallback).ensureChannels(false).build();
    final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
    final InventorySyncStatistics inventorySyncStatistics = inventorySync.sync(newInventories).toCompletableFuture().join();
    assertThat(inventorySyncStatistics).hasValues(3, 0, 1, 1);
    assertThat(invocationCounter.get()).isEqualTo(1);
}
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) Optional(java.util.Optional) InventorySync(com.commercetools.sync.inventories.InventorySync) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) SyncException(com.commercetools.sync.commons.exceptions.SyncException) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Test(org.junit.jupiter.api.Test)

Example 2 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions in project commercetools-sync-java by commercetools.

the class InventorySyncIT method sync_ShouldReturnProperStatisticsObject.

@Test
void sync_ShouldReturnProperStatisticsObject() {
    // 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);
    assertThat(inventorySyncStatistics.getLatestBatchProcessingTimeInMillis()).isGreaterThan(0L);
}
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)

Example 3 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions in project commercetools-sync-java by commercetools.

the class InventorySyncIT method sync_WithNewSupplyChannelAndChannelsEnsured_ShouldCreateNewSupplyChannel.

@Test
void sync_WithNewSupplyChannelAndChannelsEnsured_ShouldCreateNewSupplyChannel() {
    // Ensure that supply channel doesn't exist before sync.
    final Optional<Channel> oldSupplyChannelBeforeSync = getChannelByKey(CTP_TARGET_CLIENT, SUPPLY_CHANNEL_KEY_2);
    assertThat(oldSupplyChannelBeforeSync).isEmpty();
    // Prepare sync data.
    final ResourceIdentifier<Channel> newSupplyChannelReference = ResourceIdentifier.ofKey(SUPPLY_CHANNEL_KEY_2);
    final InventoryEntryDraft newInventoryDraft = InventoryEntryDraftBuilder.of(SKU_1, QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2, newSupplyChannelReference).build();
    final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).ensureChannels(true).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, 1, 0, 0);
    // Ensure that supply channel exists before sync.
    final Optional<Channel> oldSupplyChannelAfterSync = getChannelByKey(CTP_TARGET_CLIENT, SUPPLY_CHANNEL_KEY_2);
    assertThat(oldSupplyChannelAfterSync).isNotEmpty();
    assertThat(oldSupplyChannelAfterSync.get().getKey()).isEqualTo(SUPPLY_CHANNEL_KEY_2);
}
Also used : InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) 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) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) Test(org.junit.jupiter.api.Test)

Example 4 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions in project commercetools-sync-java by commercetools.

the class InventorySyncIT method sync_FromSourceToTargetWithoutChannelsEnsured_ShouldReturnProperStatistics.

@Test
void sync_FromSourceToTargetWithoutChannelsEnsured_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(false).build();
    final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
    final InventorySyncStatistics inventorySyncStatistics = inventorySync.sync(newInventories).toCompletableFuture().join();
    assertThat(inventorySyncStatistics).hasValues(3, 0, 1, 1);
}
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)

Example 5 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions in project commercetools-sync-java by commercetools.

the class InventorySyncIT method sync_WithNewInventory_ShouldCreateInventory.

@Test
void sync_WithNewInventory_ShouldCreateInventory() {
    // Ensure that old entry has correct values before sync.
    final Optional<InventoryEntry> oldInventoryBeforeSync = getInventoryEntryBySkuAndSupplyChannel(CTP_TARGET_CLIENT, SKU_2, null);
    assertThat(oldInventoryBeforeSync).isEmpty();
    // Prepare sync data.
    final InventoryEntryDraft newInventoryDraft = InventoryEntryDraftBuilder.of(SKU_2, 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, 1, 0, 0);
    // Ensure that old entry has correct values after sync.
    final Optional<InventoryEntry> oldInventoryAfterSync = getInventoryEntryBySkuAndSupplyChannel(CTP_TARGET_CLIENT, SKU_2, null);
    assertThat(oldInventoryAfterSync).isNotEmpty();
    assertValues(oldInventoryAfterSync.get(), QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2);
}
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