Search in sources :

Example 1 with CustomObjectSyncOptions

use of com.commercetools.sync.customobjects.CustomObjectSyncOptions 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 CustomObjectSyncOptions

use of com.commercetools.sync.customobjects.CustomObjectSyncOptions 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 CustomObjectSyncOptions

use of com.commercetools.sync.customobjects.CustomObjectSyncOptions in project commercetools-sync-java by commercetools.

the class BaseServiceImplTest method cacheKeysToIds_WithEmptyCache_ShouldMakeRequestAndReturnCacheEntries.

@Test
void cacheKeysToIds_WithEmptyCache_ShouldMakeRequestAndReturnCacheEntries() {
    // preparation
    final CustomObjectSyncOptions customObjectSyncOptions = CustomObjectSyncOptionsBuilder.of(client).build();
    final CustomObjectServiceImpl serviceImpl = new CustomObjectServiceImpl(customObjectSyncOptions);
    final PagedQueryResult pagedQueryResult = mock(PagedQueryResult.class);
    final CustomObject customObject = mock(CustomObject.class);
    final String customObjectId = "customObjectId";
    final String customObjectContainer = "customObjectContainer";
    final String customObjectKey = "customObjectKey";
    when(customObject.getId()).thenReturn(customObjectId);
    when(customObject.getKey()).thenReturn(customObjectKey);
    when(customObject.getContainer()).thenReturn(customObjectContainer);
    when(pagedQueryResult.getResults()).thenReturn(singletonList(customObject));
    when(client.execute(any())).thenReturn(completedFuture(pagedQueryResult));
    final Map<String, String> result = serviceImpl.cacheKeysToIds(singleton(CustomObjectCompositeIdentifier.of(customObjectKey, customObjectContainer))).toCompletableFuture().join();
    assertAll(() -> assertThat(result).hasSize(1), () -> assertThat(result.get(CustomObjectCompositeIdentifier.of(customObjectKey, customObjectContainer).toString())).isEqualTo(customObjectId));
    verify(client).execute(any(CustomObjectQuery.class));
}
Also used : CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) CustomObject(io.sphere.sdk.customobjects.CustomObject) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with CustomObjectSyncOptions

use of com.commercetools.sync.customobjects.CustomObjectSyncOptions in project commercetools-sync-java by commercetools.

the class BaseServiceImplTest method cacheKeysToIds_With500CustomObjectIdentifiers_ShouldChunkAndMakeRequestAndReturnCacheEntries.

@Test
void cacheKeysToIds_With500CustomObjectIdentifiers_ShouldChunkAndMakeRequestAndReturnCacheEntries() {
    // preparation
    Set<CustomObjectCompositeIdentifier> randomIdentifiers = new HashSet<>();
    IntStream.range(0, 500).forEach(i -> randomIdentifiers.add(CustomObjectCompositeIdentifier.of("customObjectId" + i, "customObjectContainer" + i)));
    final CustomObjectSyncOptions customObjectSyncOptions = CustomObjectSyncOptionsBuilder.of(client).build();
    final CustomObjectServiceImpl serviceImpl = new CustomObjectServiceImpl(customObjectSyncOptions);
    final PagedQueryResult pagedQueryResult = mock(PagedQueryResult.class);
    final CustomObject customObject = mock(CustomObject.class);
    final String customObjectId = randomIdentifiers.stream().findFirst().get().getKey();
    final String customObjectContainer = randomIdentifiers.stream().findFirst().get().getContainer();
    final String customObjectKey = "customObjectKey";
    when(customObject.getId()).thenReturn(customObjectId);
    when(customObject.getKey()).thenReturn(customObjectKey);
    when(customObject.getContainer()).thenReturn(customObjectContainer);
    when(pagedQueryResult.getResults()).thenReturn(singletonList(customObject));
    when(client.execute(any())).thenReturn(completedFuture(pagedQueryResult));
    // test
    serviceImpl.cacheKeysToIds(randomIdentifiers).toCompletableFuture().join();
    // assertion
    verify(client, times(2)).execute(any(CustomObjectQuery.class));
}
Also used : CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) CustomObject(io.sphere.sdk.customobjects.CustomObject) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) HashSet(java.util.HashSet) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with CustomObjectSyncOptions

use of com.commercetools.sync.customobjects.CustomObjectSyncOptions in project commercetools-sync-java by commercetools.

the class CustomObjectServiceImplTest method setup.

@BeforeEach
void setup() {
    customObjectId = RandomStringUtils.random(15, true, true);
    customObjectContainer = RandomStringUtils.random(15, true, true);
    customObjectKey = RandomStringUtils.random(15, true, true);
    CustomObjectSyncOptions customObjectSyncOptions = CustomObjectSyncOptionsBuilder.of(client).build();
    service = new CustomObjectServiceImpl(customObjectSyncOptions);
}
Also used : CustomObjectSyncOptions(com.commercetools.sync.customobjects.CustomObjectSyncOptions) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

CustomObjectSyncOptions (com.commercetools.sync.customobjects.CustomObjectSyncOptions)15 Test (org.junit.jupiter.api.Test)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 CustomObjectQuery (io.sphere.sdk.customobjects.queries.CustomObjectQuery)10 CustomObjectCompositeIdentifier (com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier)9 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 CustomObjectSync (com.commercetools.sync.customobjects.CustomObjectSync)8 CustomObjectSyncOptionsBuilder (com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder)8 CustomObjectSyncStatistics (com.commercetools.sync.customobjects.helpers.CustomObjectSyncStatistics)8 SphereClient (io.sphere.sdk.client.SphereClient)8 CustomObjectDraft (io.sphere.sdk.customobjects.CustomObjectDraft)8 List (java.util.List)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 CustomObjectITUtils.createCustomObject (com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.createCustomObject)7 CustomObjectITUtils.deleteCustomObject (com.commercetools.sync.integration.commons.utils.CustomObjectITUtils.deleteCustomObject)7 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)7 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)7 BadGatewayException (io.sphere.sdk.client.BadGatewayException)7 CompletableFutureUtils (io.sphere.sdk.utils.CompletableFutureUtils)7 ArrayList (java.util.ArrayList)7