use of com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics in project commercetools-sync-java by commercetools.
the class CustomObjectSyncTest method sync_UpdateWithConcurrentModificationExceptionAndRetryWithFetchException_ShouldIncrementFailed.
@Test
void sync_UpdateWithConcurrentModificationExceptionAndRetryWithFetchException_ShouldIncrementFailed() {
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("someContainer");
when(existingCustomObject.getKey()).thenReturn("someKey");
when(existingCustomObject.getValue()).thenReturn(JsonNodeFactory.instance.numberNode(2020));
final Set<CustomObject<JsonNode>> existingCustomObjectSet = new HashSet<CustomObject<JsonNode>>();
existingCustomObjectSet.add(existingCustomObject);
final CustomObject<JsonNode> updatedCustomObject = mock(CustomObject.class);
when(updatedCustomObject.getContainer()).thenReturn("someContainer");
when(updatedCustomObject.getKey()).thenReturn("someKey");
when(updatedCustomObject.getValue()).thenReturn(newCustomObjectDraft.getValue());
final CustomObjectService customObjectService = mock(CustomObjectService.class);
when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(existingCustomObjectSet));
when(customObjectService.upsertCustomObject(any())).thenReturn(supplyAsync(() -> {
throw new ConcurrentModificationException();
}));
when(customObjectService.fetchCustomObject(any(CustomObjectCompositeIdentifier.class))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
// test
CustomObjectSyncStatistics syncStatistics = new CustomObjectSync(spyCustomObjectSyncOptions, customObjectService).sync(singletonList(newCustomObjectDraft)).toCompletableFuture().join();
// assertion
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));
assertThat(exceptions).hasSize(1);
assertThat(errorMessages).hasSize(1).singleElement().isEqualTo(format("Failed to update custom object with key: '%s'. Reason: %s", CustomObjectCompositeIdentifier.of(newCustomObjectDraft).toString(), "Failed to fetch from CTP while retrying after concurrency modification."));
verify(customObjectService).fetchCustomObject(any(CustomObjectCompositeIdentifier.class));
verify(customObjectService).upsertCustomObject(any());
verify(customObjectService).fetchMatchingCustomObjects(any());
}
use of com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics in project commercetools-sync-java by commercetools.
the class CustomObjectSyncTest method sync_WithDifferentKeysAndSameContainers_ShouldCreateSuccessfully.
@Test
void sync_WithDifferentKeysAndSameContainers_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("someContainer");
when(existingCustomObject.getKey()).thenReturn("otherKey");
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);
}
use of com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics in project commercetools-sync-java by commercetools.
the class CustomObjectSyncTest method sync_WithSameIdentifiersAndIdenticalValues_ShouldProcessedAndNotUpdated.
@Test
void sync_WithSameIdentifiersAndIdenticalValues_ShouldProcessedAndNotUpdated() {
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(newCustomObjectDraft.getValue());
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(0), () -> 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_UpdateWithSphereExceptionAndRetryWithFetchException_ShouldIncrementFailed.
@Test
void sync_UpdateWithSphereExceptionAndRetryWithFetchException_ShouldIncrementFailed() {
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("someContainer");
when(existingCustomObject.getKey()).thenReturn("someKey");
when(existingCustomObject.getValue()).thenReturn(JsonNodeFactory.instance.numberNode(2020));
final Set<CustomObject<JsonNode>> existingCustomObjectSet = new HashSet<CustomObject<JsonNode>>();
existingCustomObjectSet.add(existingCustomObject);
final CustomObject<JsonNode> updatedCustomObject = mock(CustomObject.class);
when(updatedCustomObject.getContainer()).thenReturn("someContainer");
when(updatedCustomObject.getKey()).thenReturn("someKey");
when(updatedCustomObject.getValue()).thenReturn(newCustomObjectDraft.getValue());
final CustomObjectService customObjectService = mock(CustomObjectService.class);
when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(existingCustomObjectSet));
when(customObjectService.upsertCustomObject(any())).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
when(customObjectService.fetchCustomObject(any(CustomObjectCompositeIdentifier.class))).thenReturn(supplyAsync(() -> {
throw new SphereException();
}));
// test
CustomObjectSyncStatistics syncStatistics = new CustomObjectSync(spyCustomObjectSyncOptions, customObjectService).sync(singletonList(newCustomObjectDraft)).toCompletableFuture().join();
// assertion
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));
assertThat(exceptions).hasSize(1);
assertThat(errorMessages).hasSize(1).singleElement().isEqualTo(format("Failed to update custom object with key: '%s'. Reason: %s", CustomObjectCompositeIdentifier.of(newCustomObjectDraft).toString(), exceptions.get(0).getMessage()));
verify(customObjectService).upsertCustomObject(any());
verify(customObjectService).fetchMatchingCustomObjects(any());
}
use of com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics in project commercetools-sync-java by commercetools.
the class CustomObjectSyncTest method sync_WithDifferentIdentifiers_ShouldCreateSuccessfully.
@Test
void sync_WithDifferentIdentifiers_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("otherKey");
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