Search in sources :

Example 1 with CustomObjectServiceImpl

use of com.commercetools.sync.services.impl.CustomObjectServiceImpl 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 CustomObjectServiceImpl

use of com.commercetools.sync.services.impl.CustomObjectServiceImpl 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 CustomObjectServiceImpl

use of com.commercetools.sync.services.impl.CustomObjectServiceImpl 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)

Aggregations

CustomObjectSyncOptions (com.commercetools.sync.customobjects.CustomObjectSyncOptions)3 CustomObjectSyncOptionsBuilder (com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder)3 CustomObjectCompositeIdentifier (com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier)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 CustomObjectService (com.commercetools.sync.services.CustomObjectService)3 CustomObjectServiceImpl (com.commercetools.sync.services.impl.CustomObjectServiceImpl)3 CompletionStageUtil.executeBlocking (com.commercetools.tests.utils.CompletionStageUtil.executeBlocking)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)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 CustomObject (io.sphere.sdk.customobjects.CustomObject)3 CustomObjectDraft (io.sphere.sdk.customobjects.CustomObjectDraft)3 CustomObjectQuery (io.sphere.sdk.customobjects.queries.CustomObjectQuery)3 CompletableFutureUtils (io.sphere.sdk.utils.CompletableFutureUtils)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3