Search in sources :

Example 11 with TypeSyncStatistics

use of com.commercetools.sync.types.helpers.TypeSyncStatistics in project commercetools-sync-java by commercetools.

the class TypeSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.

@Test
void sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
    // preparation
    final TypeDraft newTypeDraft = TypeDraftBuilder.of("foo", ofEnglish("name"), ResourceTypeIdsSetBuilder.of().addCategories().build()).description(ofEnglish("desc")).fieldDefinitions(emptyList()).build();
    final List<String> errorMessages = new ArrayList<>();
    final List<Throwable> exceptions = new ArrayList<>();
    final TypeSyncOptions syncOptions = TypeSyncOptionsBuilder.of(mock(SphereClient.class)).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorMessages.add(exception.getMessage());
        exceptions.add(exception.getCause());
    }).build();
    final TypeService mockTypeService = mock(TypeService.class);
    when(mockTypeService.fetchMatchingTypesByKeys(singleton(newTypeDraft.getKey()))).thenReturn(supplyAsync(() -> {
        throw new SphereException();
    }));
    final TypeSync typeSync = new TypeSync(syncOptions, mockTypeService);
    // test
    final TypeSyncStatistics typeSyncStatistics = typeSync.sync(singletonList(newTypeDraft)).toCompletableFuture().join();
    // assertions
    assertThat(errorMessages).hasSize(1).singleElement(as(STRING)).isEqualTo("Failed to fetch existing types with keys: '[foo]'.");
    assertThat(exceptions).hasSize(1).singleElement(as(THROWABLE)).isExactlyInstanceOf(CompletionException.class).hasCauseExactlyInstanceOf(SphereException.class);
    assertThat(typeSyncStatistics).hasValues(1, 0, 0, 1);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TypeDraft(io.sphere.sdk.types.TypeDraft) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ResourceTypeIdsSetBuilder(io.sphere.sdk.types.ResourceTypeIdsSetBuilder) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) Mockito.spy(org.mockito.Mockito.spy) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Collections.singleton(java.util.Collections.singleton) Assertions.as(org.assertj.core.api.Assertions.as) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) TypeService(com.commercetools.sync.services.TypeService) THROWABLE(org.assertj.core.api.InstanceOfAssertFactories.THROWABLE) SphereClient(io.sphere.sdk.client.SphereClient) SphereException(io.sphere.sdk.models.SphereException) TypeDraftBuilder(io.sphere.sdk.types.TypeDraftBuilder) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) TypeSyncStatistics(com.commercetools.sync.types.helpers.TypeSyncStatistics) CompletionException(java.util.concurrent.CompletionException) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) Optional(java.util.Optional) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Mockito.mock(org.mockito.Mockito.mock) Type(io.sphere.sdk.types.Type) ArrayList(java.util.ArrayList) SphereException(io.sphere.sdk.models.SphereException) TypeSyncStatistics(com.commercetools.sync.types.helpers.TypeSyncStatistics) TypeDraft(io.sphere.sdk.types.TypeDraft) SphereClient(io.sphere.sdk.client.SphereClient) CompletionException(java.util.concurrent.CompletionException) TypeService(com.commercetools.sync.services.TypeService) Test(org.junit.jupiter.api.Test)

Example 12 with TypeSyncStatistics

use of com.commercetools.sync.types.helpers.TypeSyncStatistics in project commercetools-sync-java by commercetools.

the class TypeSyncBenchmark method sync_WithSomeExistingTypes_ShouldSyncTypes.

