use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class MockUtils method getMockTypeService.
/**
* Creates a mock {@link TypeService} that returns a dummy type id of value "typeId" instance
* whenever the following method is called on the service:
*
* <ul>
* <li>{@link TypeService#fetchCachedTypeId(String)}
* </ul>
*
* @return the created mock of the {@link TypeService}.
*/
public static TypeService getMockTypeService() {
final TypeService typeService = mock(TypeService.class);
when(typeService.fetchCachedTypeId(anyString())).thenReturn(completedFuture(Optional.of("typeId")));
when(typeService.cacheKeysToIds(anySet())).thenReturn(completedFuture(Collections.singletonMap("typeKey", "typeId")));
return typeService;
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class TextLineItemListUpdateActionUtilsTest method getMockTypeService.
@Nonnull
private static TypeService getMockTypeService() {
final TypeService typeService = mock(TypeService.class);
when(typeService.fetchCachedTypeId(anyString())).thenReturn(completedFuture(Optional.of("custom_type_id")));
return typeService;
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class TypeSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
// preparation
final TypeDraft newTypeDraft = TypeDraftBuilder.of("foo", ofEnglish("name"), ResourceTypeIdsSetBuilder.of().addCategories().build()).description(ofEnglish("desc")).fieldDefinitions(emptyList()).build();
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final TypeSyncOptions syncOptions = TypeSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception.getCause());
}).build();
final TypeService mockTypeService = mock(TypeService.class);
when(mockTypeService.fetchMatchingTypesByKeys(singleton(newTypeDraft.getKey()))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final TypeSync typeSync = new TypeSync(syncOptions, mockTypeService);
// test
final TypeSyncStatistics typeSyncStatistics = typeSync.sync(singletonList(newTypeDraft)).toCompletableFuture().join();
// assertions
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to fetch existing types with keys: '[foo]'.");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(CompletionException.class).hasCauseExactlyInstanceOf(SphereException.class);
assertThat(typeSyncStatistics).hasValues(1, 0, 0, 1);
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class TypeSyncTest method sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback.
@Test
void sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback() {
// preparation
final TypeDraft newTypeDraft = TypeDraftBuilder.of("newType", ofEnglish("typeName"), ResourceTypeIdsSetBuilder.of().addChannels()).build();
final TypeSyncOptions typeSyncOptions = TypeSyncOptionsBuilder.of(mock(SphereClient.class)).build();
final Type mockedExistingType = mock(Type.class);
when(mockedExistingType.getKey()).thenReturn(newTypeDraft.getKey());
final TypeService typeService = mock(TypeService.class);
when(typeService.fetchMatchingTypesByKeys(anySet())).thenReturn(completedFuture(singleton(mockedExistingType)));
when(typeService.updateType(any(), any())).thenReturn(completedFuture(mockedExistingType));
final TypeSyncOptions spyTypeSyncOptions = spy(typeSyncOptions);
// test
new TypeSync(spyTypeSyncOptions, typeService).sync(singletonList(newTypeDraft)).toCompletableFuture().join();
// assertion
verify(spyTypeSyncOptions).applyBeforeUpdateCallback(any(), any(), any());
verify(spyTypeSyncOptions, never()).applyBeforeCreateCallback(newTypeDraft);
}
use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.
the class ShoppingListSyncTest method sync_WithExceptionOnReferenceResolution_ShouldFailToUpdateAndIncreaseFailedCounter.
@Test
void sync_WithExceptionOnReferenceResolution_ShouldFailToUpdateAndIncreaseFailedCounter() {
// preparation
final ShoppingListService mockShoppingListService = mock(ShoppingListService.class);
final ShoppingList mockShoppingList = mock(ShoppingList.class);
when(mockShoppingList.getKey()).thenReturn("shoppingListKey");
when(mockShoppingListService.fetchMatchingShoppingListsByKeys(anySet())).thenReturn(completedFuture(singleton(mockShoppingList)));
final ShoppingListSyncOptions spySyncOptions = spy(syncOptions);
final TypeService typeService = mock(TypeService.class);
when(typeService.fetchCachedTypeId(anyString())).thenReturn(CompletableFutureUtils.failed(new SphereException("CTP error on fetch")));
final ShoppingListSync shoppingListSync = new ShoppingListSync(spySyncOptions, mockShoppingListService, mock(CustomerService.class), typeService);
final ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("shoppingListName")).key("shoppingListKey").build();
// test
final ShoppingListSyncStatistics shoppingListSyncStatistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
// assertions
AssertionsForStatistics.assertThat(shoppingListSyncStatistics).hasValues(1, 0, 0, 1);
assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to process the ShoppingListDraft with key:'shoppingListKey'");
assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class);
}
Aggregations