Search in sources :

Example 6 with CustomObjectService

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());
}
Also used : CustomObjectService(com.commercetools.sync.services.CustomObjectService) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test)

Example 7 with CustomObjectService

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));
}
Also used : CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObject(io.sphere.sdk.customobjects.CustomObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 8 with CustomObjectService

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);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CustomObjectITUtils.deleteCustomObject(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteCustomObject) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) CustomObject(io.sphere.sdk.customobjects.CustomObject) Mockito.spy(org.mockito.Mockito.spy) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) SphereClient(io.sphere.sdk.client.SphereClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CustomObjectDraft(io.sphere.sdk.customobjects.CustomObjectDraft) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) CustomObjectSyncOptionsBuilder(com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) CustomObjectITUtils.createCustomObject(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.createCustomObject) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Optional(java.util.Optional) CustomObjectServiceImpl(com.commercetools.sync.services.impl.CustomObjectServiceImpl) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) SphereClient(io.sphere.sdk.client.SphereClient) CustomObjectServiceImpl(com.commercetools.sync.services.impl.CustomObjectServiceImpl) BadGatewayException(io.sphere.sdk.client.BadGatewayException) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 9 with CustomObjectService

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);
}
Also used : CustomObject(io.sphere.sdk.customobjects.CustomObject) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 10 with CustomObjectService

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());
}
Also used : ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) CustomObject(io.sphere.sdk.customobjects.CustomObject) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) SphereException(io.sphere.sdk.models.SphereException) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

CustomObjectService (com.commercetools.sync.services.CustomObjectService)15 Test (org.junit.jupiter.api.Test)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 CustomObject (io.sphere.sdk.customobjects.CustomObject)10 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)10 CustomObjectSyncStatistics (com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics)9 CustomObjectCompositeIdentifier (com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier)5 CustomObjectSyncOptions (com.commercetools.sync.customobjects.CustomObjectSyncOptions)3 CustomObjectSyncOptionsBuilder (com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder)3 CustomObjectITUtils.createCustomObject (com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.createCustomObject)3 CustomObjectITUtils.deleteCustomObject (com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteCustomObject)3 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)3 CustomObjectServiceImpl (com.commercetools.sync.services.impl.CustomObjectServiceImpl)3 CompletionStageUtil.executeBlocking (com.commercetools.tests.utils.CompletionStageUtil.executeBlocking)3 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 BadGatewayException (io.sphere.sdk.client.BadGatewayException)3 SphereClient (io.sphere.sdk.client.SphereClient)3 CustomObjectDraft (io.sphere.sdk.customobjects.CustomObjectDraft)3