use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ShoppingListTransformServiceImplTest method transform_ShoppingListReferences_ShouldResolveReferencesUsingCacheAndMapToShoppingListDraft.
@Test
void transform_ShoppingListReferences_ShouldResolveReferencesUsingCacheAndMapToShoppingListDraft() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
final ShoppingListTransformService shoppingListTransformService = new ShoppingListTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final String customTypeId = UUID.randomUUID().toString();
final String lineItemCustomTypeId = UUID.randomUUID().toString();
final String textLineItemCustomTypeId = UUID.randomUUID().toString();
final String customTypeKey = "customTypeKey";
final String lineItemCustomTypeKey = "lineItemCustomTypeKey";
final String textLineItemCustomTypeKey = "textLineItemCustomTypeKey";
final String shoppingListKey = "shoppingListKeyValue";
final String textLineItemName = "textLineItemName";
final ProductVariant mockProductVariant = mock(ProductVariant.class);
when(mockProductVariant.getSku()).thenReturn("variant-sku");
final List<ShoppingList> mockShoppingListPage = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final ShoppingList mockShoppingList = mock(ShoppingList.class);
final CustomFields mockCustomFields = mock(CustomFields.class);
final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
when(mockCustomFields.getType()).thenReturn(typeReference);
when(mockShoppingList.getCustom()).thenReturn(mockCustomFields);
when(mockShoppingList.getKey()).thenReturn(shoppingListKey);
final CustomFields mockLineItemCustomFields = mock(CustomFields.class);
final Reference<Type> lineItemTypeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", lineItemCustomTypeId);
when(mockLineItemCustomFields.getType()).thenReturn(lineItemTypeReference);
final LineItem mockLineItem = mock(LineItem.class);
when(mockLineItem.getVariant()).thenReturn(mockProductVariant);
when(mockLineItem.getCustom()).thenReturn(mockLineItemCustomFields);
final CustomFields mockTextLineItemCustomFields = mock(CustomFields.class);
final Reference<Type> textLineItemTypeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", textLineItemCustomTypeId);
when(mockTextLineItemCustomFields.getType()).thenReturn(textLineItemTypeReference);
final TextLineItem mockTextLineItem = mock(TextLineItem.class);
when(mockTextLineItem.getName()).thenReturn(LocalizedString.ofEnglish(textLineItemName));
when(mockTextLineItem.getCustom()).thenReturn(mockTextLineItemCustomFields);
when(mockShoppingList.getLineItems()).thenReturn(Collections.singletonList(mockLineItem));
when(mockShoppingList.getTextLineItems()).thenReturn(Collections.singletonList(mockTextLineItem));
mockShoppingListPage.add(mockShoppingList);
}
final String jsonStringCustomTypes = "{\"results\":[{\"id\":\"" + customTypeId + "\"," + "\"key\":\"" + customTypeKey + "\"}, " + " {\"id\":\"" + lineItemCustomTypeId + "\"," + "\"key\":\"" + lineItemCustomTypeKey + "\"}, " + " {\"id\":\"" + textLineItemCustomTypeId + "\"," + "\"key\":\"" + textLineItemCustomTypeKey + "\"} ]}";
final ResourceKeyIdGraphQlResult customTypesResult = SphereJsonUtils.readObject(jsonStringCustomTypes, ResourceKeyIdGraphQlResult.class);
when(sourceClient.execute(any(ResourceIdsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(customTypesResult));
// test
final List<ShoppingListDraft> shoppingListDrafts = shoppingListTransformService.toShoppingListDrafts(mockShoppingListPage).toCompletableFuture().join();
// assertions
assertThat(shoppingListDrafts).hasSize(10);
shoppingListDrafts.forEach(draft -> {
assertThat(draft.getCustom().getType().getKey()).isEqualTo(customTypeKey);
assertThat(draft.getLineItems()).containsExactly(LineItemDraftBuilder.ofSku("variant-sku", 0L).custom(CustomFieldsDraft.ofTypeKeyAndJson(lineItemCustomTypeKey, emptyMap())).build());
assertThat(draft.getTextLineItems()).containsExactly(TextLineItemDraftBuilder.of(LocalizedString.ofEnglish(textLineItemName), 0L).custom(CustomFieldsDraft.ofTypeKeyAndJson(textLineItemCustomTypeKey, emptyMap())).build());
});
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class BaseServiceImplTest method cacheKeysToIdsUsingGraphQl_WithBadGateWayException_ShouldCompleteExceptionally.
@Test
void cacheKeysToIdsUsingGraphQl_WithBadGateWayException_ShouldCompleteExceptionally() {
// preparation
final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
final ResourceKeyId mockResourceKeyId = mock(ResourceKeyId.class);
final String key = "testKey";
final String id = "testId";
when(mockResourceKeyId.getKey()).thenReturn(key);
when(mockResourceKeyId.getId()).thenReturn(id);
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(singleton(mockResourceKeyId));
when(client.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(CompletableFutureUtils.exceptionallyCompletedFuture(new BadGatewayException()));
// test
final CompletionStage<Map<String, String>> result = service.cacheKeysToIds(singleton("testKey"));
// assertions
assertThat(result).failsWithin(1, TimeUnit.SECONDS).withThrowableOfType(ExecutionException.class).withCauseExactlyInstanceOf(BadGatewayException.class);
verify(client, times(1)).execute(any(ResourceKeyIdGraphQlRequest.class));
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class BaseServiceImplTest method cacheKeysToIdsUsingGraphQl_With500Keys_ShouldChunkAndMakeRequestAndReturnCachedEntry.
@Test
void cacheKeysToIdsUsingGraphQl_With500Keys_ShouldChunkAndMakeRequestAndReturnCachedEntry() {
// preparation
Set<String> randomKeys = new HashSet<>();
IntStream.range(0, 500).forEach(ignore -> randomKeys.add(RandomStringUtils.random(15)));
final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
final ResourceKeyId mockResourceKeyId = mock(ResourceKeyId.class);
final String key = randomKeys.stream().findFirst().get();
final String id = "testId";
when(mockResourceKeyId.getKey()).thenReturn(key);
when(mockResourceKeyId.getId()).thenReturn(id);
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(singleton(mockResourceKeyId));
when(client.execute(any())).thenReturn(completedFuture(resourceKeyIdGraphQlResult));
// test
final Map<String, String> optional = service.cacheKeysToIds(randomKeys).toCompletableFuture().join();
// assertions
assertThat(optional).containsExactly(MapEntry.entry(key, id));
verify(client, times(2)).execute(any(ResourceKeyIdGraphQlRequest.class));
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class InventoryEntryTransformServiceImplTest method transform_InventoryReferences_ShouldResolveReferencesUsingCacheAndMapToInventoryEntryDraft.
@Test
void transform_InventoryReferences_ShouldResolveReferencesUsingCacheAndMapToInventoryEntryDraft() {
// preparation
final SphereClient sourceClient = mock(SphereClient.class);
final ReferenceIdToKeyCache referenceIdToKeyCache = new CaffeineReferenceIdToKeyCacheImpl();
final InventoryEntryTransformService inventoryEntryTransformService = new InventoryEntryTransformServiceImpl(sourceClient, referenceIdToKeyCache);
final String customTypeId = UUID.randomUUID().toString();
final String customTypeKey = "customTypeKey";
final String inventoryEntrySku = "inventoryEntrySkuValue";
final List<InventoryEntry> mockInventoryEntriesPage = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final InventoryEntry mockInventoryEntry = mock(InventoryEntry.class);
final CustomFields mockCustomFields = mock(CustomFields.class);
final Reference<Type> typeReference = Reference.ofResourceTypeIdAndId("resourceTypeId", customTypeId);
when(mockCustomFields.getType()).thenReturn(typeReference);
when(mockInventoryEntry.getCustom()).thenReturn(mockCustomFields);
when(mockInventoryEntry.getSku()).thenReturn(inventoryEntrySku);
mockInventoryEntriesPage.add(mockInventoryEntry);
}
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<InventoryEntryDraft> inventoryEntryDraftsResolved = inventoryEntryTransformService.toInventoryEntryDrafts(mockInventoryEntriesPage).toCompletableFuture().join();
// assertions
final Optional<InventoryEntryDraft> inventoryEntryDraft1 = inventoryEntryDraftsResolved.stream().filter(inventoryEntryDraft -> inventoryEntrySku.equals(inventoryEntryDraft.getSku())).findFirst();
assertThat(inventoryEntryDraft1).hasValueSatisfying(inventoryEntryDraft -> assertThat(inventoryEntryDraft.getCustom().getType().getKey()).isEqualTo(customTypeKey));
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class CtpQueryUtilsTest method queryAll_WithGraphQlRequest_ShouldFetchPagedResult.
@Test
void queryAll_WithGraphQlRequest_ShouldFetchPagedResult() {
// preparation
final SphereClient sphereClient = mock(SphereClient.class);
final List<ResourceKeyId> resultPage = IntStream.range(0, 500).mapToObj(i -> mock(ResourceKeyId.class)).collect(Collectors.toList());
final ResourceKeyId lastCategory = resultPage.get(resultPage.size() - 1);
when(lastCategory.getId()).thenReturn(UUID.randomUUID().toString());
final Set<ResourceKeyId> mockPage = resultPage.stream().collect(Collectors.toSet());
final ResourceKeyIdGraphQlResult pagedResourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
when(pagedResourceKeyIdGraphQlResult.getResults()).thenReturn(mockPage).thenReturn(mockPage).thenReturn(mockPage).thenReturn(mockPage).thenReturn(mockPage.stream().limit(10).collect(Collectors.toSet()));
when(sphereClient.execute(any())).thenReturn(completedFuture(pagedResourceKeyIdGraphQlResult));
final Set<String> keysToQuery = IntStream.range(1, 2010).mapToObj(i -> "key" + i).collect(Collectors.toSet());
ResourceKeyIdGraphQlRequest resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(keysToQuery, GraphQlQueryResources.CATEGORIES);
// test
queryAll(sphereClient, resourceKeyIdGraphQlRequest, results -> identity()).toCompletableFuture().join();
// assertions
verify(sphereClient, times(5)).execute(graphQlRequestArgumentCaptor.capture());
assertThat(graphQlRequestArgumentCaptor.getAllValues()).containsExactly(resourceKeyIdGraphQlRequest.withLimit(500), resourceKeyIdGraphQlRequest.withLimit(500).withPredicate(format("id > \\\\\\\"%s\\\\\\\"", "id")), resourceKeyIdGraphQlRequest.withLimit(500).withPredicate(format("id > \\\\\\\"%s\\\\\\\"", "id")), resourceKeyIdGraphQlRequest.withLimit(500).withPredicate(format("id > \\\\\\\"%s\\\\\\\"", "id")), resourceKeyIdGraphQlRequest.withLimit(500).withPredicate(format("id > \\\\\\\"%s\\\\\\\"", "id")));
}
Aggregations