Search in sources :

Example 21 with ResourceKeyIdGraphQlResult

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);
    });
}
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 ResourceKeyIdGraphQlResult

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();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) 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) CompletableFutureUtils(io.sphere.sdk.utils.CompletableFutureUtils) Arrays.asList(java.util.Arrays.asList) TestLogger(uk.org.lidalia.slf4jtest.TestLogger) ProductSync(com.commercetools.sync.products.ProductSync) SphereClient(io.sphere.sdk.client.SphereClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) TestUtils.getMockedClock(com.commercetools.project.sync.util.TestUtils.getMockedClock) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) ProductSyncCustomRequest(com.commercetools.project.sync.model.ProductSyncCustomRequest) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) QueryPredicate(io.sphere.sdk.queries.QueryPredicate) Mockito.when(org.mockito.Mockito.when) Product(io.sphere.sdk.products.Product) Test(org.junit.jupiter.api.Test) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) ProductProjectionType(io.sphere.sdk.products.ProductProjectionType) Optional(java.util.Optional) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) ReferenceTransformException(com.commercetools.sync.commons.exceptions.ReferenceTransformException) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraft(io.sphere.sdk.products.ProductDraft) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) JsonNode(com.fasterxml.jackson.databind.JsonNode) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) Test(org.junit.jupiter.api.Test)

Example 23 with ResourceKeyIdGraphQlResult

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));
}
Also used : StateDraft(io.sphere.sdk.states.StateDraft) State(io.sphere.sdk.states.State) SphereClient(io.sphere.sdk.client.SphereClient) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) Arrays.asList(java.util.Arrays.asList) List(java.util.List) LocalizedString(io.sphere.sdk.models.LocalizedString) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest) Test(org.junit.jupiter.api.Test)

Example 24 with ResourceKeyIdGraphQlResult

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));
}
Also used : ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ResourceIdsGraphQlRequest(com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest)

Example 25 with ResourceKeyIdGraphQlResult

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);
}
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)

Aggregations

ResourceKeyIdGraphQlResult (com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult)36 Test (org.junit.jupiter.api.Test)35 SphereClient (io.sphere.sdk.client.SphereClient)22 List (java.util.List)18 ResourceIdsGraphQlRequest (com.commercetools.sync.commons.models.ResourceIdsGraphQlRequest)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)16 Mockito.mock (org.mockito.Mockito.mock)16 Mockito.when (org.mockito.Mockito.when)16 CompletableFuture (java.util.concurrent.CompletableFuture)15 ResourceKeyId (com.commercetools.sync.commons.models.ResourceKeyId)14 SphereJsonUtils (io.sphere.sdk.json.SphereJsonUtils)13 CaffeineReferenceIdToKeyCacheImpl (com.commercetools.sync.commons.utils.CaffeineReferenceIdToKeyCacheImpl)12 ReferenceIdToKeyCache (com.commercetools.sync.commons.utils.ReferenceIdToKeyCache)12 ResourceKeyIdGraphQlRequest (com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest)11 Arrays.asList (java.util.Arrays.asList)11 Optional (java.util.Optional)11 CompletionStage (java.util.concurrent.CompletionStage)11 SphereJsonUtils.readObjectFromResource (io.sphere.sdk.json.SphereJsonUtils.readObjectFromResource)10 PagedQueryResult (io.sphere.sdk.queries.PagedQueryResult)10