Search in sources :

Example 6 with TypeService

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;
}
Also used : TypeService(com.commercetools.sync.services.TypeService)

Example 7 with 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;
}
Also used : TypeService(com.commercetools.sync.services.TypeService) Nonnull(javax.annotation.Nonnull)

Example 8 with 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);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TypeDraft(io.sphere.sdk.types.TypeDraft) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ResourceTypeIdsSetBuilder(io.sphere.sdk.types.ResourceTypeIdsSetBuilder) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) Mockito.spy(org.mockito.Mockito.spy) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Collections.singleton(java.util.Collections.singleton) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) TypeService(com.commercetools.sync.services.TypeService) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) TypeDraftBuilder(io.sphere.sdk.types.TypeDraftBuilder) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) TypeSyncStatistics(com.commercetools.sync.types.helpers.TypeSyncStatistics) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Mockito.mock(org.mockito.Mockito.mock) Type(io.sphere.sdk.types.Type) ArrayList(java.util.ArrayList) SphereException(io.sphere.sdk.models.SphereException) TypeSyncStatistics(com.commercetools.sync.types.helpers.TypeSyncStatistics) TypeDraft(io.sphere.sdk.types.TypeDraft) SphereClient(io.sphere.sdk.client.SphereClient) CompletionException(java.util.concurrent.CompletionException) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 9 with TypeService

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);
}
Also used : Type(io.sphere.sdk.types.Type) TypeDraft(io.sphere.sdk.types.TypeDraft) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 10 with TypeService

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);
}
Also used : ShoppingListSyncStatistics(com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics) ShoppingListService(com.commercetools.sync.services.ShoppingListService) CustomerService(com.commercetools.sync.services.CustomerService) ShoppingList(io.sphere.sdk.shoppinglists.ShoppingList) ShoppingListDraft(io.sphere.sdk.shoppinglists.ShoppingListDraft) SphereException(io.sphere.sdk.models.SphereException) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Aggregations

TypeService (com.commercetools.sync.services.TypeService)20 Test (org.junit.jupiter.api.Test)16 TypeServiceImpl (com.commercetools.sync.services.impl.TypeServiceImpl)10 SphereClient (io.sphere.sdk.client.SphereClient)10 TypeDraft (io.sphere.sdk.types.TypeDraft)8 ArrayList (java.util.ArrayList)8 Collections.singletonList (java.util.Collections.singletonList)8 List (java.util.List)8 Optional (java.util.Optional)8 Assertions.as (org.assertj.core.api.Assertions.as)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 STRING (org.assertj.core.api.InstanceOfAssertFactories.STRING)8 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)8 Mockito.spy (org.mockito.Mockito.spy)8 Mockito.verify (org.mockito.Mockito.verify)8 Mockito.when (org.mockito.Mockito.when)8 TypeQuery (io.sphere.sdk.types.queries.TypeQuery)7 Type (io.sphere.sdk.types.Type)6 Collections.singleton (java.util.Collections.singleton)6 LocalizedString.ofEnglish (io.sphere.sdk.models.LocalizedString.ofEnglish)5