Search in sources :

Example 1 with CategoryServiceImpl

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

the class CategoryServiceImplIT method setupTest.

/**
 * Deletes Categories and Types from target CTP projects, then it populates target CTP project
 * with category test data.
 */
@BeforeEach
void setupTest() {
    errorCallBackMessages = new ArrayList<>();
    errorCallBackExceptions = new ArrayList<>();
    warningCallBackMessages = new ArrayList<>();
    deleteAllCategories(CTP_TARGET_CLIENT);
    final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).warningCallback((exception, oldResource, newResource) -> warningCallBackMessages.add(exception.getMessage())).build();
    // Create a mock new category in the target project.
    final CategoryDraft oldCategoryDraft = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "furniture"), LocalizedString.of(Locale.ENGLISH, "furniture")).key(oldCategoryKey).custom(getCustomFieldsDraft()).build();
    oldCategory = CTP_TARGET_CLIENT.execute(CategoryCreateCommand.of(oldCategoryDraft)).toCompletableFuture().join();
    categoryService = new CategoryServiceImpl(categorySyncOptions);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) StringUtils(org.apache.commons.lang3.StringUtils) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ChangeName(io.sphere.sdk.categories.commands.updateactions.ChangeName) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) ArrayList(java.util.ArrayList) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) CategoryITUtils.deleteAllCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Category(io.sphere.sdk.categories.Category) CategoryCreateCommand(io.sphere.sdk.categories.commands.CategoryCreateCommand) Set(java.util.Set) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.times(org.mockito.Mockito.times) CategoryService(com.commercetools.sync.services.CategoryService) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) ChangeSlug(io.sphere.sdk.categories.commands.updateactions.ChangeSlug) Mockito.verify(org.mockito.Mockito.verify) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) CategoryITUtils.getCustomFieldsDraft(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCustomFieldsDraft) Optional(java.util.Optional) Collections(java.util.Collections) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with CategoryServiceImpl

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

the class CategoryServiceImplIT method fetchMatchingCategoriesByKeys_WithBadGateWayExceptionAlways_ShouldFail.

@Test
void fetchMatchingCategoriesByKeys_WithBadGateWayExceptionAlways_ShouldFail() {
    // Mock sphere client to return BadGatewayException on any request.
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    when(spyClient.execute(any(CategoryQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
    final CategorySyncOptions spyOptions = CategorySyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final CategoryService spyCategoryService = new CategoryServiceImpl(spyOptions);
    final Set<String> keys = new HashSet<>();
    keys.add(oldCategoryKey);
    // test and assert
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(spyCategoryService.fetchMatchingCategoriesByKeys(keys)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) StringUtils(org.apache.commons.lang3.StringUtils) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ChangeName(io.sphere.sdk.categories.commands.updateactions.ChangeName) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) ArrayList(java.util.ArrayList) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) CategoryITUtils.deleteAllCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Category(io.sphere.sdk.categories.Category) CategoryCreateCommand(io.sphere.sdk.categories.commands.CategoryCreateCommand) Set(java.util.Set) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.times(org.mockito.Mockito.times) CategoryService(com.commercetools.sync.services.CategoryService) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) ChangeSlug(io.sphere.sdk.categories.commands.updateactions.ChangeSlug) Mockito.verify(org.mockito.Mockito.verify) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) CategoryITUtils.getCustomFieldsDraft(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCustomFieldsDraft) Optional(java.util.Optional) Collections(java.util.Collections) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) SphereClient(io.sphere.sdk.client.SphereClient) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) BadGatewayException(io.sphere.sdk.client.BadGatewayException) LocalizedString(io.sphere.sdk.models.LocalizedString) CategoryService(com.commercetools.sync.services.CategoryService) ExecutionException(java.util.concurrent.ExecutionException) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with CategoryServiceImpl

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

the class CategorySyncTest method sync_WithFailOnFetchingCategories_ShouldTriggerErrorCallbackAndReturnProperStats.

