Search in sources :

Example 21 with ReferenceIdToKeyCache

use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.

the class CustomerTransformServiceImplTest method transform_CustomerReferences_ShouldResolveReferencesUsingCacheAndMapToCustomerDraft.

@Test
void transform_CustomerReferences_ShouldResolveReferencesUsingCacheAndMapToCustomerDraft() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
    final CustomerTransformService CustomerTransformService = new CustomerTransformServiceImpl(sourceClient, referenceIdToKeyCache);
    final String customerKey = "customerKey";
    final String customTypeId = UUID.randomUUID().toString();
    final String customTypeKey = "customTypeKey";
    final String customerGroupId = UUID.randomUUID().toString();
    final String customerGroupKey = "customerGroupKey";
    final List<Customer> mockCustomersPage = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final Customer mockCustomer = mock(Customer.class);
        final CustomFields mockCustomFields = mock(CustomFields.class);
        final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
        when(mockCustomFields.getType()).thenReturn(typeReference);
        when(mockCustomer.getCustom()).thenReturn(mockCustomFields);
        when(mockCustomer.getKey()).thenReturn(customerKey);
        final Reference<CustomerGroup> customerGroupReference = Reference.ofResourceTypeIdAndId("resourceCustomerGroupId", customerGroupId);
        when(mockCustomer.getCustomerGroup()).thenReturn(customerGroupReference);
        mockCustomersPage.add(mockCustomer);
    }
    final String jsonStringCustomTypes = "{\"results\":[{\"id\":\"" + customTypeId + "\"," + "\"key\":\"" + customTypeKey + "\"}]}";
    final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
    final String jsonStringCustomerGroups = "{\"results\":[{\"id\":\"" + customerGroupId + "\"," + "\"key\":\"" + customerGroupKey + "\"}]}";
    final ResourceKeyIdGraphQlResult customerGroupsResult = SphereJsonUtils.readObject(jsonStringCustomerGroups, ResourceKeyIdGraphQlResult.class);
    when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(customTypesResult)).thenReturn(CompletableFuture.completedFuture(customerGroupsResult));
    // test
    final List<CustomerDraft> customersResolved = CustomerTransformService.toCustomerDrafts(mockCustomersPage).toCompletableFuture().join();
    // assertions
    final Optional<CustomerDraft> customerKey1 = customersResolved.stream().filter(customerDraft -> customerKey.equals(customerDraft.getKey())).findFirst();
    assertThat(customerKey1).hasValueSatisfying(customerDraft -> {
        assertThat(customerDraft.getCustom().getType().getKey()).isEqualTo(customTypeKey);
        assertThat(customerDraft.getCustomerGroup().getKey()).isEqualTo(customerGroupKey);
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) CustomFields(io.sphere.sdk.types.CustomFields) CustomerGroup(io.sphere.sdk.customergroups.CustomerGroup) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) Reference(io.sphere.sdk.models.Reference) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Customer(io.sphere.sdk.customers.Customer) CustomerTransformService(com.commercetools.sync.customers.service.CustomerTransformService) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) List(java.util.List) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) SphereClient(io.sphere.sdk.client.SphereClient) Optional(java.util.Optional) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) Mockito.mock(org.mockito.Mockito.mock) Type(io.sphere.sdk.types.Type) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) CustomerTransformService(com.commercetools.sync.customers.service.CustomerTransformService) Customer(io.sphere.sdk.customers.Customer) CustomerGroup(io.sphere.sdk.customergroups.CustomerGroup) ArrayList(java.util.ArrayList) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) CustomFields(io.sphere.sdk.types.CustomFields) Type(io.sphere.sdk.types.Type) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) Test(org.junit.jupiter.api.Test)

Example 22 with ReferenceIdToKeyCache

use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-project-sync by commercetools.

the class CategorySyncerTest method transform_ShouldReplaceCategoryReferenceIdsWithKeys.

