use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult 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.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ChunkUtilsTest method executeChunks_withGraphqlRequests_ShouldReturnResults.
@Test
void executeChunks_withGraphqlRequests_ShouldReturnResults() {
final SphereClient client = mock(SphereClient.class);
final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(new HashSet<>(Arrays.asList(new ResourceKeyId("coKey1", "coId1"), new ResourceKeyId("coKey2", "coId2"))));
when(client.execute(any(ResourceKeyIdGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(resourceKeyIdGraphQlResult));
final ResourceKeyIdGraphQlRequest request = new ResourceKeyIdGraphQlRequest(singleton("key-1"), GraphQlQueryResources.CATEGORIES);
final List<ResourceKeyIdGraphQlResult> results = ChunkUtils.executeChunks(client, asList(request, request, request)).join();
assertThat(results).hasSize(3);
final Set<ResourceKeyId> resourceKeyIds = ChunkUtils.flattenGraphQLBaseResults(results);
assertThat(resourceKeyIds).hasSize(2);
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ResourceKeyIdGraphQlRequestTest method deserialize_WithMultipleResults_ShouldReturnCorrectResult.
@Test
void deserialize_WithMultipleResults_ShouldReturnCorrectResult() throws JsonProcessingException {
// preparation
String jsonAsString = "{\"data\":{\"categories\":{\"results\":[{\"id\":\"id-1\",\"key\":\"key-1\"}," + "{\"id\":\"id-2\",\"key\":\"key-2\"},{\"id\":\"id-3\",\"key\":\"key-3\"}]}}}";
final HttpResponse httpResponse = HttpResponse.of(200, jsonAsString);
final ResourceKeyIdGraphQlRequest resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(singleton("key-1"), GraphQlQueryResources.CATEGORIES);
// test
final ResourceKeyIdGraphQlResult result = resourceKeyIdGraphQlRequest.deserialize(httpResponse);
// assertions
assertThat(result).isNotNull();
assertThat(result.getResults()).hasSize(3);
assertThat(result.getResults()).extracting("key").containsExactlyInAnyOrder("key-1", "key-2", "key-3");
assertThat(result.getResults()).extracting("id").containsExactlyInAnyOrder("id-1", "id-2", "id-3");
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class ResourceKeyIdGraphQlRequestTest method httpRequestIntent_WithKeyAndPredicate_ShouldReturnCorrectQueryString.
@Test
void httpRequestIntent_WithKeyAndPredicate_ShouldReturnCorrectQueryString() {
// preparation
final GraphQlBaseRequest<ResourceKeyIdGraphQlResult> resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(singleton("key1"), GraphQlQueryResources.CATEGORIES).withPredicate("id > \\\\\\\"id" + "\\\\\\\"");
// test
final HttpRequestIntent httpRequestIntent = resourceKeyIdGraphQlRequest.httpRequestIntent();
// assertions
assertThat(httpRequestIntent.getBody()).isExactlyInstanceOf(StringHttpRequestBody.class);
assertThat(httpRequestIntent.getHttpMethod()).isEqualByComparingTo(HttpMethod.POST);
final StringHttpRequestBody requestBody = (StringHttpRequestBody) httpRequestIntent.getBody();
assertThat(requestBody).isNotNull();
assertThat(requestBody.getString()).isEqualTo("{\"query\": \"{categories(limit: 500, where: \\\"key" + " in (\\\\\\\"key1\\\\\\\") AND id > \\\\\\\"id\\\\\\\"\\\", sort: [\\\"id asc\\\"])" + " { results { id key } }}\"}");
}
use of com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult in project commercetools-sync-java by commercetools.
the class CleanupUnresolvedReferenceCustomObjectsTest method cleanup_withNotFound404Exception_ShouldNotIncrementFailedCounter.
@Test
void cleanup_withNotFound404Exception_ShouldNotIncrementFailedCounter() {
final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(Collections.singleton(new ResourceKeyId("coKey1", "coId1")));
when(mockClient.execute(any(FetchCustomObjectsGraphQlRequest.class))).thenReturn(CompletableFuture.completedFuture(resourceKeyIdGraphQlResult));
when(mockClient.execute(any(CustomObjectDeleteCommand.class))).thenReturn(CompletableFuture.completedFuture(mock(CustomObject.class))).thenReturn(CompletableFutureUtils.failed(new NotFoundException()));
final CleanupUnresolvedReferenceCustomObjects.Statistics statistics = CleanupUnresolvedReferenceCustomObjects.of(mockClient).cleanup(deleteDaysAfterLastModification).join();
assertThat(statistics.getTotalDeleted()).isEqualTo(1);
assertThat(statistics.getTotalFailed()).isEqualTo(0);
assertThat(statistics.getReportMessage()).isEqualTo("Summary: 1 custom objects were deleted in total (0 failed to delete).");
}
Aggregations