use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult 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.models.ResourceKeyIdGraphQlResult in project commercetools-project-sync by commercetools.
the class ProductSyncerTest method transform_WithAttributeReferences_ShouldReplaceProductReferenceIdsWithKeys.
@Test
void transform_WithAttributeReferences_ShouldReplaceProductReferenceIdsWithKeys() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ProductSyncer productSyncer = ProductSyncer.of(sourceClient, mock(SphereClient.class), getMockedClock(), null);
final List<ProductProjection> productPage = Collections.singletonList(readObjectFromResource("product-key-4.json", Product.class).toProjection(ProductProjectionType.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> draftsFromPageStage = productSyncer.transform(productPage).toCompletableFuture().join();
// assertions
final Optional<ProductDraft> productDraftKey1 = draftsFromPageStage.stream().filter(productDraft -> "productKey4".equals(productDraft.getKey())).findFirst();
assertThat(productDraftKey1).hasValueSatisfying(productDraft -> assertThat(productDraft.getMasterVariant().getAttributes()).anySatisfy(attributeDraft -> {
assertThat(attributeDraft.getName()).isEqualTo("productReference");
final JsonNode referenceSet = attributeDraft.getValue();
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("prod1"));
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("prod2"));
}));
assertThat(productDraftKey1).hasValueSatisfying(productDraft -> assertThat(productDraft.getMasterVariant().getAttributes()).anySatisfy(attributeDraft -> {
assertThat(attributeDraft.getName()).isEqualTo("categoryReference");
final JsonNode referenceSet = attributeDraft.getValue();
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("cat1"));
assertThat(referenceSet).anySatisfy(reference -> assertThat(reference.get("id").asText()).isEqualTo("cat2"));
}));
assertThat(productDraftKey1).hasValueSatisfying(productDraft -> assertThat(productDraft.getMasterVariant().getAttributes()).anySatisfy(attributeDraft -> {
assertThat(attributeDraft.getName()).isEqualTo("productTypeReference");
assertThat(attributeDraft.getValue().get("id").asText()).isEqualTo("prodType1");
}));
assertThat(testLogger.getAllLoggingEvents()).isEmpty();
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-project-sync by commercetools.
the class StateSyncerTest method transform_ShouldReplaceStateTransitionIdsWithKeys.
@Test
void transform_ShouldReplaceStateTransitionIdsWithKeys() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final StateSyncer stateSyncer = StateSyncer.of(sourceClient, mock(SphereClient.class), getMockedClock());
final List<State> states = asList(readObjectFromResource("state-1.json", State.class), readObjectFromResource("state-2.json", State.class));
final String jsonStringTransitions = "{\"results\":[{\"id\":\"ab949f1b-c441-4c70-9cf0-4182c36d6a6c\"," + "\"key\":\"Initial\"} ]}";
final ResourceKeyIdGraphQlResult transitionsResult = SphereJsonUtils.readObject(jsonStringTransitions, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(transitionsResult));
// test
final CompletionStage<List<StateDraft>> stateDrafts = stateSyncer.transform(states);
// assertions
final StateDraft draft1 = StateDraftBuilder.of("State 1", StateType.LINE_ITEM_STATE).roles(Collections.emptySet()).description(LocalizedString.ofEnglish("State 1")).name(LocalizedString.ofEnglish("State 1")).initial(true).build();
final StateDraft draft2 = StateDraftBuilder.of("State 2", StateType.LINE_ITEM_STATE).roles(Collections.emptySet()).description(LocalizedString.ofEnglish("State 2")).name(LocalizedString.ofEnglish("State 2")).initial(false).transitions(Collections.singleton(State.referenceOfId("Initial"))).build();
assertThat(stateDrafts).isCompletedWithValue(Arrays.asList(draft1, draft2));
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-project-sync by commercetools.
the class TestUtils method mockResourceIdsGraphQlRequest.
public static void mockResourceIdsGraphQlRequest(SphereClient client, String id, String key) {
final String jsonResponseString = "{\"results\":[{\"id\":\"" + id + "\"," + "\"key\":\"" + key + "\"}]}";
final ResourceKeyIdGraphQlResult result = SphereJsonUtils.readObject(jsonResponseString, ResourceKeyIdGraphQlResult.class);
when(client.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(result));
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult 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);
}
Aggregations