use of com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics in project commercetools-sync-java by commercetools.
the class CustomObjectSyncIT method sync_withNewCustomObjectAndBadRequest_shouldNotCreateButHandleError.
@Test
void sync_withNewCustomObjectAndBadRequest_shouldNotCreateButHandleError() {
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final CustomObjectUpsertCommand upsertCommand = any(CustomObjectUpsertCommand.class);
when(spyClient.execute(upsertCommand)).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadRequestException("bad request"))).thenCallRealMethod();
final ObjectNode newCustomObjectValue = JsonNodeFactory.instance.objectNode().put("name", "value2");
final CustomObjectDraft<JsonNode> newCustomObjectDraft = CustomObjectDraft.ofUnversionedUpsert("container2", "key2", newCustomObjectValue);
List<String> errorCallBackMessages = new ArrayList<>();
List<Throwable> errorCallBackExceptions = new ArrayList<>();
final CustomObjectSyncOptions spyOptions = CustomObjectSyncOptionsBuilder.of(spyClient).errorCallback((exception, oldResource, newResource, updateActions) -> {
errorCallBackMessages.add(exception.getMessage());
errorCallBackExceptions.add(exception.getCause());
}).build();
final CustomObjectSync customObjectSync = new CustomObjectSync(spyOptions);
final CustomObjectSyncStatistics customObjectSyncStatistics = customObjectSync.sync(Collections.singletonList(newCustomObjectDraft)).toCompletableFuture().join();
assertThat(customObjectSyncStatistics).hasValues(1, 0, 0, 1);
Assertions.assertThat(errorCallBackMessages).hasSize(1);
Assertions.assertThat(errorCallBackExceptions).hasSize(1);
Assertions.assertThat(errorCallBackExceptions.get(0)).isExactlyInstanceOf(CompletionException.class);
Assertions.assertThat(errorCallBackExceptions.get(0).getCause()).isExactlyInstanceOf(BadRequestException.class);
Assertions.assertThat(errorCallBackMessages.get(0)).contains(format("Failed to create custom object with key: '%s'.", CustomObjectCompositeIdentifier.of(newCustomObjectDraft)));
}
Aggregations