use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategorySyncTest method sync_WithErrorOnFetchingUnresolvableCategory_ShouldNotCreateCategory.
@Test
void sync_WithErrorOnFetchingUnresolvableCategory_ShouldNotCreateCategory() {
final Category mockRootCategory = getMockCategory(UUID.randomUUID().toString(), "root");
final Category mockParentCategory = getMockCategory(UUID.randomUUID().toString(), "parentKey");
final CategoryService mockCategoryService = mockCategoryService(Collections.singleton(mockRootCategory), mockParentCategory);
when(mockCategoryService.fetchCachedCategoryId(Mockito.eq("root"))).thenReturn(completedFuture(Optional.of("rootID")));
final CompletableFuture<Set<WaitingToBeResolvedCategories>> futureThrowingSphereException = new CompletableFuture<>();
futureThrowingSphereException.completeExceptionally(new SphereException("CTP error on fetch"));
when(mockUnresolvedReferencesService.fetch(any(), any(), any())).thenReturn(futureThrowingSphereException);
final CategorySync mockCategorySync = new CategorySync(categorySyncOptions, getMockTypeService(), mockCategoryService, mockUnresolvedReferencesService);
final List<CategoryDraft> categoryDrafts = new ArrayList<>();
categoryDrafts.add(getMockCategoryDraft(Locale.ENGLISH, "name", "child", "parentKey", "customTypeId", new HashMap<>()));
categoryDrafts.add(getMockCategoryDraft(Locale.ENGLISH, "name", "parentKey", "root", "customTypeId", new HashMap<>()));
final CategorySyncStatistics syncStatistics = mockCategorySync.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(2, 1, 0, 1);
assertThat(errorCallBackMessages).hasSize(1);
assertThat(errorCallBackExceptions).hasSize(1);
assertThat(errorCallBackMessages.get(0)).isEqualTo("Failed to fetch CategoryDraft waiting to be resolved with parent keys: '[child]'.");
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategorySyncTest method sync_WithExistingCategory_ShouldUpdateCategory.
@Test
void sync_WithExistingCategory_ShouldUpdateCategory() {
final Category mockCategory = getMockCategory(UUID.randomUUID().toString(), "key");
final Category mockParentCategory = getMockCategory(UUID.randomUUID().toString(), "parentKey");
HashSet<Category> existingCategories = new HashSet<>();
existingCategories.add(mockCategory);
existingCategories.add(mockParentCategory);
final CategoryService mockCategoryService = mockCategoryService(existingCategories, null, mockCategory);
when(mockCategoryService.fetchCachedCategoryId(Mockito.eq("parentKey"))).thenReturn(CompletableFuture.completedFuture(Optional.of("parentId")));
when(mockCategoryService.fetchCategory(Mockito.eq("key"))).thenReturn(CompletableFuture.completedFuture(Optional.of(mockCategory)));
final CategorySync mockCategorySync = new CategorySync(categorySyncOptions, getMockTypeService(), mockCategoryService, mockUnresolvedReferencesService);
final List<CategoryDraft> categoryDrafts = singletonList(getMockCategoryDraft(Locale.ENGLISH, "name", "key", "parentKey", "customTypeId", new HashMap<>()));
final CategorySyncStatistics syncStatistics = mockCategorySync.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(1, 0, 1, 0);
assertThat(errorCallBackMessages).hasSize(0);
assertThat(errorCallBackExceptions).hasSize(0);
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategorySyncTest method sync_WithDefaultBatchSize_ShouldCallSyncOnEachBatch.
@Test
void sync_WithDefaultBatchSize_ShouldCallSyncOnEachBatch() {
// preparation
final int numberOfCategoryDrafts = 160;
final List<Category> mockedCreatedCategories = IntStream.range(0, numberOfCategoryDrafts).mapToObj(i -> getMockCategory(UUID.randomUUID().toString(), "key" + i)).collect(Collectors.toList());
final List<CategoryDraft> categoryDrafts = mockedCreatedCategories.stream().map(category -> CategoryDraftBuilder.of(category).build()).collect(Collectors.toList());
final Category createdCategory = mock(Category.class);
when(createdCategory.getKey()).thenReturn("foo");
when(createdCategory.getId()).thenReturn(UUID.randomUUID().toString());
final CategoryService mockCategoryService = mockCategoryService(emptySet(), createdCategory);
final CategorySyncMock categorySync = new CategorySyncMock(categorySyncOptions, getMockTypeService(), mockCategoryService, mockUnresolvedReferencesService);
final CategorySyncMock syncSpy = spy(categorySync);
// test
syncSpy.sync(categoryDrafts).toCompletableFuture().join();
// assertion
final int expectedNumberOfCalls = (int) (Math.ceil(numberOfCategoryDrafts / (double) CategorySyncOptionsBuilder.BATCH_SIZE_DEFAULT) + 1);
verify(syncSpy, times(expectedNumberOfCalls)).syncBatches(any(), any());
assertThat(errorCallBackMessages).hasSize(0);
assertThat(errorCallBackExceptions).hasSize(0);
}
use of com.commercetools.sync.services.CategoryService 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);
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategorySyncTest method sync_WithADraftWithNoSetKey_ShouldFailSync.
@Test
void sync_WithADraftWithNoSetKey_ShouldFailSync() {
final CategoryService mockCategoryService = mockCategoryService(emptySet(), null);
final CategorySync mockCategorySync = new CategorySync(categorySyncOptions, getMockTypeService(), mockCategoryService, mockUnresolvedReferencesService);
final List<CategoryDraft> categoryDrafts = singletonList(getMockCategoryDraft(Locale.ENGLISH, "noKeyDraft", "no-key-id-draft", null));
final CategorySyncStatistics syncStatistics = mockCategorySync.sync(categoryDrafts).toCompletableFuture().join();
assertThat(syncStatistics).hasValues(1, 0, 0, 1);
assertThat(errorCallBackMessages).hasSize(1);
assertThat(errorCallBackMessages.get(0)).isEqualTo("CategoryDraft with name: " + "LocalizedString(en -> noKeyDraft) doesn't have a key. Please make sure all category drafts have keys.");
assertThat(errorCallBackExceptions).hasSize(1);
assertThat(errorCallBackExceptions.get(0)).isEqualTo(null);
}
Aggregations