Search in sources :

Example 31 with CategoryService

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

the class CategorySyncTest method sync_WithANullDraft_ShouldBeCountedAsFailed.

@Test
void sync_WithANullDraft_ShouldBeCountedAsFailed() {
    final CategoryService mockCategoryService = mockCategoryService(emptySet(), null);
    final CategorySync mockCategorySync = new CategorySync(categorySyncOptions, getMockTypeService(), mockCategoryService, mockUnresolvedReferencesService);
    final CategorySyncStatistics syncStatistics = mockCategorySync.sync(singletonList(null)).toCompletableFuture().join();
    assertThat(syncStatistics).hasValues(1, 0, 0, 1);
    assertThat(errorCallBackMessages).hasSize(1);
    assertThat(errorCallBackMessages.get(0)).isEqualTo("CategoryDraft is null.");
    assertThat(errorCallBackExceptions).hasSize(1);
    assertThat(errorCallBackExceptions.get(0)).isEqualTo(null);
}
Also used : 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 32 with CategoryService

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

the class CategorySyncTest method sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats.

@Test
void sync_WithFailOnCachingKeysToIds_ShouldTriggerErrorCallbackAndReturnProperStats() {
    // preparation
    final SphereClient mockClient = mock(SphereClient.class);
    when(mockClient.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    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);
    CategoryDraft categoryDraft = getMockCategoryDraft(Locale.ENGLISH, "name", "newKey", "parentKey", "customTypeId", new HashMap<>());
    // 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 build a cache of keys to ids.");
    assertThat(errorCallBackExceptions).hasSize(1).singleElement().matches(throwable -> throwable instanceof CompletionException && throwable.getCause() instanceof SphereException);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collections.singletonList(java.util.Collections.singletonList) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) Collections.singleton(java.util.Collections.singleton) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) Locale(java.util.Locale) Map(java.util.Map) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) CategoryCreateCommand(io.sphere.sdk.categories.commands.CategoryCreateCommand) Set(java.util.Set) CategoryService(com.commercetools.sync.services.CategoryService) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) CategorySyncMockUtils.getMockCategoryDraft(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraft) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) CATEGORY_KEY_1_RESOURCE_PATH(com.commercetools.sync.products.ProductSyncMockUtils.CATEGORY_KEY_1_RESOURCE_PATH) Optional(java.util.Optional) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) WaitingToBeResolvedCategories(com.commercetools.sync.commons.models.WaitingToBeResolvedCategories) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) TypeService(com.commercetools.sync.services.TypeService) SphereException(io.sphere.sdk.models.SphereException) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) Nonnull(javax.annotation.Nonnull) Collections.emptySet(java.util.Collections.emptySet) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) CustomFieldsDraft(io.sphere.sdk.types.CustomFieldsDraft) MockUtils.getMockTypeService(com.commercetools.sync.commons.MockUtils.getMockTypeService) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) Collections(java.util.Collections) BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER(com.commercetools.sync.commons.helpers.BaseReferenceResolver.BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER) SphereClient(io.sphere.sdk.client.SphereClient) CategorySyncMockUtils.getMockCategoryDraft(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraft) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CompletionException(java.util.concurrent.CompletionException) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) CategoryService(com.commercetools.sync.services.CategoryService) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) SphereException(io.sphere.sdk.models.SphereException) CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) Test(org.junit.jupiter.api.Test)

Example 33 with CategoryService

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

the class CategorySyncTest method sync_WithExistingCategoryButWithEmptyCustomTypeReference_ShouldFailSync.

@Test
void sync_WithExistingCategoryButWithEmptyCustomTypeReference_ShouldFailSync() {
    final Category mockCategory = getMockCategory(UUID.randomUUID().toString(), "key");
    final Category mockParentCategory = getMockCategory(UUID.randomUUID().toString(), "parentKey");
    Set<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")));
    final CategorySync categorySync = new CategorySync(categorySyncOptions, getMockTypeService(), mockCategoryService, mockUnresolvedReferencesService);
    final List<CategoryDraft> categoryDrafts = singletonList(getMockCategoryDraft(Locale.ENGLISH, "name", "key", "parentKey", "", 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)).isEqualTo(format("Failed to process the CategoryDraft with" + " key: 'key'. Reason: %s: Failed to resolve custom type reference on CategoryDraft " + "with key:'key'. Reason: %s", ReferenceResolutionException.class.getCanonicalName(), BLANK_KEY_VALUE_ON_RESOURCE_IDENTIFIER));
    assertThat(errorCallBackExceptions).hasSize(1);
    assertThat(errorCallBackExceptions.get(0)).isExactlyInstanceOf(CompletionException.class);
    assertThat(errorCallBackExceptions.get(0).getCause()).isExactlyInstanceOf(ReferenceResolutionException.class);
}
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) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 34 with CategoryService

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

the class CategoryReferenceResolverTest method resolveParentReference_WithExceptionOnFetch_ShouldNotResolveReferences.

@Test
void resolveParentReference_WithExceptionOnFetch_ShouldNotResolveReferences() {
    // Preparation
    final SphereClient ctpClient = mock(SphereClient.class);
    final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(ctpClient).build();
    final CategoryService categoryService = new CategoryServiceImpl(categorySyncOptions);
    final CompletableFuture<PagedQueryResult<Category>> futureThrowingSphereException = new CompletableFuture<>();
    futureThrowingSphereException.completeExceptionally(new SphereException("CTP error on fetch"));
    when(ctpClient.execute(any(CategoryQuery.class))).thenReturn(futureThrowingSphereException);
    final CategoryDraftBuilder categoryDraft = getMockCategoryDraftBuilder(Locale.ENGLISH, "myDraft", "key", "nonExistingCategoryKey", "customTypeKey", new HashMap<>());
    final CategoryReferenceResolver categoryReferenceResolver = new CategoryReferenceResolver(categorySyncOptions, typeService, categoryService);
    // Test and assertion
    assertThat(categoryReferenceResolver.resolveParentReference(categoryDraft)).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(SphereException.class).withMessageContaining("CTP error on fetch");
}
Also used : PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) CompletableFuture(java.util.concurrent.CompletableFuture) SphereClient(io.sphere.sdk.client.SphereClient) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) CategorySyncOptions(com.commercetools.sync.categories.CategorySyncOptions) CategoryDraftBuilder(io.sphere.sdk.categories.CategoryDraftBuilder) CategorySyncMockUtils.getMockCategoryDraftBuilder(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraftBuilder) CategoryService(com.commercetools.sync.services.CategoryService) SphereException(io.sphere.sdk.models.SphereException) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) Test(org.junit.jupiter.api.Test)

Example 35 with CategoryService

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

the class MockUtils method mockCategoryService.

public static CategoryService mockCategoryService(@Nonnull final Set<Category> existingCategories, @Nullable final Category createdCategory) {
    final CategoryService mockCategoryService = mock(CategoryService.class);
    when(mockCategoryService.fetchMatchingCategoriesByKeys(any())).thenReturn(CompletableFuture.completedFuture(existingCategories));
    final Map<String, String> keyToIds = existingCategories.stream().collect(Collectors.toMap(Category::getKey, Category::getId));
    when(mockCategoryService.cacheKeysToIds(anySet())).thenReturn(completedFuture(keyToIds));
    when(mockCategoryService.createCategory(any())).thenReturn(CompletableFuture.completedFuture(ofNullable(createdCategory)));
    return mockCategoryService;
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CategoryService(com.commercetools.sync.services.CategoryService)

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