@Test
void sync_WithFailOnFetchingCategories_ShouldTriggerErrorCallbackAndReturnProperStats() {
    // preparation
    final SphereClient mockClient = mock(SphereClient.class);
    final String categoryKey = "key";
    final Category mockCategory = getMockCategory("foo", categoryKey);
    ResourceKeyId resourceKeyId = new ResourceKeyId(categoryKey, "foo");
    @SuppressWarnings("unchecked") final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
    when(resourceKeyIdGraphQlResult.getResults()).thenReturn(singleton(resourceKeyId));
    // successful caching
    when(mockClient.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(resourceKeyIdGraphQlResult));
    // exception on fetch.
    when(mockClient.execute(any(CategoryQuery.class))).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    when(mockClient.execute(any(CategoryCreateCommand.class))).thenReturn(CompletableFuture.completedFuture(mockCategory));
    final CategorySyncOptions syncOptions = CategorySyncOptionsBuilder.of(mockClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final CategoryService categoryServiceSpy = spy(new CategoryServiceImpl(syncOptions));
    final CategorySync mockCategorySync = new CategorySync(syncOptions, getMockTypeService(), categoryServiceSpy, mockUnresolvedReferencesService);
    final CategoryDraft categoryDraft = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "name"), LocalizedString.of(Locale.ENGLISH, "name")).key(categoryKey).custom(CustomFieldsDraft.ofTypeKeyAndJson("customTypeId", new HashMap<>())).build();
    // test
    final CategorySyncStatistics syncStatistics = mockCategorySync.sync(singletonList(categoryDraft)).toCompletableFuture().join();
    // assertions
    assertThat(syncStatistics).hasValues(1, 0, 0, 1);
    assertThat(errorCallBackMessages).hasSize(1).singleElement(as(STRING)).contains("Failed to fetch existing categories");
    assertThat(errorCallBackExceptions).hasSize(1).singleElement(as(THROWABLE)).hasCauseExactlyInstanceOf(SphereException.class);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collections.singletonList(java.util.Collections.singletonList) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) Collections.singleton(java.util.Collections.singleton) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) Locale(java.util.Locale) Map(java.util.Map) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) CategoryCreateCommand(io.sphere.sdk.categories.commands.CategoryCreateCommand) Set(java.util.Set) CategoryService(com.commercetools.sync.services.CategoryService) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) CategorySyncMockUtils.getMockCategoryDraft(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraft) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) CATEGORY_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.CATEGORY_KEY_1_RESOURCE_PATH) Optional(java.util.Optional) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) WaitingToBeResolvedCategories(com.commercetools.sync.commons.models.WaitingToBeResolvedCategories) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) 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) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) Nonnull(javax.annotation.Nonnull) Collections.emptySet(java.util.Collections.emptySet) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) MockUtils.getMockTypeService(com.commercetools.sync.commons.MockUtils.getMockTypeService) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) Collections(java.util.Collections) BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER(com.commercetools.sync.commons.helpers.BaseReferenceResolver.BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER) Category(io.sphere.sdk.categories.Category) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) LocalizedString(io.sphere.sdk.models.LocalizedString) SphereException(io.sphere.sdk.models.SphereException) CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) CategorySyncMockUtils.getMockCategoryDraft(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraft) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategoryCreateCommand(io.sphere.sdk.categories.commands.CategoryCreateCommand) CategoryService(com.commercetools.sync.services.CategoryService) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) Test(org.junit.jupiter.api.Test)

Example 4 with CategoryServiceImpl

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

the class CategoryServiceImplIT method createCategory_WithValidCategory_ShouldCreateCategoryAndCacheId.

