use of com.commercetools.sync.services.CustomObjectService in project commercetools-sync-java by commercetools.
the class CustomObjectSyncTest method sync_WithOnlyDraftsToUpdate_ShouldCallBeforeCreateCallback_ShouldNotCallBeforeUpdateCallback.
@Test
void sync_WithOnlyDraftsToUpdate_ShouldCallBeforeCreateCallback_ShouldNotCallBeforeUpdateCallback() {
final CustomObjectSyncOptions spyCustomObjectSyncOptions = initCustomObjectSyncOptions(emptyList(), emptyList());
final CustomObject<JsonNode> mockedExistingCustomObject = mock(CustomObject.class);
when(mockedExistingCustomObject.getKey()).thenReturn(newCustomObjectDraft.getKey());
when(mockedExistingCustomObject.getContainer()).thenReturn("differentContainer");
final CustomObjectService customObjectService = mock(CustomObjectService.class);
when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(singleton(mockedExistingCustomObject)));
when(customObjectService.upsertCustomObject(any())).thenReturn(completedFuture(Optional.of(mockedExistingCustomObject)));
// test
new CustomObjectSync(spyCustomObjectSyncOptions, customObjectService).sync(singletonList(newCustomObjectDraft)).toCompletableFuture().join();
// assertion
verify(spyCustomObjectSyncOptions).applyBeforeCreateCallback(newCustomObjectDraft);
verify(spyCustomObjectSyncOptions, never()).applyBeforeUpdateCallback(any(), any(), any());
}
use of com.commercetools.sync.services.CustomObjectService 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.services.CustomObjectService in project commercetools-sync-java by commercetools.
the class CustomObjectServiceImplIT method fetchMatchingCustomObjectsByCompositeIdentifiers_WithBadGateWayExceptionAlways_ShouldFail.
@Test
void fetchMatchingCustomObjectsByCompositeIdentifiers_WithBadGateWayExceptionAlways_ShouldFail() {
// Mock sphere client to return BadGatewayException on any request.
final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any(CustomObjectQuery.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException())).thenCallRealMethod();
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 Set<CustomObjectCompositeIdentifier> customObjectCompositeIdentifiers = new HashSet<>();
customObjectCompositeIdentifiers.add(CustomObjectCompositeIdentifier.of(OLD_CUSTOM_OBJECT_KEY, OLD_CUSTOM_OBJECT_CONTAINER));
// test and assert
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(spyCustomObjectService.fetchMatchingCustomObjects(customObjectCompositeIdentifiers)).failsWithin(10, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
}
use of com.commercetools.sync.services.CustomObjectService 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);
}
use of com.commercetools.sync.services.CustomObjectService 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());
}
Aggregations