use of com.commercetools.sync.services.InventoryService in project commercetools-sync-java by commercetools.
the class InventorySyncTest method getInventorySync.
private InventorySync getInventorySync(int batchSize, boolean ensureChannels) {
final InventorySyncOptions options = getInventorySyncOptions(batchSize, ensureChannels);
final InventoryService inventoryService = getMockInventoryService(existingInventories, mock(InventoryEntry.class), mock(InventoryEntry.class));
return new InventorySync(options, inventoryService, createMockChannelService(), mock(TypeService.class));
}
use of com.commercetools.sync.services.InventoryService in project commercetools-sync-java by commercetools.
the class InventorySyncMockUtils method getMockInventoryService.
/**
* Returns mock instance of {@link InventoryService}. Executing any method with any parameter on
* this instance returns values passed in parameters, wrapped in {@link CompletionStage}.
*
* @param inventoryEntries result of calling {@link
* InventoryService#fetchInventoryEntriesByIdentifiers(Set)}
* @param createdInventoryEntry result of calling {@link
* InventoryService#createInventoryEntry(InventoryEntryDraft)}
* @param updatedInventoryEntry result of calling {@link
* InventoryService#updateInventoryEntry(InventoryEntry, List)}
* @return mock instance of {@link InventoryService}
*/
static InventoryService getMockInventoryService(final Set<InventoryEntry> inventoryEntries, final InventoryEntry createdInventoryEntry, final InventoryEntry updatedInventoryEntry) {
final InventoryService inventoryService = mock(InventoryService.class);
when(inventoryService.fetchInventoryEntriesByIdentifiers(any())).thenReturn(completedFuture(inventoryEntries));
when(inventoryService.createInventoryEntry(any())).thenReturn(completedFuture(Optional.ofNullable(createdInventoryEntry)));
when(inventoryService.updateInventoryEntry(any(), any())).thenReturn(completedFuture(updatedInventoryEntry));
return inventoryService;
}
use of com.commercetools.sync.services.InventoryService in project commercetools-sync-java by commercetools.
the class InventorySyncTest method sync_WithExistingInventoryEntryButWithEmptyCustomTypeReference_ShouldFailSync.
@Test
void sync_WithExistingInventoryEntryButWithEmptyCustomTypeReference_ShouldFailSync() {
final InventorySyncOptions options = getInventorySyncOptions(3, false);
final InventoryService inventoryService = getMockInventoryService(existingInventories, mock(InventoryEntry.class), mock(InventoryEntry.class));
final ChannelService channelService = mock(ChannelService.class);
when(channelService.fetchCachedChannelId(anyString())).thenReturn(completedFuture(Optional.of(REF_2)));
final InventorySync inventorySync = new InventorySync(options, inventoryService, channelService, mock(TypeService.class));
final List<InventoryEntryDraft> newDrafts = new ArrayList<>();
final InventoryEntryDraft draftWithNullCustomTypeId = InventoryEntryDraft.of(SKU_1, QUANTITY_1, DATE_1, RESTOCKABLE_1, null).withCustom(CustomFieldsDraft.ofTypeKeyAndJson("", new HashMap<>()));
newDrafts.add(draftWithNullCustomTypeId);
final InventorySyncStatistics syncStatistics = inventorySync.sync(newDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(1, 0, 0, 1);
assertThat(errorCallBackMessages).isNotEmpty();
assertThat(errorCallBackMessages.get(0)).contains(format("Failed to process the" + " InventoryEntryDraft with SKU:'%s'. Reason: %s: Failed to resolve custom type resource identifier on " + "InventoryEntryDraft with SKU:'1000'. Reason: %s", SKU_1, ReferenceResolutionException.class.getCanonicalName(), BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER));
assertThat(errorCallBackExceptions).isNotEmpty();
assertThat(errorCallBackExceptions.get(0)).isExactlyInstanceOf(CompletionException.class);
assertThat(errorCallBackExceptions.get(0).getCause()).isExactlyInstanceOf(ReferenceResolutionException.class);
}
use of com.commercetools.sync.services.InventoryService in project commercetools-sync-java by commercetools.
the class InventorySyncTest method sync_WithExceptionWhenUpdatingEntries_ShouldNotSync.
@Test
void sync_WithExceptionWhenUpdatingEntries_ShouldNotSync() {
final InventorySyncOptions options = getInventorySyncOptions(3, false);
final InventoryService inventoryService = getMockInventoryService(existingInventories, mock(InventoryEntry.class), mock(InventoryEntry.class));
when(inventoryService.updateInventoryEntry(any(), any())).thenReturn(getCompletionStageWithException());
final ChannelService channelService = getMockChannelService(getMockSupplyChannel(REF_2, KEY_2));
final InventorySync inventorySync = new InventorySync(options, inventoryService, channelService, mock(TypeService.class));
final InventoryEntryDraft inventoryEntryDraft = InventoryEntryDraftBuilder.of(SKU_2, QUANTITY_2, DATE_1, RESTOCKABLE_1, ResourceIdentifier.ofKey(KEY_2)).build();
final InventorySyncStatistics stats = inventorySync.sync(Collections.singletonList(inventoryEntryDraft)).toCompletableFuture().join();
assertThat(stats).hasValues(1, 0, 0, 1);
assertThat(errorCallBackMessages).hasSize(1);
assertThat(errorCallBackExceptions).hasSize(1);
assertThat(errorCallBackMessages.get(0)).isEqualTo(format("Failed to update inventory entry of SKU '%s' and supply channel id '%s'.", SKU_2, REF_2));
assertThat(errorCallBackExceptions.get(0)).isExactlyInstanceOf(RuntimeException.class);
}
use of com.commercetools.sync.services.InventoryService in project commercetools-sync-java by commercetools.
the class InventorySyncTest method sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback.
@Test
void sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback() {
// preparation
final InventoryEntryDraft inventoryEntryDraft = InventoryEntryDraftBuilder.of(SKU_1, 1L).build();
final InventorySyncOptions optionsSpy = spy(getInventorySyncOptions(1, false));
final InventoryService inventoryService = getMockInventoryService(existingInventories, mock(InventoryEntry.class), mock(InventoryEntry.class));
final ChannelService channelService = getMockChannelService(getMockSupplyChannel(REF_3, KEY_3));
final InventorySync inventorySync = new InventorySync(optionsSpy, inventoryService, channelService, mock(TypeService.class));
// test
inventorySync.sync(singletonList(inventoryEntryDraft)).toCompletableFuture().join();
// assertion
verify(optionsSpy).applyBeforeUpdateCallback(any(), any(), any());
verify(optionsSpy, never()).applyBeforeCreateCallback(any());
}
Aggregations