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