Search in sources :

Example 6 with TypeServiceImpl

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

the class PriceReferenceResolverTest method resolveCustomTypeReference_WithExceptionOnCustomTypeFetch_ShouldNotResolveReferences.

@Test
void resolveCustomTypeReference_WithExceptionOnCustomTypeFetch_ShouldNotResolveReferences() {
    // Preparation
    final SphereClient ctpClient = mock(SphereClient.class);
    final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(ctpClient).build();
    final TypeService typeService = new TypeServiceImpl(productSyncOptions);
    final CompletableFuture<PagedQueryResult<Type>> futureThrowingSphereException = new CompletableFuture<>();
    futureThrowingSphereException.completeExceptionally(new SphereException("CTP error on fetch"));
    when(ctpClient.execute(any(TypeQuery.class))).thenReturn(futureThrowingSphereException);
    final String customTypeKey = "customTypeKey";
    final PriceDraftBuilder priceBuilder = PriceDraftBuilder.of(MoneyImpl.of(BigDecimal.TEN, DefaultCurrencyUnits.EUR)).country(CountryCode.DE).custom(CustomFieldsDraft.ofTypeKeyAndJson(customTypeKey, new HashMap<>()));
    final PriceReferenceResolver priceReferenceResolver = new PriceReferenceResolver(productSyncOptions, typeService, channelService, customerGroupService);
    // Test and assertion
    assertThat(priceReferenceResolver.resolveCustomTypeReference(priceBuilder)).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(SphereException.class).withMessageContaining("CTP error on fetch");
}
Also used : PriceDraftBuilder(io.sphere.sdk.products.PriceDraftBuilder) HashMap(java.util.HashMap) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TypeQuery(io.sphere.sdk.types.queries.TypeQuery) SphereException(io.sphere.sdk.models.SphereException) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) CompletableFuture(java.util.concurrent.CompletableFuture) SphereClient(io.sphere.sdk.client.SphereClient) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) TypeService(com.commercetools.sync.services.TypeService) MockUtils.getMockTypeService(com.commercetools.sync.commons.MockUtils.getMockTypeService) Test(org.junit.jupiter.api.Test)

Example 7 with TypeServiceImpl

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

the class TypeServiceImplIT method createType_WithValidType_ShouldCreateTypeAndCacheId.

@Test
void createType_WithValidType_ShouldCreateTypeAndCacheId() {
    final TypeDraft newTypeDraft = TypeDraftBuilder.of(TYPE_KEY_1, TYPE_NAME_1, ResourceTypeIdsSetBuilder.of().addCategories().build()).description(TYPE_DESCRIPTION_1).fieldDefinitions(singletonList(FIELD_DEFINITION_1)).build();
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    final TypeSyncOptions spyOptions = TypeSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final TypeService spyTypeService = new TypeServiceImpl(spyOptions);
    // test
    final Optional<Type> createdType = spyTypeService.createType(newTypeDraft).toCompletableFuture().join();
    final Optional<Type> queriedOptional = CTP_TARGET_CLIENT.execute(TypeQuery.of().withPredicates(typeQueryModel -> typeQueryModel.key().is(TYPE_KEY_1))).toCompletableFuture().join().head();
    assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdType).hasValueSatisfying(created -> {
        assertThat(created.getKey()).isEqualTo(queried.getKey());
        assertThat(created.getDescription()).isEqualTo(queried.getDescription());
        assertThat(created.getName()).isEqualTo(queried.getName());
        assertThat(created.getFieldDefinitions()).isEqualTo(queried.getFieldDefinitions());
    }));
    // Assert that the created type is cached
    final Optional<String> typeId = spyTypeService.fetchCachedTypeId(TYPE_KEY_1).toCompletableFuture().join();
    assertThat(typeId).isPresent();
    verify(spyClient, times(0)).execute(any(TypeQuery.class));
}
Also used : TypeDraft(io.sphere.sdk.types.TypeDraft) BeforeEach(org.junit.jupiter.api.BeforeEach) TYPE_DESCRIPTION_1(com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_DESCRIPTION_1) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) Collections.singleton(java.util.Collections.singleton) Locale(java.util.Locale) SphereClient(io.sphere.sdk.client.SphereClient) ChangeKey(io.sphere.sdk.types.commands.updateactions.ChangeKey) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Set(java.util.Set) FIELD_DEFINITION_1(com.commercetools.sync.integration.commons.utils.TypeITUtils.FIELD_DEFINITION_1) Test(org.junit.jupiter.api.Test) TYPE_KEY_1(com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_KEY_1) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) Optional(java.util.Optional) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TypeSyncOptionsBuilder(com.commercetools.sync.types.TypeSyncOptionsBuilder) ResourceTypeIdsSetBuilder(io.sphere.sdk.types.ResourceTypeIdsSetBuilder) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) TypeSyncOptions(com.commercetools.sync.types.TypeSyncOptions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assertions.as(org.assertj.core.api.Assertions.as) TypeService(com.commercetools.sync.services.TypeService) TypeQuery(io.sphere.sdk.types.queries.TypeQuery) TypeDraftBuilder(io.sphere.sdk.types.TypeDraftBuilder) TYPE_NAME_1(com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_NAME_1) CategoryITUtils.deleteAllCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) ChangeName(io.sphere.sdk.types.commands.updateactions.ChangeName) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) TypeITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.TypeITUtils.deleteTypes) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Collections(java.util.Collections) Type(io.sphere.sdk.types.Type) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Type(io.sphere.sdk.types.Type) SphereClient(io.sphere.sdk.client.SphereClient) TypeSyncOptions(com.commercetools.sync.types.TypeSyncOptions) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) TypeQuery(io.sphere.sdk.types.queries.TypeQuery) TypeDraft(io.sphere.sdk.types.TypeDraft) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 8 with TypeServiceImpl

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