@Test
void transform_ShouldReplaceCategoryReferenceIdsWithKeys() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    final CategorySyncer categorySyncer = CategorySyncer.of(sourceClient, mock(SphereClient.class), getMockedClock());
    final List<Category> categoryPage = asList(readObjectFromResource("category-key-1.json", Category.class), readObjectFromResource("category-key-2.json", Category.class));
    final List<String> referenceIds = categoryPage.stream().filter(category -> category.getCustom() != null).map(category -> category.getCustom().getType().getId()).collect(Collectors.toList());
    final String jsonStringCustomTypes = "{\"results\":[{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0c3\"," + "\"key\":\"cat1\"} ]}";
    final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
    when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(customTypesResult));
    // test
    final CompletionStage<List<CategoryDraft>> draftsFromPageStage = categorySyncer.transform(categoryPage);
    // assertions
    final List<CategoryDraft> expectedResult = toCategoryDrafts(sourceClient, referenceIdToKeyCache, categoryPage).join();
    final List<String> referenceKeys = expectedResult.stream().filter(category -> category.getCustom() != null).map(category -> category.getCustom().getType().getId()).collect(Collectors.toList());
    assertThat(referenceKeys).doesNotContainSequence(referenceIds);
    assertThat(draftsFromPageStage).isCompletedWithValue(expectedResult);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) TestLoggerFactory(uk.org.lidalia.slf4jtest.TestLoggerFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) CompletableFuture(java.util.concurrent.CompletableFuture) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) Collections.singleton(java.util.Collections.singleton) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) Arrays.asList(java.util.Arrays.asList) TestLogger(uk.org.lidalia.slf4jtest.TestLogger) SphereClient(io.sphere.sdk.client.SphereClient) CategorySync(com.commercetools.sync.categories.CategorySync) LoggingEvent(uk.org.lidalia.slf4jtest.LoggingEvent) TestUtils.getMockedClock(com.commercetools.project.sync.util.TestUtils.getMockedClock) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) SphereApiConfig(io.sphere.sdk.client.SphereApiConfig) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Category(io.sphere.sdk.categories.Category) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) CategoryTransformUtils.toCategoryDrafts(com.commercetools.sync.categories.utils.CategoryTransformUtils.toCategoryDrafts) Clock(java.time.Clock) Optional(java.util.Optional) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Category(io.sphere.sdk.categories.Category) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 23 with ReferenceIdToKeyCache

use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-project-sync by commercetools.

the class InventoryEntrySyncerTest method transform_ShouldReplaceInventoryEntryReferenceIdsWithKeys.

@Test
void transform_ShouldReplaceInventoryEntryReferenceIdsWithKeys() {
    // preparation
    final SphereClient sourceClient = mock(SphereClient.class);
    final InventoryEntrySyncer inventoryEntrySyncer = InventoryEntrySyncer.of(sourceClient, mock(SphereClient.class), getMockedClock());
    final List<InventoryEntry> inventoryPage = asList(readObjectFromResource("inventory-sku-1.json", InventoryEntry.class), readObjectFromResource("inventory-sku-2.json", InventoryEntry.class));
    final List<String> referenceIds = inventoryPage.stream().filter(inventoryEntry -> inventoryEntry.getSupplyChannel() != null).filter(inventoryEntry -> inventoryEntry.getCustom() != null).flatMap(inventoryEntry -> Stream.of(inventoryEntry.getCustom().getType().getId(), inventoryEntry.getSupplyChannel().getId())).collect(Collectors.toList());
    final String jsonStringCustomTypes = "{\"results\":[{\"id\":\"02e915e7-7763-48d1-83bd-d4e940a1a368\"," + "\"key\":\"test-custom-type-key\"} ]}";
    final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
    final String jsonStringSupplyChannels = "{\"results\":[{\"id\":\"5c0516b5-f506-4b6a-b4d1-c06ca29ab7e1\"," + "\"key\":\"test-channel-key\"} ]}";
    final ResourceKeyIdGraphQlResult supplyChannelsResult = SphereJsonUtils.readObject(jsonStringSupplyChannels, ResourceKeyIdGraphQlResult.class);
    when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(customTypesResult)).thenReturn(CompletableFuture.completedFuture(supplyChannelsResult));
    // test
    final CompletionStage<List<InventoryEntryDraft>> draftsFromPageStage = inventoryEntrySyncer.transform(inventoryPage);
    // assertions
    final List<InventoryEntryDraft> expectedResult = toInventoryEntryDrafts(sourceClient, referenceIdToKeyCache, inventoryPage).join();
    final List<String> referenceKeys = expectedResult.stream().filter(inventoryEntry -> inventoryEntry.getSupplyChannel() != null).filter(inventoryEntry -> inventoryEntry.getCustom() != null).flatMap(inventoryEntry -> Stream.of(inventoryEntry.getCustom().getType().getId(), inventoryEntry.getSupplyChannel().getId())).collect(Collectors.toList());
    assertThat(referenceKeys).doesNotContainAnyElementsOf(referenceIds);
    assertThat(draftsFromPageStage).isCompletedWithValue(expectedResult);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) SphereJsonUtils.readObjectFromResource(io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) TestLoggerFactory(uk.org.lidalia.slf4jtest.TestLoggerFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.mockResourceIdsGraphQlRequest(com.commercetools.project.sync.util.TestUtils.mockResourceIdsGraphQlRequest) SphereJsonUtils(io.sphere.sdk.json.SphereJsonUtils) CompletableFuture(java.util.concurrent.CompletableFuture) InventoryTransformUtils.toInventoryEntryDrafts(com.commercetools.sync.inventories.utils.InventoryTransformUtils.toInventoryEntryDrafts) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) Arrays.asList(java.util.Arrays.asList) TestLogger(uk.org.lidalia.slf4jtest.TestLogger) SphereClient(io.sphere.sdk.client.SphereClient) InventoryEntryQuery(io.sphere.sdk.inventory.queries.InventoryEntryQuery) LoggingEvent(uk.org.lidalia.slf4jtest.LoggingEvent) TestUtils.getMockedClock(com.commercetools.project.sync.util.TestUtils.getMockedClock) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) SphereApiConfig(io.sphere.sdk.client.SphereApiConfig) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) Mockito.when(org.mockito.Mockito.when) InventorySync(com.commercetools.sync.inventories.InventorySync) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) Clock(java.time.Clock) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) InventoryEntryDraft(io.sphere.sdk.inventory.InventoryEntryDraft) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 24 with ReferenceIdToKeyCache

