Search in sources :

Example 16 with CategorySyncStatistics

use of com.commercetools.sync.categories.helpers.CategorySyncStatistics 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]'.");
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Category(io.sphere.sdk.categories.Category) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) Set(java.util.Set) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) HashSet(java.util.HashSet) Collections.emptySet(java.util.Collections.emptySet) HashMap(java.util.HashMap) CategorySyncMockUtils.getMockCategoryDraft(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraft) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ArrayList(java.util.ArrayList) 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) Test(org.junit.jupiter.api.Test)

Example 17 with CategorySyncStatistics

use of com.commercetools.sync.categories.helpers.CategorySyncStatistics 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);
}
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 18 with CategorySyncStatistics

use of com.commercetools.sync.categories.helpers.CategorySyncStatistics 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);
}
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) Category(io.sphere.sdk.categories.Category) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) LocalizedString(io.sphere.sdk.models.LocalizedString) SphereException(io.sphere.sdk.models.SphereException) CategorySyncStatistics(com.commercetools.sync.categories.helpers.CategorySyncStatistics) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) CategorySyncMockUtils.getMockCategoryDraft(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraft) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategoryCreateCommand(io.sphere.sdk.categories.commands.CategoryCreateCommand) CategoryService(com.commercetools.sync.services.CategoryService) MockUtils.mockCategoryService(com.commercetools.sync.commons.MockUtils.mockCategoryService) Test(org.junit.jupiter.api.Test)

Example 19 with CategorySyncStatistics

use of com.commercetools.sync.categories.helpers.CategorySyncStatistics 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);
}
Also used : 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) Test(org.junit.jupiter.api.Test)

Example 20 with CategorySyncStatistics

use of com.commercetools.sync.categories.helpers.CategorySyncStatistics in project commercetools-sync-java by commercetools.

the class CategorySyncTest method sync_WithIdenticalExistingCategory_ShouldNotUpdateCategory.

@Test
void sync_WithIdenticalExistingCategory_ShouldNotUpdateCategory() {
    final Category mockCategory = getMockCategory(UUID.randomUUID().toString(), "key");
    final CategoryDraft identicalCategoryDraft = CategoryDraftBuilder.of(mockCategory).build();
    final CategoryService mockCategoryService = mockCategoryService(singleton(mockCategory), null, mockCategory);
    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(identicalCategoryDraft);
    final CategorySyncStatistics syncStatistics = mockCategorySync.sync(categoryDrafts).toCompletableFuture().join();
    assertThat(errorCallBackMessages).hasSize(0);
    assertThat(errorCallBackExceptions).hasSize(0);
    assertThat(syncStatistics).hasValues(1, 0, 0, 0);
}
Also used : Category(io.sphere.sdk.categories.Category) CategorySyncMockUtils.getMockCategory(com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory) 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) Test(org.junit.jupiter.api.Test)

Aggregations

CategorySyncStatistics (com.commercetools.sync.categories.helpers.CategorySyncStatistics)36 Test (org.junit.jupiter.api.Test)36 CategoryDraft (io.sphere.sdk.categories.CategoryDraft)34 Category (io.sphere.sdk.categories.Category)22 ArrayList (java.util.ArrayList)14 MockUtils.mockCategoryService (com.commercetools.sync.commons.MockUtils.mockCategoryService)13 CategoryService (com.commercetools.sync.services.CategoryService)13 HashMap (java.util.HashMap)13 LocalizedString (io.sphere.sdk.models.LocalizedString)12 CategorySyncMockUtils.getMockCategoryDraft (com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategoryDraft)11 CategorySyncMockUtils.getMockCategory (com.commercetools.sync.categories.CategorySyncMockUtils.getMockCategory)10 CategorySync (com.commercetools.sync.categories.CategorySync)9 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)7 CategoryDraftBuilder (io.sphere.sdk.categories.CategoryDraftBuilder)7 CategoryCreateCommand (io.sphere.sdk.categories.commands.CategoryCreateCommand)7 CategoryQuery (io.sphere.sdk.categories.queries.CategoryQuery)7 ResourceIdentifier (io.sphere.sdk.models.ResourceIdentifier)7 CustomFieldsDraft (io.sphere.sdk.types.CustomFieldsDraft)7 Collections (java.util.Collections)7 List (java.util.List)7