@Test
void sync_WithSomeExistingTypes_ShouldSyncTypes() throws IOException {
    // preparation
    final List<TypeDraft> typeDrafts = buildTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
    final int halfNumberOfDrafts = typeDrafts.size() / 2;
    final List<TypeDraft> firstHalf = typeDrafts.subList(0, halfNumberOfDrafts);
    // Create first half of drafts to target project with different field definition name
    CompletableFuture.allOf(firstHalf.stream().map(TypeDraftBuilder::of).map(TypeSyncBenchmark::applyFieldDefinitionNameChange).map(TypeDraftBuilder::build).map(draft -> CTP_TARGET_CLIENT.execute(TypeCreateCommand.of(draft))).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new)).join();
    final TypeSync typeSync = new TypeSync(typeSyncOptions);
    // benchmark
    final long beforeSyncTime = System.currentTimeMillis();
    final TypeSyncStatistics syncStatistics = executeBlocking(typeSync.sync(typeDrafts));
    final long totalTime = System.currentTimeMillis() - beforeSyncTime;
    // assert on threshold (based on history of benchmarks; highest was ~12 seconds)
    // double of the highest benchmark
    final int threshold = 24000;
    assertThat(totalTime).withFailMessage(format(THRESHOLD_EXCEEDED_ERROR, totalTime, threshold)).isLessThan(threshold);
    // Assert actual state of CTP project (number of updated types)
    final CompletableFuture<Integer> totalNumberOfUpdatedTypesWithOldFieldDefinitionName = CTP_TARGET_CLIENT.execute(TypeQuery.of().withPredicates(p -> p.fieldDefinitions().name().is(FIELD_DEFINITION_NAME_1 + "_old"))).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
    executeBlocking(totalNumberOfUpdatedTypesWithOldFieldDefinitionName);
    assertThat(totalNumberOfUpdatedTypesWithOldFieldDefinitionName).isCompletedWithValue(0);
    // Assert actual state of CTP project (total number of existing types)
    final CompletableFuture<Integer> totalNumberOfTypes = CTP_TARGET_CLIENT.execute(TypeQuery.of()).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
    executeBlocking(totalNumberOfTypes);
    assertThat(totalNumberOfTypes).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
    // Assert statistics
    assertThat(syncStatistics).hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, halfNumberOfDrafts, halfNumberOfDrafts, 0);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    saveNewResult(TYPE_SYNC, CREATES_AND_UPDATES, totalTime);
}
Also used : PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) TypeSyncStatistics(com.commercetools.sync.types.helpers.TypeSyncStatistics) TypeDraft(io.sphere.sdk.types.TypeDraft) TypeSync(com.commercetools.sync.types.TypeSync) Test(org.junit.jupiter.api.Test)

Example 13 with TypeSyncStatistics

use of com.commercetools.sync.types.helpers.TypeSyncStatistics in project commercetools-sync-java by commercetools.

the class TypeSyncBenchmark method sync_ExistingTypes_ShouldUpdateTypes.

@Test
void sync_ExistingTypes_ShouldUpdateTypes() throws IOException {
    // preparation
    final List<TypeDraft> typeDrafts = buildTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
    // Create drafts to target project with different field type name
    CompletableFuture.allOf(typeDrafts.stream().map(TypeDraftBuilder::of).map(TypeSyncBenchmark::applyFieldDefinitionNameChange).map(TypeDraftBuilder::build).map(draft -> CTP_TARGET_CLIENT.execute(TypeCreateCommand.of(draft))).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new)).join();
    final TypeSync typeSync = new TypeSync(typeSyncOptions);
    // benchmark
    final long beforeSyncTime = System.currentTimeMillis();
    final TypeSyncStatistics syncStatistics = executeBlocking(typeSync.sync(typeDrafts));
    final long totalTime = System.currentTimeMillis() - beforeSyncTime;
    // assert on threshold (based on history of benchmarks; highest was ~13 seconds)
    // double of the highest benchmark
    final int threshold = 26000;
    assertThat(totalTime).withFailMessage(format(THRESHOLD_EXCEEDED_ERROR, totalTime, threshold)).isLessThan(threshold);
    // Assert actual state of CTP project (number of updated types)
    final CompletableFuture<Integer> totalNumberOfUpdatedTypes = CTP_TARGET_CLIENT.execute(TypeQuery.of().withPredicates(p -> p.fieldDefinitions().name().is(FIELD_DEFINITION_NAME_1))).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
    executeBlocking(totalNumberOfUpdatedTypes);
    assertThat(totalNumberOfUpdatedTypes).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
    // Assert actual state of CTP project (total number of existing types)
    final CompletableFuture<Integer> totalNumberOfTypes = CTP_TARGET_CLIENT.execute(TypeQuery.of()).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
    executeBlocking(totalNumberOfTypes);
    assertThat(totalNumberOfTypes).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
    // Assert statistics
    assertThat(syncStatistics).hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, 0, NUMBER_OF_RESOURCE_UNDER_TEST, 0);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    saveNewResult(TYPE_SYNC, UPDATES_ONLY, totalTime);
}
Also used : PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) TypeSyncStatistics(com.commercetools.sync.types.helpers.TypeSyncStatistics) TypeDraft(io.sphere.sdk.types.TypeDraft) TypeSync(com.commercetools.sync.types.TypeSync) Test(org.junit.jupiter.api.Test)

Example 14 with TypeSyncStatistics

use of com.commercetools.sync.types.helpers.TypeSyncStatistics in project commercetools-sync-java by commercetools.

