use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class ProductTypeWithNestedAttributeSyncIT method setup.
/**
* Deletes product types from source and target CTP projects. Populates source and target CTP
* projects with test data.
*/
@BeforeEach
void setup() {
removeAttributeReferencesAndDeleteProductTypes(CTP_SOURCE_CLIENT);
removeAttributeReferencesAndDeleteProductTypes(CTP_TARGET_CLIENT);
populateProjectWithNestedAttributes(CTP_SOURCE_CLIENT);
builtUpdateActions = new ArrayList<>();
errorMessages = new ArrayList<>();
exceptions = new ArrayList<>();
productTypeSyncOptions = ProductTypeSyncOptionsBuilder.of(CTP_TARGET_CLIENT).beforeUpdateCallback((actions, draft, oldProductType) -> {
builtUpdateActions.addAll(actions);
return actions;
}).errorCallback((exception, oldResource, newResource, actions) -> {
errorMessages.add(exception.getMessage());
exceptions.add(exception);
}).build();
referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
}
use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithUpdatedTransitionAndClientThrowsError_ShouldAddErrorMessage.
@Test
void sync_WithUpdatedTransitionAndClientThrowsError_ShouldAddErrorMessage() {
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 SphereClient spyClient = spy(CTP_TARGET_CLIENT);
final StateUpdateCommand updateCommand = any(StateUpdateCommand.class);
when(spyClient.execute(updateCommand)).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, 0, 0, 1, 0);
Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
Assertions.assertThat(errorCallBackMessages).isNotEmpty();
Assertions.assertThat(errorCallBackMessages.get(0)).contains(" detailMessage: a test exception");
Assertions.assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class StateSyncIT method sync_WithFailureInKeysToIdCreation_ShouldAddErrorMessage.
@Test
void sync_WithFailureInKeysToIdCreation_ShouldAddErrorMessage() {
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 SphereClient spyClient = spy(CTP_TARGET_CLIENT);
when(spyClient.execute(any(ResourceKeyIdGraphQlRequest.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 List<StateDraft> stateDrafts = StateTransformUtils.toStateDrafts(CTP_SOURCE_CLIENT, referenceIdToKeyCache, Arrays.asList(stateA, stateB, stateC)).join();
// test
final StateSyncStatistics stateSyncStatistics = new StateSync(stateSyncOptions).sync(stateDrafts).toCompletableFuture().join();
assertThat(stateSyncStatistics).hasValues(3, 0, 0, 3, 0);
Assertions.assertThat(errorCallBackExceptions).isNotEmpty();
Assertions.assertThat(errorCallBackMessages).isNotEmpty();
Assertions.assertThat(errorCallBackMessages.get(0)).isEqualTo("Failed to build a cache of keys to ids.");
Assertions.assertThat(warningCallBackMessages).isEmpty();
}
use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class CartDiscountTransformServiceImplTest method transform_CartDiscountReferences_ShouldResolveReferencesUsingCacheAndMapToCartDiscountDraft.
@Test
void transform_CartDiscountReferences_ShouldResolveReferencesUsingCacheAndMapToCartDiscountDraft() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
final CartDiscountTransformService cartDiscountTransformService = new CartDiscountTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final String cartDiscountKey = "cartDiscountKey";
final String customTypeId = UUID.randomUUID().toString();
final String customTypeKey = "customTypeKey";
final List<CartDiscount> mockCartDiscountsPage = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final CartDiscount mockCartDiscount = mock(CartDiscount.class);
final CustomFields mockCustomFields = mock(CustomFields.class);
final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
when(mockCustomFields.getType()).thenReturn(typeReference);
when(mockCartDiscount.getCustom()).thenReturn(mockCustomFields);
when(mockCartDiscount.getKey()).thenReturn(cartDiscountKey);
mockCartDiscountsPage.add(mockCartDiscount);
}
final String jsonStringCustomTypes = "{\"results\":[{\"id\":\"" + customTypeId + "\"," + "\"key\":\"" + customTypeKey + "\"}]}";
final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(customTypesResult));
// test
final List<CartDiscountDraft> cartDiscountsResolved = cartDiscountTransformService.toCartDiscountDrafts(mockCartDiscountsPage).toCompletableFuture().join();
// assertions
final Optional<CartDiscountDraft> cartDiscountKey1 = cartDiscountsResolved.stream().filter(cartDiscountDraft -> cartDiscountKey.equals(cartDiscountDraft.getKey())).findFirst();
assertThat(cartDiscountKey1).hasValueSatisfying(cartDiscountDraft -> assertThat(cartDiscountDraft.getCustom().getType().getKey()).isEqualTo(customTypeKey));
}
use of com.commercetools.sync.commons.utils.ReferenceIdToKeyCache in project commercetools-sync-java by commercetools.
the class ProductTransformServiceImplTest method transform_WithAttributeReferences_ShouldReplaceAttributeReferenceIdsWithKeys.
@Test
void transform_WithAttributeReferences_ShouldReplaceAttributeReferenceIdsWithKeys() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ProductTransformService productTransformService = new ProductTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final List<ProductProjection> productPage = asList(readObjectFromResource("product-key-4.json", Product.class).toProjection(STAGED));
String jsonStringProducts = "{\"results\":[{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0d2\",\"key\":\"prod1\"}," + "{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0d6\",\"key\":\"prod2\"}]}";
final ResourceKeyIdGraphQlResult productsResult = SphereJsonUtils.readObject(jsonStringProducts, ResourceKeyIdGraphQlResult.class);
String jsonStringProductTypes = "{\"results\":[{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0d3\"," + "\"key\":\"prodType1\"}]}";
final ResourceKeyIdGraphQlResult productTypesResult = SphereJsonUtils.readObject(jsonStringProductTypes, ResourceKeyIdGraphQlResult.class);
String jsonStringCategories = "{\"results\":[{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0d4\",\"key\":\"cat1\"}," + "{\"id\":\"53c4a8b4-754f-4b95-b6f2-3e1e70e3d0d5\",\"key\":\"cat2\"}]}";
final ResourceKeyIdGraphQlResult categoriesResult = SphereJsonUtils.readObject(jsonStringCategories, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(productsResult)).thenReturn(CompletableFuture.completedFuture(productTypesResult)).thenReturn(CompletableFuture.completedFuture(categoriesResult));
// test
final List<ProductDraft> productsResolved = productTransformService.toProductDrafts(productPage).toCompletableFuture().join();
// assertions
final Optional<ProductDraft> productKey1 = productsResolved.stream().filter(productDraft -> "productKey4".equals(productDraft.getKey())).findFirst();
assertThat(productKey1).hasValueSatisfying(product -> assertThat(product.getMasterVariant().getAttributes()).anySatisfy(attribute -> {
assertThat(attribute.getName()).isEqualTo("productReference");
final JsonNode referenceSet = attribute.getValue();
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("prod1"));
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("prod2"));
}));
assertThat(productKey1).hasValueSatisfying(product -> assertThat(product.getMasterVariant().getAttributes()).anySatisfy(attribute -> {
assertThat(attribute.getName()).isEqualTo("categoryReference");
final JsonNode referenceSet = attribute.getValue();
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("cat1"));
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("cat2"));
}));
assertThat(productKey1).hasValueSatisfying(product -> assertThat(product.getMasterVariant().getAttributes()).anySatisfy(attribute -> {
assertThat(attribute.getName()).isEqualTo("productTypeReference");
assertThat(attribute.getValue().get("id").asText()).isEqualTo("prodType1");
}));
}
Aggregations