Search in sources :

Example 6 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions 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);
}
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) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 7 with InventorySyncOptions

use of com.commercetools.sync.inventories.InventorySyncOptions 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);
}
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 8 with InventorySyncOptions

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

the class InventoryUpdateActionUtilsIT method buildCustomUpdateActions_ShouldBuildActionThatSetCustomField.

@Test
void buildCustomUpdateActions_ShouldBuildActionThatSetCustomField() {
    // Fetch old inventory and ensure it has custom fields.
    final Optional<InventoryEntry> oldInventoryOptional = getInventoryEntryBySkuAndSupplyChannel(CTP_TARGET_CLIENT, SKU_1, null);
    assertThat(oldInventoryOptional).isNotEmpty();
    final InventoryEntry oldInventoryBeforeSync = oldInventoryOptional.get();
    assertThat(oldInventoryBeforeSync.getCustom()).isNotNull();
    assertThat(oldInventoryBeforeSync.getCustom().getType().getObj()).isNotNull();
    assertThat(oldInventoryBeforeSync.getCustom().getType().getObj().getKey()).isEqualTo(CUSTOM_TYPE);
    // Prepare draft with updated data.
    final CustomFieldsDraft customFieldsDraft = CustomFieldsDraftBuilder.ofTypeId(oldInventoryBeforeSync.getCustom().getType().getId()).addObject(CUSTOM_FIELD_NAME, CUSTOM_FIELD_VALUE).build();
    final InventoryEntryDraft newInventory = InventoryEntryDraft.of(SKU_1, QUANTITY_ON_STOCK_2, EXPECTED_DELIVERY_2, RESTOCKABLE_IN_DAYS_2, null).withCustom(customFieldsDraft);
    // Build update actions.
    final InventorySyncOptions options = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
    final List<UpdateAction<InventoryEntry>> updateActions = buildPrimaryResourceCustomUpdateActions(oldInventoryBeforeSync, newInventory, new InventoryCustomActionBuilder(), options);
    assertThat(updateActions).isNotEmpty();
    // Execute update command and ensure returned entry is properly updated.
    final InventoryEntry oldEntryAfterSync = CTP_TARGET_CLIENT.execute(InventoryEntryUpdateCommand.of(oldInventoryBeforeSync, updateActions)).toCompletableFuture().join();
    assertThat(oldEntryAfterSync.getCustom()).isNotNull();
    assertThat(oldEntryAfterSync.getCustom().getFieldAsString(CUSTOM_FIELD_NAME)).isEqualTo(CUSTOM_FIELD_VALUE);
}
Also used : InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) UpdateAction(io.sphere.sdk.commands.UpdateAction) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) InventoryCustomActionBuilder(com.commercetools.sync.inventories.helpers.InventoryCustomActionBuilder) Test(org.junit.jupiter.api.Test)

Example 9 with InventorySyncOptions

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

the class ChannelServiceImplIT method setup.

/**
 * Deletes inventories and supply channels from source and target CTP projects. Populates target
 * CTP project with test data.
 */
@BeforeEach
void setup() {
    deleteInventoryEntriesFromTargetAndSource();
    deleteTypesFromTargetAndSource();
    deleteChannelsFromTargetAndSource();
    populateTargetProject();
    final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
    channelService = new ChannelServiceImpl(inventorySyncOptions, singleton(ChannelRole.INVENTORY_SUPPLY));
}
Also used : ChannelServiceImpl(com.commercetools.sync.services.impl.ChannelServiceImpl) InventorySyncOptions(com.commercetools.sync.inventories.InventorySyncOptions) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with InventorySyncOptions

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

the class InventoryReferenceResolverTest method resolveSupplyChannelReference_WithNonExistingChannelAndEnsureChannel_ShouldResolveSupplyChannelReference.

@Test
void resolveSupplyChannelReference_WithNonExistingChannelAndEnsureChannel_ShouldResolveSupplyChannelReference() {
    final InventorySyncOptions optionsWithEnsureChannels = InventorySyncOptionsBuilder.of(mock(SphereClient.class)).ensureChannels(true).build();
    when(channelService.fetchCachedChannelId(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
    final InventoryEntryDraft draft = InventoryEntryDraftBuilder.of(SKU, QUANTITY, DATE_1, RESTOCKABLE_IN_DAYS, ResourceIdentifier.ofKey(CHANNEL_KEY)).custom(CustomFieldsDraft.ofTypeKeyAndJson(CUSTOM_TYPE_KEY, new HashMap<>())).build();
    final InventoryReferenceResolver referenceResolver = new InventoryReferenceResolver(optionsWithEnsureChannels, typeService, channelService);
    referenceResolver.resolveSupplyChannelReference(InventoryEntryDraftBuilder.of(draft)).thenApply(InventoryEntryDraftBuilder::build).thenAccept(resolvedDraft -> {
        assertThat(resolvedDraft.getSupplyChannel()).isNotNull();
        assertThat(resolvedDraft.getSupplyChannel().getId()).isEqualTo(CHANNEL_ID);
    }).toCompletableFuture().join();
}
Also used : InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) SphereClient(io.sphere.sdk.client.SphereClient) 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