use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.

the class StateSyncIT method sync_WithExceptionWhenFetchingUnresolvedTransition_ShouldPrintErrorMessage.

@Test
void sync_WithExceptionWhenFetchingUnresolvedTransition_ShouldPrintErrorMessage() {
    final StateDraft stateCDraft = createStateDraft(keyC);
    final State stateC = createStateInSource(stateCDraft);
    final StateDraft stateBDraft = createStateDraft(keyB, stateC);
    final State stateB = createStateInSource(stateBDraft);
    final StateDraft stateADraft = createStateDraft(keyA, stateB, stateC);
    final State stateA = createStateInSource(stateADraft);
    final SphereClient spyClient = spy(CTP_TARGET_CLIENT);
    when(spyClient.execute(any(CustomObjectQuery.class))).thenReturn(exceptionallyCompletedFuture(new BadRequestException("a test exception"))).thenReturn(exceptionallyCompletedFuture(new ConcurrentModificationException())).thenCallRealMethod();
    final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(spyClient).batchSize(3).errorCallback((exception, oldResource, newResource, updateActions) -> {
        errorCallBackMessages.add(exception.getMessage());
        errorCallBackExceptions.add(exception.getCause());
    }).warningCallback((exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage())).build();
    final StateSync stateSync = new StateSync(stateSyncOptions);
    final List<StateDraft> stateDrafts = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, Arrays.asList(stateA, stateB, stateC)).join();
    // test
    final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
    assertThat(stateSyncStatistics).hasValues(3, 1, 0, 2, 1);
    Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
    Assertions.assertThat(errorCallBackMessages).isNotEmpty();
    Assertions.assertThat(errorCallBackMessages.get(0)).contains(format("Failed to fetch StateDrafts waiting to be resolved with keys"));
}
Also used : CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) Arrays(java.util.Arrays) Reference(io.sphere.sdk.models.Reference) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) StateTransformUtils(com.commercetools.sync.states.utils.StateTransformUtils) StateCreateCommand(io.sphere.sdk.states.commands.StateCreateCommand) Arrays.asList(java.util.Arrays.asList) StateITUtils.getStateByKey(com.commercetools.sync.integration.commons.utils.StateITUtils.getStateByKey) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) StateType(io.sphere.sdk.states.StateType) Assertions(org.assertj.core.api.Assertions) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ThreadLocalRandom.current(java.util.concurrent.ThreadLocalRandom.current) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) StateUpdateCommand(io.sphere.sdk.states.commands.StateUpdateCommand) Set(java.util.Set) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) State(io.sphere.sdk.states.State) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) StateITUtils.deleteStatesFromTargetAndSource(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStatesFromTargetAndSource) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) List(java.util.List) CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) CompletionStage(java.util.concurrent.CompletionStage) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) Optional(java.util.Optional) StateDraft(io.sphere.sdk.states.StateDraft) StateITUtils.deleteStates(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStates) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) StateQuery(io.sphere.sdk.states.queries.StateQuery) BadRequestException(io.sphere.sdk.client.BadRequestException) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) Mockito.spy(org.mockito.Mockito.spy) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) State.referenceOfId(io.sphere.sdk.states.State.referenceOfId) StateReferenceResolver(com.commercetools.sync.states.helpers.StateReferenceResolver) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) StateRole(io.sphere.sdk.states.StateRole) Nonnull(javax.annotation.Nonnull) StateDraftBuilder(io.sphere.sdk.states.StateDraftBuilder) StateQueryBuilder(io.sphere.sdk.states.queries.StateQueryBuilder) StateSyncOptionsBuilder(com.commercetools.sync.states.StateSyncOptionsBuilder) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) StateSync(com.commercetools.sync.states.StateSync) Mockito.when(org.mockito.Mockito.when) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) CtpQueryUtils(com.commercetools.sync.commons.utils.CtpQueryUtils) WaitingToBeResolvedTransitions(com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Collections(java.util.Collections) StateSync(com.commercetools.sync.states.StateSync) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) StateDraft(io.sphere.sdk.states.StateDraft) State(io.sphere.sdk.states.State) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) SphereClient(io.sphere.sdk.client.SphereClient) BadRequestException(io.sphere.sdk.client.BadRequestException) CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Test(org.junit.jupiter.api.Test)

