Search in sources :

Example 1 with CategoryService

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

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

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

the class CategorySyncTest method sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback.

@Test
void sync_WithOnlyDraftsToUpdate_ShouldOnlyCallBeforeUpdateCallback() {
    // preparation
    final CategoryDraft categoryDraft = getMockCategoryDraft(Locale.ENGLISH, "name", "categoryKey1", "parentKey", "customTypeId", new HashMap<>());
    final Category mockedExistingCategory = readObjectFromResource(CATEGORY_KEY_1_RESOURCE_PATH, Category.class);
    final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(mock(SphereClient.class)).build();
    final CategoryService categoryService = mockCategoryService(singleton(mockedExistingCategory), mockedExistingCategory, mockedExistingCategory);
    Map<String, String> cache = new HashMap<>();
    cache.put("categoryKey1", mockedExistingCategory.getId());
    cache.put("parentKey", mockedExistingCategory.getParent().getId());
    when(categoryService.cacheKeysToIds(anySet())).thenReturn(completedFuture(cache));
    when(categoryService.fetchCachedCategoryId(Mockito.eq("parentKey"))).thenReturn(CompletableFuture.completedFuture(Optional.of(mockedExistingCategory.getParent().getId())));
    when(categoryService.fetchCategory(Mockito.eq("categoryKey1"))).thenReturn(CompletableFuture.completedFuture(Optional.of(mockedExistingCategory)));
    final CategorySyncOptions spyCategorySyncOptions = spy(categorySyncOptions);
    // test
    new CategorySync(spyCategorySyncOptions, getMockTypeService(), categoryService, mockUnresolvedReferencesService).sync(singletonList(categoryDraft)).toCompletableFuture().join();
    // assertion
    verify(spyCategorySyncOptions).applyBeforeUpdateCallback(any(), any(), any());
    verify(spyCategorySyncOptions, never()).applyBeforeCreateCallback(any());
}
Also used : Category(io.sphere.sdk.categories.Category) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) HashMap(java.util.HashMap) CategorySyncMockUtils.getMockCategoryDraft(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraft) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) LocalizedString(io.sphere.sdk.models.LocalizedString) CategoryService(com.commercetools.sync.services.CategoryService) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) Test(org.junit.jupiter.api.Test)

Example 4 with CategoryService

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

the class CategorySyncTest method sync_WithExistingCategoryButWithNullParentReference_ShouldFailSync.

@Test
void sync_WithExistingCategoryButWithNullParentReference_ShouldFailSync() {
    final Category mockCategory = getMockCategory(UUID.randomUUID().toString(), "key");
    final CategoryService mockCategoryService = mockCategoryService(singleton(mockCategory), null, mockCategory);
    final CategorySync categorySync = new CategorySync(categorySyncOptions, getMockTypeService(), mockCategoryService, mockUnresolvedReferencesService);
    final List<CategoryDraft> categoryDrafts = singletonList(getMockCategoryDraft(Locale.ENGLISH, "name", "key", null, "customTypeId", new HashMap<>()));
    final CategorySyncStatistics syncStatistics = categorySync.sync(categoryDrafts).toCompletableFuture().join();
    assertThat(syncStatistics).hasValues(1, 0, 0, 1);
    assertThat(errorCallBackMessages).hasSize(1);
    assertThat(errorCallBackMessages.get(0)).contains(BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER);
    assertThat(errorCallBackExceptions).hasSize(1);
}
Also used : Category(io.sphere.sdk.categories.Category) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) HashMap(java.util.HashMap) CategorySyncMockUtils.getMockCategoryDraft(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraft) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategoryService(com.commercetools.sync.services.CategoryService) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) Test(org.junit.jupiter.api.Test)

Example 5 with CategoryService

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

the class CategorySyncTest method sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback.

@Test
void sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback() {
    // preparation
    final CategoryDraft categoryDraft = getMockCategoryDraft(Locale.ENGLISH, "name", "foo", "parentKey", "customTypeId", new HashMap<>());
    final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(mock(SphereClient.class)).build();
    final Category createdCategory = mock(Category.class);
    when(createdCategory.getKey()).thenReturn(categoryDraft.getKey());
    final CategoryService categoryService = mockCategoryService(emptySet(), createdCategory);
    when(categoryService.fetchCachedCategoryId(Mockito.eq("parentKey"))).thenReturn(CompletableFuture.completedFuture(Optional.of("parentId")));
    Map<String, String> cache = new HashMap<>();
    cache.put("1", UUID.randomUUID().toString());
    cache.put("parentKey", UUID.randomUUID().toString());
    when(categoryService.cacheKeysToIds(anySet())).thenReturn(completedFuture(cache));
    final CategorySyncOptions spyCategorySyncOptions = spy(categorySyncOptions);
    // test
    new CategorySync(spyCategorySyncOptions, getMockTypeService(), categoryService, mockUnresolvedReferencesService).sync(singletonList(categoryDraft)).toCompletableFuture().join();
    // assertion
    verify(spyCategorySyncOptions).applyBeforeCreateCallback(any());
    verify(spyCategorySyncOptions, never()).applyBeforeUpdateCallback(any(), any(), any());
}
Also used : Category(io.sphere.sdk.categories.Category) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) HashMap(java.util.HashMap) CategorySyncMockUtils.getMockCategoryDraft(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraft) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) LocalizedString(io.sphere.sdk.models.LocalizedString) CategoryService(com.commercetools.sync.services.CategoryService) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) Test(org.junit.jupiter.api.Test)

Aggregations

CategoryService (com.commercetools.sync.services.CategoryService)35 Test (org.junit.jupiter.api.Test)32 MockUtils.mockCategoryService (com.commercetools.sync.commons.MockUtils.mockCategoryService)27 Category (io.sphere.sdk.categories.Category)24 CategorySyncMockUtils.getMockCategory (com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory)20 CategoryDraft (io.sphere.sdk.categories.CategoryDraft)19 HashSet (java.util.HashSet)18 CategorySyncMockUtils.getMockCategoryDraft (com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraft)15 CategorySyncStatistics (com.commercetools.sync.categories.helpers.CategorySyncStatistics)15 SphereClient (io.sphere.sdk.client.SphereClient)15 Set (java.util.Set)15 Collections (java.util.Collections)14 List (java.util.List)14 Map (java.util.Map)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Mockito.when (org.mockito.Mockito.when)14 ReferenceResolutionException (com.commercetools.sync.commons.exceptions.ReferenceResolutionException)12 SphereException (io.sphere.sdk.models.SphereException)12 CompletableFuture (java.util.concurrent.CompletableFuture)12 ResourceIdentifier (io.sphere.sdk.models.ResourceIdentifier)11