use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategoryReferenceResolverTest method resolveCategoryReferences_WithEmptyKeyOnCategoryReference_ShouldNotResolveReference.
@Test
void resolveCategoryReferences_WithEmptyKeyOnCategoryReference_ShouldNotResolveReference() {
// preparation
final CategoryService mockCategoryService = mockCategoryService(emptySet(), null);
final ProductDraftBuilder productBuilder = getBuilderWithRandomProductType().categories(singleton(ResourceIdentifier.ofKey("")));
final ProductReferenceResolver productReferenceResolver = createProductReferenceResolver(mockCategoryService);
// test and assertion
assertThat(productReferenceResolver.resolveCategoryReferences(productBuilder)).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(ReferenceResolutionException.class).withMessageContaining(format("Failed to resolve 'category' resource identifier on ProductDraft with " + "key:'%s'. Reason: %s", productBuilder.getKey(), BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER));
}
use of com.commercetools.sync.services.CategoryService 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));
}
use of com.commercetools.sync.services.CategoryService 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);
}
use of com.commercetools.sync.services.CategoryService in project commercetools-sync-java by commercetools.
the class CategorySyncTest method sync_WithExistingCategoryButWithEmptyParentReference_ShouldFailSync.
@Test
void sync_WithExistingCategoryButWithEmptyParentReference_ShouldFailSync() {
final Category mockCategory = getMockCategory(UUID.randomUUID().toString(), "key");
final CategoryService mockCategoryService = mockCategoryService(singleton(mockCategory), null, mockCategory);
when(mockCategoryService.fetchCategory(Mockito.eq("key"))).thenReturn(CompletableFuture.completedFuture(Optional.of(mockCategory)));
final CategorySync categorySync = new CategorySync(categorySyncOptions, getMockTypeService(), mockCategoryService, mockUnresolvedReferencesService);
final List<CategoryDraft> categoryDrafts = singletonList(getMockCategoryDraft(Locale.ENGLISH, "name", "key", "", "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_WithBatchSizeSet_ShouldCallSyncOnEachBatch.
@Test
void sync_WithBatchSizeSet_ShouldCallSyncOnEachBatch() {
// preparation
final int batchSize = 1;
final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).batchSize(batchSize).build();
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
int expectedNumberOfCalls = (int) (Math.ceil(numberOfCategoryDrafts / batchSize) + 1);
verify(syncSpy, times(expectedNumberOfCalls)).syncBatches(any(), any());
assertThat(errorCallBackMessages).hasSize(0);
assertThat(errorCallBackExceptions).hasSize(0);
}
Aggregations