Search in sources :

Example 11 with TypeService

use of com.commercetools.sync.services.TypeService 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 12 with TypeService

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

the class VariantReferenceResolverTest method setup.

@BeforeEach
void setup() {
    final TypeService typeService = getMockTypeService();
    final ChannelService channelService = getMockChannelService(getMockSupplyChannel(CHANNEL_ID, CHANNEL_KEY));
    final ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(mock(SphereClient.class)).build();
    referenceResolver = new VariantReferenceResolver(syncOptions, typeService, channelService, mock(CustomerGroupService.class), getMockProductService(PRODUCT_ID), getMockProductTypeService(PRODUCT_TYPE_ID), getMockCategoryService(CATEGORY_ID), getMockCustomObjectService(CUSTOM_OBJECT_ID), getMockStateService(STATE_ID), getMockCustomerService(CUSTOMER_ID));
}
Also used : ChannelService(com.commercetools.sync.services.ChannelService) InventorySyncMockUtils.getMockChannelService(com.commercetools.sync.inventories.InventorySyncMockUtils.getMockChannelService) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) TypeService(com.commercetools.sync.services.TypeService) ProductSyncMockUtils.getMockProductTypeService(com.commercetools.sync.products.ProductSyncMockUtils.getMockProductTypeService) MockUtils.getMockTypeService(com.commercetools.sync.commons.MockUtils.getMockTypeService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 13 with TypeService

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

the class TypeSyncTest method sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback.

@Test
void sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback() {
    // preparation
    final TypeDraft newTypeDraft = TypeDraftBuilder.of("newType", ofEnglish("typeName"), ResourceTypeIdsSetBuilder.of().addChannels()).build();
    final TypeSyncOptions typeSyncOptions = TypeSyncOptionsBuilder.of(mock(SphereClient.class)).build();
    final TypeService typeService = mock(TypeService.class);
    when(typeService.fetchMatchingTypesByKeys(anySet())).thenReturn(completedFuture(emptySet()));
    when(typeService.createType(any())).thenReturn(completedFuture(Optional.empty()));
    final TypeSyncOptions spyTypeSyncOptions = spy(typeSyncOptions);
    // test
    new TypeSync(spyTypeSyncOptions, typeService).sync(singletonList(newTypeDraft)).toCompletableFuture().join();
    // assertion
    verify(spyTypeSyncOptions).applyBeforeCreateCallback(newTypeDraft);
    verify(spyTypeSyncOptions, never()).applyBeforeUpdateCallback(any(), any(), any());
}
Also used : TypeDraft(io.sphere.sdk.types.TypeDraft) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 14 with TypeService

use of com.commercetools.sync.services.TypeService 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 15 with TypeService

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

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