@Test
void createCategory_WithValidCategory_ShouldCreateCategoryAndCacheId() {
    // preparation
    final String newCategoryKey = "newCategoryKey";
    final CategoryDraft categoryDraft = CategoryDraftBuilder.of(LocalizedString.of(Locale.ENGLISH, "classic furniture"), LocalizedString.of(Locale.ENGLISH, "classic-furniture", Locale.GERMAN, "klassische-moebel")).key(newCategoryKey).custom(getCustomFieldsDraft()).build();
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    final CategorySyncOptions spyOptions = CategorySyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final CategoryService spyProductService = new CategoryServiceImpl(spyOptions);
    // test
    final Optional<Category> createdOptional = categoryService.createCategory(categoryDraft).toCompletableFuture().join();
    // assertion
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    // assert CTP state
    final Optional<Category> queriedOptional = CTP_TARGET_CLIENT.execute(CategoryQuery.of().withPredicates(categoryQueryModel -> categoryQueryModel.key().is(newCategoryKey))).toCompletableFuture().join().head();
    assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdOptional).hasValueSatisfying(created -> {
        assertThat(queried.getName()).isEqualTo(created.getName());
        assertThat(queried.getSlug()).isEqualTo(created.getSlug());
        assertThat(queried.getCustom()).isNotNull();
        assertThat(queried.getKey()).isEqualTo(newCategoryKey);
    }));
    // Assert that the created category is cached
    final Optional<String> productId = spyProductService.fetchCachedCategoryId(newCategoryKey).toCompletableFuture().join();
    assertThat(productId).isPresent();
    verify(spyClient, times(0)).execute(any(ProductTypeQuery.class));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) StringUtils(org.apache.commons.lang3.StringUtils) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ChangeName(io.sphere.sdk.categories.commands.updateactions.ChangeName) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) ArrayList(java.util.ArrayList) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) CategoryITUtils.deleteAllCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Category(io.sphere.sdk.categories.Category) CategoryCreateCommand(io.sphere.sdk.categories.commands.CategoryCreateCommand) Set(java.util.Set) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.times(org.mockito.Mockito.times) CategoryService(com.commercetools.sync.services.CategoryService) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) ChangeSlug(io.sphere.sdk.categories.commands.updateactions.ChangeSlug) Mockito.verify(org.mockito.Mockito.verify) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) CategoryITUtils.getCustomFieldsDraft(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCustomFieldsDraft) Optional(java.util.Optional) Collections(java.util.Collections) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) Category(io.sphere.sdk.categories.Category) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) SphereClient(io.sphere.sdk.client.SphereClient) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) LocalizedString(io.sphere.sdk.models.LocalizedString) CategoryService(com.commercetools.sync.services.CategoryService) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) Test(org.junit.jupiter.api.Test)

Example 5 with CategoryServiceImpl

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

the class CategoryServiceImplIT method fetchCategory_WithBadGateWayExceptionAlways_ShouldFail.

@Test
void fetchCategory_WithBadGateWayExceptionAlways_ShouldFail() {
    // Mock sphere client to return BadGatewayException on any request.
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    when(spyClient.execute(any(CategoryQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
    final CategorySyncOptions spyOptions = CategorySyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    final CategoryService spyCategoryService = new CategoryServiceImpl(spyOptions);
    // test and assertion
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(spyCategoryService.fetchCategory(oldCategoryKey)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) StringUtils(org.apache.commons.lang3.StringUtils) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ChangeName(io.sphere.sdk.categories.commands.updateactions.ChangeName) DuplicateFieldError(io.sphere.sdk.models.errors.DuplicateFieldError) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) ArrayList(java.util.ArrayList) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ProductTypeQuery(io.sphere.sdk.producttypes.queries.ProductTypeQuery) CategoryITUtils.deleteAllCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Category(io.sphere.sdk.categories.Category) CategoryCreateCommand(io.sphere.sdk.categories.commands.CategoryCreateCommand) Set(java.util.Set) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Mockito.times(org.mockito.Mockito.times) CategoryService(com.commercetools.sync.services.CategoryService) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) ChangeSlug(io.sphere.sdk.categories.commands.updateactions.ChangeSlug) Mockito.verify(org.mockito.Mockito.verify) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) CategoryITUtils.getCustomFieldsDraft(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCustomFieldsDraft) Optional(java.util.Optional) Collections(java.util.Collections) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) SphereClient(io.sphere.sdk.client.SphereClient) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CategoryService(com.commercetools.sync.services.CategoryService) ExecutionException(java.util.concurrent.ExecutionException) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) Test(org.junit.jupiter.api.Test)

Aggregations

CategoryService (com.commercetools.sync.services.CategoryService)7 CategoryServiceImpl (com.commercetools.sync.services.impl.CategoryServiceImpl)7 CategoryDraftBuilder (io.sphere.sdk.categories.CategoryDraftBuilder)7 CategoryQuery (io.sphere.sdk.categories.queries.CategoryQuery)7 SphereClient (io.sphere.sdk.client.SphereClient)7 Test (org.junit.jupiter.api.Test)7 Category (io.sphere.sdk.categories.Category)6 CategoryDraft (io.sphere.sdk.categories.CategoryDraft)6 CategoryCreateCommand (io.sphere.sdk.categories.commands.CategoryCreateCommand)6 LocalizedString (io.sphere.sdk.models.LocalizedString)6 ArrayList (java.util.ArrayList)6 Collections (java.util.Collections)6 HashSet (java.util.HashSet)6 List (java.util.List)6 Locale (java.util.Locale)6 Map (java.util.Map)6 Optional (java.util.Optional)6 Set (java.util.Set)6 CompletionException (java.util.concurrent.CompletionException)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6