use of com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics in project commercetools-sync-java by commercetools.
the class CustomObjectSyncIT method sync_withConcurrentModificationExceptionAndUnexpectedDelete_shouldFailToReFetchAndUpdate.
@Test
void sync_withConcurrentModificationExceptionAndUnexpectedDelete_shouldFailToReFetchAndUpdate() {
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final CustomObjectUpsertCommand customObjectUpsertCommand = any(CustomObjectUpsertCommand.class);
when(spyClient.execute(customObjectUpsertCommand)).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new ConcurrentModificationException())).thenCallRealMethod();
final CustomObjectQuery customObjectQuery = any(CustomObjectQuery.class);
when(spyClient.execute(customObjectQuery)).thenCallRealMethod().thenReturn(CompletableFuture.completedFuture(PagedQueryResult.empty()));
final ObjectNode newCustomObjectValue = JsonNodeFactory.instance.objectNode().put("name", "value2");
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 CustomObjectDraft<JsonNode> customObjectDraft = CustomObjectDraft.ofUnversionedUpsert("container1", "key1", newCustomObjectValue);
final CustomObjectSyncStatistics customObjectSyncStatistics = customObjectSync.sync(Collections.singletonList(customObjectDraft)).toCompletableFuture().join();
assertThat(customObjectSyncStatistics).hasValues(1, 0, 0, 1);
Assertions.assertThat(errorCallBackMessages).hasSize(1);
Assertions.assertThat(errorCallBackExceptions).hasSize(1);
Assertions.assertThat(errorCallBackMessages.get(0)).contains(format("Failed to update custom object with key: '%s'. Reason: Not found when attempting to fetch while" + " retrying after concurrency modification.", CustomObjectCompositeIdentifier.of(customObjectDraft)));
}
use of com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics 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.customobjects.helpers.CustomObjectSyncStatistics 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);
}
use of com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics in project commercetools-sync-java by commercetools.
the class CustomObjectSyncTest method sync_WithSameIdentifiersAndDifferentValues_ShouldUpdateSuccessfully.
@Test
void sync_WithSameIdentifiersAndDifferentValues_ShouldUpdateSuccessfully() {
final CustomObjectSyncOptions spyCustomObjectSyncOptions = initCustomObjectSyncOptions(emptyList(), emptyList());
final CustomObject<JsonNode> existingCustomObject = mock(CustomObject.class);
when(existingCustomObject.getContainer()).thenReturn("someContainer");
when(existingCustomObject.getKey()).thenReturn("someKey");
when(existingCustomObject.getValue()).thenReturn(JsonNodeFactory.instance.numberNode(2020));
final CustomObject<JsonNode> updatedCustomObject = mock(CustomObject.class);
when(updatedCustomObject.getContainer()).thenReturn("someContainer");
when(updatedCustomObject.getKey()).thenReturn("someKey");
when(updatedCustomObject.getValue()).thenReturn(newCustomObjectDraft.getValue());
final Set<CustomObject<JsonNode>> existingCustomObjectSet = new HashSet<CustomObject<JsonNode>>();
existingCustomObjectSet.add(existingCustomObject);
final CustomObjectService customObjectService = mock(CustomObjectService.class);
when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(existingCustomObjectSet));
when(customObjectService.upsertCustomObject(any())).thenReturn(completedFuture(Optional.of(updatedCustomObject)));
// test
CustomObjectSyncStatistics syncStatistics = new CustomObjectSync(spyCustomObjectSyncOptions, customObjectService).sync(singletonList(newCustomObjectDraft)).toCompletableFuture().join();
// assertion
assertAll(() -> assertThat(syncStatistics.getProcessed().get()).isEqualTo(1), () -> assertThat(syncStatistics.getUpdated().get()).isEqualTo(1), () -> assertThat(syncStatistics.getCreated().get()).isEqualTo(0), () -> assertThat(syncStatistics.getFailed().get()).isEqualTo(0));
}
use of com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics in project commercetools-sync-java by commercetools.
the class CustomObjectSyncTest method sync_WithSameKeysAndDifferentContainers_ShouldCreateSuccessfully.
@Test
void sync_WithSameKeysAndDifferentContainers_ShouldCreateSuccessfully() {
final List<String> errorMessages = new ArrayList<>();
final List<Throwable> exceptions = new ArrayList<>();
final CustomObjectSyncOptions spyCustomObjectSyncOptions = initCustomObjectSyncOptions(errorMessages, exceptions);
final CustomObject<JsonNode> existingCustomObject = mock(CustomObject.class);
when(existingCustomObject.getContainer()).thenReturn("otherContainer");
when(existingCustomObject.getKey()).thenReturn("someKey");
when(existingCustomObject.getValue()).thenReturn(JsonNodeFactory.instance.numberNode(2020));
final CustomObject<JsonNode> updatedCustomObject = mock(CustomObject.class);
when(updatedCustomObject.getContainer()).thenReturn("someContainer");
when(updatedCustomObject.getKey()).thenReturn("someKey");
when(updatedCustomObject.getValue()).thenReturn(newCustomObjectDraft.getValue());
final Set<CustomObject<JsonNode>> existingCustomObjectSet = new HashSet<CustomObject<JsonNode>>();
existingCustomObjectSet.add(existingCustomObject);
final CustomObjectService customObjectService = mock(CustomObjectService.class);
when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(existingCustomObjectSet));
when(customObjectService.upsertCustomObject(any())).thenReturn(completedFuture(Optional.of(updatedCustomObject)));
// test
CustomObjectSyncStatistics syncStatistics = new CustomObjectSync(spyCustomObjectSyncOptions, customObjectService).sync(singletonList(newCustomObjectDraft)).toCompletableFuture().join();
// assertion
assertThat(exceptions).hasSize(0);
assertThat(errorMessages).hasSize(0);
assertAll(() -> assertThat(syncStatistics.getProcessed().get()).isEqualTo(1), () -> assertThat(syncStatistics.getCreated().get()).isEqualTo(1), () -> assertThat(syncStatistics.getUpdated().get()).isEqualTo(0), () -> assertThat(syncStatistics.getFailed().get()).isEqualTo(0));
verify(spyCustomObjectSyncOptions).applyBeforeCreateCallback(newCustomObjectDraft);
}
Aggregations