use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithNullTransitions_TargetStateTransitionsShouldBeNull.
@Test
void sync_WithNullTransitions_TargetStateTransitionsShouldBeNull() {
final StateDraft stateDraft = createStateDraft(keyA);
final State state = createStateInSource(stateDraft);
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(state)).join();
// test
final StateSyncStatistics stateSyncStatistics = stateSync.sync(stateDrafts).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(1, 1, 0, 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();
}
use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithUpdatedTransition_ShouldUpdateTransitions.
@Test
void sync_WithUpdatedTransition_ShouldUpdateTransitions() {
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, stateB, stateC);
final State stateA = createStateInSource(stateADraft);
final StateDraft tagetStateADraft = createStateDraft(keyA, targetStateB);
final State targetStateA = createStateInTarget(tagetStateADraft);
Assertions.assertThat(targetStateB.getTransitions().size()).isEqualTo(1);
Assertions.assertThat(targetStateA.getTransitions().size()).isEqualTo(1);
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().size()).isEqualTo(2);
}).toCompletableFuture().join();
}
use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithDeletedTransition_ShouldRemoveTransitions.
@Test
void sync_WithDeletedTransition_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, stateB);
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().size()).isEqualTo(1);
}).toCompletableFuture().join();
}
use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class CategoryTransformServiceImplTest method transform_ShouldReplaceCategoryReferenceIdsWithKeys.
@Test
void transform_ShouldReplaceCategoryReferenceIdsWithKeys() {
// preparation
final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
final SphereClient sourceClient = mock(SphereClient.class);
final CategoryTransformService categoryTransformService = new CategoryTransformServiceImpl(sourceClient, referenceIdToKeyCache);
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());
String jsonStringCategories = "{\"results\":[{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0c3\",\"key\":\"cat1\"}]}";
final ResourceKeyIdGraphQlResult categoriesResult = SphereJsonUtils.readObject(jsonStringCategories, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any())).thenReturn(CompletableFuture.completedFuture(categoriesResult));
// test
final CompletionStage<List<CategoryDraft>> draftsFromPageStage = categoryTransformService.toCategoryDrafts(categoryPage);
// assertions
final List<CategoryDraft> expectedResult = mapToCategoryDrafts(categoryPage, referenceIdToKeyCache);
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-sync-java by commercetools.
the class ProductTransformServiceImplTest method transform_ProductReferences_ShouldReplaceReferencesIdsWithKeysAndMapToProductDraft.
@Test
void transform_ProductReferences_ShouldReplaceReferencesIdsWithKeysAndMapToProductDraft() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ProductTransformService productTransformService = new ProductTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final List<ProductProjection> productPage = asList(readObjectFromResource("product-with-unresolved-references.json", Product.class).toProjection(STAGED));
String jsonStringProductTypes = "{\"results\":[{\"id\":\"cda0dbf7-b42e-40bf-8453-241d5b587f93\"," + "\"key\":\"productTypeKey\"}]}";
final ResourceKeyIdGraphQlResult productTypesResult = SphereJsonUtils.readObject(jsonStringProductTypes, ResourceKeyIdGraphQlResult.class);
String jsonStringState = "{\"results\":[{\"id\":\"ste95fb-2282-4f9a-8747-fbe440e02dcs0\"," + "\"key\":\"stateKey\"}]}";
final ResourceKeyIdGraphQlResult statesResult = SphereJsonUtils.readObject(jsonStringState, ResourceKeyIdGraphQlResult.class);
String jsonStringTaxCategory = "{\"results\":[{\"id\":\"ebbe95fb-2282-4f9a-8747-fbe440e02dc0\"," + "\"key\":\"taxCategoryKey\"}]}";
final ResourceKeyIdGraphQlResult taxCategoryResult = SphereJsonUtils.readObject(jsonStringTaxCategory, ResourceKeyIdGraphQlResult.class);
String jsonStringCustomerGroup = "{\"results\":[{\"id\":\"d1229e6f-2b79-441e-b419-180311e52754\"," + "\"key\":\"customerGroupKey\"}]}";
final ResourceKeyIdGraphQlResult customerGroupResult = SphereJsonUtils.readObject(jsonStringCustomerGroup, ResourceKeyIdGraphQlResult.class);
String jsonStringChannel = "{\"results\":[{\"id\":\"cdcf8bea-48f2-54bc-b3c2-cdc94bf94f2c\"," + "\"key\":\"channelKey\"}]}";
final ResourceKeyIdGraphQlResult channelResult = SphereJsonUtils.readObject(jsonStringChannel, ResourceKeyIdGraphQlResult.class);
String jsonStringCategories = "{\"results\":[{\"id\":\"1dfc8bea-84f2-45bc-b3c2-cdc94bf96f1f\",\"key\":\"categoryKey1\"}," + "{\"id\":\"2dfc8bea-84f2-45bc-b3c2-cdc94bf96f1f\",\"key\":\"categoryKey2\"}]}";
final ResourceKeyIdGraphQlResult categoriesResult = SphereJsonUtils.readObject(jsonStringCategories, ResourceKeyIdGraphQlResult.class);
String jsonStringCustomTypes = "{\"results\":[{\"id\":\"custom_type_id\"," + "\"key\":\"customTypeId\"}]}";
final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(productTypesResult)).thenReturn(CompletableFuture.completedFuture(categoriesResult)).thenReturn(CompletableFuture.completedFuture(statesResult)).thenReturn(CompletableFuture.completedFuture(channelResult)).thenReturn(CompletableFuture.completedFuture(taxCategoryResult)).thenReturn(CompletableFuture.completedFuture(customerGroupResult)).thenReturn(CompletableFuture.completedFuture(customTypesResult));
mockAttributeCustomObjectReference(sourceClient);
// test
final List<ProductDraft> productsResolved = productTransformService.toProductDrafts(productPage).toCompletableFuture().join();
// assertions
final Optional<ProductDraft> productKey1 = productsResolved.stream().filter(productDraft -> "productKeyResolved".equals(productDraft.getKey())).findFirst();
assertThat(productKey1).hasValueSatisfying(productDraft -> assertThat(productDraft.getMasterVariant().getPrices()).anySatisfy(priceDraft -> {
assertThat(priceDraft.getChannel().getKey()).isEqualTo("channelKey");
assertThat(priceDraft.getCustomerGroup().getKey()).isEqualTo("customerGroupKey");
}));
assertThat(productKey1).hasValueSatisfying(productDraft -> {
assertThat(productDraft.getProductType().getKey()).isEqualTo("productTypeKey");
assertThat(productDraft.getState().getKey()).isEqualTo("stateKey");
assertThat(productDraft.getTaxCategory().getKey()).isEqualTo("taxCategoryKey");
assertThat(productDraft.getCategories()).anySatisfy(categoryDraft -> {
assertThat(categoryDraft.getKey()).isEqualTo("categoryKey1");
});
});
}
Aggregations