Search in sources :

Example 16 with TypeService

use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.

the class LineItemListUpdateActionUtilsTest 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 17 with TypeService

use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.

the class CategoryReferenceResolverTest method resolveCustomTypeReference_WithExceptionOnCustomTypeFetch_ShouldNotResolveReferences.

@Test
void resolveCustomTypeReference_WithExceptionOnCustomTypeFetch_ShouldNotResolveReferences() {
    // Preparation
    final SphereClient ctpClient = mock(SphereClient.class);
    final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(ctpClient).build();
    final TypeService typeService = new TypeServiceImpl(categorySyncOptions);
    final CompletableFuture<PagedQueryResult<Type>> futureThrowingSphereException = new CompletableFuture<>();
    futureThrowingSphereException.completeExceptionally(new SphereException("CTP error on fetch"));
    when(ctpClient.execute(any(TypeQuery.class))).thenReturn(futureThrowingSphereException);
    final CategoryDraftBuilder categoryDraft = getMockCategoryDraftBuilder(Locale.ENGLISH, "myDraft", "key", null, "customTypeId", new HashMap<>());
    final CategoryReferenceResolver categoryReferenceResolver = new CategoryReferenceResolver(categorySyncOptions, typeService, categoryService);
    // Test and assertion
    assertThat(categoryReferenceResolver.resolveCustomTypeReference(categoryDraft)).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(SphereException.class).withMessageContaining("CTP error on fetch");
}
Also used : PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) CompletableFuture(java.util.concurrent.CompletableFuture) SphereClient(io.sphere.sdk.client.SphereClient) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) CategorySyncMockUtils.getMockCategoryDraftBuilder(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraftBuilder) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) TypeQuery(io.sphere.sdk.types.queries.TypeQuery) SphereException(io.sphere.sdk.models.SphereException) TypeService(com.commercetools.sync.services.TypeService) MockUtils.getMockTypeService(com.commercetools.sync.commons.MockUtils.getMockTypeService) Test(org.junit.jupiter.api.Test)

Example 18 with TypeService

use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.

the class CustomerSyncTest method sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats.

@Test
void sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats() {
    // preparation
    final TypeService typeService = spy(new TypeServiceImpl(syncOptions));
    when(typeService.cacheKeysToIds(anySet())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final CustomerSync customerSync = new CustomerSync(syncOptions, mock(CustomerService.class), typeService, mock(CustomerGroupService.class));
    final CustomerDraft customerDraft = CustomerDraftBuilder.of("email", "pass").key("customerKey").customerGroup(ResourceIdentifier.ofKey("customerGroupKey")).custom(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap())).stores(asList(ResourceIdentifier.ofKey("storeKey1"), ResourceIdentifier.ofKey("storeKey2"), ResourceIdentifier.ofId("storeId3"))).build();
    // test
    final CustomerSyncStatistics customerSyncStatistics = customerSync.sync(singletonList(customerDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(customerSyncStatistics).hasValues(1, 0, 0, 1);
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to build a cache of keys to ids.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
}
Also used : CustomerService(com.commercetools.sync.services.CustomerService) CompletionException(java.util.concurrent.CompletionException) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) SphereException(io.sphere.sdk.models.SphereException) CustomerSyncStatistics(com.commercetools.sync.customers.helpers.CustomerSyncStatistics) TypeService(com.commercetools.sync.services.TypeService) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) Test(org.junit.jupiter.api.Test)

Example 19 with TypeService

use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.

the class CustomerSyncTest method sync_WithNonExistingTypeReference_ShouldTriggerErrorCallbackAndReturnProperStats.

@Test
void sync_WithNonExistingTypeReference_ShouldTriggerErrorCallbackAndReturnProperStats() {
    // preparation
    final TypeService mockTypeService = mock(TypeService.class);
    when(mockTypeService.fetchCachedTypeId(anyString())).thenReturn(completedFuture(empty()));
    when(mockTypeService.cacheKeysToIds(anySet())).thenReturn(completedFuture(emptyMap()));
    final CustomerService mockCustomerService = mock(CustomerService.class);
    when(mockCustomerService.fetchMatchingCustomersByKeys(singleton("customerKey"))).thenReturn(completedFuture(new HashSet<>(singletonList(mock(Customer.class)))));
    final CustomerSync customerSync = new CustomerSync(syncOptions, mockCustomerService, mockTypeService, mock(CustomerGroupService.class));
    final CustomerDraft customerDraft = CustomerDraftBuilder.of("email", "pass").key("customerKey").custom(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap())).build();
    // test
    final CustomerSyncStatistics customerSyncStatistics = customerSync.sync(singletonList(customerDraft)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(customerSyncStatistics).hasValues(1, 0, 0, 1);
    final String expectedExceptionMessage = format(FAILED_TO_RESOLVE_CUSTOM_TYPE, customerDraft.getKey());
    final String expectedMessageWithCause = format("%s Reason: %s", expectedExceptionMessage, format(TYPE_DOES_NOT_EXIST, "typeKey"));
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains(expectedMessageWithCause);
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(ReferenceResolutionException.class);
}
Also used : CustomerService(com.commercetools.sync.services.CustomerService) CompletionException(java.util.concurrent.CompletionException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CustomerSyncStatistics(com.commercetools.sync.customers.helpers.CustomerSyncStatistics) TypeService(com.commercetools.sync.services.TypeService) HashSet(java.util.HashSet) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) Test(org.junit.jupiter.api.Test)

Example 20 with TypeService

use of com.commercetools.sync.services.TypeService in project commercetools-sync-java by commercetools.

the class ShoppingListSyncTest method sync_WithExceptionOnCachingKeysToIds_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithExceptionOnCachingKeysToIds_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    // preparation
    final TypeService typeService = mock(TypeService.class);
    final CustomerService customerService = mock(CustomerService.class);
    when(typeService.cacheKeysToIds(any())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    when(customerService.cacheKeysToIds(any())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final ShoppingListSync shoppingListSync = new ShoppingListSync(syncOptions, mock(ShoppingListService.class), customerService, typeService);
    ShoppingListDraft shoppingListDraft = ShoppingListDraftBuilder.of(LocalizedString.ofEnglish("shopping-list-name")).key("shopping-list-key").customer(ResourceIdentifier.ofKey("customer-key")).custom(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap())).build();
    // test
    ShoppingListSyncStatistics statistics = shoppingListSync.sync(singletonList(shoppingListDraft)).toCompletableFuture().join();
    AssertionsForStatistics.assertThat(statistics).hasValues(1, 0, 0, 1);
    // assertions
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to build a cache of keys to ids.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(SphereException.class);
}
Also used : ShoppingListSyncStatistics(com.commercetools.sync.shoppinglists.helpers.ShoppingListSyncStatistics) CustomerService(com.commercetools.sync.services.CustomerService) ShoppingListService(com.commercetools.sync.services.ShoppingListService) CompletionException(java.util.concurrent.CompletionException) 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