use of com.commercetools.sync.types.TypeSyncOptions 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.TypeSyncOptions 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);
}
use of com.commercetools.sync.types.TypeSyncOptions in project commercetools-sync-java by commercetools.
the class TypeSyncIT method sync_WithUpdatedType_ShouldUpdateType.
@Test
void sync_WithUpdatedType_ShouldUpdateType() {
// preparation
final Optional<Type> oldTypeBefore = getTypeByKey(CTP_TARGET_CLIENT, TYPE_KEY_1);
assertThat(oldTypeBefore).isNotEmpty();
final TypeDraft newTypeDraft = TypeDraftBuilder.of(TYPE_KEY_1, TYPE_NAME_2, ResourceTypeIdsSetBuilder.of().addCategories().build()).description(TYPE_DESCRIPTION_2).fieldDefinitions(singletonList(FIELD_DEFINITION_1)).build();
final TypeSyncOptions typeSyncOptions = TypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final TypeSync typeSync = new TypeSync(typeSyncOptions);
// test
final TypeSyncStatistics typeSyncStatistics = typeSync.sync(singletonList(newTypeDraft)).toCompletableFuture().join();
// assertions
assertThat(typeSyncStatistics).hasValues(1, 0, 1, 0);
final Optional<Type> oldTypeAfter = getTypeByKey(CTP_TARGET_CLIENT, TYPE_KEY_1);
assertThat(oldTypeAfter).isNotEmpty();
assertThat(oldTypeAfter).hasValueSatisfying(type -> {
assertThat(type.getName()).isEqualTo(TYPE_NAME_2);
assertThat(type.getDescription()).isEqualTo(TYPE_DESCRIPTION_2);
assertFieldDefinitionsAreEqual(type.getFieldDefinitions(), singletonList(FIELD_DEFINITION_1));
});
}
use of com.commercetools.sync.types.TypeSyncOptions in project commercetools-sync-java by commercetools.
the class TypeSyncIT method sync_WithUpdatedType_WithoutOldFieldDefinition_ShouldUpdateTypeRemovingFieldDefinition.
@Test
void sync_WithUpdatedType_WithoutOldFieldDefinition_ShouldUpdateTypeRemovingFieldDefinition() {
final Optional<Type> oldTypeBefore = getTypeByKey(CTP_TARGET_CLIENT, TYPE_KEY_1);
assertThat(oldTypeBefore).isNotEmpty();
final TypeDraft newTypeDraft = TypeDraftBuilder.of(TYPE_KEY_1, TYPE_NAME_1, ResourceTypeIdsSetBuilder.of().addCategories().build()).description(TYPE_DESCRIPTION_1).fieldDefinitions(singletonList(FIELD_DEFINITION_1)).build();
final TypeSyncOptions typeSyncOptions = TypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
final TypeSync typeSync = new TypeSync(typeSyncOptions);
final TypeSyncStatistics typeSyncStatistics = typeSync.sync(singletonList(newTypeDraft)).toCompletableFuture().join();
assertThat(typeSyncStatistics).hasValues(1, 0, 1, 0);
final Optional<Type> oldTypeAfter = getTypeByKey(CTP_TARGET_CLIENT, TYPE_KEY_1);
assertThat(oldTypeAfter).hasValueSatisfying(type -> assertFieldDefinitionsAreEqual(type.getFieldDefinitions(), singletonList(FIELD_DEFINITION_1)));
}
use of com.commercetools.sync.types.TypeSyncOptions in project commercetools-sync-java by commercetools.
the class TypeServiceImplIT method createType_WithValidType_ShouldCreateTypeAndCacheId.
@Test
void createType_WithValidType_ShouldCreateTypeAndCacheId() {
final TypeDraft newTypeDraft = TypeDraftBuilder.of(TYPE_KEY_1, TYPE_NAME_1, ResourceTypeIdsSetBuilder.of().addCategories().build()).description(TYPE_DESCRIPTION_1).fieldDefinitions(singletonList(FIELD_DEFINITION_1)).build();
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final TypeSyncOptions spyOptions = TypeSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final TypeService spyTypeService = new TypeServiceImpl(spyOptions);
// test
final Optional<Type> createdType = spyTypeService.createType(newTypeDraft).toCompletableFuture().join();
final Optional<Type> queriedOptional = CTP_TARGET_CLIENT.execute(TypeQuery.of().withPredicates(typeQueryModel -> typeQueryModel.key().is(TYPE_KEY_1))).toCompletableFuture().join().head();
assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdType).hasValueSatisfying(created -> {
assertThat(created.getKey()).isEqualTo(queried.getKey());
assertThat(created.getDescription()).isEqualTo(queried.getDescription());
assertThat(created.getName()).isEqualTo(queried.getName());
assertThat(created.getFieldDefinitions()).isEqualTo(queried.getFieldDefinitions());
}));
// Assert that the created type is cached
final Optional<String> typeId = spyTypeService.fetchCachedTypeId(TYPE_KEY_1).toCompletableFuture().join();
assertThat(typeId).isPresent();
verify(spyClient, times(0)).execute(any(TypeQuery.class));
}
Aggregations