use of com.commercetools.sync.commons.models.ResourceKeyId in project commercetools-sync-java by commercetools.
the class CleanupUnresolvedReferenceCustomObjectsTest method cleanup_withBadRequest400Exception_ShouldIncrementFailedCounterAndTriggerErrorCallback.
@Test
void cleanup_withBadRequest400Exception_ShouldIncrementFailedCounterAndTriggerErrorCallback() {
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));
final Throwable badRequestException = new BadRequestException("key is not valid");
when(mockClient.execute(any(CustomObjectDeleteCommand.class))).thenReturn(CompletableFuture.completedFuture(mock(CustomObject.class))).thenReturn(CompletableFutureUtils.failed(badRequestException));
final List<Throwable> exceptions = new ArrayList<>();
final CleanupUnresolvedReferenceCustomObjects.Statistics statistics = CleanupUnresolvedReferenceCustomObjects.of(mockClient).errorCallback(exceptions::add).cleanup(deleteDaysAfterLastModification).join();
assertThat(statistics.getTotalDeleted()).isEqualTo(1);
assertThat(statistics.getTotalFailed()).isEqualTo(2);
assertThat(exceptions).contains(badRequestException);
assertThat(statistics.getReportMessage()).isEqualTo("Summary: 1 custom objects were deleted in total (2 failed to delete).");
}
use of com.commercetools.sync.commons.models.ResourceKeyId in project commercetools-sync-java by commercetools.
the class BaseServiceImplTest method cacheKeysToIds_WithCachedKeysExceedingCacheSize_ShouldNotReturnLeastUsedKeys.
@Test
void cacheKeysToIds_WithCachedKeysExceedingCacheSize_ShouldNotReturnLeastUsedKeys() {
// preparation
final PagedQueryResult pagedQueryResult = mock(PagedQueryResult.class);
final ProductProjection product1 = mock(ProductProjection.class);
when(product1.getKey()).thenReturn("key-1");
when(product1.getId()).thenReturn("id-1");
final ProductProjection product2 = mock(ProductProjection.class);
when(product2.getKey()).thenReturn("key-2");
when(product2.getId()).thenReturn("id-2");
when(pagedQueryResult.getResults()).thenReturn(Arrays.asList(product1, product2));
final ResourceKeyIdGraphQlResult resourceKeyIdGraphQlResult = mock(ResourceKeyIdGraphQlResult.class);
final ResourceKeyId resourceKeyId = mock(ResourceKeyId.class);
when(resourceKeyId.getKey()).thenReturn("testKey");
when(resourceKeyId.getId()).thenReturn("testId");
when(resourceKeyIdGraphQlResult.getResults()).thenReturn(singleton(resourceKeyId));
when(client.execute(any())).thenReturn(completedFuture(pagedQueryResult)).thenReturn(completedFuture(resourceKeyIdGraphQlResult));
service.fetchMatchingProductsByKeys(Arrays.asList("key-1", "key-2").stream().collect(Collectors.toSet()));
// access the first added cache entry
service.getIdFromCacheOrFetch("key-1");
// test
final Map<String, String> optional = service.cacheKeysToIds(singleton("testKey")).toCompletableFuture().join();
// assertions
assertThat(optional).containsExactly(MapEntry.entry("key-1", "id-1"), MapEntry.entry("testKey", "testId"));
verify(client, times(1)).execute(any(ProductProjectionQuery.class));
verify(client, times(1)).execute(any(ResourceKeyIdGraphQlRequest.class));
}
use of com.commercetools.sync.commons.models.ResourceKeyId 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.ResourceKeyId 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.ResourceKeyId in project commercetools-sync-java by commercetools.
the class CtpQueryUtilsIT method queryAll_WithIdCollectorConsumerWithSinglePage_ShouldCollectIds.
@Test
void queryAll_WithIdCollectorConsumerWithSinglePage_ShouldCollectIds() {
final int numberOfCategories = 100;
List<CategoryDraft> categoryDrafts = getCategoryDraftsWithPrefix(Locale.ENGLISH, "new", null, numberOfCategories);
List<Category> categories = createCategories(CTP_TARGET_CLIENT, categoryDrafts);
Set<String> allCategoryKeys = categories.stream().map(category -> category.getKey()).collect(Collectors.toSet());
final List<String> categoryIds = new ArrayList<>();
final Consumer<Set<ResourceKeyId>> categoryPageConsumer = categoryPageResults -> categoryPageResults.forEach(category -> categoryIds.add(category.getId()));
ResourceKeyIdGraphQlRequest resourceKeyIdGraphQlRequest = new ResourceKeyIdGraphQlRequest(allCategoryKeys, GraphQlQueryResources.CATEGORIES);
queryAll(CTP_TARGET_CLIENT, resourceKeyIdGraphQlRequest, categoryPageConsumer).toCompletableFuture().join();
assertThat(categoryIds).hasSize(numberOfCategories);
}
Aggregations