the class InventorySyncTest method sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats.

@Test
void sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats() {
    // preparation
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, actions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception.getCause());
    }).build();
    final TypeService typeService = spy(new TypeServiceImpl(inventorySyncOptions));
    when(typeService.cacheKeysToIds(anySet())).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final InventorySync inventorySync = new InventorySync(inventorySyncOptions, mock(InventoryService.class), mock(ChannelService.class), typeService);
    final InventoryEntryDraft newInventoryDraftWithCustomType = mock(InventoryEntryDraft.class);
    when(newInventoryDraftWithCustomType.getSku()).thenReturn("sku");
    when(newInventoryDraftWithCustomType.getCustom()).thenReturn(CustomFieldsDraft.ofTypeKeyAndJson("typeKey", emptyMap()));
    // test
    final InventorySyncStatistics inventorySyncStatistics = inventorySync.sync(singletonList(newInventoryDraftWithCustomType)).toCompletableFuture().join();
    // assertions
    AssertionsForStatistics.assertThat(inventorySyncStatistics).hasValues(1, 0, 0, 1);
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to build a cache of keys to ids.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(CompletionException.class).hasCauseExactlyInstanceOf(SphereException.class);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Reference(io.sphere.sdk.models.Reference) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ZonedDateTime(java.time.ZonedDateTime) InventorySyncMockUtils.getCompletionStageWithException(com.commercetools.sync.inventories.InventorySyncMockUtils.getCompletionStageWithException) Channel(io.sphere.sdk.channels.Channel) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) InventorySyncMockUtils.getMockSupplyChannel(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockSupplyChannel) InventorySyncMockUtils.getMockInventoryService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryService) Collections.emptyList(java.util.Collections.emptyList) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) String.format(java.lang.String.format) ZoneId(java.time.ZoneId) Test(org.junit.jupiter.api.Test) InventoryEntryDraftBuilder(io.sphere.sdk.inventory.InventoryEntryDraftBuilder) ChannelService(com.commercetools.sync.services.ChannelService) List(java.util.List) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) Optional(java.util.Optional) InventorySyncMockUtils.getMockInventoryEntry(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryEntry) AssertionsForStatistics(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) InventorySyncMockUtils.getMockChannelService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService) InventoryService(com.commercetools.sync.services.InventoryService) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) TypeService(com.commercetools.sync.services.TypeService) SphereException(io.sphere.sdk.models.SphereException) Collections.emptyMap(java.util.Collections.emptyMap) Mockito.when(org.mockito.Mockito.when) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Collections(java.util.Collections) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER(com.commercetools.sync.commons.helpers.BaseReferenceResolver.BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InventorySyncMockUtils.getMockInventoryService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockInventoryService) InventoryService(com.commercetools.sync.services.InventoryService) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) SphereException(io.sphere.sdk.models.SphereException) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) ChannelService(com.commercetools.sync.services.ChannelService) InventorySyncMockUtils.getMockChannelService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService) SphereClient(io.sphere.sdk.client.SphereClient) CompletionException(java.util.concurrent.CompletionException) InventorySyncStatistics(com.commercetools.sync.inventories.helpers.InventorySyncStatistics) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 9 with TypeServiceImpl

use of com.commercetools.sync.services.impl.TypeServiceImpl 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 10 with TypeServiceImpl

use of com.commercetools.sync.services.impl.TypeServiceImpl 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)

Aggregations

TypeService (com.commercetools.sync.services.TypeService)10 TypeServiceImpl (com.commercetools.sync.services.impl.TypeServiceImpl)10 Test (org.junit.jupiter.api.Test)10 SphereClient (io.sphere.sdk.client.SphereClient)9 TypeQuery (io.sphere.sdk.types.queries.TypeQuery)7 ArrayList (java.util.ArrayList)7 Collections.singletonList (java.util.Collections.singletonList)7 List (java.util.List)7 Optional (java.util.Optional)7 Assertions.as (org.assertj.core.api.Assertions.as)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 STRING (org.assertj.core.api.InstanceOfAssertFactories.STRING)7 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)7 Mockito.spy (org.mockito.Mockito.spy)7 Mockito.verify (org.mockito.Mockito.verify)7 Mockito.when (org.mockito.Mockito.when)7 CategoryITUtils.createCategoriesCustomType (com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType)5 CategoryITUtils.deleteAllCategories (com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories)5 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)5 FIELD_DEFINITION_1 (com.commercetools.sync.integration.commons.utils.TypeITUtils.FIELD_DEFINITION_1)5