Search in sources :

Example 11 with CustomObjectService

use of com.commercetools.sync.services.CustomObjectService 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);
}
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 12 with CustomObjectService

use of com.commercetools.sync.services.CustomObjectService 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));
}
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 13 with CustomObjectService

use of com.commercetools.sync.services.CustomObjectService in project commercetools-sync-java by commercetools.

the class CustomObjectSyncTest method sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback_ShouldNotCallBeforeUpdateCallback.

@Test
void sync_WithOnlyDraftsToCreate_ShouldCallBeforeCreateCallback_ShouldNotCallBeforeUpdateCallback() {
    final CustomObjectSyncOptions spyCustomObjectSyncOptions = initCustomObjectSyncOptions(emptyList(), emptyList());
    final CustomObjectService customObjectService = mock(CustomObjectService.class);
    when(customObjectService.fetchMatchingCustomObjects(anySet())).thenReturn(completedFuture(emptySet()));
    when(customObjectService.upsertCustomObject(any())).thenReturn(completedFuture(Optional.empty()));
    // 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) Test(org.junit.jupiter.api.Test)

Example 14 with CustomObjectService

use of com.commercetools.sync.services.CustomObjectService 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());
}
Also used : 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)

Example 15 with CustomObjectService

use of com.commercetools.sync.services.CustomObjectService 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);
}
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)

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