use of com.commercetools.sync.types.TypeSync 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);
}
use of com.commercetools.sync.types.TypeSync in project commercetools-project-sync by commercetools.
the class TypeSyncer method of.
@Nonnull
public static TypeSyncer of(@Nonnull final SphereClient sourceClient, @Nonnull final SphereClient targetClient, @Nonnull final Clock clock) {
final QuadConsumer<SyncException, Optional<TypeDraft>, Optional<Type>, List<UpdateAction<Type>>> logErrorCallback = (exception, newResourceDraft, oldResource, updateActions) -> logErrorCallback(LOGGER, "type", exception, oldResource, updateActions);
final TriConsumer<SyncException, Optional<TypeDraft>, Optional<Type>> logWarningCallback = (exception, newResourceDraft, oldResource) -> logWarningCallback(LOGGER, "type", exception, oldResource);
final TypeSyncOptions syncOptions = TypeSyncOptionsBuilder.of(targetClient).errorCallback(logErrorCallback).warningCallback(logWarningCallback).build();
final TypeSync typeSync = new TypeSync(syncOptions);
final CustomObjectService customObjectService = new CustomObjectServiceImpl(targetClient);
return new TypeSyncer(typeSync, sourceClient, targetClient, customObjectService, clock);
}
use of com.commercetools.sync.types.TypeSync 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);
}
use of com.commercetools.sync.types.TypeSync 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);
}
use of com.commercetools.sync.types.TypeSync 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);
}
Aggregations