Search in sources :

Example 6 with ResourceKeyId

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).");
}
Also used : CustomObject(io.sphere.sdk.customobjects.CustomObject) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ArrayList(java.util.ArrayList) BadRequestException(io.sphere.sdk.client.BadRequestException) FetchCustomObjectsGraphQlRequest(com.commercetools.sync.commons.models.FetchCustomObjectsGraphQlRequest) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) Test(org.junit.jupiter.api.Test)

Example 7 with ResourceKeyId

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));
}
Also used : PagedQueryResult(io.sphere.sdk.queries.PagedQueryResult) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductProjectionQuery(io.sphere.sdk.products.queries.ProductProjectionQuery) ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with ResourceKeyId

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));
}
Also used : ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) BadGatewayException(io.sphere.sdk.client.BadGatewayException) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with ResourceKeyId

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));
}
Also used : ResourceKeyIdGraphQlResult(com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with ResourceKeyId

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);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CategoryITUtils.createCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategories) CtpQueryUtils.queryAll(com.commercetools.sync.commons.utils.CtpQueryUtils.queryAll) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) ArrayList(java.util.ArrayList) CategoryQuery(io.sphere.sdk.categories.queries.CategoryQuery) HashSet(java.util.HashSet) AfterAll(org.junit.jupiter.api.AfterAll) Collections.singleton(java.util.Collections.singleton) ResourceKeyId(com.commercetools.sync.commons.models.ResourceKeyId) BeforeAll(org.junit.jupiter.api.BeforeAll) Locale(java.util.Locale) GraphQlQueryResources(com.commercetools.sync.commons.models.GraphQlQueryResources) OLD_CATEGORY_CUSTOM_TYPE_KEY(com.commercetools.sync.integration.commons.utils.CategoryITUtils.OLD_CATEGORY_CUSTOM_TYPE_KEY) ITUtils.deleteTypes(com.commercetools.sync.integration.commons.utils.ITUtils.deleteTypes) CategoryITUtils.deleteAllCategories(com.commercetools.sync.integration.commons.utils.CategoryITUtils.deleteAllCategories) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CategoryITUtils.createCategoriesCustomType(com.commercetools.sync.integration.commons.utils.CategoryITUtils.createCategoriesCustomType) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) CategoryITUtils.getCategoryDraftsWithPrefix(com.commercetools.sync.integration.commons.utils.CategoryITUtils.getCategoryDraftsWithPrefix) CTP_TARGET_CLIENT(com.commercetools.sync.integration.commons.utils.SphereClientUtils.CTP_TARGET_CLIENT) Category(io.sphere.sdk.categories.Category) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) ResourceKeyIdGraphQlRequest(com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest) CategoryDraft(io.sphere.sdk.categories.CategoryDraft) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceKeyId (com.commercetools.sync.commons.models.ResourceKeyId)16 Test (org.junit.jupiter.api.Test)16 ResourceKeyIdGraphQlRequest (com.commercetools.sync.commons.helpers.ResourceKeyIdGraphQlRequest)13 ResourceKeyIdGraphQlResult (com.commercetools.sync.commons.models.ResourceKeyIdGraphQlResult)13 CategoryQuery (io.sphere.sdk.categories.queries.CategoryQuery)7 List (java.util.List)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 Category (io.sphere.sdk.categories.Category)6 SphereClient (io.sphere.sdk.client.SphereClient)6 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 Collections.singleton (java.util.Collections.singleton)5 GraphQlQueryResources (com.commercetools.sync.commons.models.GraphQlQueryResources)4 CtpQueryUtils.queryAll (com.commercetools.sync.commons.utils.CtpQueryUtils.queryAll)4 CategoryDraft (io.sphere.sdk.categories.CategoryDraft)4 Locale (java.util.Locale)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4