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);
}
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);
}
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());
}
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);
}
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());
}
Aggregations