Example 25 with ReferenceIdToKeyCache

use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.

the class StateSyncIT method sync_WithEmptyNewTransition_ShouldRemoveTransitions.

@Test
void sync_WithEmptyNewTransition_ShouldRemoveTransitions() {
    final StateDraft stateCDraft = createStateDraft(keyC);
    final State stateC = createStateInSource(stateCDraft);
    final StateDraft tagetStateCDraft = createStateDraft(keyC);
    final State targetStateC = createStateInTarget(tagetStateCDraft);
    final StateDraft stateBDraft = createStateDraft(keyB, stateC);
    final State stateB = createStateInSource(stateBDraft);
    final StateDraft tagetStateBDraft = createStateDraft(keyB, targetStateC);
    final State targetStateB = createStateInTarget(tagetStateBDraft);
    final StateDraft stateADraft = createStateDraft(keyA);
    final State stateA = createStateInSource(stateADraft);
    final StateDraft tagetStateADraft = createStateDraft(keyA, targetStateB, targetStateC);
    final State targetStateA = createStateInTarget(tagetStateADraft);
    Assertions.assertThat(targetStateB.getTransitions().size()).isEqualTo(1);
    Assertions.assertThat(targetStateA.getTransitions().size()).isEqualTo(2);
    final StateSyncOptions stateSyncOptions = StateSyncOptionsBuilder.of(CTP_TARGET_CLIENT).batchSize(3).build();
    final StateSync stateSync = new StateSync(stateSyncOptions);
    final List<StateDraft> stateDrafts = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, Arrays.asList(stateA, stateB, stateC)).join();
    // test
    final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
    assertThat(stateSyncStatistics).hasValues(3, 0, 1, 0, 0);
    CtpQueryUtils.queryAll(CTP_TARGET_CLIENT, StateQueryBuilder.of().plusPredicates(q -> q.key().is(keyA)).build(), Function.identity()).thenApply(fetchedCategories -> fetchedCategories.stream().flatMap(List::stream).collect(Collectors.toList())).thenAccept(resultStates -> {
        Assertions.assertThat(resultStates.size()).isEqualTo(1);
        Assertions.assertThat(resultStates.get(0).getTransitions()).isNull();
    }).toCompletableFuture().join();
}
Also used : CustomObjectQuery(io.sphere.sdk.customobjects.queries.CustomObjectQuery) BeforeEach(org.junit.jupiter.api.BeforeEach) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) Arrays(java.util.Arrays) Reference(io.sphere.sdk.models.Reference) CTP_SOURCE_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Collections.singletonList(java.util.Collections.singletonList) AfterAll(org.junit.jupiter.api.AfterAll) ReferenceIdToKeyCache(com.commercetools.sync.commons.utils.ReferenceIdToKeyCache) StateTransformUtils(com.commercetools.sync.states.utils.StateTransformUtils) StateCreateCommand(io.sphere.sdk.states.commands.StateCreateCommand) Arrays.asList(java.util.Arrays.asList) StateITUtils.getStateByKey(com.commercetools.sync.integration.commons.utils.StateITUtils.getStateByKey) SphereClient(io.sphere.sdk.client.SphereClient) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) StateType(io.sphere.sdk.states.StateType) Assertions(org.assertj.core.api.Assertions) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ThreadLocalRandom.current(java.util.concurrent.ThreadLocalRandom.current) CompletionStageUtil.executeBlocking(com.commercetools.tests.utils.CompletionStageUtil.executeBlocking) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) StateUpdateCommand(io.sphere.sdk.states.commands.StateUpdateCommand) Set(java.util.Set) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) State(io.sphere.sdk.states.State) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) StateITUtils.deleteStatesFromTargetAndSource(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStatesFromTargetAndSource) Test(org.junit.jupiter.api.Test) LocalizedString(io.sphere.sdk.models.LocalizedString) List(java.util.List) CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_TRANSITION_CONTAINER_KEY) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) CompletionStage(java.util.concurrent.CompletionStage) SphereInternalUtils.asSet(io.sphere.sdk.utils.SphereInternalUtils.asSet) Optional(java.util.Optional) StateDraft(io.sphere.sdk.states.StateDraft) StateITUtils.deleteStates(com.commercetools.sync.integration.commons.utils.StateITUtils.deleteStates) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) StateQuery(io.sphere.sdk.states.queries.StateQuery) BadRequestException(io.sphere.sdk.client.BadRequestException) AssertionsForStatistics.assertThat(com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat) Mockito.spy(org.mockito.Mockito.spy) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) State.referenceOfId(io.sphere.sdk.states.State.referenceOfId) StateReferenceResolver(com.commercetools.sync.states.helpers.StateReferenceResolver) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) StateRole(io.sphere.sdk.states.StateRole) Nonnull(javax.annotation.Nonnull) StateDraftBuilder(io.sphere.sdk.states.StateDraftBuilder) StateQueryBuilder(io.sphere.sdk.states.queries.StateQueryBuilder) StateSyncOptionsBuilder(com.commercetools.sync.states.StateSyncOptionsBuilder) CaffeineReferenceIdToKeyCacheImpl(com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl) StateSync(com.commercetools.sync.states.StateSync) Mockito.when(org.mockito.Mockito.when) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) CtpQueryUtils(com.commercetools.sync.commons.utils.CtpQueryUtils) WaitingToBeResolvedTransitions(com.commercetools.sync.commons.models.WaitingToBeResolvedTransitions) PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ConcurrentModificationException(io.sphere.sdk.client.ConcurrentModificationException) Collections(java.util.Collections) StateSync(com.commercetools.sync.states.StateSync) StateDraft(io.sphere.sdk.states.StateDraft) State(io.sphere.sdk.states.State) StateSyncStatistics(com.commercetools.sync.states.helpers.StateSyncStatistics) StateSyncOptions(com.commercetools.sync.states.StateSyncOptions) Test(org.junit.jupiter.api.Test)

Aggregations

CaffeineReferenceIdToKeyCacheImpl (com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl)35 ReferenceIdToKeyCache (com.commercetools.sync.commons.utils.ReferenceIdToKeyCache)35 Test (org.junit.jupiter.api.Test)35 List (java.util.List)34 ArrayList (java.util.ArrayList)26 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)26 BeforeEach (org.junit.jupiter.api.BeforeEach)24 CTP_SOURCE_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_SOURCE_CLIENT)22 CTP_TARGET_CLIENT (com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT)22 SphereClient (io.sphere.sdk.client.SphereClient)22 AfterAll (org.junit.jupiter.api.AfterAll)21 AssertionsForStatistics.assertThat (com.commercetools.sync.commons.asserts.statistics.AssertionsForStatistics.assertThat)20 Collectors (java.util.stream.Collectors)20 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)20 Mockito.when (org.mockito.Mockito.when)20 Arrays.asList (java.util.Arrays.asList)19 Collections.singletonList (java.util.Collections.singletonList)18 Collections (java.util.Collections)17 Optional (java.util.Optional)17 CompletionStage (java.util.concurrent.CompletionStage)17