the class TypeSyncBenchmark method sync_NewTypes_ShouldCreateTypes.

@Test
void sync_NewTypes_ShouldCreateTypes() throws IOException {
    // preparation
    final List<TypeDraft> typeDrafts = buildTypeDrafts(NUMBER_OF_RESOURCE_UNDER_TEST);
    final TypeSync typeSync = new TypeSync(typeSyncOptions);
    // benchmark
    final long beforeSyncTime = System.currentTimeMillis();
    final TypeSyncStatistics syncStatistics = executeBlocking(typeSync.sync(typeDrafts));
    final long totalTime = System.currentTimeMillis() - beforeSyncTime;
    // assert on threshold (based on history of benchmarks; highest was ~13 seconds)
    // double of the highest benchmark
    final int threshold = 26000;
    assertThat(totalTime).withFailMessage(format(THRESHOLD_EXCEEDED_ERROR, totalTime, threshold)).isLessThan(threshold);
    // Assert actual state of CTP project (total number of existing types)
    final CompletableFuture<Integer> totalNumberOfTypes = CTP_TARGET_CLIENT.execute(TypeQuery.of()).thenApply(PagedQueryResult::getTotal).thenApply(Long::intValue).toCompletableFuture();
    executeBlocking(totalNumberOfTypes);
    assertThat(totalNumberOfTypes).isCompletedWithValue(NUMBER_OF_RESOURCE_UNDER_TEST);
    assertThat(syncStatistics).hasValues(NUMBER_OF_RESOURCE_UNDER_TEST, NUMBER_OF_RESOURCE_UNDER_TEST, 0, 0);
    assertThat(errorCallBackExceptions).isEmpty();
    assertThat(errorCallBackMessages).isEmpty();
    assertThat(warningCallBackMessages).isEmpty();
    saveNewResult(TYPE_SYNC, CREATES_ONLY, totalTime);
}
Also used : PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) TypeSyncStatistics(com.commercetools.sync.types.helpers.TypeSyncStatistics) TypeDraft(io.sphere.sdk.types.TypeDraft) TypeSync(com.commercetools.sync.types.TypeSync) Test(org.junit.jupiter.api.Test)

Example 15 with TypeSyncStatistics

use of com.commercetools.sync.types.helpers.TypeSyncStatistics in project commercetools-sync-java by commercetools.

the class TypeSyncIT method sync_WithSeveralBatches_ShouldReturnProperStatistics.

