Search in sources :

Example 1 with CustomObjectService

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

the class CustomObjectServiceImplIT method setup.

/**
 * Deletes customObjects from the target CTP project, then it populates the project with test
 * data.
 */
@BeforeEach
void setup() {
    errorCallBackMessages = new ArrayList<>();
    errorCallBackExceptions = new ArrayList<>();
    deleteCustomObject(CTP_TARGET_CLIENT, OLD_CUSTOM_OBJECT_KEY, OLD_CUSTOM_OBJECT_CONTAINER);
    deleteCustomObject(CTP_TARGET_CLIENT, NEW_CUSTOM_OBJECT_KEY, NEW_CUSTOM_OBJECT_CONTAINER);
    createCustomObject(CTP_TARGET_CLIENT, OLD_CUSTOM_OBJECT_KEY, OLD_CUSTOM_OBJECT_CONTAINER, OLD_CUSTOM_OBJECT_VALUE);
    final CustomObjectSyncOptions customObjectSyncOptions = CustomObjectSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).build();
    customObjectService = new CustomObjectServiceImpl(customObjectSyncOptions);
}
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) CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) CustomObjectServiceImpl(com.commercetools.sync.services.impl.CustomObjectServiceImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with CustomObjectService

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

the class CustomObjectServiceImplIT method upsertCustomObject_WithValidCustomObject_ShouldCreateCustomObjectAndCacheId.

@Test
void upsertCustomObject_WithValidCustomObject_ShouldCreateCustomObjectAndCacheId() {
    final CustomObjectDraft<JsonNode> newCustomObjectDraft = CustomObjectDraft.ofUnversionedUpsert(NEW_CUSTOM_OBJECT_CONTAINER, NEW_CUSTOM_OBJECT_KEY, NEW_CUSTOM_OBJECT_VALUE);
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    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 Optional<CustomObject<JsonNode>> createdCustomObject = spyCustomObjectService.upsertCustomObject(newCustomObjectDraft).toCompletableFuture().join();
    final Optional<CustomObject<JsonNode>> queriedOptional = CTP_TARGET_CLIENT.execute(CustomObjectQuery.ofJsonNode().withPredicates(customObjectQueryModel -> customObjectQueryModel.container().is(NEW_CUSTOM_OBJECT_CONTAINER).and(customObjectQueryModel.key().is(NEW_CUSTOM_OBJECT_KEY)))).toCompletableFuture().join().head();
    assertThat(queriedOptional).hasValueSatisfying(queried -> assertThat(createdCustomObject).hasValueSatisfying(created -> {
        assertThat(created.getKey()).isEqualTo(queried.getKey());
        assertThat(created.getContainer()).isEqualTo(queried.getContainer());
        assertThat(created.getId()).isEqualTo(queried.getId());
        assertThat(created.getValue()).isEqualTo(queried.getValue());
    }));
    // Assert that the created customObject is cached
    final Optional<String> customObjectId = spyCustomObjectService.fetchCachedCustomObjectId(CustomObjectCompositeIdentifier.of(NEW_CUSTOM_OBJECT_KEY, NEW_CUSTOM_OBJECT_CONTAINER)).toCompletableFuture().join();
    assertThat(customObjectId).isPresent();
    verify(spyClient, times(0)).execute(any(CustomObjectQuery.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) CustomObjectITUtils.deleteCustomObject(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteCustomObject) CustomObject(io.sphere.sdk.customobjects.CustomObject) CustomObjectITUtils.createCustomObject(com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.createCustomObject) SphereClient(io.sphere.sdk.client.SphereClient) CustomObjectServiceImpl(com.commercetools.sync.services.impl.CustomObjectServiceImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) Test(org.junit.jupiter.api.Test)

Example 3 with CustomObjectService

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

the class ProductSyncMockUtils method getMockCustomObjectService.

/**
 * Creates a mock {@link CustomObjectService} that returns a completed {@link CompletableFuture}
 * containing an {@link Optional} containing the id of the supplied value whenever the following
 * method is called on the service:
 *
 * <ul>
 *   <li>{@link CustomObjectService#fetchCachedCustomObjectId}
 * </ul>
 *
 * @return the created mock of the {@link CustomObjectService}.
 */
public static CustomObjectService getMockCustomObjectService(@Nonnull final String id) {
    final CustomObjectService customObjectService = mock(CustomObjectService.class);
    when(customObjectService.fetchCachedCustomObjectId(any())).thenReturn(CompletableFuture.completedFuture(Optional.of(id)));
    return customObjectService;
}
Also used : CustomObjectService(com.commercetools.sync.services.CustomObjectService)

Example 4 with CustomObjectService

use of com.commercetools.sync.services.CustomObjectService 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));
}
Also used : CustomObjectService(com.commercetools.sync.services.CustomObjectService) ArrayList(java.util.ArrayList) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) Test(org.junit.jupiter.api.Test)

Example 5 with CustomObjectService

use of com.commercetools.sync.services.CustomObjectService 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);
}
Also used : CustomObjectService(com.commercetools.sync.services.CustomObjectService) ArrayList(java.util.ArrayList) SphereException(io.sphere.sdk.models.SphereException) CustomObjectSyncStatistics(com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics) 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