use of com.commercetools.sync.services.CustomObjectService in project commercetools-sync-java by commercetools.
the class CustomObjectServiceImplIT method setup.
/**
* Deletes customObjects from the target CTP project, then it populates the project with test
* data.
*/
@BeforeEach
void setup() {
errorCallBackMessages = new ArrayList<>();
errorCallBackExceptions = new ArrayList<>();
deleteCustomObject(CTP_TARGET_CLIENT, OLD_CUSTOM_OBJECT_KEY, OLD_CUSTOM_OBJECT_CONTAINER);
deleteCustomObject(CTP_TARGET_CLIENT, NEW_CUSTOM_OBJECT_KEY, NEW_CUSTOM_OBJECT_CONTAINER);
createCustomObject(CTP_TARGET_CLIENT, OLD_CUSTOM_OBJECT_KEY, OLD_CUSTOM_OBJECT_CONTAINER, OLD_CUSTOM_OBJECT_VALUE);
final CustomObjectSyncOptions customObjectSyncOptions = CustomObjectSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
customObjectService = new CustomObjectServiceImpl(customObjectSyncOptions);
}
use of com.commercetools.sync.services.CustomObjectService in project commercetools-sync-java by commercetools.
the class CustomObjectServiceImplIT method upsertCustomObject_WithValidCustomObject_ShouldCreateCustomObjectAndCacheId.
@Test
void upsertCustomObject_WithValidCustomObject_ShouldCreateCustomObjectAndCacheId() {
final CustomObjectDraft<JsonNode> newCustomObjectDraft = CustomObjectDraft.ofUnversionedUpsert(NEW_CUSTOM_OBJECT_CONTAINER, NEW_CUSTOM_OBJECT_KEY, NEW_CUSTOM_OBJECT_VALUE);
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final CustomObjectSyncOptions spyOptions = CustomObjectSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final CustomObjectService spyCustomObjectService = new CustomObjectServiceImpl(spyOptions);
final Optional<CustomObject<JsonNode>> createdCustomObject = spyCustomObjectService.upsertCustomObject(newCustomObjectDraft).toCompletableFuture().join();
final Optional<CustomObject<JsonNode>> queriedOptional = CTP_TARGET_CLIENT.execute(CustomObjectQuery.ofJsonNode().withPredicates(customObjectQueryModel -> customObjectQueryModel.container().is(NEW_CUSTOM_OBJECT_CONTAINER).and(customObjectQueryModel.key().is(NEW_CUSTOM_OBJECT_KEY)))).toCompletableFuture().join().head();
assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdCustomObject).hasValueSatisfying(created -> {
assertThat(created.getKey()).isEqualTo(queried.getKey());
assertThat(created.getContainer()).isEqualTo(queried.getContainer());
assertThat(created.getId()).isEqualTo(queried.getId());
assertThat(created.getValue()).isEqualTo(queried.getValue());
}));
// Assert that the created customObject is cached
final Optional<String> customObjectId = spyCustomObjectService.fetchCachedCustomObjectId(CustomObjectCompositeIdentifier.of(NEW_CUSTOM_OBJECT_KEY, NEW_CUSTOM_OBJECT_CONTAINER)).toCompletableFuture().join();
assertThat(customObjectId).isPresent();
verify(spyClient, times(0)).execute(any(CustomObjectQuery.class));
}
use of com.commercetools.sync.services.CustomObjectService in project commercetools-sync-java by commercetools.
the class ProductSyncMockUtils method getMockCustomObjectService.
/**
* Creates a mock {@link CustomObjectService} that returns a completed {@link CompletableFuture}
* containing an {@link Optional} containing the id of the supplied value whenever the following
* method is called on the service:
*
* <ul>
* <li>{@link CustomObjectService#fetchCachedCustomObjectId}
* </ul>
*
* @return the created mock of the {@link CustomObjectService}.
*/
public static CustomObjectService getMockCustomObjectService(@Nonnull final String id) {
final CustomObjectService customObjectService = mock(CustomObjectService.class);
when(customObjectService.fetchCachedCustomObjectId(any())).thenReturn(CompletableFuture.completedFuture(Optional.of(id)));
return customObjectService;
}
use of com.commercetools.sync.services.CustomObjectService in project commercetools-sync-java by commercetools.
the class CustomObjectSyncTest method sync_WitEmptyValidDrafts_ShouldFailed.
@Test
void sync_WitEmptyValidDrafts_ShouldFailed() {
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final CustomObjectSyncOptions spyCustomObjectSyncOptions = initCustomObjectSyncOptions(errorMessages, exceptions);
final CustomObjectService customObjectService = mock(CustomObjectService.class);
when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(emptySet()));
when(customObjectService.upsertCustomObject(any())).thenReturn(completedFuture(Optional.empty()));
// test
CustomObjectSyncStatistics syncStatistics = new CustomObjectSync(spyCustomObjectSyncOptions, customObjectService).sync(singletonList(null)).toCompletableFuture().join();
// assertion
assertThat(exceptions).hasSize(1);
assertThat(errorMessages).hasSize(1);
assertAll(() -> assertThat(syncStatistics.getProcessed().get()).isEqualTo(1), () -> assertThat(syncStatistics.getCreated().get()).isEqualTo(0), () -> assertThat(syncStatistics.getUpdated().get()).isEqualTo(0), () -> assertThat(syncStatistics.getFailed().get()).isEqualTo(1));
}
use of com.commercetools.sync.services.CustomObjectService in project commercetools-sync-java by commercetools.
the class CustomObjectSyncTest method sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter.
@Test
void sync_WithErrorFetchingExistingKeys_ShouldExecuteCallbackOnErrorAndIncreaseFailedCounter() {
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final CustomObjectSyncOptions spyCustomObjectSyncOptions = initCustomObjectSyncOptions(errorMessages, exceptions);
final CustomObjectService mockCustomObjectService = mock(CustomObjectService.class);
when(mockCustomObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
final CustomObjectSync customObjectSync = new CustomObjectSync(spyCustomObjectSyncOptions, mockCustomObjectService);
// test
final CustomObjectSyncStatistics customObjectSyncStatistics = customObjectSync.sync(singletonList(newCustomObjectDraft)).toCompletableFuture().join();
// assertion
assertThat(errorMessages).hasSize(1).singleElement().asString().isEqualTo("Failed to fetch existing custom objects with keys: " + "'[someContainer|someKey]'.");
assertThat(exceptions).hasSize(1).singleElement().isInstanceOfSatisfying(Throwable.class, throwable -> {
assertThat(throwable).isExactlyInstanceOf(CompletionException.class);
assertThat(throwable).hasCauseExactlyInstanceOf(SphereException.class);
});
assertThat(customObjectSyncStatistics).hasValues(1, 0, 0, 1);
}
Aggregations