@Test
void sync_WithSeveralBatches_ShouldReturnProperStatistics() {
    // preparation
    // Default batch size is 50 (check TypeSyncOptionsBuilder) so we have 2 batches of 50
    final List<TypeDraft> typeDrafts = IntStream.range(0, 100).mapToObj(i -> TypeDraftBuilder.of("key__" + Integer.toString(i), LocalizedString.ofEnglish("name__" + Integer.toString(i)), ResourceTypeIdsSetBuilder.of().addCategories().build()).description(LocalizedString.ofEnglish("description__" + Integer.toString(i))).fieldDefinitions(singletonList(FIELD_DEFINITION_1)).build()).collect(Collectors.toList());
    final TypeSyncOptions typeSyncOptions = TypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
    final TypeSync typeSync = new TypeSync(typeSyncOptions);
    // test
    final TypeSyncStatistics typeSyncStatistics = typeSync.sync(typeDrafts).toCompletableFuture().join();
    // assertion
    assertThat(typeSyncStatistics).hasValues(100, 100, 0, 0);
}
Also used : TypeDraft(io.sphere.sdk.types.TypeDraft) BeforeEach(org.junit.jupiter.api.BeforeEach) TYPE_DESCRIPTION_1(com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_DESCRIPTION_1) TYPE_DESCRIPTION_2(com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_DESCRIPTION_2) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collections.singletonList(java.util.Collections.singletonList) FieldDefinition(io.sphere.sdk.types.FieldDefinition) LocalizedEnumFieldType(io.sphere.sdk.types.LocalizedEnumFieldType) AfterAll(org.junit.jupiter.api.AfterAll) FIELD_DEFINITION_LABEL_1(com.commercetools.sync.integration.commons.utils.TypeITUtils.FIELD_DEFINITION_LABEL_1) Arrays.asList(java.util.Arrays.asList) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) EnumValue(io.sphere.sdk.models.EnumValue) ITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes) BadGatewayException(io.sphere.sdk.client.BadGatewayException) TypeSyncStatistics(com.commercetools.sync.types.helpers.TypeSyncStatistics) TypeSync(com.commercetools.sync.types.TypeSync) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) FIELD_DEFINITION_1(com.commercetools.sync.integration.commons.utils.TypeITUtils.FIELD_DEFINITION_1) Test(org.junit.jupiter.api.Test) TYPE_KEY_1(com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_KEY_1) LocalizedString(io.sphere.sdk.models.LocalizedString) FIELD_DEFINITION_3(com.commercetools.sync.integration.commons.utils.TypeITUtils.FIELD_DEFINITION_3) List(java.util.List) FIELD_DEFINITION_2(com.commercetools.sync.integration.commons.utils.TypeITUtils.FIELD_DEFINITION_2) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) TextInputHint(io.sphere.sdk.models.TextInputHint) TYPE_KEY_2(com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_KEY_2) TypeITUtils.populateTargetProject(com.commercetools.sync.integration.commons.utils.TypeITUtils.populateTargetProject) Optional(java.util.Optional) Mockito.any(org.mockito.Mockito.any) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) IntStream(java.util.stream.IntStream) TypeSyncOptionsBuilder(com.commercetools.sync.types.TypeSyncOptionsBuilder) TypeUpdateCommand(io.sphere.sdk.types.commands.TypeUpdateCommand) ResourceTypeIdsSetBuilder(io.sphere.sdk.types.ResourceTypeIdsSetBuilder) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) TypeSyncOptions(com.commercetools.sync.types.TypeSyncOptions) ArrayList(java.util.ArrayList) Assertions.as(org.assertj.core.api.Assertions.as) TypeQuery(io.sphere.sdk.types.queries.TypeQuery) TypeDraftBuilder(io.sphere.sdk.types.TypeDraftBuilder) Nonnull(javax.annotation.Nonnull) StringFieldType(io.sphere.sdk.types.StringFieldType) TYPE_NAME_1(com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_NAME_1) FIELD_DEFINITION_NAME_1(com.commercetools.sync.integration.commons.utils.TypeITUtils.FIELD_DEFINITION_NAME_1) TYPE_NAME_2(com.commercetools.sync.integration.commons.utils.TypeITUtils.TYPE_NAME_2) TypeCreateCommand(io.sphere.sdk.types.commands.TypeCreateCommand) LocalizedEnumValue(io.sphere.sdk.models.LocalizedEnumValue) Mockito.when(org.mockito.Mockito.when) ErrorResponseException(io.sphere.sdk.client.ErrorResponseException) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) EnumFieldType(io.sphere.sdk.types.EnumFieldType) TypeITUtils.getTypeByKey(com.commercetools.sync.integration.commons.utils.TypeITUtils.getTypeByKey) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) Collections(java.util.Collections) SetFieldType(io.sphere.sdk.types.SetFieldType) Type(io.sphere.sdk.types.Type) TypeSyncOptions(com.commercetools.sync.types.TypeSyncOptions) TypeSyncStatistics(com.commercetools.sync.types.helpers.TypeSyncStatistics) TypeDraft(io.sphere.sdk.types.TypeDraft) TypeSync(com.commercetools.sync.types.TypeSync) Test(org.junit.jupiter.api.Test)

Aggregations

TypeSyncStatistics (com.commercetools.sync.types.helpers.TypeSyncStatistics)21 TypeDraft (io.sphere.sdk.types.TypeDraft)21 Test (org.junit.jupiter.api.Test)21 TypeSync (com.commercetools.sync.types.TypeSync)20 Type (io.sphere.sdk.types.Type)18 TypeSyncOptions (com.commercetools.sync.types.TypeSyncOptions)17 EnumFieldType (io.sphere.sdk.types.EnumFieldType)15 LocalizedEnumFieldType (io.sphere.sdk.types.LocalizedEnumFieldType)15 SetFieldType (io.sphere.sdk.types.SetFieldType)15 StringFieldType (io.sphere.sdk.types.StringFieldType)15 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)11 PagedQueryResult (io.sphere.sdk.queries.PagedQueryResult)11 TypeDraftBuilder (io.sphere.sdk.types.TypeDraftBuilder)11 ArrayList (java.util.ArrayList)11 List (java.util.List)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)10 TypeITUtils.populateTargetProject (com.commercetools.sync.integration.commons.utils.TypeITUtils.populateTargetProject)10 TypeSyncOptionsBuilder (com.commercetools.sync.types.TypeSyncOptionsBuilder)10 LocalizedString (io.sphere.sdk.models.LocalizedString)10