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);
});
}
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);
}
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);
}
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"));
}
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();
}
Aggregations